Skip to content

Commit

Permalink
Add instrumentation extensions (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
glucaci authored Feb 2, 2022
1 parent 4e43bc1 commit b5075d8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Context.InterferingTests/MongoDatabaseBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
Expand Down Expand Up @@ -450,6 +451,24 @@ public void ConfigureCollection_SetDifferentSettingsToCollection_CollectionConfi

#endregion

#region AddInstrumentation Tests

[Fact]
public void AddInstrumentation_Command_ActivityCreated()
{
// Arrange
var mongoDatabaseBuilder = new MongoDatabaseBuilder(_mongoOptions);
mongoDatabaseBuilder.AddInstrumentation();

// Act
MongoDbContextData result = mongoDatabaseBuilder.Build();

// Assert
result.Client.Settings.ClusterConfigurator.Should().NotBeNull();
}

#endregion

#region Private Helpers

private class DuplicateTestConvention1 : ConventionBase
Expand Down Expand Up @@ -573,6 +592,6 @@ object IBsonSerializer.Deserialize(
}
}

#endregion
#endregion
}
}
1 change: 1 addition & 0 deletions src/Context/Context.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.14.1" />
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="1.1.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0'">
Expand Down
36 changes: 36 additions & 0 deletions src/Context/MongoInstrumentationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using MongoDB.Driver;
using MongoDB.Driver.Core.Configuration;
using MongoDB.Driver.Core.Extensions.DiagnosticSources;

namespace MongoDB.Extensions.Context;

public static class MongoInstrumentationExtensions
{
public static IMongoDatabaseBuilder AddInstrumentation(
this IMongoDatabaseBuilder mongoDatabaseBuilder,
Action<InstrumentationOptions>? configureInstrumentation = default,
Action<ClusterBuilder>? configureCluster = default)
{
return mongoDatabaseBuilder
.ConfigureConnection(s => s
.AddInstrumentation(configureInstrumentation, configureCluster));
}

public static MongoClientSettings AddInstrumentation(
this MongoClientSettings mongoClientSettings,
Action<InstrumentationOptions>? configureInstrumentation = default,
Action<ClusterBuilder>? configureCluster = default)
{
var instrumentationOptions = new InstrumentationOptions { CaptureCommandText = true };
configureInstrumentation?.Invoke(instrumentationOptions);

mongoClientSettings.ClusterConfigurator = builder =>
{
builder.Subscribe(new DiagnosticsActivityEventSubscriber(instrumentationOptions));
configureCluster?.Invoke(builder);
};

return mongoClientSettings;
}
}

0 comments on commit b5075d8

Please sign in to comment.