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

Go SDK Quickstart & Distributed Tracing

The TrustPort Beacon Go SDK (github.com/trustportidentity/beacon-go) gives you fast and lightweight performance monitoring for Go applications with almost zero CPU overhead.

1Install the Module

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

go get github.com/trustportidentity/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"
    "os"

    "github.com/gin-gonic/gin"
    "github.com/trustportidentity/beacon-go"
    "github.com/trustportidentity/beacon-go/adapter/beacongin"
)

func main() {
    // 1. Initialize Beacon Client
    beacon.Init(beacon.Config{
        IngestURL:   "https://beacon-api.trustportidentity.com",
        APIKey:      os.Getenv("BEACON_API_KEY"), // tb_live_...
        ServiceName: "payment-gateway",
        Environment: "production",
        BatchSize:   500, // Asynchronous batch flushing
    })
    defer beacon.Close()

    r := gin.Default()

    // 2. Attach W3C Traceparent Middleware
    r.Use(beacongin.Middleware("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))

4Control Ingest Volume with SampleRate

Every request is already batched (BatchSize/FlushPeriod) instead of sent one call per request. On top of that, set Config.SampleRate (0.0–1.0, default 1.0) to trace only a fraction of requests in high-traffic services β€” this is what actually keeps you inside your plan's monthly quota. Exceptions are always sent regardless of sampling.

beacon.Init(beacon.Config{ ..., SampleRate: 0.2 }) // trace ~20% of requests
Need help configuring Go gRPC tracing?

Our senior architects can inspect your pipeline configuration.

Consult Architect