Still using Jaeger/Sentry? Uptrace is a distributed tracing tool that monitors performance, errors, and logs using OpenTelemetry.

OpenTelemetry Distributed Tracing

What is tracing?

Distributed tracing allows you to see how a request progresses through different services and systems, timings of each operation, any logs and errors as they occur.

In a distributed environment, tracing toolsopen in new window also help you understand relationships and interactions between microservices. Distributed tracing allows you to see how a particular microservice is performing and how that service affects other microservices.

Spans

Span represents an operation (unit of work) in a trace. A span could be a remote procedure call (RPC), a database query, or an in-process function call. A span has:

  • A parent span.
  • A span name (operation name).
  • A span kind.
  • Start and end time.
  • A status that reports whether operation succeeded or failed.
  • A set of key-value attributes describing the operation.
  • A timeline of events.
  • A list of links to other spans.
  • A span context that propagates trace ID and other data between different services.

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.

Span names

Backends use span names and some attributes to group similar spans together. To group spans properly, give them short and concise names. The total number of unique span names should be less than 1000. Otherwise, you will have too many span groups and your Uptrace experience will suffer.

The following names are good because they are short, distinctive, and help grouping similar spans together:

Span nameComment
GET /projects/:idGood. A route name with placeholders instead of params.
select_projectGood. A function name without arguments.
SELECT * FROM projects WHERE id = ?Good. A database query with placeholders.

The following span names are bad because they contain params and args:

Span nameComment
GET /projects/42Bad. Contains a variable param 42.
select_project(42)Bad. Contains a variable 42.
SELECT * FROM projects WHERE id = 42Bad. Contains a variable arg 42.

Span kind

Span kind must have one of the following values:

  • server for server operations, for example, HTTP server handler.
  • client for client operations, for example, HTTP client requests.
  • producer for message producers, for example, Kafka producer.
  • consumer for message consumers and async processing in general, for example, Kafka consumer.
  • internal for internal operations.

Status code

Status code indicates whether an operation succeeded or failed. It must have one of the following values:

  • ok - success.
  • error - failure.
  • unset - the default value which allows backends to assign the status.

Attributes

To record contextual information, you can annotate spans with attributes that carry information specific to the operation. For example, an HTTP endpoint may have such attributes as http.method = GET and http.route = /projects/:id.

You can name attributes as you want, but for common operations you should use semantic attributes convention. It defines a list of common attribute keys with their meaning and possible values.

Events

You can also annotate spans with events that have start time and arbitrary number of attributes. The main difference between events and spans is that events don't have end time (and therefore no duration).

Events usually represent exceptions, errors, logs, and messages (such as in RPC), but you can create custom events as well.

Context

Trace/span context is a request-scoped data such as:

  • trace id - unique trace identificator;
  • span id - unique span identificator;
  • trace flags - various flags such as sampled, deferred, and debug.

You can use data from a context for spans correlation or sampling, for example, you can use trace id to know which spans belong to which traces.

Context propagation

OpenTemetry propagates context between functions within a process (in-process propagation) and even from one service to another (distributed propagation).

In-process propagation can be implicit or explicit depending on the programming language your are using. Implicit propagation is done automatically by storing the active context in thread-local variables (Java, Python, Ruby, NodeJS). Explicit propagation requires explicitly passing the active context around in function arguments (Go).

For distributed context propagation, OpenTelemetry supports several protocols that define how to serialize and pass context data:

W3C trace context is the recommended propagator that is enabled by default.

Baggage

Baggageopen in new window works similarly to a span context and allows you to propagate user-defined key:value pairs (attributes) from one service to another. In gRPC world, a similar concept is called gRPC metadata.

For example, you can use baggage to propagate information about the service that created the trace to all other services.

Instrumentations

Instrumentations are plugins for popular frameworks and libraries that use OpenTelemetry API to record important operations, for example, HTTP requests, DB queries, logs, errors, and more.

An instrumentation library is the library which performs the instrumentation itself, not the target of the instrumentation. You provide an instrumentation library name when you create a tracer.

What to instrument

You don't need to instrument all operations to get the most out of tracing. It can take a lot of time and usually is not needed. Consider prioritizing the following operations:

  • Network operations, for example, HTTP requests or RPC calls.
  • Filesystem operations, for example, reading/writing to a file.
  • Database queries which combine network and filesystem operations. Uptrace intelligently groups SQL queries and pretty-prints them for you which can be invaluable for debugging.
  • Errors and logs.

Tracing tools

Distributed tracing tools monitor microservices performance and allow you to track user requests across multiple servers in a distributed system. Use the following links to pick the best tracing tool for your needs:

What's next?

Next, learn about OpenTelemetry tracing API for your programming language:

Last Updated: