diff --git a/src/MongoDB.Driver.Core.Extensions.DiagnosticSources/DiagnosticsActivityEventSubscriber.cs b/src/MongoDB.Driver.Core.Extensions.DiagnosticSources/DiagnosticsActivityEventSubscriber.cs index 946d56e..da29405 100644 --- a/src/MongoDB.Driver.Core.Extensions.DiagnosticSources/DiagnosticsActivityEventSubscriber.cs +++ b/src/MongoDB.Driver.Core.Extensions.DiagnosticSources/DiagnosticsActivityEventSubscriber.cs @@ -46,16 +46,17 @@ private void Handle(CommandStartedEvent @event) return; } + var databaseName = @event.DatabaseNamespace?.DatabaseName; var collectionName = @event.GetCollectionName(); - // https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/database.md - activity.DisplayName = collectionName == null ? $"mongodb.{@event.CommandName}" : $"{collectionName}.{@event.CommandName}"; + // https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md + activity.DisplayName = string.IsNullOrEmpty(collectionName) ? $"{@event.CommandName} {databaseName}" : $"{@event.CommandName} {collectionName}"; 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("db.namespace", databaseName); + activity.AddTag("db.collection.name", collectionName); + activity.AddTag("db.operation.name", @event.CommandName); activity.AddTag("network.transport", "tcp"); var endPoint = @event.ConnectionId?.ServerId?.EndPoint; @@ -73,7 +74,7 @@ private void Handle(CommandStartedEvent @event) if (activity.IsAllDataRequested && _options.CaptureCommandText) { - activity.AddTag("db.statement", @event.Command.ToString()); + activity.AddTag("db.query.text", @event.Command.ToString()); } _activityMap.TryAdd(@event.RequestId, activity); @@ -96,14 +97,18 @@ private void Handle(CommandFailedEvent @event) { WithReplacedActivityCurrent(activity, () => { - if (activity.IsAllDataRequested) + var tags = new ActivityTagsCollection { - activity.SetStatus(ActivityStatusCode.Error, @event.Failure.Message); - activity.AddTag("exception.type", @event.Failure.GetType().FullName); - activity.AddTag("exception.message", @event.Failure.Message); - activity.AddTag("exception.stacktrace", @event.Failure.StackTrace); + { "exception.type", @event.Failure.GetType().FullName }, + { "exception.stacktrace", @event.Failure.ToString() }, + }; + + if (!string.IsNullOrEmpty(@event.Failure.Message)) + { + tags.Add("exception.message", @event.Failure.Message); } + activity.AddEvent(new ActivityEvent("exception", DateTimeOffset.UtcNow, tags)); activity.SetStatus(ActivityStatusCode.Error); activity.Stop(); }); diff --git a/tests/MongoDB.Driver.Core.Extensions.DiagnosticSources.Tests/DiagnosticsActivityEventSubscriberTests.cs b/tests/MongoDB.Driver.Core.Extensions.DiagnosticSources.Tests/DiagnosticsActivityEventSubscriberTests.cs index f292cb2..b04730c 100644 --- a/tests/MongoDB.Driver.Core.Extensions.DiagnosticSources.Tests/DiagnosticsActivityEventSubscriberTests.cs +++ b/tests/MongoDB.Driver.Core.Extensions.DiagnosticSources.Tests/DiagnosticsActivityEventSubscriberTests.cs @@ -179,15 +179,15 @@ public void Should_record_all_data() { activity.ShouldNotBeNull(); activity.OperationName.ShouldBe(DiagnosticsActivityEventSubscriber.ActivityName); - var instanceTag = activity.Tags.SingleOrDefault(t => t.Key == "db.name"); + var instanceTag = activity.Tags.SingleOrDefault(t => t.Key == "db.namespace"); instanceTag.ShouldNotBe(default); 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 == "db.collection.name").Value.ShouldBe("my_collection"); + activity.Tags.SingleOrDefault(t => t.Key == "db.operation.name").Value.ShouldBe("update"); + activity.Tags.SingleOrDefault(t => t.Key == "db.query.text").ShouldBe(default); activity.Tags.SingleOrDefault(t => t.Key == "server.address").Value.ShouldBe("localhost"); activity.Tags.SingleOrDefault(t => t.Key == "server.port").Value.ShouldBe("8000"); activity.Status.ShouldBe(ActivityStatusCode.Unset); @@ -240,7 +240,7 @@ public void Should_record_command_text_when_option_set() { activity.ShouldNotBeNull(); activity.OperationName.ShouldBe(DiagnosticsActivityEventSubscriber.ActivityName); - var statementTag = activity.Tags.SingleOrDefault(t => t.Key == "db.statement"); + var statementTag = activity.Tags.SingleOrDefault(t => t.Key == "db.query.text"); statementTag.ShouldNotBe(default); statementTag.Value.ShouldBe(command.ToString());