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

[Instrumentation.Hangfire] Added support for custom job display names #756

Merged
merged 4 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
OpenTelemetry.Trace.HangfireInstrumentationOptions
OpenTelemetry.Trace.HangfireInstrumentationOptions.DisplayNameFunc.get -> System.Func<Hangfire.BackgroundJob, string>
OpenTelemetry.Trace.HangfireInstrumentationOptions.DisplayNameFunc.set -> void
OpenTelemetry.Trace.HangfireInstrumentationOptions.HangfireInstrumentationOptions() -> void
OpenTelemetry.Trace.HangfireInstrumentationOptions.RecordException.get -> bool
OpenTelemetry.Trace.HangfireInstrumentationOptions.RecordException.set -> void
Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Instrumentation.Hangfire/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Add support for custom job display names
([#756](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/756))

## 1.0.0-beta.3

Released 2022-Oct-26
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
// limitations under the License.
// </copyright>

using System;
using Hangfire;
using OpenTelemetry.Instrumentation.Hangfire.Implementation;

namespace OpenTelemetry.Trace;

/// <summary>
Expand All @@ -28,4 +32,12 @@ public class HangfireInstrumentationOptions
/// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/exceptions.md.
/// </remarks>
public bool RecordException { get; set; }

/// <summary>
/// Gets or sets a delegate used to format the job name.
/// </summary>
/// <remarks>
/// Defaults to <c>{backgroundJob.Job.Type.Name}.{backgroundJob.Job.Method.Name}</c>.
/// </remarks>
public Func<BackgroundJob, string> DisplayNameFunc { get; set; } = HangfireInstrumentation.DefaultDisplayNameFunc;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

using Hangfire;

namespace OpenTelemetry.Instrumentation.Hangfire.Implementation;

using System;
Expand Down Expand Up @@ -41,4 +43,10 @@ internal static class HangfireInstrumentation
/// The activity source.
/// </summary>
internal static readonly ActivitySource ActivitySource = new(ActivitySourceName, Version.ToString());

/// <summary>
/// The default display name delegate.
/// </summary>
internal static readonly Func<BackgroundJob, string> DefaultDisplayNameFunc =
backgroundJob => $"JOB {backgroundJob.Job.Type.Name}.{backgroundJob.Job.Method.Name}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// limitations under the License.
// </copyright>

using System;
using Hangfire;

namespace OpenTelemetry.Instrumentation.Hangfire.Implementation;

using System.Collections.Generic;
Expand Down Expand Up @@ -56,7 +59,10 @@ public void OnPerforming(PerformingContext performingContext)

if (activity != null)
{
activity.DisplayName = $"JOB {performingContext.BackgroundJob.Job.Type.Name}.{performingContext.BackgroundJob.Job.Method.Name}";
Func<BackgroundJob, string> displayNameFunc =
this.options.DisplayNameFunc ?? HangfireInstrumentation.DefaultDisplayNameFunc;

activity.DisplayName = displayNameFunc(performingContext.BackgroundJob);

if (activity.IsAllDataRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,27 @@ public async Task Should_Create_Activity_Without_Exception_Event_When_Job_Failed
Assert.Empty(activity.Events);
}

[Fact]
public async Task Should_Create_Activity_With_Custom_DisplayName()
{
// Arrange
var exportedItems = new List<Activity>();
using var tel = Sdk.CreateTracerProviderBuilder()
.AddHangfireInstrumentation(options => options.DisplayNameFunc = backgroundJob => $"JOB {backgroundJob.Id}")
.AddInMemoryExporter(exportedItems)
.Build();

// Act
var jobId = BackgroundJob.Enqueue<TestJob>(x => x.Execute());
await this.WaitJobProcessedAsync(jobId, 5);

// Assert
Assert.Single(exportedItems, i => i.GetTagItem("job.id") as string == jobId);
var activity = exportedItems.Single(i => i.GetTagItem("job.id") as string == jobId);
Assert.Contains($"JOB {jobId}", activity.DisplayName);
Assert.Equal(ActivityKind.Internal, activity.Kind);
}

private async Task WaitJobProcessedAsync(string jobId, int timeToWaitInSeconds)
{
var timeout = DateTime.Now.AddSeconds(timeToWaitInSeconds);
Expand Down