forked from open-telemetry/opentelemetry-dotnet-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(EntityFrameworkCore): server.address field to follow otel convent…
…ions 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).
- Loading branch information
Showing
3 changed files
with
73 additions
and
6 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...elemetry.Instrumentation.EntityFrameworkCore.Tests/EntityFrameworkInstrumentationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Diagnostics; | ||
using OpenTelemetry.Trace; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Instrumentation.EntityFrameworkCore.Tests; | ||
|
||
public class EntityFrameworkInstrumentationTests | ||
{ | ||
[Fact] | ||
public void ServerAddressWithoutProtocolPrefix() | ||
{ | ||
var activity = new Activity("TestActivity"); | ||
activity.Start(); | ||
|
||
var connection = new | ||
{ | ||
Host = "my.domain.example", | ||
DataSource = "tcp:my.domain.example", | ||
Port = "5432" | ||
}; | ||
|
||
var hostFetcher = new PropertyFetcher<string>("Host"); | ||
var dataSourceFetcher = new PropertyFetcher<string>("DataSource"); | ||
var portFetcher = new PropertyFetcher<string>("Port"); | ||
|
||
var host = hostFetcher.Fetch(connection); | ||
if (!string.IsNullOrEmpty(host)) | ||
{ | ||
activity.AddTag("server.address", host); | ||
} | ||
else | ||
{ | ||
var dataSource = dataSourceFetcher.Fetch(connection); | ||
if (!string.IsNullOrEmpty(dataSource)) | ||
{ | ||
activity.AddTag("server.address", dataSource); | ||
} | ||
} | ||
|
||
var port = portFetcher.Fetch(connection); | ||
if (!string.IsNullOrEmpty(port)) | ||
{ | ||
activity.AddTag("server.port", port); | ||
} | ||
|
||
activity.Stop(); | ||
|
||
Assert.Equal("my.domain.example", activity.Tags.FirstOrDefault(t => t.Key == "server.address").Value); | ||
Assert.Equal("5432", activity.Tags.FirstOrDefault(t => t.Key == "server.port").Value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters