The Baselime Go OpenTelemetry SDK enables you to instrument your Go services with OpenTelemetry without the boilerplate of using the OpenTelemetry SDK directly.
This SDK uses OpenTelemetry for Go and provides a layer that facilitates instrumenting your Go applications.
#Step 2: Add the OpenTelemetry Instrumentation to your application
main.go
package main
import("context"
baselime_opentelemetry "github.com/baselime/go-opentelemetry""go.opentelemetry.io/otel""go.opentelemetry.io/otel/attribute"
otelMetric "go.opentelemetry.io/otel/metric""log""net/http""os")// Example tracer and countervar tracer = otel.Tracer("flyio_tracer")var reqCounter,_= otel.Meter("your_service_name").Int64Counter("http.request")funcmain(){// Initialise Baselime OTEL distro
params := baselime_opentelemetry.Config{}
otelShutdown, err := baselime_opentelemetry.ConfigureOpenTelemetry(params)if err !=nil{
log.Fatalf("error setting up OTel SDK - %e", err)}deferotelShutdown()
http.HandleFunc("/",func(w http.ResponseWriter, r *http.Request){// Create new span for each request
ctx, span := tracer.Start(r.Context(),"request-received")defer span.End()// Increment counter on each request, with path as an attribute
reqCounter.Add(r.Context(),1, otelMetric.WithAttributes(
attribute.String("path", r.URL.Path),))// Step into function and produce nested spansomeCustomFunction(ctx)})
log.Fatal(http.ListenAndServe(":"+port,nil))}// A function that produces its own spanfuncsomeCustomFunction(ctx context.Context){
ctx, span := tracer.Start(ctx,"get-data-environments")defer span.End()}
In Go you have to manually instrument the libraries you use. You can find instrumentation for popular libraries on the go ecosystem registry
Once you have installed the instrumentation you can find the instructions on how to apply it in their github repo, each instrumentation could be slightly different.
main.go
package main
import("context""github.com/aws/aws-sdk-go-v2/aws""go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws")funcmain(){// init aws config
cfg, err := aws.LoadDefaultConfig(context.TODO())if err !=nil{panic("configuration error, "+ err.Error())}// instrument all aws clients
otelaws.AppendMiddlewares(&cfg.APIOptions)}