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.Quartz] Fix analysis warnings #966

Merged
merged 2 commits into from
Feb 22, 2023
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
Expand Up @@ -53,10 +53,10 @@ public override void OnStartActivity(Activity activity, object payload)
return;
}

activity.DisplayName = this.GetDisplayName(activity);
activity.DisplayName = GetDisplayName(activity);

ActivityInstrumentationHelper.SetActivitySourceProperty(activity, ActivitySource);
ActivityInstrumentationHelper.SetKindProperty(activity, this.GetActivityKind(activity));
ActivityInstrumentationHelper.SetKindProperty(activity, GetActivityKind(activity));

try
{
Expand Down Expand Up @@ -115,17 +115,17 @@ public override void OnException(Activity activity, object payload)
}
}

private string GetDisplayName(Activity activity)
private static string GetDisplayName(Activity activity)
{
return activity.OperationName switch
{
OperationName.Job.Execute => $"execute {this.GetTag(activity.Tags, TagName.JobName)}",
OperationName.Job.Veto => $"veto {this.GetTag(activity.Tags, TagName.JobName)}",
OperationName.Job.Execute => $"execute {GetTag(activity.Tags, TagName.JobName)}",
OperationName.Job.Veto => $"veto {GetTag(activity.Tags, TagName.JobName)}",
_ => activity.DisplayName,
};
}

private ActivityKind GetActivityKind(Activity activity)
private static ActivityKind GetActivityKind(Activity activity)
{
return activity.OperationName switch
{
Expand All @@ -135,7 +135,7 @@ private ActivityKind GetActivityKind(Activity activity)
};
}

private string GetTag(IEnumerable<KeyValuePair<string, string>> tags, string tagName)
private static string GetTag(IEnumerable<KeyValuePair<string, string>> tags, string tagName)
{
var tag = tags.SingleOrDefault(kv => kv.Key == tagName);
return tag.Value;
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Instrumentation.Quartz/OperationName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public static class OperationName
/// <summary>
/// Quartz Job category constants.
/// </summary>
#pragma warning disable CA1034 // Nested types should not be visible
public static class Job
#pragma warning restore CA1034 // Nested types should not be visible
{
/// <summary>
/// Quartz job execute diagnostic source operation name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@ public class QuartzInstrumentationOptions
/// <summary>
/// Gets or sets traced operations set.
/// </summary>
#pragma warning disable CA2227 // Collection properties should be read only
public HashSet<string> TracedOperations { get; set; } = new(DefaultTracedOperations);
#pragma warning restore CA2227 // Collection properties should be read only
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using OpenTelemetry.Instrumentation.Quartz;
using OpenTelemetry.Instrumentation.Quartz.Implementation;
using OpenTelemetry.Internal;

// ReSharper disable once CheckNamespace
namespace OpenTelemetry.Trace;
Expand Down Expand Up @@ -44,6 +45,8 @@ public static TracerProviderBuilder AddQuartzInstrumentation(
this TracerProviderBuilder builder,
Action<QuartzInstrumentationOptions> configure)
{
Guard.ThrowIfNull(builder);

var options = new QuartzInstrumentationOptions();
configure?.Invoke(options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public async Task Should_Create_Activity_And_Enrich_When_Enrich()
if (payload is IJobDetail jobDetail)
{
var dataMap = jobDetail.JobDataMap;
if (dataMap.ContainsKey("TestId"))
if (dataMap.TryGetValue("TestId", out var value))
{
a.SetTag("test.id", dataMap["TestId"]);
a.SetTag("test.id", value);
}
}
})
Expand Down Expand Up @@ -229,9 +229,9 @@ public async Task Should_Enrich_Exception_When_Record_Exception_Enabled_And_Enri
if (p is IJobDetail jobDetail)
{
var dataMap = jobDetail.JobDataMap;
if (dataMap.ContainsKey("TestId"))
if (dataMap.TryGetValue("TestId", out var value))
{
a.SetTag("test.id", dataMap["TestId"]);
a.SetTag("test.id", value);
}
}
};
Expand Down