Otelic Logo

How Do Spans Work in Traces? How Do Spans Capture Metadata in Traces?

Spans are the building blocks of traces. Each span represents a single step in a process, like a database query, an API call, or a function execution. Together, spans form the complete trace, showing the journey of a request through your system. Spans capture important details like timing, errors, and relationships, giving developers a detailed map to debug and optimize their applications.

1. What Are Spans?

Spans are units of work that make up a trace. For example, when a user sends a request to your app, the trace will include multiple spans showing each step, such as:

A database query to fetch data.
A function that processes the request.
An API call to another service.

2. How Do Spans Work in Traces?

Spans are connected in a parent-child hierarchy. The "parent" span represents the main task, and "child" spans represent subtasks. This structure allows you to understand how different parts of your system interact.

1import { trace } from '@opentelemetry/api';
2
3const tracer = trace.getTracer('example-service');
4const parentSpan = tracer.startSpan('process-order');
5
6const childSpan = tracer.startSpan('fetch-database', { parent: parentSpan });
7childSpan.end();
8
9parentSpan.end();

In the example above, the parent span tracks the overall process, while the child span captures the database query.

3. How Do Spans Capture Metadata?

Spans collect metadata to provide insights about each step. This metadata includes:

Start and end time: Measures how long a task took.
Attributes: Key-value pairs like database name or user ID.
Logs: Event details like errors or warnings.
Parent-child relationships: Links to other spans.

4. Why Are Spans Useful for Debugging?

Spans make debugging easier by showing exactly where time is spent and where problems occur. For example:

Identify a slow database query causing delays.
Spot an external API call that’s returning errors.
Trace the full path of a failed request.

5. How Otelic.com Helps You Manage Traces and Spans

Otelic.com collects and visualizes traces and spans in one platform. With its ClickHouse-powered search, you can quickly find specific spans or traces, speeding up debugging. Otelic removes the need for SSH access, enhancing security while making logs and traces easy to analyze.

For support, contact us at support@otelic.com

© 2024 Otelic.com - All Rights Reserved.