#OpenTelemetry for Go

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.


#Instrumentation

#Step 1: Install the SDKs

Install the Baselime Go OpenTelemetry SDK.

terminal
go get github.com/baselime/go-opentelemetry
1

#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 counter var tracer = otel.Tracer("flyio_tracer") var reqCounter, _ = otel.Meter("your_service_name").Int64Counter("http.request") func main() { // 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) } defer otelShutdown() 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 span someCustomFunction(ctx) }) log.Fatal(http.ListenAndServe(":"+port, nil)) } // A function that produces its own span func someCustomFunction(ctx context.Context) { ctx, span := tracer.Start(ctx, "get-data-environments") defer span.End() }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#Step 3: Run your application

Now your Go application is instrumented with OpenTelemetry and will send traces and metrics to Baselime.

terminal
BASELIME_API_KEY=your_api_key go run main.go
1

Traces in console.baselime.io
Traces in console.baselime.io


The BaselimeSDK takes the following configuration options.

#Configuration

Field NameDescription
BaselimeApiKeyAPI key for Baselime service
ServiceNameName of the service
NamespaceNamespace identifier
CollectorUrlURL for the data collector
ProtocolCommunication protocol (grpc / http)

#Instrumenting Libraries

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" ) func main() { // 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) }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19