Go SDK

Go SDK for JustAnalytics distributed tracing and error tracking.

Installation#

go get github.com/justanalytics/justanalytics-go

Quick Start#

package main

import (
    ja "github.com/justanalytics/justanalytics-go"
)

func main() {
    ja.Init(ja.Options{
        SiteID:      os.Getenv("JUSTANALYTICS_SITE_ID"),
        APIKey:      os.Getenv("JUSTANALYTICS_API_KEY"),
        ServiceName: "go-api",
        Environment: "production",
    })
    defer ja.Close()

    // Your application code
}

Tracing#

func handleRequest(ctx context.Context) error {
    ctx, span := ja.StartSpan(ctx, "process-order")
    defer span.End()

    span.SetAttribute("order.id", orderID)

    // Nested span
    user, err := fetchUser(ctx, userID)
    if err != nil {
        span.SetError(err)
        return err
    }

    return nil
}

func fetchUser(ctx context.Context, id string) (*User, error) {
    ctx, span := ja.StartSpan(ctx, "fetch-user")
    defer span.End()

    span.SetAttribute("user.id", id)
    return db.GetUser(ctx, id)
}

Error Tracking#

if err := riskyOperation(); err != nil {
    ja.CaptureException(err, ja.CaptureOptions{
        Tags: map[string]string{"module": "payments"},
    })
}

HTTP Middleware#

net/http#

mux := http.NewServeMux()
mux.HandleFunc("/api/users", handleUsers)

handler := ja.HTTPMiddleware(mux)
http.ListenAndServe(":8080", handler)

Gin#

r := gin.Default()
r.Use(ja.GinMiddleware())
r.GET("/api/users/:id", handleGetUser)

Chi#

r := chi.NewRouter()
r.Use(ja.ChiMiddleware)
r.Get("/api/users/{id}", handleGetUser)

Logging#

ja.Logger.Info("Request processed", "path", "/api/orders", "duration", 245)
ja.Logger.Error("Payment failed", "orderID", "123", "reason", "declined")

Custom Metrics#

ja.RecordMetric("custom.queue_size", 42, map[string]interface{}{
    "queue": "emails",
})

Requirements#

  • Go 1.21+
  • Supports context propagation via context.Context