-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added instrumentation via System.Diagnostics.Activity (#110)
- Loading branch information
1 parent
d443882
commit ccd70d1
Showing
5 changed files
with
182 additions
and
8 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
54 changes: 54 additions & 0 deletions
54
src/Tingle.PeriodicTasks/Diagnostics/PeriodicTasksActivitySource.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,54 @@ | ||
using System.Diagnostics; | ||
using System.Reflection; | ||
|
||
namespace Tingle.PeriodicTasks.Diagnostics; | ||
|
||
/// | ||
public static class PeriodicTasksActivitySource | ||
{ | ||
private static readonly AssemblyName AssemblyName = typeof(PeriodicTasksActivitySource).Assembly.GetName(); | ||
private static readonly string ActivitySourceName = AssemblyName.Name!; | ||
private static readonly Version Version = AssemblyName.Version!; | ||
private static readonly ActivitySource ActivitySource = new(ActivitySourceName, Version.ToString()); | ||
|
||
/// <summary> | ||
/// Creates a new activity if there are active listeners for it, using the specified | ||
/// name, activity kind, and parent Id. | ||
/// </summary> | ||
/// <param name="name"></param> | ||
/// <param name="kind"></param> | ||
/// <param name="parentId"></param> | ||
/// <returns></returns> | ||
public static Activity? StartActivity(string name, ActivityKind kind = ActivityKind.Internal, string? parentId = null) | ||
{ | ||
return parentId is not null | ||
? ActivitySource.StartActivity(name: name, kind: kind, parentId: parentId) | ||
: ActivitySource.StartActivity(name: name, kind: kind); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Names for activities generated by PeriodicTasks | ||
/// </summary> | ||
public static class ActivityNames | ||
{ | ||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
public const string Execute = "Tingle.PeriodicTasks.Execute"; | ||
public const string ExecuteAttempt = "Tingle.PeriodicTasks.ExecuteAttempt"; | ||
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member | ||
} | ||
|
||
/// <summary> | ||
/// Names for tags added to activities generated by PeriodicTasks | ||
/// </summary> | ||
public static class ActivityTagNames | ||
{ | ||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
public const string PeriodicTaskType = "periodictask.type"; | ||
public const string PeriodicTaskName = "periodictask.name"; | ||
public const string PeriodicTaskSchedule = "periodictask.schedule"; | ||
public const string PeriodicTaskTimezone = "periodictask.timezone"; | ||
public const string PeriodicTaskDeadline = "periodictask.deadline"; | ||
public const string PeriodicTaskAttemptNumber = "periodictask.attempt_number"; | ||
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member | ||
} |
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,57 @@ | ||
namespace System.Diagnostics; | ||
|
||
internal static class ActivityExtensions | ||
{ | ||
// Copied from https://github.com/dotnet/runtime/pull/102905/files | ||
#if !NET9_0_OR_GREATER | ||
public static Activity AddException(this Activity activity, Exception exception, TagList tags = default, DateTimeOffset timestamp = default) | ||
{ | ||
ArgumentNullException.ThrowIfNull(activity); | ||
ArgumentNullException.ThrowIfNull(exception); | ||
|
||
TagList exceptionTags = tags; | ||
|
||
const string ExceptionEventName = "exception"; | ||
const string ExceptionMessageTag = "exception.message"; | ||
const string ExceptionStackTraceTag = "exception.stacktrace"; | ||
const string ExceptionTypeTag = "exception.type"; | ||
|
||
bool hasMessage = false; | ||
bool hasStackTrace = false; | ||
bool hasType = false; | ||
|
||
for (int i = 0; i < exceptionTags.Count; i++) | ||
{ | ||
if (exceptionTags[i].Key == ExceptionMessageTag) | ||
{ | ||
hasMessage = true; | ||
} | ||
else if (exceptionTags[i].Key == ExceptionStackTraceTag) | ||
{ | ||
hasStackTrace = true; | ||
} | ||
else if (exceptionTags[i].Key == ExceptionTypeTag) | ||
{ | ||
hasType = true; | ||
} | ||
} | ||
|
||
if (!hasMessage) | ||
{ | ||
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionMessageTag, exception.Message)); | ||
} | ||
|
||
if (!hasStackTrace) | ||
{ | ||
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionStackTraceTag, exception.ToString())); | ||
} | ||
|
||
if (!hasType) | ||
{ | ||
exceptionTags.Add(new KeyValuePair<string, object?>(ExceptionTypeTag, exception.GetType().ToString())); | ||
} | ||
|
||
return activity.AddEvent(new ActivityEvent(ExceptionEventName, timestamp)); | ||
} | ||
#endif | ||
} |
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