Skip to content

Commit

Permalink
Merge pull request #37 from joegoldman2/feat/otel
Browse files Browse the repository at this point in the history
Update attributes to comply with OTel spec
  • Loading branch information
jbogard authored Sep 27, 2024
2 parents 2805272 + 42db035 commit 4e3a389
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
run: ./Push.ps1
shell: pwsh
- name: Artifacts
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: artifacts
path: artifacts/**/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var mongoClient = new MongoClient(clientSettings);

This package exposes an [`ActivitySource`](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.activitysource?view=net-5.0) with a `Name` the same as the assembly, `MongoDB.Driver.Core.Extensions.DiagnosticSources`. Use this name in any [`ActivityListener`](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.activitylistener?view=net-5.0)-based listeners.

All the available [OpenTelemetry semantic tags](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/database.md) are set.
All the available [OpenTelemetry semantic tags](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md) are set.

This package supports MongoDB C# Driver versions 2.28.0 to 3.0.

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 4e3a389

Please sign in to comment.