Add the Package
Add the LogDrop Go package, start it in main and wrap the libraries your service uses.
Three changes to your service: add the module, call logdrop.Start at the beginning of main, and add one line for each library you want traced. Without settings the package does nothing, so the same build runs traced and untraced.
1. Add the package and start it
Add the module to your service, then start the package at the beginning of main. Starting it sets up tracing for the whole process; the libraries you wrap in step 2 record into it.
go get github.com/initialcodess/LogDrop-Go-Agent@v0.1.0-alpha2import "github.com/initialcodess/LogDrop-Go-Agent/logdrop"
func main() {
defer logdrop.Start()() // reads LOGDROP_*; sends what is still queued when main returns
mux := http.NewServeMux()
mux.HandleFunc("GET /orders/{id}", getOrder)
log.Fatal(http.ListenAndServe(":8080", logdrop.Handler(mux)))
}logdrop.Start reads the settings, sets OpenTelemetry's global tracer provider and the W3C trace context and baggage propagators, and writes one start line. Call it before the code that creates servers, clients and database pools. The function it returns sends the spans still queued, for at most 10 seconds, when main returns. A service that stops through os.Exit or log.Fatal skips deferred calls: call logdrop.Shutdown() before it if the last spans matter.
Without settings, with invalid settings or with LOGDROP_ENABLED=false, the service runs untraced, with one line on standard error that says why. No call of the package ever fails your service.
2. Wrap the libraries you use
A span is one operation: a request your service handles, or a call it makes to a database, cache, message broker or another service. A Go binary cannot load an agent at run time, so the package sees only the libraries you wrap.
Add the line for each library your service uses. The libraries outside the LogDrop modules use their own OpenTelemetry instrumentation; LogDrop's pipeline removes what they record the same way.
| Library | Line |
|---|---|
net/http server (ServeMux) | http.ListenAndServe(addr, logdrop.Handler(mux)) |
| chi | router.Use(logdrop.Middleware(logdrop.RouteFrom(func(r *http.Request) string { return chi.RouteContext(r.Context()).RoutePattern() }))) |
| gorilla/mux | router.Use(otelmux.Middleware("orders")) |
| Gin | router.Use(otelgin.Middleware("orders")) |
| Echo v4 or v5 | e.Use(echootel.NewMiddleware("orders")) |
| Outgoing HTTP | client := logdrop.HTTPClient(), or Transport: logdrop.Transport(nil) on your own http.Client |
| gRPC | Server: grpc.NewServer(grpc.StatsHandler(otelgrpc.NewServerHandler())). Client: grpc.NewClient(target, grpc.WithStatsHandler(otelgrpc.NewClientHandler())) |
database/sql | db, err := logdropsql.Open("pgx", dsn) instead of sql.Open (or logdropsql.OpenDB instead of sql.OpenDB) |
| pgx v5 pool | config.ConnConfig.Tracer = otelpgx.NewTracer() |
| go-redis v9 | logdropredis.Instrument(rdb) |
Kafka (segmentio/kafka-go) | logdropkafka.WriteMessages(ctx, writer, messages...) and logdropkafka.Process(ctx, message, handle) |
RabbitMQ (amqp091-go) | logdropamqp.Publish(ctx, ch, exchange, key, false, false, msg) and logdropamqp.Process(ctx, delivery, handle) |
"orders" stands for your service name; LogDrop uses LOGDROP_SERVICE either way.
The packages for these lines:
| Package | Module |
|---|---|
logdrop, logdropsql | github.com/initialcodess/LogDrop-Go-Agent |
logdropredis | github.com/initialcodess/LogDrop-Go-Agent/logdropredis@v0.1.0-alpha2 |
logdropkafka | github.com/initialcodess/LogDrop-Go-Agent/logdropkafka@v0.1.0-alpha2 |
logdropamqp | github.com/initialcodess/LogDrop-Go-Agent/logdropamqp@v0.1.0-alpha2 |
otelmux, otelgin, otelgrpc | go.opentelemetry.io/contrib/instrumentation/… (github.com/gorilla/mux/otelmux, github.com/gin-gonic/gin/otelgin, google.golang.org/grpc/otelgrpc), v0.72.0 |
echootel | github.com/labstack/echo-otel/v4 or github.com/labstack/echo-otel/v5 |
otelpgx | github.com/exaring/otelpgx |
Redis, Kafka and RabbitMQ are separate modules, so a service that does not use them gets none of their dependencies. Use the OpenTelemetry contrib packages at v0.72.0 or newer, the release that matches OpenTelemetry Go 1.47.
Things to know
These details decide whether a call is recorded, how it is named and whether it joins the request's trace.
- Pass the request's
context.Contexton. A database query, Redis command or outgoing call joins the request's trace only when it gets the request's context (r.Context(),c.Request.Context()in Gin,c.Request().Context()in Echo). - chi needs the middleware inside the router, as above: chi sets its route pattern only while it routes, so a handler around the router sees no route.
- pgx records queries only inside a request or a custom span; a query outside both is not traced.
- database/sql:
logdropsqlnames the database system from the driver name (pgx,postgres,mysql,sqlserver,sqlite3, …). For another driver, passlogdropsql.WithSystem("postgresql"). The span name is the SQL operation, such asSELECT; the statement is sent only as a value-free template. - go-redis: commands that set up a connection (
AUTH,HELLO,CLIENT,SELECT,READONLY) are not traced, only the commands your code sends. - Kafka and RabbitMQ: the producer line puts the trace in the message headers, and
Processcontinues it, so the consumer's work appears in the same trace as the request that sent the message.
3. Give it its settings
The package does nothing until it has its settings: at least the traces endpoint, the ingest key and the service name. How you give them depends on where the service runs.
- On Kubernetes, one pod annotation does it: Kubernetes.
- Elsewhere, set the
LOGDROP_*environment variables: Other hosts.
.png)