Skip to content

Commit

Permalink
Replace our own instrumentation for MongoDB with MongoDB.Driver.Core.…
Browse files Browse the repository at this point in the history
…Extensions.DiagnosticSources

The less maintenance work around such, the better.

Effects:
1. Change listened to ActivitySource from `Tingle.Extensions.MongoDB` to `MongoDB.Driver.Core.Extensions.DiagnosticSources`
2. The tags use for instrumentation are the official supported ones by OpenTelemetry. See [docs](https://opentelemetry.io/docs/specs/semconv/database/database-spans/)
  • Loading branch information
mburumaxwell committed May 22, 2024
1 parent b9a8e1d commit 20c1d74
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 520 deletions.
145 changes: 0 additions & 145 deletions src/Tingle.Extensions.MongoDB/Diagnostics/MongoDbDiagnosticEvents.cs

This file was deleted.

58 changes: 13 additions & 45 deletions src/Tingle.Extensions.MongoDB/MongoDbContextOptions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using MongoDB.Driver;
using MongoDB.Driver.Core.Events;
using MongoDB.Driver.Core.Extensions.DiagnosticSources;
using System.Diagnostics.CodeAnalysis;
using Tingle.Extensions.MongoDB.Diagnostics;

namespace Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -122,16 +121,9 @@ public virtual MongoDbContextOptionsBuilder UseApplicationServiceProvider(IServi
/// Sets the <see cref="MongoUrl"/> to use when configuring the context.
/// </summary>
/// <param name="url">The <see cref="MongoUrl"/> to be used</param>
/// <param name="instrumentCommandText">
/// Whether the command text should be captured in instrumentation.
/// </param>
/// <param name="shouldInstrument">
/// Delegate for determining if a <see cref="CommandStartedEvent"/> should be instrumented.
/// </param>
/// <param name="instrumentationOptions">The options to use for instrumentation.</param>
/// <returns></returns>
public virtual MongoDbContextOptionsBuilder UseMongoUrl(MongoUrl url,
bool instrumentCommandText = true,
Func<CommandStartedEvent, bool>? shouldInstrument = null)
public virtual MongoDbContextOptionsBuilder UseMongoUrl(MongoUrl url, InstrumentationOptions? instrumentationOptions = null)
{
ArgumentNullException.ThrowIfNull(url);
if (string.IsNullOrWhiteSpace(url.DatabaseName))
Expand All @@ -146,10 +138,7 @@ public virtual MongoDbContextOptionsBuilder UseMongoUrl(MongoUrl url,
{
settings.ClusterConfigurator = builder =>
{
builder.Subscribe(
new MongoDbDiagnosticEvents(
captureCommandText: instrumentCommandText,
shouldStartActivity: shouldInstrument));
builder.Subscribe(new DiagnosticsActivityEventSubscriber(instrumentationOptions ?? new() { CaptureCommandText = true }));
};
});

Expand All @@ -165,19 +154,12 @@ public virtual MongoDbContextOptionsBuilder UseMongoUrl(MongoUrl url,
/// <c>mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]</c>
/// e.g. <c>mongodb://localhost:27017/myDatabase</c>
/// </param>
/// <param name="instrumentCommandText">
/// Whether the command text should be captured in instrumentation.
/// </param>
/// <param name="shouldInstrument">
/// Delegate for determining if a <see cref="CommandStartedEvent"/> should be instrumented.
/// </param>
/// <param name="instrumentationOptions">The options to use for instrumentation.</param>
/// <returns></returns>
public virtual MongoDbContextOptionsBuilder UseMongoConnectionString(string connectionString,
bool instrumentCommandText = true,
Func<CommandStartedEvent, bool>? shouldInstrument = null)
public virtual MongoDbContextOptionsBuilder UseMongoConnectionString(string connectionString, InstrumentationOptions? instrumentationOptions = null)
{
ArgumentNullException.ThrowIfNull(connectionString);
return UseMongoUrl(new MongoUrl(connectionString), instrumentCommandText, shouldInstrument);
return UseMongoUrl(new MongoUrl(connectionString), instrumentationOptions);
}

/// <summary>
Expand Down Expand Up @@ -246,17 +228,10 @@ public MongoDbContextOptionsBuilder() : this(new MongoDbContextOptions<TContext>
/// Sets the <see cref="MongoUrl"/> to use when configuring the context.
/// </summary>
/// <param name="url">The <see cref="MongoUrl"/> to be used</param>
/// <param name="instrumentCommandText">
/// Whether the command text should be captured in instrumentation.
/// </param>
/// <param name="shouldInstrument">
/// Delegate for determining if a <see cref="CommandStartedEvent"/> should be instrumented.
/// </param>
/// <param name="instrumentationOptions">The options to use for instrumentation.</param>
/// <returns></returns>
public new virtual MongoDbContextOptionsBuilder<TContext> UseMongoUrl(MongoUrl url,
bool instrumentCommandText = true,
Func<CommandStartedEvent, bool>? shouldInstrument = null)
=> (MongoDbContextOptionsBuilder<TContext>)base.UseMongoUrl(url, instrumentCommandText, shouldInstrument);
public new virtual MongoDbContextOptionsBuilder<TContext> UseMongoUrl(MongoUrl url, InstrumentationOptions? instrumentationOptions = null)
=> (MongoDbContextOptionsBuilder<TContext>)base.UseMongoUrl(url, instrumentationOptions);

/// <summary>
/// Sets the <see cref="MongoUrl"/> to use when configuring the context by
Expand All @@ -267,17 +242,10 @@ public MongoDbContextOptionsBuilder() : this(new MongoDbContextOptions<TContext>
/// <c>mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]</c>
/// e.g. <c>mongodb://localhost:27017/myDatabase</c>
/// </param>
/// <param name="instrumentCommandText">
/// Whether the command text should be captured in instrumentation.
/// </param>
/// <param name="shouldInstrument">
/// Delegate for determining if a <see cref="CommandStartedEvent"/> should be instrumented.
/// </param>
/// <param name="instrumentationOptions">The options to use for instrumentation.</param>
/// <returns></returns>
public new virtual MongoDbContextOptionsBuilder<TContext> UseMongoConnectionString(string connectionString,
bool instrumentCommandText = true,
Func<CommandStartedEvent, bool>? shouldInstrument = null)
=> (MongoDbContextOptionsBuilder<TContext>)base.UseMongoConnectionString(connectionString, instrumentCommandText, shouldInstrument);
public new virtual MongoDbContextOptionsBuilder<TContext> UseMongoConnectionString(string connectionString, InstrumentationOptions? instrumentationOptions=null)
=> (MongoDbContextOptionsBuilder<TContext>)base.UseMongoConnectionString(connectionString, instrumentationOptions);

/// <summary>
/// Further configure the existing instance of <see cref="MongoClientSettings"/>.
Expand Down
2 changes: 1 addition & 1 deletion src/Tingle.Extensions.MongoDB/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ If MongoDB client is configured with a connection string, add the `ConnectionStr

## Diagnostics

Events for Mongo are produced on an `ActivitySource` named `MongoDB`. This is done by registering and instance of `IEventSubscriber` named `MongoDbDiagnosticEvents` to the `ClusterConfigurator`. However, this is done automatically when using `MongoDbContext`.
Events for Mongo are produced on an `ActivitySource` named [`MongoDB.Driver.Core.Extensions.DiagnosticSources`](https://github.com/jbogard/MongoDB.Driver.Core.Extensions.DiagnosticSources), automatically when using `MongoDbContext`.

## HealthChecks

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="8.0.5" />
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="1.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 20c1d74

Please sign in to comment.