forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Hangfire instrumentation (open-telemetry#271)
- Loading branch information
Showing
12 changed files
with
472 additions
and
0 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
45 changes: 45 additions & 0 deletions
45
src/OpenTelemetry.Instrumentation.Hangfire/Implementation/HangfireInstrumentation.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,45 @@ | ||
// <copyright file="HangfireInstrumentation.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> | ||
|
||
namespace OpenTelemetry.Instrumentation.Hangfire.Implementation | ||
{ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Reflection; | ||
|
||
internal static class HangfireInstrumentation | ||
{ | ||
/// <summary> | ||
/// The assembly name. | ||
/// </summary> | ||
internal static readonly AssemblyName AssemblyName = typeof(HangfireInstrumentation).Assembly.GetName(); | ||
|
||
/// <summary> | ||
/// The activity source name. | ||
/// </summary> | ||
internal static readonly string ActivitySourceName = AssemblyName.Name; | ||
|
||
/// <summary> | ||
/// The version. | ||
/// </summary> | ||
internal static readonly Version Version = AssemblyName.Version; | ||
|
||
/// <summary> | ||
/// The activity source. | ||
/// </summary> | ||
internal static readonly ActivitySource ActivitySource = new(ActivitySourceName, Version.ToString()); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...OpenTelemetry.Instrumentation.Hangfire/Implementation/HangfireInstrumentationConstants.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,27 @@ | ||
// <copyright file="HangfireInstrumentationConstants.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> | ||
|
||
namespace OpenTelemetry.Instrumentation.Hangfire.Implementation | ||
{ | ||
internal static class HangfireInstrumentationConstants | ||
{ | ||
public const string JobIdTag = "job.id"; | ||
public const string JobCreatedAtTag = "job.createdat"; | ||
|
||
public const string ActivityName = "JOB"; | ||
public const string ActivityKey = "opentelemetry_activity_key"; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...etry.Instrumentation.Hangfire/Implementation/HangfireInstrumentationJobFilterAttribute.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,69 @@ | ||
// <copyright file="HangfireInstrumentationJobFilterAttribute.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> | ||
|
||
namespace OpenTelemetry.Instrumentation.Hangfire.Implementation | ||
{ | ||
using System.Diagnostics; | ||
using global::Hangfire.Common; | ||
using global::Hangfire.Server; | ||
|
||
internal class HangfireInstrumentationJobFilterAttribute : JobFilterAttribute, IServerFilter | ||
{ | ||
public void OnPerforming(PerformingContext performingContext) | ||
{ | ||
// Short-circuit if nobody is listening | ||
if (!HangfireInstrumentation.ActivitySource.HasListeners()) | ||
{ | ||
return; | ||
} | ||
|
||
var activity = HangfireInstrumentation.ActivitySource | ||
.StartActivity(HangfireInstrumentationConstants.ActivityName, ActivityKind.Internal, parentContext: default); | ||
|
||
if (activity != null) | ||
{ | ||
activity.DisplayName = $"JOB {performingContext.BackgroundJob.Job.Type.Name}.{performingContext.BackgroundJob.Job.Method.Name}"; | ||
|
||
if (activity.IsAllDataRequested) | ||
{ | ||
activity.SetTag(HangfireInstrumentationConstants.JobIdTag, performingContext.BackgroundJob.Id); | ||
activity.SetTag(HangfireInstrumentationConstants.JobCreatedAtTag, performingContext.BackgroundJob.CreatedAt.ToString("O")); | ||
} | ||
|
||
performingContext.Items.Add(HangfireInstrumentationConstants.ActivityKey, activity); | ||
} | ||
} | ||
|
||
public void OnPerformed(PerformedContext performedContext) | ||
{ | ||
// Short-circuit if nobody is listening | ||
if (!HangfireInstrumentation.ActivitySource.HasListeners() || !performedContext.Items.ContainsKey(HangfireInstrumentationConstants.ActivityKey)) | ||
{ | ||
return; | ||
} | ||
|
||
if (performedContext.Items[HangfireInstrumentationConstants.ActivityKey] is Activity activity) | ||
{ | ||
if (performedContext.Exception != null) | ||
{ | ||
activity.SetStatus(ActivityStatusCode.Error, performedContext.Exception.Message); | ||
} | ||
|
||
activity.Dispose(); | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/OpenTelemetry.Instrumentation.Hangfire/OpenTelemetry.Instrumentation.Hangfire.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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<Description>OpenTelemetry Hangfire Instrumentation</Description> | ||
<PackageTags>$(PackageTags);Hangfire</PackageTags> | ||
<MinVerTagPrefix>Instrumentation.Hangfire-</MinVerTagPrefix> | ||
<SignAssembly>false</SignAssembly> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Hangfire.Core" Version="[1.7.0,1.8.0)" /> | ||
<PackageReference Include="OpenTelemetry.Api" Version="1.2.0-rc4" /> | ||
</ItemGroup> | ||
</Project> |
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,62 @@ | ||
# Hangfire Instrumentation for OpenTelemetry .NET | ||
|
||
[![NuGet](https://img.shields.io/nuget/v/OpenTelemetry.Instrumentation.Hangfire.svg)](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.Hangfire) | ||
[![NuGet](https://img.shields.io/nuget/dt/OpenTelemetry.Instrumentation.Hangfire.svg)](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.Hangfire) | ||
|
||
This is an | ||
[Instrumentation Library](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/glossary.md#instrumentation-library), | ||
which instruments | ||
[Hangfire](https://www.nuget.org/packages/Hangfire/) | ||
and collects telemetry about BackgroundJob. | ||
|
||
## Steps to enable OpenTelemetry.Instrumentation.Hangfire | ||
|
||
### Step 1: Install and configure Hangfire | ||
|
||
[Getting Started](https://docs.hangfire.io/en/latest/getting-started/index.html) | ||
|
||
### Step 2: Install Hangfire instrumentation Package | ||
|
||
Add a reference to the | ||
[`OpenTelemetry.Instrumentation.Hangfire`](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.Hangfire) | ||
package. Also, add any other instrumentations & exporters you will need. | ||
|
||
```shell | ||
dotnet add package OpenTelemetry.Instrumentation.Hangfire --prerelease | ||
``` | ||
|
||
### Step 3: Enable Hangfire Instrumentation at application startup | ||
|
||
Hangfire instrumentation must be enabled at application startup. | ||
|
||
The following example demonstrates adding Hangfire instrumentation to a | ||
console application. This example also sets up the OpenTelemetry Console | ||
exporter, which requires adding the package | ||
[`OpenTelemetry.Exporter.Console`](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Exporter.Console/README.md) | ||
to the application. | ||
|
||
```csharp | ||
using OpenTelemetry.Trace; | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
using var tracerProvider = Sdk.CreateTracerProviderBuilder() | ||
.AddHangfireInstrumentation() | ||
.AddConsoleExporter() | ||
.Build(); | ||
} | ||
} | ||
``` | ||
|
||
For an ASP.NET Core application, adding instrumentation is typically done in | ||
the `ConfigureServices` of your `Startup` class. Refer to [example](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/examples/AspNetCore/Program.cs). | ||
|
||
For an ASP.NET application, adding instrumentation is typically done in the | ||
`Global.asax.cs`. Refer to [example](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/examples/AspNet/Global.asax.cs). | ||
|
||
## References | ||
|
||
* [OpenTelemetry Project](https://opentelemetry.io/) | ||
* [Hangfire Project](https://www.hangfire.io/) |
44 changes: 44 additions & 0 deletions
44
src/OpenTelemetry.Instrumentation.Hangfire/TracerProviderBuilderExtensions.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,44 @@ | ||
// <copyright file="TracerProviderBuilderExtensions.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> | ||
|
||
namespace OpenTelemetry.Trace | ||
{ | ||
using System; | ||
using OpenTelemetry.Instrumentation.Hangfire.Implementation; | ||
|
||
/// <summary> | ||
/// Extension methods to simplify registering of Hangfire job instrumentation. | ||
/// </summary> | ||
public static class TracerProviderBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds Hangfire instrumentation to the tracer provider. | ||
/// </summary> | ||
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param> | ||
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns> | ||
public static TracerProviderBuilder AddHangfireInstrumentation(this TracerProviderBuilder builder) | ||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
Hangfire.GlobalJobFilters.Filters.Add(new HangfireInstrumentationJobFilterAttribute()); | ||
|
||
return builder.AddSource(HangfireInstrumentation.ActivitySourceName); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
test/OpenTelemetry.Instrumentation.Hangfire.Tests/HangfireFixture.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,43 @@ | ||
// <copyright file="HangfireFixture.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> | ||
|
||
using System; | ||
using Hangfire; | ||
using Hangfire.MemoryStorage; | ||
using Hangfire.Storage; | ||
|
||
namespace OpenTelemetry.Instrumentation.Hangfire.Tests | ||
{ | ||
public class HangfireFixture : IDisposable | ||
{ | ||
public HangfireFixture() | ||
{ | ||
GlobalConfiguration.Configuration | ||
.UseMemoryStorage(); | ||
this.Server = new BackgroundJobServer(); | ||
this.MonitoringApi = JobStorage.Current.GetMonitoringApi(); | ||
} | ||
|
||
public BackgroundJobServer Server { get; } | ||
|
||
public IMonitoringApi MonitoringApi { get; } | ||
|
||
public void Dispose() | ||
{ | ||
this.Server.Dispose(); | ||
} | ||
} | ||
} |
Oops, something went wrong.