Skip to content

Commit 23e2123

Browse files
authored
[Instrumentation.StackExchangeRedis] Adopt v1.23 database span network attributes (#2468)
1 parent 5bcb9e7 commit 23e2123

File tree

5 files changed

+46
-23
lines changed

5 files changed

+46
-23
lines changed

src/OpenTelemetry.Instrumentation.StackExchangeRedis/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
* Updated OpenTelemetry core component version(s) to `1.11.0`.
66
([#2470](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2470))
7+
* Rename span network attributes to comply with
8+
[v1.23.0 of Semantic Conventions for Database Client Calls](https://github.com/open-telemetry/semantic-conventions/blob/release/v1.23.x/docs/database/database-spans.md)
9+
([#2468](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2468))
710

811
## 1.10.0-beta.1
912

src/OpenTelemetry.Instrumentation.StackExchangeRedis/Implementation/RedisProfilerEntryToActivityConverter.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
using System.Diagnostics;
55
using System.Net;
6+
#if NET8_0_OR_GREATER
7+
using System.Net.Sockets;
8+
#endif
69
using System.Reflection;
710
using System.Reflection.Emit;
811
using OpenTelemetry.Trace;
@@ -135,18 +138,23 @@ static bool GetCommandAndKey(
135138
{
136139
if (command.EndPoint is IPEndPoint ipEndPoint)
137140
{
138-
activity.SetTag(SemanticConventions.AttributeNetPeerIp, ipEndPoint.Address.ToString());
139-
activity.SetTag(SemanticConventions.AttributeNetPeerPort, ipEndPoint.Port);
141+
activity.SetTag(SemanticConventions.AttributeServerAddress, ipEndPoint.Address.ToString());
142+
activity.SetTag(SemanticConventions.AttributeServerPort, ipEndPoint.Port);
143+
activity.SetTag(SemanticConventions.AttributeNetworkPeerAddress, ipEndPoint.Address.ToString());
144+
activity.SetTag(SemanticConventions.AttributeNetworkPeerPort, ipEndPoint.Port);
140145
}
141146
else if (command.EndPoint is DnsEndPoint dnsEndPoint)
142147
{
143-
activity.SetTag(SemanticConventions.AttributeNetPeerName, dnsEndPoint.Host);
144-
activity.SetTag(SemanticConventions.AttributeNetPeerPort, dnsEndPoint.Port);
148+
activity.SetTag(SemanticConventions.AttributeServerAddress, dnsEndPoint.Host);
149+
activity.SetTag(SemanticConventions.AttributeServerPort, dnsEndPoint.Port);
145150
}
146-
else
151+
#if NET8_0_OR_GREATER
152+
else if (command.EndPoint is UnixDomainSocketEndPoint unixDomainSocketEndPoint)
147153
{
148-
activity.SetTag(SemanticConventions.AttributePeerService, command.EndPoint.ToString());
154+
activity.SetTag(SemanticConventions.AttributeServerAddress, unixDomainSocketEndPoint.ToString());
155+
activity.SetTag(SemanticConventions.AttributeNetworkPeerAddress, unixDomainSocketEndPoint.ToString());
149156
}
157+
#endif
150158
}
151159

152160
activity.SetTag(StackExchangeRedisConnectionInstrumentation.RedisDatabaseIndexKeyName, command.Db);

src/Shared/SemanticConventions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ internal static class SemanticConventions
2525
public const string AttributeEnduserRole = "enduser.role";
2626
public const string AttributeEnduserScope = "enduser.scope";
2727

28-
public const string AttributePeerService = "peer.service";
29-
3028
public const string AttributeHttpMethod = "http.method";
3129
public const string AttributeHttpUrl = "http.url";
3230
public const string AttributeHttpTarget = "http.target";
@@ -116,6 +114,11 @@ internal static class SemanticConventions
116114
public const string AttributeServerPort = "server.port"; // replaces: "net.host.port" (AttributeNetHostPort)
117115
public const string AttributeUserAgentOriginal = "user_agent.original"; // replaces: http.user_agent (AttributeHttpUserAgent)
118116

117+
// v1.23.0 Database spans
118+
// https://github.com/open-telemetry/semantic-conventions/blob/release/v1.23.x/docs/database/database-spans.md
119+
public const string AttributeNetworkPeerAddress = "network.peer.address"; // replaces: "net.peer.ip" (AttributeNetPeerIp)
120+
public const string AttributeNetworkPeerPort = "network.peer.port"; // replaces: "net.peer.port" (AttributeNetPeerPort)
121+
119122
// v1.24.0 Messaging spans
120123
// https://github.com/open-telemetry/semantic-conventions/blob/v1.24.0/docs/messaging/messaging-spans.md
121124
public const string AttributeMessagingClientId = "messaging.client_id";

test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/Implementation/RedisProfilerEntryToActivityConverterTests.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public void ProfilerCommandToActivity_UsesIpEndPointAsEndPoint()
116116
{
117117
long address = 1;
118118
var port = 2;
119+
var ip = $"{address}.0.0.0";
119120

120121
var activity = new Activity("redis-profiler");
121122
var ipLocalEndPoint = new IPEndPoint(address, port);
@@ -124,10 +125,14 @@ public void ProfilerCommandToActivity_UsesIpEndPointAsEndPoint()
124125
var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand, new StackExchangeRedisInstrumentationOptions());
125126

126127
Assert.NotNull(result);
127-
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeNetPeerIp));
128-
Assert.Equal($"{address}.0.0.0", result.GetTagValue(SemanticConventions.AttributeNetPeerIp));
129-
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeNetPeerPort));
130-
Assert.Equal(port, result.GetTagValue(SemanticConventions.AttributeNetPeerPort));
128+
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeServerAddress));
129+
Assert.Equal(ip, result.GetTagValue(SemanticConventions.AttributeServerAddress));
130+
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeServerPort));
131+
Assert.Equal(port, result.GetTagValue(SemanticConventions.AttributeServerPort));
132+
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeNetworkPeerAddress));
133+
Assert.Equal(ip, result.GetTagValue(SemanticConventions.AttributeNetworkPeerAddress));
134+
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeNetworkPeerPort));
135+
Assert.Equal(port, result.GetTagValue(SemanticConventions.AttributeNetworkPeerPort));
131136
}
132137

133138
[Fact]
@@ -141,10 +146,10 @@ public void ProfilerCommandToActivity_UsesDnsEndPointAsEndPoint()
141146
var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand, new StackExchangeRedisInstrumentationOptions());
142147

143148
Assert.NotNull(result);
144-
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeNetPeerName));
145-
Assert.Equal(dnsEndPoint.Host, result.GetTagValue(SemanticConventions.AttributeNetPeerName));
146-
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeNetPeerPort));
147-
Assert.Equal(dnsEndPoint.Port, result.GetTagValue(SemanticConventions.AttributeNetPeerPort));
149+
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeServerAddress));
150+
Assert.Equal(dnsEndPoint.Host, result.GetTagValue(SemanticConventions.AttributeServerAddress));
151+
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeServerPort));
152+
Assert.Equal(dnsEndPoint.Port, result.GetTagValue(SemanticConventions.AttributeServerPort));
148153
}
149154

150155
#if !NETFRAMEWORK
@@ -158,8 +163,10 @@ public void ProfilerCommandToActivity_UsesOtherEndPointAsEndPoint()
158163
var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand, new StackExchangeRedisInstrumentationOptions());
159164

160165
Assert.NotNull(result);
161-
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributePeerService));
162-
Assert.Equal(unixEndPoint.ToString(), result.GetTagValue(SemanticConventions.AttributePeerService));
166+
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeServerAddress));
167+
Assert.Equal(unixEndPoint.ToString(), result.GetTagValue(SemanticConventions.AttributeServerAddress));
168+
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeNetworkPeerAddress));
169+
Assert.Equal(unixEndPoint.ToString(), result.GetTagValue(SemanticConventions.AttributeNetworkPeerAddress));
163170
}
164171
#endif
165172
}

test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/StackExchangeRedisCallsInstrumentationTests.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,17 +417,19 @@ private static void VerifyActivityData(Activity activity, bool isSet, EndPoint e
417417

418418
if (endPoint is IPEndPoint ipEndPoint)
419419
{
420-
Assert.Equal(ipEndPoint.Address.ToString(), activity.GetTagValue(SemanticConventions.AttributeNetPeerIp));
421-
Assert.Equal(ipEndPoint.Port, activity.GetTagValue(SemanticConventions.AttributeNetPeerPort));
420+
Assert.Equal(ipEndPoint.Address.ToString(), activity.GetTagValue(SemanticConventions.AttributeServerAddress));
421+
Assert.Equal(ipEndPoint.Port, activity.GetTagValue(SemanticConventions.AttributeServerPort));
422422
}
423423
else if (endPoint is DnsEndPoint dnsEndPoint)
424424
{
425-
Assert.Equal(dnsEndPoint.Host, activity.GetTagValue(SemanticConventions.AttributeNetPeerName));
426-
Assert.Equal(dnsEndPoint.Port, activity.GetTagValue(SemanticConventions.AttributeNetPeerPort));
425+
Assert.Equal(dnsEndPoint.Host, activity.GetTagValue(SemanticConventions.AttributeServerAddress));
426+
Assert.Equal(dnsEndPoint.Port, activity.GetTagValue(SemanticConventions.AttributeServerPort));
427427
}
428428
else
429429
{
430-
Assert.Equal(endPoint.ToString(), activity.GetTagValue(SemanticConventions.AttributePeerService));
430+
var tags = activity.Tags.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
431+
Assert.DoesNotContain(SemanticConventions.AttributeServerAddress, tags.Keys);
432+
Assert.DoesNotContain(SemanticConventions.AttributeServerPort, tags.Keys);
431433
}
432434
}
433435

0 commit comments

Comments
 (0)