OpenTelemetry Support
Using OpenTelemetry SDKs
OTel For Go
15min
in this guide, we will walk you through the process of setting up and using opentelemetry in go you will learn how to instrument a simple application to produce tracing telemetry with opentelemetry go step 1 prerequisites before diving into opentelemetry, be sure to have go 1 16 or newer installed on your local machine step 2 example application for this tutorial, we will be using an application that computes fibonacci numbers for users let’s start by creating a new directory named fib this directory will contain the fibonacci project mkdir fib cd fib 2\ create a file named fib go in the fib directory and add the following code to it package main func fibonacci(n uint) (uint64, error) { if n <= 1 { return uint64(n), nil } var n2, n1 uint64 = 0, 1 for i = uint(2); i < n; i++ { n2, n1 = n1, n1+n2 } return n2 + n1, nil } this is the core logic around which we will create our application 3\ create another file named app go and add the following code to it package main import ( "context" "fmt" "io" "log" ) type app struct { r io reader l log logger } func newapp(r io reader, l log logger) app { return \&app{r r, l l} } func (a app) run(ctx context context) error { for { n, err = a poll(ctx) if err != nil { return err } a write(ctx, n) } } func (a app) poll(ctx context context) (uint, error) { a l print("what fibonacci number would you like to know ") var n uint , err = fmt fscanf(a r, "%d\n", \&n) return n, err } func (a app) write(ctx context context, n uint) { f, err = fibonacci(n) if err != nil { a l printf("fibonacci(%d) %v\n", n, err) } else { a l printf("fibonacci(%d) = %d\n", n, f) } } 4\ create a new file named main go , which will be used to run the application add the following code to it package main import ( "context" "log" "os" "os/signal" ) func main() { l = log new(os stdout, "", 0) sigch = make(chan os signal, 1) signal notify(sigch, os interrupt) errch = make(chan error) app = newapp(os stdin, l) go func() { errch < app run(context background()) }() select { case < sigch l println("\ngoodbye") return case err = < errch if err != nil { l fatal(err) } } } 5\ run the following command in your terminal in the fib directory to initialize it as a go module go mod init fib 6\ run the application using the following command go run step 3 trace instrumentation now that our application code is ready, we will be using the opentelemetry trace api from the go opentelemetry io/otel/trace package to instrument the application code run the following command in your working directory to install the necessary packages for the trace api go get go opentelemetry io/otel \\ go opentelemetry io/otel/trace 2\ next, add imports to your application by adding the following code to the app go file import ( "context" "fmt" "io" "log" "strconv" "go opentelemetry io/otel" "go opentelemetry io/otel/attribute" "go opentelemetry io/otel/trace" ) 3\ the trace api provides a tracer to create traces add the following code line in app go to uniquely identify your application the tracer // name is the tracer name used to identify this instrumentation library const name = "fib" 4\ instrument the run method in your app go file by updating it with the following code // run starts polling users for fibonacci number requests and writes results func (a app) run(ctx context context) error { for { // each execution of the run loop, we should get a new "root" span and context newctx, span = otel tracer(name) start(ctx, "run") n, err = a poll(newctx) if err != nil { span end() return err } a write(newctx, n) span end() } } 5\ instrument the poll method in your app go file by updating it with the following code // poll asks a user for input and returns the request func (a app) poll(ctx context context) (uint, error) { , span = otel tracer(name) start(ctx, "poll") defer span end() a l print("what fibonacci number would you like to know ") var n uint , err = fmt fscanf(a r, "%d\n", \&n) // store n as a string to not overflow an int64 nstr = strconv formatuint(uint64(n), 10) span setattributes(attribute string("request n", nstr)) return n, err } 6\ instrument the write method in your app go file by updating it with the following code // write writes the n th fibonacci number back to the user func (a app) write(ctx context context, n uint) { var span trace span ctx, span = otel tracer(name) start(ctx, "write") defer span end() f, err = func(ctx context context) (uint64, error) { , span = otel tracer(name) start(ctx, "fibonacci") defer span end() return fibonacci(n) }(ctx) if err != nil { a l printf("fibonacci(%d) %v\n", n, err) } else { a l printf("fibonacci(%d) = %d\n", n, f) } } step 4 sdk installation the opentelemetry go project provides go opentelemetry io/otel/sdk sdk package that implements the trace api that we used in the previous step to instrument the application code run the following command in the fib directory to install the trace stdout exporter and the sdk go get go opentelemetry io/otel/sdk \\ go opentelemetry io/otel/exporters/stdout/stdouttrace \\ go opentelemetry io/otel/exporters/otlp/otlptrace/otlptracehttp 2\ next, add the necessary imports to the main go file import ( "context" "io" "log" "os" "os/signal" "go opentelemetry io/otel" "go opentelemetry io/otel/attribute" "go opentelemetry io/otel/exporters/stdout/stdouttrace" "go opentelemetry io/otel/sdk/resource" "go opentelemetry io/otel/sdk/trace" semconv "go opentelemetry io/otel/semconv/v1 17 0" "go opentelemetry io/otel/exporters/otlp/otlptrace/otlptracehttp" "sdktrace "go opentelemetry io/otel/sdk/trace" ) step 5 create a console exporter exporter packages are required to send the telemetry data to the target system or kloudmate in our case to create and initialize a console exporter, add the following function to your main go file // kmtraceexporter returns an exporter with km endpoint func kmtraceexporter() ( sdktrace spanexporter, error) { 	return otlptracehttp new(context background(), 	 otlptracehttp withendpoint("otel kloudmate com 4318"), 	 otlptracehttp withheaders(map\[string]string{ 	 "authorization" "\<your secret key>", 	 })) } the otlptracehttp exporter, along with the kloudmate api key as the authorization header will send the data from the collector to kloudmate step 6 create a resource when telemetry data is produced, it is important to be able to identify what service or service instance the data is coming from in order to solve issues with the service to represent the entity producing the telemetry data, opentelemetry uses a resource to create a resource for your application, add the following function to the main go file // newresource returns a resource describing this application func newresource() resource resource { r, = resource merge( resource default(), resource newwithattributes( semconv schemaurl, semconv servicename("fib"), semconv serviceversion("v0 1 0"), attribute string("environment", "demo"), ), ) return r } step 7 install a tracer provider the instrumented code will produce the telemetry data using tracer the trace provider is required to send the telemetry data from these tracers to the exporter to configure a traceprovider , update your main function in the main go file with the following func main() { l = log new(os stdout, "", 0) // write telemetry data to a file f, err = os create("traces txt") if err != nil { l fatal(err) } defer f close() exp, err = newexporter(f) if err != nil { l fatal(err) } tp = trace newtracerprovider( trace withbatcher(exp), trace withresource(newresource()), ) defer func() { if err = tp shutdown(context background()); err != nil { l fatal(err) } }() otel settracerprovider(tp) / … / } step 8 run the instrumented app you should now have a working application that produces trace telemetry data run the application using the following command go run conclusion when you run the instrumented application, a new file named traces txt will be created in your working directory this file will contain all the traces created from running the application to learn more, explore the official documentation of the opentelemetry go project source url for the example application https //opentelemetry io/docs/instrumentation/go/getting started/ related resources otel for python https //docs kloudmate com/otel for python otel for node js https //docs kloudmate com/otel for nodejs