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

MongoDB - instrumenation for .NET Framework #2390

Merged
merged 13 commits into from
Apr 14, 2023
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This component adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.h
the entry assembly name instead, only falling back to the process name
in case of an error. If the application uses .NET Framework and is hosted
on IIS, the service name is determined using `SiteName/ApplicationVirtualPath`.
- Add MongoDB instrumentation support for .NET Framework.

### Changed

Expand Down
22 changes: 0 additions & 22 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -286,33 +286,11 @@ are under the following copyright:
Copyright (c) .NET Foundation and Contributors under the MIT License (MIT)
(<https://github.com/dotnet/wcf/blob/main/LICENSE.TXT>).

Library DnsClient is under the following copyright:
Copyright The DnsClient.NET Authors under Apache License Version 2.0
(<https://github.com/opentracing/opentracing-csharp/blob/master/LICENSE>).

Libraries

- MongoDB.Bson,
- MongoDB.Driver.Core,
- MongoDB.Libmongocrypt,
- libmongocrypt,
- libzstd,
- mongocrypt
- snappy32,
- snappy64,
are under the following copyright:
Copyright 2010-present MongoDB Inc. under Apache License Version 2.0
(<https://github.com/mongodb/mongo-csharp-driver/blob/master/License.txt>).

Library MongoDB.Driver.Core.Extensions.DiagnosticSources is under the following copyright:
Copyright MongoDB.Driver.Core.Extensions.DiagnosticSources Authors under Apache
License Version 2.0
(<https://github.com/jbogard/MongoDB.Driver.Core.Extensions.DiagnosticSources/blob/master/LICENSE>).

Library SharpCompress is under the following copyright:
Copyright (c) 2014 Adam Hathcock under the MIT License (MIT)
(<https://github.com/adamhathcock/sharpcompress/blob/master/LICENSE.txt>).

Component Serilog compiled into OpenTelemetry.AutoInstrumentation, OpenTelemetry.AutoInstrumentation.Loader, OpenTelemetry.AutoInstrumentation.StartupHook
is under the following copyright:
Copyright 2013-2017 Serilog Contributors under Apache License Version 2.0
Expand Down
9 changes: 8 additions & 1 deletion build/Build.Steps.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Immutable;
using DependencyListGenerator;
using Nuke.Common;
using Nuke.Common.IO;
Expand Down Expand Up @@ -130,6 +131,7 @@ partial class Build
{
var project = Solution.GetProject(Projects.AutoInstrumentation).GetMSBuildProject();
var packages = Solution.Directory / "src" / "Directory.Packages.props";
var commonExcludedAssets = Solution.Directory / "src" / "CommonExcludedAssets.props";

const string label = $"Transient dependencies auto-generated by {nameof(GenerateNetFxTransientDependencies)}";

Expand All @@ -149,7 +151,12 @@ partial class Build
versionGroup.Label = label;
}

var deps = Generator.EnumerateDependencies(project.FullPath);
var commonExcludedAssetsProject = ProjectModelTasks.ParseProject(commonExcludedAssets);
var excludedProjectsGroup = commonExcludedAssetsProject.Xml.ItemGroups.First(x => x.Condition == string.Empty);

var excludedDependencies = excludedProjectsGroup.Items.Select(x => x.Include).ToImmutableList();

var deps = Generator.EnumerateDependencies(project.FullPath, excludedDependencies);
foreach (var item in deps)
{
if (!packagesGroup.Items.Any(x => x.Include == item.Name))
Expand Down
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ due to lack of stable semantic convention.
| `HTTPCLIENT` | [System.Net.Http.HttpClient](https://docs.microsoft.com/dotnet/api/system.net.http.httpclient) and [System.Net.HttpWebRequest](https://docs.microsoft.com/dotnet/api/system.net.httpwebrequest) | * | source | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| `QUARTZ` | [Quartz](https://www.nuget.org/packages/Quartz) **Not supported on .NET Framework 4.7.1 and older** | ≥3.4.0 | source | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| `MASSTRANSIT` | [MassTransit](https://www.nuget.org/packages/MassTransit) **Not supported on .NET Framework** | ≥8.0.0 | source | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| `MONGODB` | [MongoDB.Driver.Core](https://www.nuget.org/packages/MongoDB.Driver.Core) **Not supported on .NET Framework** | ≥2.13.3 & < 3.0.0 | source & bytecode | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| `MONGODB` | [MongoDB.Driver.Core](https://www.nuget.org/packages/MongoDB.Driver.Core) | ≥2.13.3 & < 3.0.0 | source & bytecode | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| `MYSQLDATA` | [MySql.Data](https://www.nuget.org/packages/MySql.Data) **Not supported on .NET Framework** | ≥6.10.7 | source & bytecode \[2\] | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| `NPGSQL` | [Npgsql](https://www.nuget.org/packages/Npgsql) | ≥6.0.0 | source | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| `NSERVICEBUS` | [NServiceBus](https://www.nuget.org/packages/NServiceBus) | ≥8.0.0 | source & bytecode | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
Expand Down
25 changes: 25 additions & 0 deletions src/CommonExcludedAssets.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,29 @@
<!-- Microsoft.Bcl.AsyncInterfaces is required by StackExchange.Redis. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" ExcludeAssets="all" />
</ItemGroup>

<ItemGroup>
<!-- DnsClient is required by MongoDB.Driver.Core.Extensions.DiagnosticSources. ExcludeAssets="all" prevents copying it to the output -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid the verbosity and the copy/paste mistake it seems better to have a single comment before the ItemGroup. Something like "Transient dependencies from MongoDB.Driver.Core.Extensions.DiagnosticSources that are shipped with the applications. ExcludeAssets="all" prevents copying them to the output"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted common comment. b7ca218
I would keep explanation per library - I expect more to come. Part of them probably can be linked by more than one library.

<PackageReference Include="MongoDB.Driver.Core" ExcludeAssets="all" />
<!-- DnsClient is tranistive dependency required by MongoDB.Driver.Core.Extensions.DiagnosticSources. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="DnsClient" ExcludeAssets="all" />
<!-- MongoDB.Bson is tranistive dependency required by MongoDB.Driver.Core.Extensions.DiagnosticSources. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="MongoDB.Bson" ExcludeAssets="all" />
<!-- MongoDB.Libmongocrypt is tranistive dependency required by MongoDB.Driver.Core.Extensions.DiagnosticSources. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="MongoDB.Libmongocrypt" ExcludeAssets="all" />
<!-- System.Net.Http is tranistive dependency required by MongoDB.Driver.Core.Extensions.DiagnosticSources. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="System.Net.Http" ExcludeAssets="all" />
<!-- System.Runtime.InteropServices.RuntimeInformation is tranistive dependency required by MongoDB.Driver.Core.Extensions.DiagnosticSources. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" ExcludeAssets="all" />
<!-- System.Security.Cryptography.Algorithms is tranistive dependency required by MongoDB.Driver.Core.Extensions.DiagnosticSources. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="System.Security.Cryptography.Algorithms" ExcludeAssets="all" />
<!-- System.Security.Cryptography.Encoding is tranistive dependency required by MongoDB.Driver.Core.Extensions.DiagnosticSources. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="System.Security.Cryptography.Encoding" ExcludeAssets="all" />
<!-- System.Security.Cryptography.Primitives is tranistive dependency required by MongoDB.Driver.Core.Extensions.DiagnosticSources. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="System.Security.Cryptography.Primitives" ExcludeAssets="all" />
<!-- System.Security.Cryptography.X509Certificates is tranistive dependency required by MongoDB.Driver.Core.Extensions.DiagnosticSources. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="System.Security.Cryptography.X509Certificates" ExcludeAssets="all" />
<!-- SharpCompress is tranistive dependency required by MongoDB.Driver.Core.Extensions.DiagnosticSources. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="SharpCompress" ExcludeAssets="all" />
</ItemGroup>
</Project>
14 changes: 13 additions & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<!-- Versions from OpenTelemetry.AutoInstrumentation.csproj -->
<ItemGroup>
<PackageVersion Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="1.3.0" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs" Version="1.4.0-rc.4" />
<PackageVersion Include="OpenTelemetry.Exporter.Prometheus.HttpListener" Version="1.4.0-rc.4" />
<PackageVersion Include="OpenTelemetry.Exporter.Zipkin" Version="1.4.0" />
Expand All @@ -29,18 +30,29 @@
<!-- Versions from CommonExcludedAssets.props -->
<ItemGroup>
<!-- Microsoft.Bcl.AsyncInterfaces is also referenced by the .NET Framework4.6.2. In this case is not excluded -->
<PackageVersion Include="DnsClient" Version="1.4.0" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageVersion Include="Microsoft.Win32.SystemEvents" Version="4.7.0" />
<PackageVersion Include="MongoDB.Bson" Version="2.13.3" />
<PackageVersion Include="MongoDB.Driver.Core" Version="2.13.3" />
<PackageVersion Include="MongoDB.Libmongocrypt" Version="1.2.2" />
<PackageVersion Include="MySql.Data" Version="6.10.7" />
<PackageVersion Include="Pipelines.Sockets.Unofficial" Version="2.1.16" />
<PackageVersion Include="SharpCompress" Version="0.23.0" />
<PackageVersion Include="StackExchange.Redis" Version="2.1.58" />
<PackageVersion Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
<PackageVersion Include="System.Diagnostics.PerformanceCounter" Version="4.7.0" />
<PackageVersion Include="System.Drawing.Common" Version="4.7.0" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
<PackageVersion Include="System.Security.Cryptography.Algorithms" Version="4.3.0" />
<PackageVersion Include="System.Security.Cryptography.Encoding" Version="4.3.0" />
<PackageVersion Include="System.Security.Cryptography.Primitives" Version="4.3.0" />
<PackageVersion Include="System.Security.Cryptography.ProtectedData" Version="4.7.0" />
<PackageVersion Include="System.Security.Cryptography.X509Certificates" Version="4.3.0" />
<PackageVersion Include="System.Windows.Extensions" Version="4.7.0" />
</ItemGroup>

<ItemGroup Label="Transient dependencies auto-generated by GenerateNetFxTransientDependencies">
<PackageVersion Include="Google.Protobuf" Version="3.22.1" />
<PackageVersion Include="Grpc" Version="2.46.6" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
<PackageReference Include="System.Diagnostics.DiagnosticSource" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" />
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="7.0.2" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Configuration" Version="7.0.0" />
<PackageVersion Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="1.3.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void CorProfiler::InitNetFxAssemblyRedirectsMap()
{ L"Microsoft.Extensions.Options.ConfigurationExtensions", {7, 0, 0, 0} },
{ L"Microsoft.Extensions.Primitives", {7, 0, 0, 0} },
{ L"Microsoft.Win32.Primitives", {4, 0, 3, 0} },
{ L"MongoDB.Driver.Core.Extensions.DiagnosticSources", {1, 0, 0, 0} },
{ L"OpenTelemetry", {1, 0, 0, 0} },
{ L"OpenTelemetry.Api", {1, 0, 0, 0} },
{ L"OpenTelemetry.Api.ProviderBuilderExtensions", {1, 0, 0, 0} },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public static TracerProviderBuilder UseEnvironmentVariables(
TracerInstrumentation.NServiceBus => builder.AddSource("NServiceBus.Core"),
TracerInstrumentation.Elasticsearch => builder.AddSource("Elastic.Clients.Elasticsearch.ElasticsearchClient"),
TracerInstrumentation.Quartz => Wrappers.AddQuartzInstrumentation(builder, pluginManager, lazyInstrumentationLoader),
TracerInstrumentation.MongoDB => builder.AddSource("MongoDB.Driver.Core.Extensions.DiagnosticSources"),
#if NET6_0_OR_GREATER
TracerInstrumentation.AspNetCore => Wrappers.AddAspNetCoreInstrumentation(builder, pluginManager, lazyInstrumentationLoader),
TracerInstrumentation.MassTransit => builder.AddSource("MassTransit"),
TracerInstrumentation.MongoDB => builder.AddSource("MongoDB.Driver.Core.Extensions.DiagnosticSources"),
TracerInstrumentation.MySqlData => Wrappers.AddMySqlClientInstrumentation(builder, pluginManager, lazyInstrumentationLoader),
TracerInstrumentation.StackExchangeRedis => builder.AddSource("OpenTelemetry.Instrumentation.StackExchangeRedis"),
TracerInstrumentation.EntityFrameworkCore => Wrappers.AddEntityFrameworkCoreInstrumentation(builder, pluginManager, lazyInstrumentationLoader),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ internal enum TracerInstrumentation
/// </summary>
GraphQL = 3,

#if NET6_0_OR_GREATER
/// <summary>
/// MongoDB instrumentation.
/// </summary>
MongoDB = 4,
#endif

/// <summary>
/// Npgsql instrumentation.
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry.AutoInstrumentation/Instrumentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ private static void AddLazilyLoadedTraceInstrumentations(LazyInstrumentationLoad
case TracerInstrumentation.EntityFrameworkCore:
DelayedInitialization.Traces.AddEntityFrameworkCore(LazyInstrumentationLoader, pluginManager);
break;
case TracerInstrumentation.MongoDB:
break;
case TracerInstrumentation.StackExchangeRedis:
break;
case TracerInstrumentation.MassTransit:
break;
#endif
case TracerInstrumentation.MongoDB:
break;
case TracerInstrumentation.GraphQL:
break;
case TracerInstrumentation.Npgsql:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
// limitations under the License.
// </copyright>

#if NET6_0_OR_GREATER
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
#endif
using OpenTelemetry.AutoInstrumentation.CallTarget;

namespace OpenTelemetry.AutoInstrumentation.Instrumentations.MongoDB;
Expand All @@ -38,9 +36,7 @@ namespace OpenTelemetry.AutoInstrumentation.Instrumentations.MongoDB;
type: InstrumentationType.Trace)]
public static class MongoClientIntegration
{
#if NET6_0_OR_GREATER
private static Delegate? _setActivityListener;
#endif

/// <summary>
/// OnMethodBegin callback
Expand All @@ -56,7 +52,6 @@ internal static CallTargetState OnMethodBegin<TTarget, TMongoClientSettings>(TTa
// Additional deps doesn't support .NET FX
// TODO: Find another way how to ship & load "MongoDB.Driver.Core.Extensions.DiagnosticSources"

#if NET6_0_OR_GREATER
var setListenerDelegate = _setActivityListener ??= GetClusterConfiguratorExpression().Compile();

var clusterConfiguratorProperty = settings
Expand All @@ -65,12 +60,10 @@ internal static CallTargetState OnMethodBegin<TTarget, TMongoClientSettings>(TTa
var existingDelegate = clusterConfiguratorProperty?.GetValue(settings) as Delegate;

clusterConfiguratorProperty?.SetValue(settings, Delegate.Combine(existingDelegate, setListenerDelegate));
#endif

return CallTargetState.GetDefault();
}

#if NET6_0_OR_GREATER
private static object GetInstrumentationOptions()
{
Type optionsType = Type.GetType("MongoDB.Driver.Core.Extensions.DiagnosticSources.InstrumentationOptions, MongoDB.Driver.Core.Extensions.DiagnosticSources")!;
Kielek marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -123,5 +116,4 @@ private static LambdaExpression GetClusterConfiguratorExpression()

return setListenerLambda;
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" />
<PackageReference Include="OpenTelemetry" />
<PackageReference Include="OpenTelemetry.Api" />
<PackageReference Include="OpenTelemetry.Exporter.Console" />
Expand Down
Loading