Skip to content

Commit

Permalink
Tracing improvements (#311)
Browse files Browse the repository at this point in the history
* Use TryGetProperty when extracting tracing metadata to prevent exception being thrown in absence thereof

* Use JsonDocument.TryParseValue

* Handle empty metadata case and dispose of JsonDocument if instantiated

* Implement short-circuit to improve performance when there are no active diagnostic listeners
  • Loading branch information
josephcummings authored Jun 6, 2024
1 parent fe24538 commit 12b6e39
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public static void TraceSubscriptionEvent(
EventStoreClientSettings settings,
UserCredentials? userCredentials
) {
if (source.HasNoActiveListeners())
return;

var parentContext = resolvedEvent.OriginalEvent.Metadata.ExtractPropagationContext();

if (parentContext is null) return;
Expand All @@ -53,6 +56,9 @@ public static void TraceSubscriptionEvent(
this ActivitySource source,
string operationName, ActivityKind activityKind, ActivityTagsCollection? tags = null, ActivityContext? parentContext = null
) {
if (source.HasNoActiveListeners())
return null;

(tags ??= new ActivityTagsCollection())
.WithRequiredTag(TelemetryTags.Database.System, "eventstoredb")
.WithRequiredTag(TelemetryTags.Database.Operation, operationName);
Expand All @@ -67,4 +73,6 @@ public static void TraceSubscriptionEvent(
)
?.Start();
}

static bool HasNoActiveListeners(this ActivitySource source) => !source.HasListeners();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ public static ReadOnlySpan<byte> InjectTracingContext(this ReadOnlyMemory<byte>

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TracingMetadata ExtractTracingMetadata(this ReadOnlyMemory<byte> eventMetadata) {
try {
using var doc = JsonDocument.Parse(eventMetadata);
if (eventMetadata.IsEmpty)
return TracingMetadata.None;

return new TracingMetadata(
doc.RootElement.GetProperty(TracingConstants.Metadata.TraceId).GetString(),
doc.RootElement.GetProperty(TracingConstants.Metadata.SpanId).GetString()
);
}
catch (Exception) {
var reader = new Utf8JsonReader(eventMetadata.Span);
if (!JsonDocument.TryParseValue(ref reader, out var doc))
return TracingMetadata.None;

using (doc) {
if (!doc.RootElement.TryGetProperty(TracingConstants.Metadata.TraceId, out var traceId)
|| !doc.RootElement.TryGetProperty(TracingConstants.Metadata.SpanId, out var spanId))
return TracingMetadata.None;

return new TracingMetadata(traceId.GetString(), spanId.GetString());
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static ReadOnlySpan<byte> InjectTracingMetadata(this ReadOnlyMemory<byte> eventMetadata, TracingMetadata tracingMetadata) {
if (tracingMetadata == TracingMetadata.None || !tracingMetadata.IsValid)
Expand Down

0 comments on commit 12b6e39

Please sign in to comment.