Skip to content

Commit f267b97

Browse files
authored
Account for standard ports when collecting tags (#328)
* Account for standard ports when collecting tags
1 parent d8978bc commit f267b97

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

src/EventStore.Client.Streams/EventStoreClient.Append.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,7 @@ ValueTask<IWriteResult> AppendToStreamInternal(
113113
UserCredentials? userCredentials,
114114
CancellationToken cancellationToken
115115
) {
116-
var tags = new ActivityTagsCollection()
117-
.WithRequiredTag(TelemetryTags.EventStore.Stream, header.Options.StreamIdentifier.StreamName.ToStringUtf8())
118-
.WithGrpcChannelServerTags(channelInfo)
119-
.WithClientSettingsServerTags(Settings)
120-
.WithOptionalTag(TelemetryTags.Database.User, userCredentials?.Username ?? Settings.DefaultCredentials?.Username);
121-
122-
return EventStoreClientDiagnostics.ActivitySource.TraceClientOperation(Operation, TracingConstants.Operations.Append, tags);
116+
return EventStoreClientDiagnostics.ActivitySource.TraceClientOperation(Operation, TracingConstants.Operations.Append, AppendTags);
123117

124118
async ValueTask<IWriteResult> Operation() {
125119
using var call = new StreamsClient(channelInfo.CallInvoker)
@@ -157,6 +151,12 @@ await call.RequestStream
157151

158152
return HandleWrongExpectedRevision(response, header, operationOptions);
159153
}
154+
155+
ActivityTagsCollection AppendTags() => new ActivityTagsCollection()
156+
.WithRequiredTag(TelemetryTags.EventStore.Stream, header.Options.StreamIdentifier.StreamName.ToStringUtf8())
157+
.WithGrpcChannelServerTags(Settings, channelInfo)
158+
.WithClientSettingsServerTags(Settings)
159+
.WithOptionalTag(TelemetryTags.Database.User, userCredentials?.Username ?? Settings.DefaultCredentials?.Username);
160160
}
161161

162162
IWriteResult HandleSuccessAppend(AppendResp response, AppendReq header) {
@@ -282,16 +282,10 @@ ValueTask<IWriteResult> AppendInternal(
282282
IEnumerable<EventData> events,
283283
CancellationToken cancellationToken
284284
) {
285-
var tags = new ActivityTagsCollection()
286-
.WithRequiredTag(TelemetryTags.EventStore.Stream, options.StreamIdentifier.StreamName.ToStringUtf8())
287-
.WithGrpcChannelServerTags(_channelInfo)
288-
.WithClientSettingsServerTags(_settings)
289-
.WithOptionalTag(TelemetryTags.Database.User, _settings.DefaultCredentials?.Username);
290-
291285
return EventStoreClientDiagnostics.ActivitySource.TraceClientOperation(
292286
Operation,
293287
TracingConstants.Operations.Append,
294-
tags
288+
AppendTags
295289
);
296290

297291
async ValueTask<IWriteResult> Operation() {
@@ -310,6 +304,12 @@ async ValueTask<IWriteResult> Operation() {
310304

311305
return await complete.Task.ConfigureAwait(false);
312306
}
307+
308+
ActivityTagsCollection AppendTags() => new ActivityTagsCollection()
309+
.WithRequiredTag(TelemetryTags.EventStore.Stream, options.StreamIdentifier.StreamName.ToStringUtf8())
310+
.WithGrpcChannelServerTags(_settings, _channelInfo)
311+
.WithClientSettingsServerTags(_settings)
312+
.WithOptionalTag(TelemetryTags.Database.User, _settings.DefaultCredentials?.Username);
313313
}
314314

315315
async Task Duplex(ValueTask<ChannelInfo> channelInfoTask) {

src/EventStore.Client/Common/Diagnostics/ActivitySourceExtensions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ public static async ValueTask<T> TraceClientOperation<T>(
1212
this ActivitySource source,
1313
Func<ValueTask<T>> tracedOperation,
1414
string operationName,
15-
ActivityTagsCollection? tags = null
15+
Func<ActivityTagsCollection?>? tagsFactory = null
1616
) {
17+
if (source.HasNoActiveListeners())
18+
return await tracedOperation().ConfigureAwait(false);
19+
20+
var tags = tagsFactory?.Invoke();
21+
1722
using var activity = StartActivity(source, operationName, ActivityKind.Client, tags, Activity.Current?.Context);
1823

1924
try {
@@ -47,7 +52,7 @@ public static void TraceSubscriptionEvent(
4752
.WithRequiredTag(TelemetryTags.EventStore.EventId, resolvedEvent.OriginalEvent.EventId.ToString())
4853
.WithRequiredTag(TelemetryTags.EventStore.EventType, resolvedEvent.OriginalEvent.EventType)
4954
// Ensure consistent server.address attribute when connecting to cluster via dns discovery
50-
.WithGrpcChannelServerTags(channelInfo)
55+
.WithGrpcChannelServerTags(settings, channelInfo)
5156
.WithClientSettingsServerTags(settings)
5257
.WithOptionalTag(
5358
TelemetryTags.Database.User,

src/EventStore.Client/Common/Diagnostics/ActivityTagsCollectionExtensions.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ namespace EventStore.Client.Diagnostics;
77

88
static class ActivityTagsCollectionExtensions {
99
[MethodImpl(MethodImplOptions.AggressiveInlining)]
10-
public static ActivityTagsCollection WithGrpcChannelServerTags(this ActivityTagsCollection tags, ChannelInfo? channelInfo) {
10+
public static ActivityTagsCollection WithGrpcChannelServerTags(this ActivityTagsCollection tags, EventStoreClientSettings settings, ChannelInfo? channelInfo) {
1111
if (channelInfo is null)
1212
return tags;
13-
14-
var authorityParts = channelInfo.Channel.Target.Split(':');
1513

16-
return tags
17-
.WithRequiredTag(TelemetryTags.Server.Address, authorityParts[0])
18-
.WithRequiredTag(TelemetryTags.Server.Port, int.Parse(authorityParts[1]));
19-
}
14+
var authorityParts = channelInfo.Channel.Target.Split([':'], StringSplitOptions.RemoveEmptyEntries);
15+
16+
string host = authorityParts[0];
17+
int port = authorityParts.Length == 1
18+
? settings.ConnectivitySettings.Insecure ? 80 : 443
19+
: int.Parse(authorityParts[1]);
20+
21+
return tags
22+
.WithRequiredTag(TelemetryTags.Server.Address, host)
23+
.WithRequiredTag(TelemetryTags.Server.Port, port);
24+
}
2025

2126
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2227
public static ActivityTagsCollection WithClientSettingsServerTags(this ActivityTagsCollection source, EventStoreClientSettings settings) {

0 commit comments

Comments
 (0)