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

StackExchange.Redis instrumentation for .NET Core 3.1+ #979

Merged
merged 15 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ updates:
interval: "daily"
open-pull-requests-limit: 20

- package-ecosystem: nuget
directory: /test/test-applications/integrations/TestApplication.StackExchangeRedis
schedule:
interval: "daily"
open-pull-requests-limit: 20

- package-ecosystem: nuget
directory: /test/test-applications/mocks/OpenTelemetry.AutoInstrumentation.Mock
schedule:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Add MySql.Data traces instrumentation.
- Add Npgsql traces instrumentation.
- Add StackExchange.Redis traces instrumentation.
- Add configuration option `none` to `OTEL_DOTNET_AUTO_TRACES_ENABLED_INSTRUMENTATIONS`
and `OTEL_DOTNET_AUTO_METRICS_ENABLED_INSTRUMENTATIONS`.

Expand Down
15 changes: 15 additions & 0 deletions OpenTelemetry.AutoInstrumentation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplication.Shared", "t
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplication.MySqlData", "test\test-applications\integrations\TestApplication.MySqlData\TestApplication.MySqlData.csproj", "{E7C2D2CF-C965-449D-A02C-02F7837D0C6D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApplication.StackExchangeRedis", "test\test-applications\integrations\TestApplication.StackExchangeRedis\TestApplication.StackExchangeRedis.csproj", "{671EB8F0-E164-4E9F-B423-27AF4B59D360}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -557,6 +559,18 @@ Global
{E7C2D2CF-C965-449D-A02C-02F7837D0C6D}.Release|x64.Build.0 = Release|x64
{E7C2D2CF-C965-449D-A02C-02F7837D0C6D}.Release|x86.ActiveCfg = Release|x86
{E7C2D2CF-C965-449D-A02C-02F7837D0C6D}.Release|x86.Build.0 = Release|x86
{671EB8F0-E164-4E9F-B423-27AF4B59D360}.Debug|Any CPU.ActiveCfg = Debug|x64
{671EB8F0-E164-4E9F-B423-27AF4B59D360}.Debug|Any CPU.Build.0 = Debug|x64
{671EB8F0-E164-4E9F-B423-27AF4B59D360}.Debug|x64.ActiveCfg = Debug|x64
{671EB8F0-E164-4E9F-B423-27AF4B59D360}.Debug|x64.Build.0 = Debug|x64
{671EB8F0-E164-4E9F-B423-27AF4B59D360}.Debug|x86.ActiveCfg = Debug|x86
{671EB8F0-E164-4E9F-B423-27AF4B59D360}.Debug|x86.Build.0 = Debug|x86
{671EB8F0-E164-4E9F-B423-27AF4B59D360}.Release|Any CPU.ActiveCfg = Release|x64
{671EB8F0-E164-4E9F-B423-27AF4B59D360}.Release|Any CPU.Build.0 = Release|x64
{671EB8F0-E164-4E9F-B423-27AF4B59D360}.Release|x64.ActiveCfg = Release|x64
{671EB8F0-E164-4E9F-B423-27AF4B59D360}.Release|x64.Build.0 = Release|x64
{671EB8F0-E164-4E9F-B423-27AF4B59D360}.Release|x86.ActiveCfg = Release|x86
{671EB8F0-E164-4E9F-B423-27AF4B59D360}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -602,6 +616,7 @@ Global
{DC54F01E-1D5C-4ECF-B5B9-14EECD2B4CF6} = {E409ADD3-9574-465C-AB09-4324D205CC7C}
{CB4EA9F4-EE1B-4009-B3CD-215DCE8BE214} = {82A3CE96-0935-45E5-A9AA-A93A5B63500B}
{E7C2D2CF-C965-449D-A02C-02F7837D0C6D} = {E409ADD3-9574-465C-AB09-4324D205CC7C}
{671EB8F0-E164-4E9F-B423-27AF4B59D360} = {E409ADD3-9574-465C-AB09-4324D205CC7C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {160A1D00-1F5B-40F8-A155-621B4459D78F}
Expand Down
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ for more details.
| `MySqlData` | [MySql.Data](https://www.nuget.org/packages/MySql.Data) **Not supported on .NET Framework** | ≥6.10.7 | source |
| `Npgsql` | [Npgsql](https://www.nuget.org/packages/Npgsql) | ≥6.0.0 | source |
| `SqlClient` | [Microsoft.Data.SqlClient](https://www.nuget.org/packages/Microsoft.Data.SqlClient) and [System.Data.SqlClient](https://www.nuget.org/packages/System.Data.SqlClient) | * | source |
| `StackExchangeRedis` | [StackExchange.Redis](https://www.nuget.org/packages/StackExchange.Redis) **Not supported on .NET Framework** | ≥2.1.58 < 3.0.0 | source & binary |

### Instrumented metrics libraries and frameworks

Expand Down
53 changes: 53 additions & 0 deletions integrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,58 @@
}
}
]
},
Kielek marked this conversation as resolved.
Show resolved Hide resolved
{
"name": "StackExchangeRedis",
"method_replacements": [
{
"caller": {},
"target": {
"assembly": "StackExchange.Redis",
"type": "StackExchange.Redis.ConnectionMultiplexer",
"method": "Connect",
"signature_types": [
"StackExchange.Redis.ConnectionMultiplexer",
"StackExchange.Redis.ConfigurationOptions",
"System.IO.TextWriter"
],
"minimum_major": 2,
"minimum_minor": 0,
"minimum_patch": 0,
Kielek marked this conversation as resolved.
Show resolved Hide resolved
"maximum_major": 2,
"maximum_minor": 65535,
"maximum_patch": 65535
},
"wrapper": {
"assembly": "OpenTelemetry.AutoInstrumentation",
"type": "OpenTelemetry.AutoInstrumentation.Instrumentations.StackExchangeRedis.StackExchangeRedisIntegration",
"action": "CallTargetModification"
}
},
{
"caller": {},
"target": {
"assembly": "StackExchange.Redis",
"type": "StackExchange.Redis.ConnectionMultiplexer",
"method": "ConnectAsync",
"signature_types": [
"System.Threading.Tasks.Task`1<StackExchange.Redis.ConnectionMultiplexer>",
"StackExchange.Redis.ConfigurationOptions",
"System.IO.TextWriter"
],
"minimum_major": 2,
"minimum_minor": 0,
"minimum_patch": 0,
"maximum_major": 2,
"maximum_minor": 65535,
"maximum_patch": 65535
},
"wrapper": {
"assembly": "OpenTelemetry.AutoInstrumentation",
"type": "OpenTelemetry.AutoInstrumentation.Instrumentations.StackExchangeRedis.StackExchangeRedisIntegrationAsync",
"action": "CallTargetModification"
}
}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal static class EnvironmentConfigurationTracerHelper
#if NETCOREAPP3_1_OR_GREATER
[TracerInstrumentation.MongoDB] = builder => builder.AddSource("MongoDB.Driver.Core.Extensions.DiagnosticSources"),
[TracerInstrumentation.MySqlData] = builder => builder.AddSource("OpenTelemetry.Instrumentation.MySqlData"),
[TracerInstrumentation.StackExchangeRedis] = builder => builder.AddSource("OpenTelemetry.Instrumentation.StackExchangeRedis"),
#endif
[TracerInstrumentation.Npgsql] = builder => builder.AddSource("Npgsql")
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public enum TracerInstrumentation
/// <summary>
/// MySqlData instrumentation.
/// </summary>
MySqlData = 6
MySqlData = 6,

/// <summary>
/// StackExchangeRedis instrumentation.
/// </summary>
StackExchangeRedis = 7
#endif
}
4 changes: 4 additions & 0 deletions src/OpenTelemetry.AutoInstrumentation/Instrumentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public static bool ProfilerAttached
}
}

#if NETCOREAPP3_1_OR_GREATER
internal static ILifespanManager LifespanManager => LazyInstrumentationLoader;
#endif

internal static TracerSettings TracerSettings { get; } = TracerSettings.FromDefaultSources();

internal static MeterSettings MeterSettings { get; } = MeterSettings.FromDefaultSources();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// <copyright file="StackExchangeRedisConstants.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

#if NETCOREAPP3_1_OR_GREATER

namespace OpenTelemetry.AutoInstrumentation.Instrumentations.StackExchangeRedis;

internal static class StackExchangeRedisConstants
{
public const string AssemblyName = "StackExchange.Redis";

public const string MinimumVersion = "2.0.0"; // 2.1.58, but dll uses 2.0.0
Kielek marked this conversation as resolved.
Show resolved Hide resolved
public const string MaximumVersion = "2.65535.65535";
public const string IntegrationName = "StackExchangeRedis";

public const string ConnectionMultiplexerTypeName = "StackExchange.Redis.ConnectionMultiplexer";
public const string ConfigurationOptionsTypeName = "StackExchange.Redis.ConfigurationOptions";
public const string TextWriterTypeName = "System.IO.TextWriter";
public const string TaskConnectionMultiplexerTypeName = $"System.Threading.Tasks.Task`1<{ConnectionMultiplexerTypeName}>";

public const string ConnectMethodName = "Connect";
public const string ConnectAsyncMethodName = "ConnectAsync";
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// <copyright file="StackExchangeRedisInitializer.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

#if NETCOREAPP3_1_OR_GREATER

using System;
using OpenTelemetry.AutoInstrumentation.Configuration;

namespace OpenTelemetry.AutoInstrumentation.Instrumentations.StackExchangeRedis;

internal static class StackExchangeRedisInitializer
{
public static void Initialize(object connection)
{
if (connection != null && Instrumentation.TracerSettings.EnabledInstrumentations.Contains(TracerInstrumentation.StackExchangeRedis))
{
var instrumentationType = Type.GetType("OpenTelemetry.Instrumentation.StackExchangeRedis.StackExchangeRedisCallsInstrumentation, OpenTelemetry.Instrumentation.StackExchangeRedis");
var optionsInstrumentationType = Type.GetType("OpenTelemetry.Instrumentation.StackExchangeRedis.StackExchangeRedisCallsInstrumentationOptions, OpenTelemetry.Instrumentation.StackExchangeRedis");

var options = Activator.CreateInstance(optionsInstrumentationType);
var instrumentation = Activator.CreateInstance(instrumentationType, connection, options);

Instrumentation.LifespanManager.Track(instrumentation);
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// <copyright file="StackExchangeRedisIntegration.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

#if NETCOREAPP3_1_OR_GREATER

using System;
using OpenTelemetry.AutoInstrumentation.CallTarget;

namespace OpenTelemetry.AutoInstrumentation.Instrumentations.StackExchangeRedis
{
/// <summary>
/// StackExchange.Redis.ConnectionMultiplexer calltarget instrumentation
/// </summary>
[InstrumentMethod(
AssemblyName = StackExchangeRedisConstants.AssemblyName,
TypeName = StackExchangeRedisConstants.ConnectionMultiplexerTypeName,
MethodName = StackExchangeRedisConstants.ConnectMethodName,
ReturnTypeName = StackExchangeRedisConstants.ConnectionMultiplexerTypeName,
ParameterTypeNames = new[] { StackExchangeRedisConstants.ConfigurationOptionsTypeName, StackExchangeRedisConstants.TextWriterTypeName },
MinimumVersion = StackExchangeRedisConstants.MinimumVersion,
MaximumVersion = StackExchangeRedisConstants.MaximumVersion,
IntegrationName = StackExchangeRedisConstants.IntegrationName)]
public class StackExchangeRedisIntegration
{
/// <summary>
/// OnMethodEnd callback
/// </summary>
/// <param name="returnValue">Return value</param>
/// <param name="exception">Exception value</param>
/// <param name="state">CallTarget state</param>
/// <typeparam name="TTarget">Type of the target</typeparam>
/// <typeparam name="TReturn">Return type</typeparam>
/// <returns>A response value, in an async scenario will be T of Task of T</returns>
public static CallTargetReturn<TReturn> OnMethodEnd<TTarget, TReturn>(TReturn returnValue, Exception exception, CallTargetState state)
{
StackExchangeRedisInitializer.Initialize(returnValue);

return new CallTargetReturn<TReturn>(returnValue);
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// <copyright file="StackExchangeRedisIntegrationAsync.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

#if NETCOREAPP3_1_OR_GREATER

using System;
using OpenTelemetry.AutoInstrumentation.CallTarget;

namespace OpenTelemetry.AutoInstrumentation.Instrumentations.StackExchangeRedis;

/// <summary>
/// StackExchange.Redis.ConnectionMultiplexer calltarget instrumentation
/// </summary>
[InstrumentMethod(
AssemblyName = StackExchangeRedisConstants.AssemblyName,
TypeName = StackExchangeRedisConstants.ConnectionMultiplexerTypeName,
MethodName = StackExchangeRedisConstants.ConnectAsyncMethodName,
ReturnTypeName = StackExchangeRedisConstants.TaskConnectionMultiplexerTypeName,
ParameterTypeNames = new[] { StackExchangeRedisConstants.ConfigurationOptionsTypeName, StackExchangeRedisConstants.TextWriterTypeName },
MinimumVersion = StackExchangeRedisConstants.MinimumVersion,
MaximumVersion = StackExchangeRedisConstants.MaximumVersion,
IntegrationName = StackExchangeRedisConstants.IntegrationName)]
public class StackExchangeRedisIntegrationAsync
{
/// <summary>
/// OnAsyncMethodEnd callback
/// </summary>
/// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
/// <param name="returnValue">Return value</param>
/// <param name="exception">Exception value</param>
/// <param name="state">CallTarget state</param>
/// <typeparam name="TTarget">Type of the target</typeparam>
/// <typeparam name="TReturn">Return type</typeparam>
/// <returns>A response value, in an async scenario will be T of Task of T</returns>
public static TReturn OnAsyncMethodEnd<TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state)
{
StackExchangeRedisInitializer.Initialize(returnValue);

return returnValue;
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void Add(InstrumentationInitializer loader)
_ = new OnAssemblyLoadInitializer(this, loader);
}

void ILifespanManager.Track(object instance)
public void Track(object instance)
{
_instrumentations.Add(instance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9.4" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.0.0-rc.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.0.0-rc9.4" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNet" Version="1.0.0-rc9.5" Condition="'$(TargetFramework)' == 'net462'" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.4" Condition="'$(TargetFramework)' == 'netcoreapp3.1'" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.3.0" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.3.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.3.0" />
<PackageReference Include="OpenTelemetry.Shims.OpenTracing" Version="1.0.0-rc9.4" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus" Version="1.3.0-rc.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.MySqlData" Version="1.0.0-beta.3" Condition="'$(TargetFramework)' == 'netcoreapp3.1'" />
<!-- MySql.Data is required by OpenTelemetry.Instrumentation.MySqlData. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="MySql.Data" Version="6.10.7" ExcludeAssets="all" Condition="'$(TargetFramework)' == 'netcoreapp3.1'" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<Reference Include="System.Configuration" />
<Reference Include="System.Web" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNet" Version="1.0.0-rc9.5" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.4" />
<PackageReference Include="OpenTelemetry.Instrumentation.MySqlData" Version="1.0.0-beta.3" />
<!-- MySql.Data is required by OpenTelemetry.Instrumentation.MySqlData. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="MySql.Data" Version="6.10.7" ExcludeAssets="all" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9.7" />
<!-- StackExchange.Redis is required by OpenTelemetry.Instrumentation.StackExchangeRedis. ExcludeAssets="all" prevents copying it to the output -->
<PackageReference Include="StackExchange.Redis" Version="2.1.58" ExcludeAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading