Monitoring Go apps with Distributed Tracing and OpenTelemetry

I’m a happy user of Bun and like the work being done at Uptrace. The following is a guest post by them about their monitoring projects. Enjoy!

What is tracing? Link to heading

Distributed tracing allows to observe requests as they propagate through distributed systems, especially those built using a microservices architecture.

Tracing provides insights into your app performance along with any errors and logs. You immediately see what conditions cause errors and how particular error affects app performance.

Uptrace tracing tool

Using tracing, you can break down requests into spans. Span is an operation (unit of work) your app performs handling a request, for example, a database query or a network call.

Trace is a tree of spans that shows the path that a request makes through an app. Root span is the first span in a trace.

Uptrace tracing tool

To learn more about tracing, see Distributed tracing using OpenTelemetry.

What is OpenTelemetry? Link to heading

OpenTelemetry is an open-source observability framework for distributed tracing (including logs and errors) and metrics.

Otel allows developers to collect and export telemetry data in a vendor agnostic way. With OpenTelemetry, you can instrument your application once and then add or change vendors without changing the instrumentation, for example, here is a list popular DataDog alternatives that support OpenTelemetry.

OpenTelemetry is available for most programming languages and provides interoperability across different languages and environments.

How to use OpenTelemetry? Link to heading

OpenTelemetry is available for most programming languages and provides interoperability across different languages and environments.

You can get started with OpenTelemetry by following these 5 steps:

OpenTelemetry Go API Link to heading

You can create spans using OpenTelemetry Go API like this:

import "go.opentelemetry.io/otel"

var tracer = otel.Tracer("app_or_package_name")

func someFunc(ctx context.Context) error {
	ctx, span := tracer.Start(ctx, "some-func")
	defer span.End()

    // the code you are measuring

	return nil
}

You can also record attributes and errors:

func someFunc(ctx context.Context) error {
	ctx, span := tracer.Start(ctx, "some-func")
	defer span.End()

	if span.IsRecording() {
		span.SetAttributes(
			attribute.Int64("enduser.id", userID),
			attribute.String("enduser.email", userEmail),
		)
	}

	if err := someOtherFunc(ctx); err != nil {
		span.RecordError(err)
		span.SetStatus(codes.Error, err.Error())
	}

	return nil
}

What is Uptrace? Link to heading

Uptrace is an open source DataDog alternative powered by OpenTelemetry and ClickHouse. It allows you to identify and fix bugs in production faster knowing what conditions lead to which errors.

Uptrace accepts data from OpenTelemetry and stores it in a ClickHouse database. ClickHouse is a columnar database that is much more efficient for traces and logs than, for example, Elastic Search. On the same hardware, ClickHouse can store 10x more traces and, at the same time, provide much better performance.

You can install Uptrace by downloading a DEB/RPM package or a pre-compiled binary.

Uptrace tracing tool

What’s next? Link to heading

Next, you can continue exploring OpenTelemetry or start instrumenting your app using popular instrumentations: