Lightstep from ServiceNow Logo






Lightstep from ServiceNow Logo

Getting Started with OpenTelemetry in Go

Get up and running with OpenTelemetry in just a few quick steps! The setup process consists of two phases--getting OpenTelemetry installed and configured, and then validating that configuration to ensure that data is being sent as expected. This guide explains how to download, install, and run OpenTelemetry in Go.


LS Note: When connecting to Lightstep, a project Access Token is required.

Install and run OpenTelemetry Go

To install OpenTelemetry, we recommend our handy OTel-Launcher, which simplifies the process.

go get ""

Once you've downloaded the launcher, you can run OpenTelemetry using the following basic configuration.

The full list of configuration options can be found in the README.

// main.go
package main

import ""

func main() {
   otelLauncher := launcher.ConfigureOpentelemetry(
       launcher.WithAccessToken("<ACCESS TOKEN>"),
   defer otelLauncher.Shutdown()

Validate installation by checking for traces

With your application running, you can now verify that you’ve installed OpenTelemetry correctly by confirming that telemetry data is being reported to your observability backend.

To do this, you need to make sure that your application is actually generating data. Applications will generally not produce traces unless they are being interacted with, and opentelemetry will often buffer data before sending it. So it may take some amount of time and interaction before your application data begins to appear in your backend.

Validate your traces in Lightstep:

  1. Trigger an action in your app that generates a web request.
  2. In Lightstep, click on the Explorer in the sidebar.
  3. Refresh your query until you see traces.
  4. View the traces and verify that important aspects of your application are captured by the trace data.

Library and framework support

Besides OpenTelemetry core modules, it is important to install instrumentation packages for every important library and framework which your service depends upon. Beyond the critical telemetry data these components emit, library and framework integrations are often required to ensure that the trace context is properly propagated.

OpenTelemetry automatically provides instrumentation for a large number of libraries and frameworks, right out of the box.

The full list of supported instrumentation can be found in the README.

Don’t see support for a framework or library you use? Join the beta by filing an issue, or try your hand at writing an instrumentation adapter yourself!

OpenTracing support in Go

The OpenTracing shim allows existing OpenTracing instrumentation to report to the OpenTelemetry SDK. OpenTracing support is not enabled by default. Instructions for enabling the shim can be found in the README.

Read more about upgrading to OpenTelemetry in our OpenTracing Migration Guide.

Troubleshooting your Go installation

Having trouble with installation? Check the following tips.

Run the launcher in debug mode

If spans are not being reported to Lightstep, try running in debug mode by setting OTEL_LOG_LEVEL=debug:

OTEL_LOG_LEVEL=debug go run main.go

The debug log level will print out the configuration information. Check to ensure that your access token looks correct. It will also emit every span to the console, which should look something like:

Span {
  attributes: {},
  links: [],
  events: [],
  status: { code: 0 },
  endTime: [ 1597810686, 885498645 ],
  _ended: true,
  _duration: [ 0, 43333 ],
  name: 'bar',
  spanContext: {
    traceId: 'eca3cc297720bd705e734f4941bca45a',
    spanId: '891016e5f8c134ad',
    traceFlags: 1,
    traceState: undefined
  parentSpanId: 'cff3a2c6bfd4bbef',
  kind: 0,
  startTime: [ 1597810686, 885455312 ],
  resource: Resource { labels: [Object] },
  instrumentationLibrary: { name: 'example', version: '*' },
  _logger: ConsoleLogger {
    debug: [Function],
    info: [Function],
    warn: [Function],
    error: [Function]
  _traceParams: {
    numberOfAttributesPerSpan: 32,
    numberOfLinksPerSpan: 32,
    numberOfEventsPerSpan: 128
  _spanProcessor: MultiSpanProcessor { _spanProcessors: [Array] }

OpenTelemetry Go Resources

Telemetry data is indexed by service. In OpenTelemetry, services are described by resources, which are set when the OpenTelemetry SDK is initialized during program startup.

We want our data to be normalized, so we can compare apples to apples. OpenTelemetry defines a schema for the keys and values which describe common service resources such as hostname, region, version, etc. These standards are called Semantic Conventions, and are defined in the OpenTelemetry Specification.

We recommend that, at minimum, the following resources be applied to every service:

service.nameLogical name of the service.
MUST be the same for all instances of horizontally scaled services.
service.versionThe version string of the service API or implementation as defined in Version Attributes.semver:2.0.0No
host.hostnameContains what the hostname command would return on the host,No

Go Configuration

Resources can be set during launcher configuration at program startup.

host, _ := os.Hostname()

	launcher.WithAccessToken("<ACCESS TOKEN>"),
		"host.hostname":  "host",
		"": "my-container-name",
    "cloud.region": "us-central1",

Semantic Conventions

Standardizing the format of your data is a critical part of using OpenTelemetry. OpenTelemetry provides a schema for describing common resources, so that backends can easily parse and identify relevant information.

It is important to understand these conventions when writing instrumentation, in order to normalize your data and increase its utility. The semantic conventions for resources can be found in the specification.

The following types of resources are currently defined:

OpenTelemetry Spans

OpenTelemetry comes with many instrumentation plugins for libraries and frameworks. This should be enough detail to get started with tracing in production.

As great as that is, you will still want to add additional spans to your application code, in order to break down larger operations and gain more detailed insights into where your application is spending its time.

When you create a new span to measure a subcomponent, that span is added to the current trace as the child of the current span, and then becomes the current span itself.

Go Spans and the Tracer API

Accessing the tracer

In order to interact with traces, you must first acquire a handle to a Tracer.

By convention, Tracers are named after the component they are instrumenting; usually a library, a package, or a class.

import ""

tracer := otel.Tracer("package-name")

Note that there is no need to "set" a tracer by name before getting it. The name you provide is to help identify which component generated which spans, and to potentially disable tracing for individual components.

We recommend creating a Tracer once per component during initialization and retaining a handle to the tracer, rather than calling global.Tracer() repeatedly.

Accessing the current span

Ideally, when tracing application code, spans are created and managed in the application framework.

Assuming that your application framework is, a trace will automatically be created for each request, and your application code will already have a span available inside a the current context, which can be used for adding application specific attributes and events.

import ""

func printSpan(ctx context.Context) {
   span := trace.SpanFromContext(ctx)
   fmt.Printf("current span: %v\n", span)

You can access the current active span via the context object.

Setting a new current span

Let’s demonstrate creating a new span by example. Imagine you have an automated kitchen, and you want to time how long the robot chef takes to bake a cake. Create a new Bake-Cake span, and propagate it via a context object.

func BakeCake(ctx context.Context, tracer trace.Tracer) {
   // the new span will automatically become the child of the span in the current context, and be set as the current span in the new context.
   childCtx, childSpan := tracer.Start(ctx, "bake-cake")
   defer childSpan.End()


When performing root cause analysis, span attributes are an important tool for pinpointing the source of performance issues.

Attributes in OpenTelemetry Go

Setting attributes

Note that it is only possible to set attributes, not to get them.

Much like how resources are used to describe your services, attributes are used to describe your spans. Here is an example of setting attributes to correctly define an HTTP client request:

  . ""

_, span := tracer.Start(

// In addition to the standard attributes, custom attributes can be added as well.
span.SetAttributes(attribute.Int("list.page_number", 2));

// To avoid collisions, always namespace your attribute keys using dot notation.
span.SetAttributes(attribute.Int("", 2));

// attributes can be added to a span at any time before the span is finished.


Spans represent specific operations in and between systems. Many operations represent well-known protocols like HTTP or database calls. Like with resources, OpenTelemetry defines a schema for the attributes which describe these common operations. These standards are called Semantic Conventions, and are defined in the OpenTelemetry Specification.

OpenTelemetry provides a schema for describing common attributes so that backends can easily parse and identify relevant information. It is important to understand these conventions when writing instrumentation, in order to normalize your data and increase its utility.

The following semantic conventions are defined for tracing:

  • General: General semantic attributes that may be used in describing different kinds of operations.
  • HTTP: Spans for HTTP client and server.
  • Database: Spans for SQL and NoSQL client calls.
  • RPC/RMI: Spans for remote procedure calls (e.g., gRPC).
  • Messaging: Spans for interaction with messaging systems (queues, publish/subscribe, etc.).
  • FaaS: Spans for Function as a Service (e.g., AWS Lambda).

The finest-grained tracing tool is the event system.

Span events are a form of structured logging. Each event has a name, a timestamp, and a set of attributes. When events are added to a span, they inherit the span's context. This additional context allows events to be searched, filtered, and grouped by trace ID and other span attributes.

Span context is one of the key differences between distributed tracing and traditional logging.

Events in OpenTelemetry GO

Events are automatically timestamped when they are added to a span. Timestamps can also be set manually if the events are being added after the fact.

For example, enqueuing an item might be recorded as an event.

import ""

// Get the span
span := trace.SpanFromContext(ctx)

// Perform the action

// Record the action
span.AddEvent("enqueued item", trace.WithAttributes(
  attribute.String("key1", "value1"),
  attribute.String("", myItem.ID()),
  attribute.String("", queue.ID()),
  attribute.Int("queue.length", queue.Length()),

Spans should be created for recording course-grained operations, and events should be created for recording fine-grained operations.

Recording exceptions

Many of the tracing conventions can apply to event attributes as well as span attributes. The most important event-specific convention is recording exceptions.

import ""

span := trace.SpanFromContext(ctx)

// RecordError converts an error into a span event.

// If the error indicates that the entire operation is an 
// errored state, update the span status.
span.SetStatus(codes.Error, "critical error")

Marking the span as an error is independent from recordings exceptions. To mark the entire span as an error, and have it count against error rates, set the SpanStatus to any value other than OK.

StatusCode definitions can be found in the OpenTelemetry specification.