diff --git a/src/OpenTelemetry.Instrumentation.EntityFrameworkCore.Tests/EntityFrameworkInstrumentationTests.cs b/src/OpenTelemetry.Instrumentation.EntityFrameworkCore.Tests/EntityFrameworkInstrumentationTests.cs new file mode 100644 index 0000000000..5d67bbc6c3 --- /dev/null +++ b/src/OpenTelemetry.Instrumentation.EntityFrameworkCore.Tests/EntityFrameworkInstrumentationTests.cs @@ -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("Host"); + var dataSourceFetcher = new PropertyFetcher("DataSource"); + var portFetcher = new PropertyFetcher("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); + } +} diff --git a/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/CHANGELOG.md index c2510f474b..e56bd7d103 100644 --- a/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* Fixed the `server.address` field in EntityFrameworkCore spans to populate with the server domain name without the `tcp` prefix. + ([#IssueNumber](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/IssueNumber)) + ## 1.10.0-beta.1 Released 2024-Dec-09 diff --git a/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/Implementation/EntityFrameworkDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/Implementation/EntityFrameworkDiagnosticListener.cs index 509402f807..b179a13d58 100644 --- a/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/Implementation/EntityFrameworkDiagnosticListener.cs +++ b/src/OpenTelemetry.Instrumentation.EntityFrameworkCore/Implementation/EntityFrameworkDiagnosticListener.cs @@ -1,6 +1,3 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - using System.Data; using System.Diagnostics; using System.Reflection; @@ -40,6 +37,8 @@ internal sealed class EntityFrameworkDiagnosticListener : ListenerHandler private readonly PropertyFetcher commandTypeFetcher = new("CommandType"); private readonly PropertyFetcher commandTextFetcher = new("CommandText"); private readonly PropertyFetcher exceptionFetcher = new("Exception"); + private readonly PropertyFetcher hostFetcher = new("Host"); + private readonly PropertyFetcher portFetcher = new("Port"); private readonly EntityFrameworkInstrumentationOptions options; @@ -140,10 +139,24 @@ public override void OnEventWritten(string name, object? payload) break; } - var dataSource = (string)this.dataSourceFetcher.Fetch(connection); - if (!string.IsNullOrEmpty(dataSource)) + var host = (string)this.hostFetcher.Fetch(connection); + if (!string.IsNullOrEmpty(host)) + { + activity.AddTag(AttributeServerAddress, host); + } + else + { + var dataSource = (string)this.dataSourceFetcher.Fetch(connection); + if (!string.IsNullOrEmpty(dataSource)) + { + activity.AddTag(AttributeServerAddress, dataSource); + } + } + + var port = (string)this.portFetcher.Fetch(connection); + if (!string.IsNullOrEmpty(port)) { - activity.AddTag(AttributeServerAddress, dataSource); + activity.AddTag(AttributeServerAddress, port); } if (this.options.EmitOldAttributes)