Skip to content

Commit

Permalink
[Instrumentation.Hangfire] Added support for custom job display names (
Browse files Browse the repository at this point in the history
…#756)

* Added support for custom job display names

* Move JOB prefix to DisplayNameFunc.

* Added entry to CHANGELOG.md

* Fixed missing period in API docs.
  • Loading branch information
prochnowc authored Nov 4, 2022
1 parent d4bd617 commit 005fdb7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
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

0 comments on commit 005fdb7

Please sign in to comment.