Context is what links traces, logs, and spans together. It tracks essential details like trace IDs, span IDs, and resources, ensuring you can see how everything in your app is connected. Context also scopes data to the right service, request, or operation, making debugging faster and more effective. Without context, you’d have isolated pieces of information that are hard to connect.
Context is the shared information that ensures all logs, spans, and traces related to a request stay linked. It includes:
Context flows through your application, carrying information that connects logs and spans to a specific trace. For example, here’s how you can use OpenTelemetry to propagate context across a service:
1import { Resource } from '@opentelemetry/resources';
2import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
3import { trace, context } from '@opentelemetry/api';
4
5// Define the resource for your service
6const resource = new Resource({
7 'service.name': 'order-service',
8 'environment': 'production',
9});
10
11// Create and register a TracerProvider with the resource
12const provider = new NodeTracerProvider({ resource });
13provider.register();
14
15// Get a tracer
16const tracer = trace.getTracer('example-service');
17
18// Start a root span and propagate context to child spans
19const rootSpan = tracer.startSpan('process-order');
20
21// Propagate context so child spans inherit the trace ID and parent ID
22context.with(trace.setSpan(context.active(), rootSpan), () => {
23 // Create a child span for a database query
24 const childSpan = tracer.startSpan('fetch-database', {
25 attributes: { query: 'SELECT * FROM orders WHERE id = 123' },
26 });
27
28 // Add a log message scoped to the child span
29 console.log('Fetching data for order', {
30 traceId: childSpan.spanContext().traceId, // Log child span's traceId
31 spanId: childSpan.spanContext().spanId, // Log child span's spanId
32 });
33
34 childSpan.end();
35
36 // Another child span for external API call
37 const apiSpan = tracer.startSpan('call-payment-api', {
38 attributes: { endpoint: '/process-payment', method: 'POST' },
39 });
40
41 console.log('Calling payment API', {
42 traceId: apiSpan.spanContext().traceId, // Log API span's traceId
43 spanId: apiSpan.spanContext().spanId, // Log API span's spanId
44 });
45
46 apiSpan.end();
47});
48
49// End the root span after all child spans
50rootSpan.end();
In the example above, the root span represents the overall request, while the child span tracks a database query. Context ensures all logs and spans are scoped to the correct request and resource.
Logs use context to connect specific events to traces and spans, ensuring all details are scoped correctly. For example:
By tying logs to spans and traces, you can quickly identify what happened, where it happened, and why.
Context simplifies debugging by ensuring all related logs and spans are connected. For example:
Otelic.com collects and organizes traces, spans, and logs, ensuring context is preserved and searchable. With its ClickHouse-powered search, you can seamlessly jump between traces and related logs, making it easy to see the full story of what happened. Whether you're debugging a slow API or tracking down an error, Otelic helps you quickly trace issues from logs to spans and root causes. No need for SSH access, making debugging secure and efficient.