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.
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:
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.
Spans collect metadata to provide insights about each step. This metadata includes:
Spans make debugging easier by showing exactly where time is spent and where problems occur. For example:
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.