# Distributed tracing

# What is tracing?

Tracing allows you to see how a request progresses through different components, timing of each operation, any logs and errors as they occur.

# Why tracing?

A typical web application consists of multiple components written in different languages and running on different platforms:

  • Load balancer (e.g. nginx).
  • Frontend code (e.g. React).
  • Backend monolith or microservices.
  • At least one database.
  • Task/job queue.

Distributed tracing collects data from such diverse environments and allows you to:

  • Monitor performance of each operation (for example, SQL query), individual components (database), and the whole request round trip.
  • Monitor logs and errors no matter where they come from.
  • Tie everything together into a single trace.

# Spans

A span represents an operation (unit of work) in a trace. A span could be a remote procedure call (RPC), a database query, or potentially interesting code. A span has:

  • A parent span.
  • A span name (operation name).
  • A span kind.
  • Start and end time (duration = end_time - start_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.

A trace is a tree of spans that shows the path that a request makes through an app. A span is an operation that your app performs handling a request.

# Span names

Uptrace uses 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.

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 variable param 42.
select_project(42)Bad. Contains variable 42.
SELECT * FROM projects WHERE id = 42Bad. Contains user data 42.

# Attributes

To record contextual information you 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 attributesopen in new window convention. It defines a list of common attribute keys with their meaning and possible values.

# Events

You can 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.

# Span kind

Span kind must have one of the following values:

  • server for server operations.
  • client for client operations.
  • producer for message producers.
  • consumer for message consumers and async processing in general.
  • internal for internal (nested) spans to further instrument spans.

# Status code

Status code reports whether 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.

# Context propagation

Trace 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.

OpenTemetry propagates context between functions within a process (in-process propagation) and even from one service to another (distributed propagation). Distributed tracing uses context for span correlation, for example, assembling spans from multiple services into a single trace.

# Sampling

See Sampling and Rate-limiting.

# What is OpenTelemetry?

OpenTelemetryopen in new window is a vendor-neutral API for distributed traces and metrics. It defines a specification that standardizes how to collect and send telemetry data to backend platforms. It means that you can instrument your application once and then add or change vendors (tracing backends) as required. OpenTelemetry is available for most programming languages and provides tracing interoperability across different languages and environments.

Uptrace uses OpenTelemetry to collect traces, logs, and errors. The outline of the process is the following:

  • OpenTelemetry API instruments your application with spans and metrics.
  • OpenTelemetry SDK exports collected data to Uptrace.
  • Uptrace uses that information to help you profile, monitor, and debug your application.

# OpenTelemetry overhead

To monitor your app or library, OpenTelemetry instrumentation introduces inevitable performance overhead and additional code dependencies. But the overhead is relatively small and a lot of effortsopen in new window have been put to minimize possible negative impact on end-user application. For example, OpenTelemetry exporter API is asynchronous and does not block user application.

When disabled, the overhead of OpenTelemetry instrumentation is few function calls with minimal memory allocations. When enabled, the overhead is specific to the instrumented library and you can sample only subset of spans that are of interest to you.

To summarize:

  • When disabled there is practically no overhead. Without an implementation OpenTelemetry does not produce telemetry.
  • When enabled the overhead is small and under your control. Whenever possible API is asynchronous and does not block your application.

# Tracing vs execution hooks

To monitor complex systems we need dozens of execution hooks that provide:

  • Connection details and time required to establish a connection.
  • A request and time required to build and write the request.
  • A response and time required to read and build the response.
  • Information about retries and errors.
  • Any other system specific information.

OpenTemetry (or tracing in general) replaces dozens of different hooks with a concise API created specifically for monitoring. Such API has similar performance overhead but is more flexible and easier to maintain.