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.
Events are readable messages that signify “something happening” at a specific moment during a span’s lifecycle. They can be considered as basic logs.
Copy
from opentelemetry import tracecurrent_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!")
Span status is used to indicate the success or failure of the code executed within the span.
Copy
from opentelemetry import tracefrom opentelemetry.trace import Status, StatusCodecurrent_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"))
Recording exceptions when they occur is advisable. This should be done alongside setting the span status.
Copy
from opentelemetry import tracefrom opentelemetry.trace import Status, StatusCodecurrent_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)