Overview
-
Basic Event Logging in OpenTelemetry: Captures significant events during a code path’s execution within a trace span, offering insights into specific actions in your application’s workflow.
-
Handling Errors with Span Status in OpenTelemetry: Assigns an error status to a trace span when an exception arises, helpful in the identify and troubleshoot issues in distributed systems.
-
Recording Errors and Exceptions in OpenTelemetry: Updates a span’s status to reflect errors and logs detailed exception information, providing a comprehensive view of failures for enhanced debugging.
Adding Events
Events are readable messages that signify “something happening” at a specific moment during a span’s lifecycle. They can be considered as basic logs.
from opentelemetry import trace
current_span = trace.get_current_span()
if current_span.is_recording():
current_span.add_event("Attempting the operation!")
# Execute the operation
# For example: result = some_operation()
current_span.add_event("Operation completed!")
from opentelemetry import trace
current_span = trace.get_current_span()
if current_span.is_recording():
current_span.add_event("Attempting the operation!")
# Execute the operation
# For example: result = some_operation()
current_span.add_event("Operation completed!")
import { trace, context } from "@opentelemetry/api";
const currentSpan = trace.getSpan(context.active());
if (currentSpan) {
currentSpan.addEvent("Attempting the operation!");
// Execute the operation
// For example: const result = someOperation();
currentSpan.add_event("Operation completed!");
}
Define Span Status
Span status is used to indicate the success or failure of the code executed within the span.
from opentelemetry import trace
from opentelemetry.trace import Status, StatusCode
current_span = trace.get_current_span()
if current_span.is_recording():
try:
# operation that might fail
# For example: risky_operation()
# If successful, you might explicitly set OK status, though it's often the default.
# current_span.set_status(Status(StatusCode.OK))
pass
except:
current_span.set_status(Status(StatusCode.ERROR, "An error occurred"))
from opentelemetry import trace
from opentelemetry.trace import Status, StatusCode
current_span = trace.get_current_span()
if current_span.is_recording():
try:
# operation that might fail
# For example: risky_operation()
# If successful, you might explicitly set OK status, though it's often the default.
# current_span.set_status(Status(StatusCode.OK))
pass
except:
current_span.set_status(Status(StatusCode.ERROR, "An error occurred"))
import { trace, context, SpanStatusCode } from "@opentelemetry/api";
const currentSpan = trace.getSpan(context.active());
if (currentSpan) {
try {
// operation that might fail
// For example: riskyOperation();
// If successful, you might explicitly set OK status, though it's often the default.
// currentSpan.setStatus({ code: SpanStatusCode.OK });
} catch (error) {
currentSpan.setStatus({ code: SpanStatusCode.ERROR, message: "An error occurred" });
}
}
Log Exceptions in Spans
Recording exceptions when they occur is advisable. This should be done alongside setting the span status.
from opentelemetry import trace
from opentelemetry.trace import Status, StatusCode
current_span = trace.get_current_span()
if current_span.is_recording():
try:
# operation that might fail
# For example: result = 1 / 0
pass
# Consider catching a more specific exception in your code
except Exception as ex:
current_span.set_status(Status(StatusCode.ERROR, str(ex)))
current_span.record_exception(ex)
from opentelemetry import trace
from opentelemetry.trace import Status, StatusCode
current_span = trace.get_current_span()
if current_span.is_recording():
try:
# operation that might fail
# For example: result = 1 / 0
pass
# Consider catching a more specific exception in your code
except Exception as ex:
current_span.set_status(Status(StatusCode.ERROR, str(ex)))
current_span.record_exception(ex)
import { trace, context, SpanStatusCode } from "@opentelemetry/api";
const currentSpan = trace.getSpan(context.active());
if (currentSpan) {
try {
// operation that might fail
// For example:
// const riskyCall = () => { throw new Error("Something went wrong!"); };
// riskyCall();
} catch (error) {
// Ensure the error is an instance of Error for proper recording
if (error instanceof Error) {
currentSpan.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
currentSpan.recordException(error);
} else {
// Handle cases where the caught object is not an Error instance
const errorMessage = typeof error === 'string' ? error : 'Unknown error during operation';
currentSpan.setStatus({ code: SpanStatusCode.ERROR, message: errorMessage });
currentSpan.recordException(errorMessage);
}
}
}