Skip to content

feat: Add support for MongoDB.Driver 3.x and above. #3065

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

Merged
merged 4 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ SPDX-License-Identifier: Apache-2.0
-->
<extension xmlns="urn:newrelic-extension">
<instrumentation>

<tracerFactory name="MongoCollectionImplWrapper">
<match assemblyName="MongoDB.Driver" className="MongoDB.Driver.MongoCollectionImpl`1">
<exactMethodMatcher methodName="AggregateAsync"/>
Expand Down Expand Up @@ -37,86 +38,115 @@ SPDX-License-Identifier: Apache-2.0
</match>
</tracerFactory>

<tracerFactory name="AsyncCursorWrapper">
<match assemblyName="MongoDB.Driver.Core" className="MongoDB.Driver.Core.Operations.AsyncCursor`1">
<exactMethodMatcher methodName="GetNextBatch"/>
<exactMethodMatcher methodName="GetNextBatchAsync"/>
</match>
</tracerFactory>
<tracerFactory name="AsyncCursorWrapper">
<match assemblyName="MongoDB.Driver.Core" className="MongoDB.Driver.Core.Operations.AsyncCursor`1" maxVersion="2.999.0">
<exactMethodMatcher methodName="GetNextBatch"/>
<exactMethodMatcher methodName="GetNextBatchAsync"/>
</match>

<match assemblyName="MongoDB.Driver" className="MongoDB.Driver.Core.Operations.AsyncCursor`1" minVersion="3.0.0">
<exactMethodMatcher methodName="GetNextBatch"/>
<exactMethodMatcher methodName="GetNextBatchAsync"/>
</match>
</tracerFactory>

<tracerFactory name="MongoDatabaseWrapper">
<match assemblyName="MongoDB.Driver" className="MongoDB.Driver.MongoDatabaseImpl">
<exactMethodMatcher methodName="Aggregate"/>
<exactMethodMatcher methodName="AggregateToCollection"/>
<exactMethodMatcher methodName="CreateCollection"/>
<exactMethodMatcher methodName="CreateView"/>
<exactMethodMatcher methodName="DropCollection"/>
<exactMethodMatcher methodName="ListCollections"/>
<exactMethodMatcher methodName="ListCollectionNames"/>
<exactMethodMatcher methodName="RenameCollection"/>
<exactMethodMatcher methodName="RunCommand"/>
<exactMethodMatcher methodName="Watch"/>
<tracerFactory name="MongoDatabaseWrapper">
<match assemblyName="MongoDB.Driver" className="MongoDB.Driver.MongoDatabaseImpl" maxVersion="2.999.0">
<exactMethodMatcher methodName="Aggregate"/>
<exactMethodMatcher methodName="AggregateToCollection"/>
<exactMethodMatcher methodName="CreateCollection"/>
<exactMethodMatcher methodName="CreateView"/>
<exactMethodMatcher methodName="DropCollection"/>
<exactMethodMatcher methodName="ListCollections"/>
<exactMethodMatcher methodName="ListCollectionNames"/>
<exactMethodMatcher methodName="RenameCollection"/>
<exactMethodMatcher methodName="RunCommand"/>
<exactMethodMatcher methodName="Watch"/>

<exactMethodMatcher methodName="AggregateAsync"/>
<exactMethodMatcher methodName="AggregateToCollectionAsync"/>
<exactMethodMatcher methodName="CreateCollectionAsync"/>
<exactMethodMatcher methodName="CreateViewAsync"/>
<exactMethodMatcher methodName="DropCollectionAsync"/>
<exactMethodMatcher methodName="ListCollectionsAsync"/>
<exactMethodMatcher methodName="ListCollectionNamesAsync"/>
<exactMethodMatcher methodName="RenameCollectionAsync"/>
<exactMethodMatcher methodName="RunCommandAsync"/>
<exactMethodMatcher methodName="WatchAsync"/>
</match>

<match assemblyName="MongoDB.Driver" className="MongoDB.Driver.MongoDatabase" minVersion="3.0.0">
<exactMethodMatcher methodName="Aggregate"/>
<exactMethodMatcher methodName="AggregateToCollection"/>
<exactMethodMatcher methodName="CreateCollection"/>
<exactMethodMatcher methodName="CreateView"/>
<exactMethodMatcher methodName="DropCollection"/>
<exactMethodMatcher methodName="ListCollections"/>
<exactMethodMatcher methodName="ListCollectionNames"/>
<exactMethodMatcher methodName="RenameCollection"/>
<exactMethodMatcher methodName="RunCommand"/>
<exactMethodMatcher methodName="Watch"/>

<exactMethodMatcher methodName="AggregateAsync"/>
<exactMethodMatcher methodName="AggregateToCollectionAsync"/>
<exactMethodMatcher methodName="CreateCollectionAsync"/>
<exactMethodMatcher methodName="CreateViewAsync"/>
<exactMethodMatcher methodName="DropCollectionAsync"/>
<exactMethodMatcher methodName="ListCollectionsAsync"/>
<exactMethodMatcher methodName="ListCollectionNamesAsync"/>
<exactMethodMatcher methodName="RenameCollectionAsync"/>
<exactMethodMatcher methodName="RunCommandAsync"/>
<exactMethodMatcher methodName="WatchAsync"/>
</match>
</tracerFactory>
<exactMethodMatcher methodName="AggregateAsync"/>
<exactMethodMatcher methodName="AggregateToCollectionAsync"/>
<exactMethodMatcher methodName="CreateCollectionAsync"/>
<exactMethodMatcher methodName="CreateViewAsync"/>
<exactMethodMatcher methodName="DropCollectionAsync"/>
<exactMethodMatcher methodName="ListCollectionsAsync"/>
<exactMethodMatcher methodName="ListCollectionNamesAsync"/>
<exactMethodMatcher methodName="RenameCollectionAsync"/>
<exactMethodMatcher methodName="RunCommandAsync"/>
<exactMethodMatcher methodName="WatchAsync"/>
</match>
</tracerFactory>

<tracerFactory name="MongoQueryProviderImplWrapper">
<match assemblyName="MongoDB.Driver" className="MongoDB.Driver.Linq.MongoQueryProviderImpl`1">
<exactMethodMatcher methodName="ExecuteModel"/>
<exactMethodMatcher methodName="ExecuteModelAsync"/>
</match>
</tracerFactory>
<tracerFactory name="MongoQueryProviderImplWrapper">
<match assemblyName="MongoDB.Driver" className="MongoDB.Driver.Linq.MongoQueryProviderImpl`1">
<exactMethodMatcher methodName="ExecuteModel"/>
<exactMethodMatcher methodName="ExecuteModelAsync"/>
</match>
</tracerFactory>

<tracerFactory name="MongoIndexManagerBaseWrapper">
<match assemblyName="MongoDB.Driver" className="MongoDB.Driver.MongoIndexManagerBase`1">
<exactMethodMatcher methodName="CreateOne"/>
<exactMethodMatcher methodName="CreateOneAsync"/>
</match>
</tracerFactory>
<tracerFactory name="MongoIndexManagerBaseWrapper">
<match assemblyName="MongoDB.Driver" className="MongoDB.Driver.MongoIndexManagerBase`1">
<exactMethodMatcher methodName="CreateOne"/>
<exactMethodMatcher methodName="CreateOneAsync"/>
</match>
</tracerFactory>

<tracerFactory name="MongoIndexManagerWrapper">
<match assemblyName="MongoDB.Driver" className="MongoDB.Driver.MongoCollectionImpl`1+MongoIndexManager">
<exactMethodMatcher methodName="CreateMany"/>
<exactMethodMatcher methodName="DropAll"/>
<exactMethodMatcher methodName="DropOne"/>
<exactMethodMatcher methodName="List"/>
<tracerFactory name="MongoIndexManagerWrapper">
<match assemblyName="MongoDB.Driver" className="MongoDB.Driver.MongoCollectionImpl`1+MongoIndexManager">
<exactMethodMatcher methodName="CreateMany"/>
<exactMethodMatcher methodName="DropAll"/>
<exactMethodMatcher methodName="DropOne"/>
<exactMethodMatcher methodName="List"/>

<exactMethodMatcher methodName="CreateManyAsync"/>
<exactMethodMatcher methodName="DropAllAsync"/>
<exactMethodMatcher methodName="DropOneAsync"/>
<exactMethodMatcher methodName="ListAsync"/>
</match>
</tracerFactory>
<exactMethodMatcher methodName="CreateManyAsync"/>
<exactMethodMatcher methodName="DropAllAsync"/>
<exactMethodMatcher methodName="DropOneAsync"/>
<exactMethodMatcher methodName="ListAsync"/>
</match>
</tracerFactory>

<tracerFactory name="MongoCollectionBaseWrapper">
<match assemblyName="MongoDB.Driver" className="MongoDB.Driver.MongoCollectionBase`1">
<exactMethodMatcher methodName="DeleteMany"/>
<exactMethodMatcher methodName="DeleteOne"/>
<exactMethodMatcher methodName="InsertOne"/>
<exactMethodMatcher methodName="InsertMany"/>
<exactMethodMatcher methodName="ReplaceOne"/>
<exactMethodMatcher methodName="UpdateMany"/>
<exactMethodMatcher methodName="UpdateOne"/>
<tracerFactory name="MongoCollectionBaseWrapper">
<match assemblyName="MongoDB.Driver" className="MongoDB.Driver.MongoCollectionBase`1">
<exactMethodMatcher methodName="DeleteMany"/>
<exactMethodMatcher methodName="DeleteOne"/>
<exactMethodMatcher methodName="InsertOne"/>
<exactMethodMatcher methodName="InsertMany"/>
<exactMethodMatcher methodName="ReplaceOne"/>
<exactMethodMatcher methodName="UpdateMany"/>
<exactMethodMatcher methodName="UpdateOne"/>

<exactMethodMatcher methodName="DeleteManyAsync"/>
<exactMethodMatcher methodName="DeleteOneAsync"/>
<exactMethodMatcher methodName="InsertOneAsync"/>
<exactMethodMatcher methodName="InsertManyAsync"/>
<exactMethodMatcher methodName="ReplaceOneAsync"/>
<exactMethodMatcher methodName="UpdateManyAsync"/>
<exactMethodMatcher methodName="UpdateOneAsync"/>
</match>
</tracerFactory>
<exactMethodMatcher methodName="DeleteManyAsync"/>
<exactMethodMatcher methodName="DeleteOneAsync"/>
<exactMethodMatcher methodName="InsertOneAsync"/>
<exactMethodMatcher methodName="InsertManyAsync"/>
<exactMethodMatcher methodName="ReplaceOneAsync"/>
<exactMethodMatcher methodName="UpdateManyAsync"/>
<exactMethodMatcher methodName="UpdateOneAsync"/>
</match>
</tracerFactory>

</instrumentation>
</extension>
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.ComponentModel.Design;
using System.Net;
using NewRelic.Agent.Extensions.Helpers;
using NewRelic.Agent.Extensions.Parsing;
using NewRelic.Agent.Extensions.Providers.Wrapper;
using NewRelic.Agent.Extensions.Parsing.ConnectionString;
using NewRelic.Reflection;
using System.Reflection;

namespace NewRelic.Providers.Wrapper.MongoDb26
{
Expand All @@ -31,38 +34,45 @@ public static class MongoDbHelper
private static Func<object, string> _getHost;
private static Func<object, int> _getPort;

private static readonly Version _mongo3Version = Version.Parse("3.0.0.0");
private static Version _version;

public static string GetCollectionName(object collectionNamespace)
{
var getter = _getCollectionName ?? (_getCollectionName = VisibilityBypasser.Instance.GeneratePropertyAccessor<string>("MongoDB.Driver.Core", "MongoDB.Driver.CollectionNamespace", "CollectionName"));
_version ??= Version.Parse(VersionHelpers.GetLibraryVersion(collectionNamespace.GetType().AssemblyQualifiedName));
var getter = _version >= _mongo3Version ? GetCollectionNameV3(collectionNamespace) : GetCollectionNameV2(collectionNamespace);
return getter(collectionNamespace);
}

public static string GetDatabaseNameFromCollectionNamespace(object collectionNamespace)
{
var databaseNamespaceGetter = _getDatabaseNamespaceFromCollectionNamespace ?? (_getDatabaseNamespaceFromCollectionNamespace = VisibilityBypasser.Instance.GeneratePropertyAccessor<object>("MongoDB.Driver.Core", "MongoDB.Driver.CollectionNamespace", "DatabaseNamespace"));
_version ??= Version.Parse(VersionHelpers.GetLibraryVersion(collectionNamespace.GetType().AssemblyQualifiedName));
var databaseNamespaceGetter = _version >= _mongo3Version ? GetDatabaseNameFromCollectionNamespaceV3(collectionNamespace) : GetDatabaseNameFromCollectionNamespaceV2(collectionNamespace);
var databaseNamespace = databaseNamespaceGetter(collectionNamespace);

return GetDatabaseNameFromDatabaseNamespace(databaseNamespace);
}

public static string GetDatabaseNameFromDatabase(object database)
{
var databaseNamespaceGetter = _getDatabaseNamespaceFromDatabase ?? (_getDatabaseNamespaceFromDatabase = VisibilityBypasser.Instance.GeneratePropertyAccessor<object>("MongoDB.Driver", "MongoDB.Driver.MongoDatabaseImpl", "DatabaseNamespace"));
_version ??= Version.Parse(VersionHelpers.GetLibraryVersion(database.GetType().AssemblyQualifiedName));
var databaseNamespaceGetter = _version >= _mongo3Version ? GetDatabaseNameFromDatabaseV3(database) : GetDatabaseNameFromDatabaseV2(database);
var databaseNamespace = databaseNamespaceGetter(database);

return GetDatabaseNameFromDatabaseNamespace(databaseNamespace);
}

private static string GetDatabaseNameFromDatabaseNamespace(object databaseNamespace)
{
var databaseNameGetter = _getDatabaseName ?? (_getDatabaseName = VisibilityBypasser.Instance.GeneratePropertyAccessor<string>("MongoDB.Driver.Core", "MongoDB.Driver.DatabaseNamespace", "DatabaseName"));
_version ??= Version.Parse(VersionHelpers.GetLibraryVersion(databaseNamespace.GetType().AssemblyQualifiedName));
var databaseNameGetter = _version >= _mongo3Version ? GetDatabaseNameFromDatabaseNamespaceV3(databaseNamespace) : GetDatabaseNameFromDatabaseNamespaceV2(databaseNamespace);
return databaseNameGetter(databaseNamespace);
}

private static EndPoint GetEndPoint(object owner)
{
var getter = _getEndPoint ?? (_getEndPoint = VisibilityBypasser.Instance.GeneratePropertyAccessor<EndPoint>("MongoDB.Driver.Core", "MongoDB.Driver.Core.Servers.Server", "EndPoint"));
_version ??= Version.Parse(VersionHelpers.GetLibraryVersion(owner.GetType().AssemblyQualifiedName));
var getter = _version >= _mongo3Version ? GetEndPointV3(owner) : GetEndPointV2(owner);
return getter(owner);
}

Expand Down Expand Up @@ -151,9 +161,10 @@ public static ConnectionInfo GetConnectionInfoFromDatabase(object database, stri

private static IList GetServersFromDatabase(object database)
{
var clientGetter = _getClient ?? (_getClient = VisibilityBypasser.Instance.GeneratePropertyAccessor<object>("MongoDB.Driver", "MongoDB.Driver.MongoDatabaseImpl", "Client"));
var settingsGetter = _getSettings ?? (_getSettings = VisibilityBypasser.Instance.GeneratePropertyAccessor<object>("MongoDB.Driver", "MongoDB.Driver.MongoClient", "Settings"));
var serversGetter = _getServers ?? (_getServers = VisibilityBypasser.Instance.GenerateFieldReadAccessor<IList>("MongoDB.Driver", "MongoDB.Driver.MongoClientSettings", "_servers"));
_version ??= Version.Parse(VersionHelpers.GetLibraryVersion(database.GetType().AssemblyQualifiedName));
var clientGetter = _version >= _mongo3Version ? GetClientFromDatabaseV3(database) : GetClientFromDatabaseV2(database);
var settingsGetter = _getSettings ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>("MongoDB.Driver", "MongoDB.Driver.MongoClient", "Settings");
var serversGetter = _getServers ??= VisibilityBypasser.Instance.GenerateFieldReadAccessor<IList>("MongoDB.Driver", "MongoDB.Driver.MongoClientSettings", "_servers");

var client = clientGetter(database);
var settings = settingsGetter(client);
Expand All @@ -162,12 +173,80 @@ private static IList GetServersFromDatabase(object database)

private static void GetHostAndPortFromServer(object server, out string host, out int port)
{
var hostGetter = _getHost ?? (_getHost = VisibilityBypasser.Instance.GeneratePropertyAccessor<string>("MongoDB.Driver", "MongoDB.Driver.MongoServerAddress", "Host"));
var portGetter = _getPort ?? (_getPort = VisibilityBypasser.Instance.GeneratePropertyAccessor<int>("MongoDB.Driver", "MongoDB.Driver.MongoServerAddress", "Port"));
var hostGetter = _getHost ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<string>("MongoDB.Driver", "MongoDB.Driver.MongoServerAddress", "Host");
var portGetter = _getPort ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<int>("MongoDB.Driver", "MongoDB.Driver.MongoServerAddress", "Port");

host = hostGetter(server);
port = portGetter(server);
}


#region Client V2

private static Func<object, string> GetCollectionNameV2(object collectionNamespace)
{
return _getCollectionName ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<string>("MongoDB.Driver.Core", "MongoDB.Driver.CollectionNamespace", "CollectionName");
}

private static Func<object, object> GetDatabaseNameFromCollectionNamespaceV2(object collectionNamespace)
{
return _getDatabaseNamespaceFromCollectionNamespace ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>("MongoDB.Driver.Core", "MongoDB.Driver.CollectionNamespace", "DatabaseNamespace");
}

private static Func<object, object> GetDatabaseNameFromDatabaseV2(object database)
{
return _getDatabaseNamespaceFromDatabase ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>("MongoDB.Driver", "MongoDB.Driver.MongoDatabaseImpl", "DatabaseNamespace");
}

private static Func<object, string> GetDatabaseNameFromDatabaseNamespaceV2(object databaseNamespace)
{
return _getDatabaseName ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<string>("MongoDB.Driver.Core", "MongoDB.Driver.DatabaseNamespace", "DatabaseName");
}

private static Func<object, EndPoint> GetEndPointV2(object owner)
{
return _getEndPoint ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<EndPoint>("MongoDB.Driver.Core", "MongoDB.Driver.Core.Servers.Server", "EndPoint");
}

private static Func<object, object> GetClientFromDatabaseV2(object database)
{
return _getClient ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>("MongoDB.Driver", "MongoDB.Driver.MongoDatabaseImpl", "Client");
}

#endregion

#region Client V3

private static Func<object, string> GetCollectionNameV3(object collectionNamespace)
{
return _getCollectionName ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<string>("MongoDB.Driver", "MongoDB.Driver.CollectionNamespace", "CollectionName");
}

private static Func<object, object> GetDatabaseNameFromCollectionNamespaceV3(object collectionNamespace)
{
return _getDatabaseNamespaceFromCollectionNamespace ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>("MongoDB.Driver", "MongoDB.Driver.CollectionNamespace", "DatabaseNamespace");
}

private static Func<object, object> GetDatabaseNameFromDatabaseV3(object database)
{
return _getDatabaseNamespaceFromDatabase ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>("MongoDB.Driver", "MongoDB.Driver.MongoDatabase", "DatabaseNamespace");
}

private static Func<object, string> GetDatabaseNameFromDatabaseNamespaceV3(object databaseNamespace)
{
return _getDatabaseName ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<string>("MongoDB.Driver", "MongoDB.Driver.DatabaseNamespace", "DatabaseName");
}

private static Func<object, EndPoint> GetEndPointV3(object owner)
{
return _getEndPoint ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<EndPoint>("MongoDB.Driver", "MongoDB.Driver.Core.Servers.Server", "EndPoint");
}

private static Func<object, object> GetClientFromDatabaseV3(object database)
{
return _getClient ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<object>("MongoDB.Driver", "MongoDB.Driver.MongoDatabase", "Client");
}

#endregion
}
}
Loading
Loading