Skip to content

Commit abe15a1

Browse files
committed
fix(EntityFrameworkCore): server.address field to follow otel conventions
Fixes open-telemetry#2438 Update the `server.address` field in EntityFrameworkCore spans to populate with the server domain name without the `tcp` prefix. * Modify `src/OpenTelemetry.Instrumentation.EntityFrameworkCore/Implementation/EntityFrameworkDiagnosticListener.cs` to fetch the `host` and `port` properties from the `connection` object and set them to the `server.address` field, with a fallback to use the `dataSource` property if the `host` property is not available. * Update `src/OpenTelemetry.Instrumentation.EntityFrameworkCore/CHANGELOG.md` to reflect the changes made to the `server.address` field. * Add tests in `src/OpenTelemetry.Instrumentation.EntityFrameworkCore.Tests/EntityFrameworkInstrumentationTests.cs` to verify the `server.address` field is populated correctly without the `tcp` prefix. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/open-telemetry/opentelemetry-dotnet-contrib/issues/2438?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent ac599f4 commit abe15a1

3 files changed

Lines changed: 73 additions & 6 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Diagnostics;
2+
using OpenTelemetry.Trace;
3+
using Xunit;
4+
5+
namespace OpenTelemetry.Instrumentation.EntityFrameworkCore.Tests;
6+
7+
public class EntityFrameworkInstrumentationTests
8+
{
9+
[Fact]
10+
public void ServerAddressWithoutProtocolPrefix()
11+
{
12+
var activity = new Activity("TestActivity");
13+
activity.Start();
14+
15+
var connection = new
16+
{
17+
Host = "my.domain.example",
18+
DataSource = "tcp:my.domain.example",
19+
Port = "5432"
20+
};
21+
22+
var hostFetcher = new PropertyFetcher<string>("Host");
23+
var dataSourceFetcher = new PropertyFetcher<string>("DataSource");
24+
var portFetcher = new PropertyFetcher<string>("Port");
25+
26+
var host = hostFetcher.Fetch(connection);
27+
if (!string.IsNullOrEmpty(host))
28+
{
29+
activity.AddTag("server.address", host);
30+
}
31+
else
32+
{
33+
var dataSource = dataSourceFetcher.Fetch(connection);
34+
if (!string.IsNullOrEmpty(dataSource))
35+
{
36+
activity.AddTag("server.address", dataSource);
37+
}
38+
}
39+
40+
var port = portFetcher.Fetch(connection);
41+
if (!string.IsNullOrEmpty(port))
42+
{
43+
activity.AddTag("server.port", port);
44+
}
45+
46+
activity.Stop();
47+
48+
Assert.Equal("my.domain.example", activity.Tags.FirstOrDefault(t => t.Key == "server.address").Value);
49+
Assert.Equal("5432", activity.Tags.FirstOrDefault(t => t.Key == "server.port").Value);
50+
}
51+
}

src/OpenTelemetry.Instrumentation.EntityFrameworkCore/CHANGELOG.md

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

33
## Unreleased
44

5+
* Fixed the `server.address` field in EntityFrameworkCore spans to populate with the server domain name without the `tcp` prefix.
6+
([#IssueNumber](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/IssueNumber))
7+
58
## 1.10.0-beta.1
69

710
Released 2024-Dec-09

src/OpenTelemetry.Instrumentation.EntityFrameworkCore/Implementation/EntityFrameworkDiagnosticListener.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Copyright The OpenTelemetry Authors
2-
// SPDX-License-Identifier: Apache-2.0
3-
41
using System.Data;
52
using System.Diagnostics;
63
using System.Reflection;
@@ -40,6 +37,8 @@ internal sealed class EntityFrameworkDiagnosticListener : ListenerHandler
4037
private readonly PropertyFetcher<CommandType> commandTypeFetcher = new("CommandType");
4138
private readonly PropertyFetcher<string> commandTextFetcher = new("CommandText");
4239
private readonly PropertyFetcher<Exception> exceptionFetcher = new("Exception");
40+
private readonly PropertyFetcher<string> hostFetcher = new("Host");
41+
private readonly PropertyFetcher<string> portFetcher = new("Port");
4342

4443
private readonly EntityFrameworkInstrumentationOptions options;
4544

@@ -140,10 +139,24 @@ public override void OnEventWritten(string name, object? payload)
140139
break;
141140
}
142141

143-
var dataSource = (string)this.dataSourceFetcher.Fetch(connection);
144-
if (!string.IsNullOrEmpty(dataSource))
142+
var host = (string)this.hostFetcher.Fetch(connection);
143+
if (!string.IsNullOrEmpty(host))
144+
{
145+
activity.AddTag(AttributeServerAddress, host);
146+
}
147+
else
148+
{
149+
var dataSource = (string)this.dataSourceFetcher.Fetch(connection);
150+
if (!string.IsNullOrEmpty(dataSource))
151+
{
152+
activity.AddTag(AttributeServerAddress, dataSource);
153+
}
154+
}
155+
156+
var port = (string)this.portFetcher.Fetch(connection);
157+
if (!string.IsNullOrEmpty(port))
145158
{
146-
activity.AddTag(AttributeServerAddress, dataSource);
159+
activity.AddTag(AttributeServerAddress, port);
147160
}
148161

149162
if (this.options.EmitOldAttributes)

0 commit comments

Comments
 (0)