Skip to content

Commit

Permalink
Update attributes to comply with OTel spec
Browse files Browse the repository at this point in the history
  • Loading branch information
joegoldman2 committed Sep 24, 2024
1 parent 2805272 commit f6223f3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());

Expand Down

0 comments on commit f6223f3

Please sign in to comment.