Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace our own instrumentation for MongoDB with MongoDB.Driver.Core.Extensions.DiagnosticSources #259

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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