Getting started with OpenTelemetry

  • Step 0. Createopen in new window an Uptrace project to obtain a DSN (connection string), for example, https://<token><project_id>.
go get
package main

import (



func main() {
	ctx := context.Background()

	// Configure OpenTelemetry with sensible defaults.
		// copy your project DSN here or use UPTRACE_DSN env var

	// Send buffered spans and free resources.
	defer uptrace.Shutdown(ctx)

	// Create a tracer. Usually, tracer is a global variable.
	tracer := otel.Tracer("app_or_package_name")

	// Create a root span (a trace) to measure some operation.
	ctx, main := tracer.Start(ctx, "main-operation")
	// End the span when the operation we are measuring is done.
	defer main.End()

	// We pass the main (parent) span in the ctx so we can reconstruct a trace later.
	_, child1 := tracer.Start(ctx, "child1-of-main")
	child1.SetAttributes(attribute.String("key1", "value1"))

	_, child2 := tracer.Start(ctx, "child2-of-main")
	child2.SetAttributes(attribute.Int("key2", 42), attribute.Float64("key3", 123.456))

	fmt.Printf("trace: %s\n", uptrace.TraceURL(main))
go run main.go

Follow the link to view the generated trace:

Basic trace

By now, you should have a configured OpenTelemetry distribution and an idea how how to create and collect spans. Next, learn about OpenTelemetry API and run more examplesopen in new window.

As a final step, you will need to instrumentopen in new window your application. To help with that, OpenTelemetry comes with plugins for popular frameworks and libraries that automatically report spans and metrics for HTTP handlers, gRPC calls, SQL queries, and more.