Skip to content

Commit

Permalink
Migrate more telemetry static methods to OpenTelemetryHelpers vs. Ext…
Browse files Browse the repository at this point in the history
…ension methods.
  • Loading branch information
houseofcat committed Apr 1, 2024
1 parent 54ef069 commit 2d81452
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
20 changes: 2 additions & 18 deletions src/HouseofCat.Dataflows/Extensions/WorkStateExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using HouseofCat.Utilities.Helpers;
using OpenTelemetry.Trace;
using System.Collections.Generic;
using System.Diagnostics;

namespace HouseofCat.Dataflows.Extensions;

Expand All @@ -18,27 +17,12 @@ public static void SetOpenTelemetryError(this IWorkState state, string message =

public static void SetCurrentActivityAsError(this IWorkState state, string message = null)
{
var activity = Activity.Current;
if (activity is null) return;

activity.SetStatus(ActivityStatusCode.Error, message ?? state.EDI.SourceException?.Message);
if (state?.EDI is not null)
{
if (state.EDI.SourceException is not null)
{
activity.RecordException(state.EDI.SourceException);
}
}
else
{
activity.SetStatus(ActivityStatusCode.Error, message);
}
OpenTelemetryHelpers.SetCurrentActivityAsError(state?.EDI?.SourceException, message);
}

public static void SetCurrentSpanAsError(this IWorkState state, string message = null)
{
var span = Tracer.CurrentSpan;
state.SetSpanAsError(span, message);
OpenTelemetryHelpers.SetCurrentSpanAsError(state?.EDI?.SourceException, message);
}

public static void SetSpanAsError(this IWorkState state, TelemetrySpan span, string message = null)
Expand Down
44 changes: 44 additions & 0 deletions src/HouseofCat.Utilities/Helpers/OpenTelemetryHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,48 @@ public static string FormatOpenTelemetryHeader(
}

#endregion

#region Error Handling

public static void SetCurrentActivityAsError(Exception ex, string message = null)
{
var activity = Activity.Current;
if (activity is null) return;

SetActivityAsError(activity, ex, message);
}

public static void SetActivityAsError(Activity activity, Exception ex, string message = null)
{
if (activity is null) return;

if (ex is not null)
{
activity.RecordException(ex);
}

activity.SetStatus(ActivityStatusCode.Error, message);
}

public static void SetCurrentSpanAsError(Exception ex, string message = null)
{
var span = Tracer.CurrentSpan;
if (span is null) return;

SetSpanAsError(span, ex, message);
}

public static void SetSpanAsError(TelemetrySpan span, Exception ex, string message = null)
{
if (span is null) return;

if (ex is not null)
{
span.RecordException(ex);
}

span.SetStatus(Status.Error.WithDescription(message));
}

#endregion
}

0 comments on commit 2d81452

Please sign in to comment.