From f267b97d4936c95cbf34b61826d65b9c60260899 Mon Sep 17 00:00:00 2001 From: William Chong Date: Wed, 8 Jan 2025 09:58:35 +0400 Subject: [PATCH] Account for standard ports when collecting tags (#328) * Account for standard ports when collecting tags --- .../EventStoreClient.Append.cs | 28 +++++++++---------- .../Diagnostics/ActivitySourceExtensions.cs | 9 ++++-- .../ActivityTagsCollectionExtensions.cs | 19 ++++++++----- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/EventStore.Client.Streams/EventStoreClient.Append.cs b/src/EventStore.Client.Streams/EventStoreClient.Append.cs index 9fa0a62d8..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(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(_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 28d336a85..71f4372da 100644 --- a/src/EventStore.Client/Common/Diagnostics/ActivitySourceExtensions.cs +++ b/src/EventStore.Client/Common/Diagnostics/ActivitySourceExtensions.cs @@ -12,8 +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 { @@ -47,7 +52,7 @@ public static void TraceSubscriptionEvent( .WithRequiredTag(TelemetryTags.EventStore.EventId, resolvedEvent.OriginalEvent.EventId.ToString()) .WithRequiredTag(TelemetryTags.EventStore.EventType, resolvedEvent.OriginalEvent.EventType) // Ensure consistent server.address attribute when connecting to cluster via dns discovery - .WithGrpcChannelServerTags(channelInfo) + .WithGrpcChannelServerTags(settings, channelInfo) .WithClientSettingsServerTags(settings) .WithOptionalTag( TelemetryTags.Database.User, diff --git a/src/EventStore.Client/Common/Diagnostics/ActivityTagsCollectionExtensions.cs b/src/EventStore.Client/Common/Diagnostics/ActivityTagsCollectionExtensions.cs index 865959eb9..bc3c2aa75 100644 --- a/src/EventStore.Client/Common/Diagnostics/ActivityTagsCollectionExtensions.cs +++ b/src/EventStore.Client/Common/Diagnostics/ActivityTagsCollectionExtensions.cs @@ -7,16 +7,21 @@ namespace EventStore.Client.Diagnostics; static class ActivityTagsCollectionExtensions { [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ActivityTagsCollection WithGrpcChannelServerTags(this ActivityTagsCollection tags, ChannelInfo? channelInfo) { + public static ActivityTagsCollection WithGrpcChannelServerTags(this ActivityTagsCollection tags, EventStoreClientSettings settings, ChannelInfo? channelInfo) { if (channelInfo is null) return tags; - - var authorityParts = channelInfo.Channel.Target.Split(':'); - return tags - .WithRequiredTag(TelemetryTags.Server.Address, authorityParts[0]) - .WithRequiredTag(TelemetryTags.Server.Port, int.Parse(authorityParts[1])); - } + var authorityParts = channelInfo.Channel.Target.Split([':'], StringSplitOptions.RemoveEmptyEntries); + + string host = authorityParts[0]; + int port = authorityParts.Length == 1 + ? settings.ConnectivitySettings.Insecure ? 80 : 443 + : int.Parse(authorityParts[1]); + + return tags + .WithRequiredTag(TelemetryTags.Server.Address, host) + .WithRequiredTag(TelemetryTags.Server.Port, port); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ActivityTagsCollection WithClientSettingsServerTags(this ActivityTagsCollection source, EventStoreClientSettings settings) {