SentryTracingAgent
A Deep Dive into Custom Tracing with Sentry
The code provided showcases SentryTracingAgent
, a custom implementation of the TracingAgent
class in Evento. This agent leverages the Sentry platform for distributed tracing, offering a more comprehensive tracing solution compared to the default implementation. Let's delve into how SentryTracingAgent
works and overrides core methods.
Initialization with Sentry DSN:
The constructor accepts the bundle ID, version, and a crucial parameter - sentryDns
. This DSN (Data Source Name) is used to configure the Sentry SDK for communication with your Sentry instance.
Overridden doTrack
Method:
This method is the heart of the tracing functionality. Here's a breakdown of its behavior:
Message and Metadata Check: It first checks if a message is provided. If not, the transaction is executed without tracing. It then retrieves the message's metadata for further processing.
Transaction Initiation:
The code utilizes a switch statement to identify the message type (command, query, event, or invocation) based on the message class.
Depending on the message type and the presence of a
SENTRY_TRACE_HEADER
in the metadata, it initiates a new transaction using the Sentry SDK.If the header exists, it attempts to create a transaction from the provided trace information.
In its absence, it creates a new transaction with a descriptive name based on the message type, component, and payload name. The
@Track
annotation presence also influences transaction creation.
Metadata Enrichment:
The
SENTRY_TRACE_HEADER
and, optionally, theBAGGAGE_HEADER
are added to the message's metadata for propagation across services.Additional data is attached to the transaction using Sentry's
setData
andsetTag
methods. This data includes information about the bundle, message, and component involved.For
DomainCommandMessage
objects, theAggregateId
is also captured.
Transaction Completion and Error Handling:
The actual transaction logic is wrapped within a
try-catch
block.For successful completions, the transaction is marked as finished with
SpanStatus.OK
.If an exception occurs:
The exception is set on the transaction using
setThrowable
.The message payload is captured as transaction data using
setData("Payload", message.getSerializedPayload().getSerializedObject())
.The transaction is marked as finished with
SpanStatus.INTERNAL_ERROR
.The exception is captured by Sentry using
Sentry.captureException
.The trace ID is printed for debugging purposes.
The exception is then re-thrown.
The
CompletableFuture
scenario is handled specifically. The returnedCompletableFuture
is transformed to capture the successful result or any exceptions that might occur asynchronously. In case of exceptions, the transaction is finalized with appropriate status and the exception is re-thrown as aCompletionException
.
Overridden correlate
Method:
This method extracts the SENTRY_TRACE_HEADER
and BAGGAGE_HEADER
(if present) from the provided handledMessage
and merges them into the existing metadata
. This ensures proper context propagation between messages.
Key Advantages of SentryTracingAgent:
Integration with Sentry: Leverages the feature-rich Sentry platform for distributed tracing, providing valuable insights into application behavior across services.
Enhanced Correlation: Extracts trace and baggage headers from messages for improved context propagation.
Detailed Spans: Captures message type, component, bundle information, and payload data within Sentry spans.
Error Handling and Reporting: Captures exceptions and message payloads for better error analysis within Sentry.
In essence, SentryTracingAgent empowers developers with a powerful tool for distributed tracing within Evento applications. By leveraging Sentry's capabilities, you gain a deeper understanding of how your application functions, identify performance bottlenecks, and effectively troubleshoot issues.
Last updated