From da2a17d755188ab3e79d39e0f755732ba8831d85 Mon Sep 17 00:00:00 2001 From: Mykhailo Shevchuk Date: Tue, 6 Sep 2022 15:38:08 +0300 Subject: [PATCH] Adjusted OTEL tags to correspond spec --- .../DiagnosticsActivityEventSubscriber.cs | 19 ++++++++++--------- ...DiagnosticsActivityEventSubscriberTests.cs | 13 +++++++++---- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/MongoDB.Driver.Core.Extensions.DiagnosticSources/DiagnosticsActivityEventSubscriber.cs b/src/MongoDB.Driver.Core.Extensions.DiagnosticSources/DiagnosticsActivityEventSubscriber.cs index 7f3ccc4..5b08d5c 100644 --- a/src/MongoDB.Driver.Core.Extensions.DiagnosticSources/DiagnosticsActivityEventSubscriber.cs +++ b/src/MongoDB.Driver.Core.Extensions.DiagnosticSources/DiagnosticsActivityEventSubscriber.cs @@ -53,19 +53,20 @@ private void Handle(CommandStartedEvent @event) activity.DisplayName = collectionName == null ? $"mongodb.{@event.CommandName}" : $"{collectionName}.{@event.CommandName}"; activity.AddTag("db.system", "mongodb"); + activity.AddTag("db.connection_id", @event.ConnectionId?.ToString()); activity.AddTag("db.name", @event.DatabaseNamespace?.DatabaseName); activity.AddTag("db.mongodb.collection", collectionName); activity.AddTag("db.operation", @event.CommandName); + activity.AddTag("net.transport", "ip_tcp"); + var endPoint = @event.ConnectionId?.ServerId?.EndPoint; switch (endPoint) { case IPEndPoint ipEndPoint: - activity.AddTag("db.user", $"mongodb://{ipEndPoint.Address}:{ipEndPoint.Port}"); - activity.AddTag("net.peer.ip", ipEndPoint.Address.ToString()); activity.AddTag("net.peer.port", ipEndPoint.Port.ToString()); + activity.AddTag("net.sock.peer.addr", ipEndPoint.Address.ToString()); break; case DnsEndPoint dnsEndPoint: - activity.AddTag("db.user", $"mongodb://{dnsEndPoint.Host}:{dnsEndPoint.Port}"); activity.AddTag("net.peer.name", dnsEndPoint.Host); activity.AddTag("net.peer.port", dnsEndPoint.Port.ToString()); break; @@ -85,7 +86,7 @@ private void Handle(CommandSucceededEvent @event) { WithReplacedActivityCurrent(activity, () => { - activity.AddTag("otel.status_code", "Ok"); + activity.AddTag("otel.status_code", "OK"); activity.Stop(); }); } @@ -99,11 +100,11 @@ private void Handle(CommandFailedEvent @event) { if (activity.IsAllDataRequested) { - activity.AddTag("otel.status_code", "Error"); + activity.AddTag("otel.status_code", "ERROR"); activity.AddTag("otel.status_description", @event.Failure.Message); - activity.AddTag("error.type", @event.Failure.GetType().FullName); - activity.AddTag("error.msg", @event.Failure.Message); - activity.AddTag("error.stack", @event.Failure.StackTrace); + activity.AddTag("exception.type", @event.Failure.GetType().FullName); + activity.AddTag("exception.message", @event.Failure.Message); + activity.AddTag("exception.stacktrace", @event.Failure.StackTrace); } activity.Stop(); @@ -125,4 +126,4 @@ private static void WithReplacedActivityCurrent(Activity activity, Action action } } } -} +} \ No newline at end of file diff --git a/tests/MongoDB.Driver.Core.Extensions.DiagnosticSources.Tests/DiagnosticsActivityEventSubscriberTests.cs b/tests/MongoDB.Driver.Core.Extensions.DiagnosticSources.Tests/DiagnosticsActivityEventSubscriberTests.cs index 2b1e15b..67432a8 100644 --- a/tests/MongoDB.Driver.Core.Extensions.DiagnosticSources.Tests/DiagnosticsActivityEventSubscriberTests.cs +++ b/tests/MongoDB.Driver.Core.Extensions.DiagnosticSources.Tests/DiagnosticsActivityEventSubscriberTests.cs @@ -136,7 +136,7 @@ public void Should_start_and_log_failed_activity() activity.OperationName.ShouldBe(DiagnosticsActivityEventSubscriber.ActivityName); var statusTag = activity.Tags.SingleOrDefault(t => t.Key == "otel.status_code"); statusTag.ShouldNotBe(default); - statusTag.Value.ShouldBe("Error"); + statusTag.Value.ShouldBe("ERROR"); exceptionFired = true; } }; @@ -185,8 +185,13 @@ public void Should_record_all_data() instanceTag.Value.ShouldBe("test"); activity.Tags.SingleOrDefault(t => t.Key == "db.system").Value.ShouldBe("mongodb"); + activity.Tags.SingleOrDefault(t => t.Key == "db.connection_id").Value.ShouldBe("{ ServerId : { ClusterId : 42, EndPoint : \"Unspecified/localhost:8000\" }, LocalValue : 43 }"); + activity.Tags.SingleOrDefault(t => t.Key == "db.mongodb.collection").Value.ShouldBe("my_collection"); activity.Tags.SingleOrDefault(t => t.Key == "db.operation").Value.ShouldBe("update"); activity.Tags.SingleOrDefault(t => t.Key == "db.statement").ShouldBe(default); + activity.Tags.SingleOrDefault(t => t.Key == "net.peer.name").Value.ShouldBe("localhost"); + activity.Tags.SingleOrDefault(t => t.Key == "net.peer.port").Value.ShouldBe("8000"); + activity.Tags.SingleOrDefault(t => t.Key == "otel.status_code").Value.ShouldBe("OK"); stopFired = true; } @@ -199,7 +204,7 @@ public void Should_record_all_data() behavior.TryGetEventHandler(out var startEvent).ShouldBeTrue(); behavior.TryGetEventHandler(out var stopEvent).ShouldBeTrue(); - var connectionId = new ConnectionId(new ServerId(new ClusterId(), new DnsEndPoint("localhost", 8000))); + var connectionId = new ConnectionId(new ServerId(new ClusterId(42), new DnsEndPoint("localhost", 8000)), 43); var databaseNamespace = new DatabaseNamespace("test"); var command = new BsonDocument(new Dictionary { @@ -211,7 +216,7 @@ public void Should_record_all_data() startFired.ShouldBeTrue(); stopFired.ShouldBeTrue(); } - + [Fact] public void Should_record_command_text_when_option_set() { @@ -334,4 +339,4 @@ public void Should_fire_activity_when_filter_is_null_or_return_true_and_should_n activities.Count.ShouldBe(shouldFireActivity ? 1 : 0); } } -} +} \ No newline at end of file