Skip to content

Commit

Permalink
Merge pull request #18 from mishamyte/adjust-otel-tags
Browse files Browse the repository at this point in the history
Adjusted OTEL tags to correspond spec
  • Loading branch information
jbogard authored Sep 13, 2022
2 parents 12fefe6 + da2a17d commit d2e453b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
});
}
Expand All @@ -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();
Expand All @@ -125,4 +126,4 @@ private static void WithReplacedActivityCurrent(Activity activity, Action action
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down Expand Up @@ -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;
}
Expand All @@ -199,7 +204,7 @@ public void Should_record_all_data()
behavior.TryGetEventHandler<CommandStartedEvent>(out var startEvent).ShouldBeTrue();
behavior.TryGetEventHandler<CommandSucceededEvent>(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<string, object>
{
Expand All @@ -211,7 +216,7 @@ public void Should_record_all_data()
startFired.ShouldBeTrue();
stopFired.ShouldBeTrue();
}

[Fact]
public void Should_record_command_text_when_option_set()
{
Expand Down Expand Up @@ -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);
}
}
}
}

0 comments on commit d2e453b

Please sign in to comment.