Add events, exceptions and status
Events, exceptions, and status to spans enriches traces with valuable context. These help highlight key actions, capture errors, and signal issues.
OTel supports Events, Exceptions and Status on spans.
Basic Event Logging in OpenTelemetry: Logs key events during the execution of a code path to a trace span, providing insights into specific actions within your application's workflow.
Error Handling with Span Status in OpenTelemetry: Marks a trace span with an error status when an exception occurs, helping to identify and troubleshoot issues in distributed systems.
Error Handling and Exception Recording in OpenTelemetry: Sets a span's status to error and records detailed information about exceptions, offering a comprehensive view of failures for more effective debugging.
Adding events
Span Events are human-readable messages that represent "something happening" at a particular moment during the lifetime of a span. You can think of it as a primitive log.
from opentelemetry import trace
# Add these lines inside the span or function currently being traced
current_span = trace.get_current_span()
span.addEvent('Doing something')
# Span Events can also have additional attributes
span.addEvent('some log', {
'log.severity': 'error',
'log.message': 'Data not found',
'request.id': requestId,
})import { trace } from '@opentelemetry/api';
const tracer = trace.getTracer('default');
const currentSpan = tracer.getCurrentSpan();
if (currentSpan) {
currentSpan.addEvent('Gonna try it!');
// Do the thing
currentSpan.addEvent('Did it!');
}singleAttrSpan.addEvent("Doing Something");
singleAttrSpan.addEvent("Doing another thing");
singleAttrSpan.end();Set span status
The Span Status allows you to signal the success or failure of the code executed within the span.
It is typically used to specify that a Span has not completed successfully - Error. By default, all spans are Unset, which means a span completed without error. The Ok status is reserved for when you need to explicitly mark a span as successful rather than stick with the default of Unset (i.e., “without error”).
The status can be set at any time before the span is finished.
from opentelemetry import trace
from opentelemetry.trace import Status, StatusCode
# Add these lines inside the span or function currently being traced
current_span = trace.get_current_span()
try:
# something that might fail
except:
current_span.set_status(Status(StatusCode.ERROR))import { trace, SpanStatusCode } from '@opentelemetry/api';
const tracer = trace.getTracer('default');
const currentSpan = tracer.getCurrentSpan();
try {
// something that might fail
} catch (error) {
if (currentSpan) {
currentSpan.setStatus({ code: SpanStatusCode.ERROR });
}
}
Record exceptions in spans
It can be a good idea to record exceptions when they happen. It’s recommended to do this in conjunction with setting span status.
from opentelemetry import trace
from opentelemetry.trace import Status, StatusCode
# Add these lines inside the span or function currently being traced
current_span = trace.get_current_span()
try:
# something that might fail
# Consider catching a more specific exception in your code
except Exception as ex:
current_span.set_status(Status(StatusCode.ERROR))
current_span.record_exception(ex)import { trace, SpanStatusCode } from '@opentelemetry/api';
const tracer = trace.getTracer('default');
const currentSpan = tracer.getCurrentSpan();
try {
// something that might fail
// Consider catching a more specific error in your code
} catch (ex) {
if (currentSpan) {
currentSpan.setStatus({ code: SpanStatusCode.ERROR });
currentSpan.recordException(ex);
}
}
Last updated
Was this helpful?

