From 3feb7c7507301a1189e48deb05394ca7fec65ab2 Mon Sep 17 00:00:00 2001 From: William Chong Date: Mon, 6 Jan 2025 21:38:32 +0400 Subject: [PATCH] Factory method for building activity tags collection --- .../EventStoreClient.Append.cs | 28 +++++++++---------- .../Diagnostics/ActivitySourceExtensions.cs | 4 ++- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/EventStore.Client.Streams/EventStoreClient.Append.cs b/src/EventStore.Client.Streams/EventStoreClient.Append.cs index 1dca9fd6a..9b244b3a4 100644 --- a/src/EventStore.Client.Streams/EventStoreClient.Append.cs +++ b/src/EventStore.Client.Streams/EventStoreClient.Append.cs @@ -113,13 +113,7 @@ ValueTask AppendToStreamInternal( UserCredentials? userCredentials, CancellationToken cancellationToken ) { - var tags = new ActivityTagsCollection() - .WithRequiredTag(TelemetryTags.EventStore.Stream, header.Options.StreamIdentifier.StreamName.ToStringUtf8()) - .WithGrpcChannelServerTags(Settings, channelInfo) - .WithClientSettingsServerTags(Settings) - .WithOptionalTag(TelemetryTags.Database.User, userCredentials?.Username ?? Settings.DefaultCredentials?.Username); - - return EventStoreClientDiagnostics.ActivitySource.TraceClientOperation(Operation, TracingConstants.Operations.Append, tags); + return EventStoreClientDiagnostics.ActivitySource.TraceClientOperation(Operation, TracingConstants.Operations.Append, AppendTags); async ValueTask Operation() { using var call = new StreamsClient(channelInfo.CallInvoker) @@ -157,6 +151,12 @@ await call.RequestStream return HandleWrongExpectedRevision(response, header, operationOptions); } + + ActivityTagsCollection AppendTags() => new ActivityTagsCollection() + .WithRequiredTag(TelemetryTags.EventStore.Stream, header.Options.StreamIdentifier.StreamName.ToStringUtf8()) + .WithGrpcChannelServerTags(Settings, channelInfo) + .WithClientSettingsServerTags(Settings) + .WithOptionalTag(TelemetryTags.Database.User, userCredentials?.Username ?? Settings.DefaultCredentials?.Username); } IWriteResult HandleSuccessAppend(AppendResp response, AppendReq header) { @@ -282,16 +282,10 @@ ValueTask AppendInternal( IEnumerable events, CancellationToken cancellationToken ) { - var tags = new ActivityTagsCollection() - .WithRequiredTag(TelemetryTags.EventStore.Stream, options.StreamIdentifier.StreamName.ToStringUtf8()) - .WithGrpcChannelServerTags(_settings, _channelInfo) - .WithClientSettingsServerTags(_settings) - .WithOptionalTag(TelemetryTags.Database.User, _settings.DefaultCredentials?.Username); - return EventStoreClientDiagnostics.ActivitySource.TraceClientOperation( Operation, TracingConstants.Operations.Append, - tags + AppendTags ); async ValueTask Operation() { @@ -310,6 +304,12 @@ async ValueTask Operation() { return await complete.Task.ConfigureAwait(false); } + + ActivityTagsCollection AppendTags() => new ActivityTagsCollection() + .WithRequiredTag(TelemetryTags.EventStore.Stream, options.StreamIdentifier.StreamName.ToStringUtf8()) + .WithGrpcChannelServerTags(_settings, _channelInfo) + .WithClientSettingsServerTags(_settings) + .WithOptionalTag(TelemetryTags.Database.User, _settings.DefaultCredentials?.Username); } async Task Duplex(ValueTask channelInfoTask) { diff --git a/src/EventStore.Client/Common/Diagnostics/ActivitySourceExtensions.cs b/src/EventStore.Client/Common/Diagnostics/ActivitySourceExtensions.cs index 778fd19ee..71f4372da 100644 --- a/src/EventStore.Client/Common/Diagnostics/ActivitySourceExtensions.cs +++ b/src/EventStore.Client/Common/Diagnostics/ActivitySourceExtensions.cs @@ -12,11 +12,13 @@ public static async ValueTask TraceClientOperation( this ActivitySource source, Func> tracedOperation, string operationName, - ActivityTagsCollection? tags = null + Func? tagsFactory = null ) { if (source.HasNoActiveListeners()) return await tracedOperation().ConfigureAwait(false); + var tags = tagsFactory?.Invoke(); + using var activity = StartActivity(source, operationName, ActivityKind.Client, tags, Activity.Current?.Context); try {