UK-1 Datacenter
|
Back to Knowledgebase
Go
Official SDK v1.4.2

Go SDK Quickstart & Distributed Tracing

The TrustPort Beacon Go SDK (beacon-go) provides high-velocity, asynchronous trace propagation with under 0.4% host CPU tax and nanosecond-level W3C header context propagation.

1Install the Module

Add the Beacon Go package to your go.mod dependencies:

go get github.com/trustport/beacon-go@latest

2Gin Framework Middleware Example

Initialize the SDK globally during startup and attach the Gin middleware to your router instance:

main.goProduction Ready
package main

import (
    "context"
    "net/http"

    "github.com/gin-gonic/gin"
    "github.com/trustport/beacon-go"
)

func main() {
    // 1. Initialize Beacon Client
    beacon.Init(beacon.Config{
        IngestURL:   "http://localhost:8443", // Or UK-1 Sovereign Zone Endpoint
        APIKey:      "tp_live_9f8a3c4e12",
        ServiceName: "payment-gateway",
        Environment: "production",
        BatchSize:   500, // Asynchronous batch flushing
    })
    defer beacon.Close()

    r := gin.Default()

    // 2. Attach W3C Traceparent Middleware
    r.Use(beacon.GinMiddleware("payment-gateway"))

    // 3. Application Route Handler
    r.POST("/api/v1/charge", func(c *gin.Context) {
        ctx := c.Request.Context()

        // Create a custom child span for database operation
        span := beacon.StartSpan(ctx, "db.postgresql.charge_account")
        
        // Perform work...
        span.SetTag("customer_id", c.GetString("user_id"))
        span.SetTag("currency", "USD")
        span.End()

        c.JSON(http.StatusOK, gin.H{
            "status":   "success",
            "trace_id": beacon.TraceID(ctx),
        })
    })

    r.Run(":8080")
}

3Fiber & Standard net/http Middleware

Fiber v2 Support

Use beaconfiber.New() for Go Fiber web servers:

app.Use(beaconfiber.Middleware("auth-service"))

Standard net/http

Wrap any standard HTTP handler with beaconhttp.Handler:

http.Handle("/api", beaconhttp.Handler(myHandler))
Need help configuring Go gRPC tracing?

Our senior architects can inspect your pipeline configuration.

Consult Architect