Skip to content

Commit

Permalink
Implement short-circuit to improve performance when there are no acti…
Browse files Browse the repository at this point in the history
…ve diagnostic listeners
  • Loading branch information
josephcummings committed Jun 6, 2024
1 parent 5380b91 commit d25dd9c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
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 @@ -23,7 +23,7 @@ public static TracingMetadata ExtractTracingMetadata(this ReadOnlyMemory<byte> e
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))
Expand Down

0 comments on commit d25dd9c

Please sign in to comment.