-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1528 from stebet/otel-integration-package
Adding proper OpenTelemetry integration via. registration helpers and better context propagation
- Loading branch information
Showing
12 changed files
with
916 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# RabbitMQ .NET Client - OpenTelemetry Instrumentation | ||
|
||
## Introduction | ||
This library makes it easy to instrument your RabbitMQ .NET Client applications with OpenTelemetry. | ||
|
||
## Examples | ||
The following examples demonstrate how to use the RabbitMQ .NET Client OpenTelemetry Instrumentation. | ||
|
||
### Basic Usage | ||
|
||
#### ASP.NET Core Configuration Example | ||
```csharp | ||
using OpenTelemetry.Trace; | ||
|
||
// Configure the OpenTelemetry SDK to trace ASP.NET Core, the RabbitMQ .NET Client and export the traces to the console. | ||
// Also configures context propagation to propagate the TraceContext and Baggage using the W3C specification. | ||
var compositeTextMapPropagator = new CompositeTextMapPropagator(new TextMapPropagator[] | ||
{ | ||
new TraceContextPropagator(), | ||
new BaggagePropagator() | ||
}); | ||
|
||
Sdk.SetDefaultTextMapPropagator(compositeTextMapPropagator); | ||
|
||
builder.Services.AddOpenTelemetry() | ||
.ConfigureResource(resource => resource | ||
.AddService(serviceName: builder.Environment.ApplicationName)) | ||
.WithTracing(tracing => tracing | ||
.AddAspNetCoreInstrumentation() | ||
.AddRabbitMQInstrumentation() | ||
.AddConsoleExporter()); | ||
``` | ||
|
||
#### Console Application Configuration Example | ||
```csharp | ||
using OpenTelemetry.Trace; | ||
|
||
// Configure the OpenTelemetry SDK to trace ASP.NET Core, the RabbitMQ .NET Client and export the traces to the console. | ||
// Also configures context propagation to propagate the TraceContext and Baggage using the W3C specification. | ||
var compositeTextMapPropagator = new CompositeTextMapPropagator(new TextMapPropagator[] | ||
{ | ||
new TraceContextPropagator(), | ||
new BaggagePropagator() | ||
}); | ||
|
||
Sdk.SetDefaultTextMapPropagator(compositeTextMapPropagator); | ||
|
||
var tracerProvider = Sdk.CreateTracerProviderBuilder() | ||
.AddRabbitMQInstrumentation() | ||
.AddConsoleExporter() | ||
.Build(); | ||
``` |
64 changes: 64 additions & 0 deletions
64
projects/RabbitMQ.Client.OpenTelemetry/RabbitMQ.Client.OpenTelemetry.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks> | ||
<NoWarn>$(NoWarn);CS1591</NoWarn> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<AssemblyTitle>RabbitMQ OpenTelemetry Integration Package for .NET</AssemblyTitle> | ||
<Authors>VMware</Authors> | ||
<Company>VMware, Inc. or its affiliates.</Company> | ||
<Copyright>Copyright © 2007-2023 VMware, Inc. or its affiliates.</Copyright> | ||
<Description>The RabbitMQ OpenTelemetry Library for .NET adds convenience extension methods for RabbitMQ/OpenTelemetry</Description> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<PackageIcon>icon.png</PackageIcon> | ||
<PackageLicenseExpression>Apache-2.0 OR MPL-2.0</PackageLicenseExpression> | ||
<PackageProjectUrl>https://www.rabbitmq.com/dotnet.html</PackageProjectUrl> | ||
<PackageTags>rabbitmq, amqp, oauth2</PackageTags> | ||
<Product>RabbitMQ</Product> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<RepositoryUrl>https://github.com/rabbitmq/rabbitmq-dotnet-client.git</RepositoryUrl> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
<AssemblyOriginatorKeyFile>../rabbit.snk</AssemblyOriginatorKeyFile> | ||
<SignAssembly>true</SignAssembly> | ||
<MinVerTagPrefix>otel-</MinVerTagPrefix> | ||
<MinVerVerbosity>minimal</MinVerVerbosity> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<PackageOutputPath>../../packages</PackageOutputPath> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<LangVersion>7.3</LangVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)' == 'Release' And '$(CI)' == 'true'"> | ||
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> | ||
<Deterministic>true</Deterministic> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Condition="'$(Configuration)' == 'Release' and '$(SourceRoot)' == ''"> | ||
<SourceRoot Include="$(MSBuildThisFileDirectory)/" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="icon.png" /> | ||
<Content Include="icon.png" PackagePath="" /> | ||
<None Include="README.md" Pack="true" PackagePath="/" /> | ||
<InternalsVisibleTo Include="Unit" /> | ||
<InternalsVisibleTo Include="Benchmarks" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" /> | ||
<PackageReference Include="MinVer" Version="5.0.0" PrivateAssets="all" /> | ||
<PackageReference Include="OpenTelemetry.Api" Version="1.7.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'"> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../RabbitMQ.Client/RabbitMQ.Client.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
58 changes: 58 additions & 0 deletions
58
projects/RabbitMQ.Client.OpenTelemetry/TraceProviderBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text; | ||
using OpenTelemetry.Context.Propagation; | ||
using RabbitMQ.Client; | ||
|
||
namespace OpenTelemetry.Trace | ||
{ | ||
public static class OpenTelemetryExtensions | ||
{ | ||
public static TracerProviderBuilder AddRabbitMQInstrumentation(this TracerProviderBuilder builder) | ||
{ | ||
RabbitMQActivitySource.UseRoutingKeyAsOperationName = true; | ||
RabbitMQActivitySource.ContextExtractor = OpenTelemetryContextExtractor; | ||
RabbitMQActivitySource.ContextInjector = OpenTelemetryContextInjector; | ||
builder.AddSource("RabbitMQ.Client.*"); | ||
return builder; | ||
} | ||
|
||
private static ActivityContext OpenTelemetryContextExtractor(IReadOnlyBasicProperties props) | ||
{ | ||
// Extract the PropagationContext of the upstream parent from the message headers. | ||
var parentContext = Propagators.DefaultTextMapPropagator.Extract(default, props.Headers, OpenTelemetryContextGetter); | ||
Baggage.Current = parentContext.Baggage; | ||
return parentContext.ActivityContext; | ||
} | ||
|
||
private static IEnumerable<string> OpenTelemetryContextGetter(IDictionary<string, object> carrier, string key) | ||
{ | ||
try | ||
{ | ||
if (carrier.TryGetValue(key, out object value) && value is byte[] bytes) | ||
{ | ||
return new[] { Encoding.UTF8.GetString(bytes) }; | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
//this.logger.LogError(ex, "Failed to extract trace context."); | ||
} | ||
|
||
return Enumerable.Empty<string>(); | ||
} | ||
|
||
private static void OpenTelemetryContextInjector(Activity activity, IDictionary<string, object> props) | ||
{ | ||
// Inject the current Activity's context into the message headers. | ||
Propagators.DefaultTextMapPropagator.Inject(new PropagationContext(activity.Context, Baggage.Current), props, OpenTelemetryContextSetter); | ||
} | ||
|
||
private static void OpenTelemetryContextSetter(IDictionary<string, object> carrier, string key, string value) | ||
{ | ||
carrier[key] = Encoding.UTF8.GetBytes(value); | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.