Skip to content

Commit

Permalink
feat: add activity extensions (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alxandr authored Aug 23, 2024
1 parent 661d6c8 commit df081df
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using Altinn.Authorization.ServiceDefaults.Telemetry;
using System.Diagnostics;

namespace Altinn.Authorization.ServiceDefaults.Npgsql.Yuniql;

Expand All @@ -11,10 +12,5 @@ internal static class YuniqlActivityProvider
private static readonly ActivitySource _activitySource = new(SourceName);

public static Activity? StartActivity(ActivityKind kind, string activityName, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
using var state = ActivityHelper.ThreadLocalState;
state.AddTags(tags);

return _activitySource.StartActivity(kind, tags: state.Tags, name: activityName);
}
=> _activitySource.StartActivity(kind, activityName, tags);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Altinn.Authorization.ServiceDefaults.Npgsql.Yuniql;
namespace Altinn.Authorization.ServiceDefaults.Telemetry;

internal static class ActivityHelper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Diagnostics;

namespace Altinn.Authorization.ServiceDefaults.Telemetry;

/// <summary>
/// Extension methods for <see cref="Activity"/> and <see cref="ActivitySource"/>.
/// </summary>
public static class AltinnActivityExtensions
{
/// <summary>
/// Creates and starts a new <see cref="Activity"/> object if there is any listener to the Activity, returns null otherwise.
/// </summary>
/// <param name="source">The <see cref="ActivitySource"/>.</param>
/// <param name="kind">The <see cref="ActivityKind"/>.</param>
/// <param name="name">The operation name of the Activity.</param>
/// <param name="tags">The operation tags.</param>
/// <returns>The created <see cref="Activity"/> object or null if there is no any event listener.</returns>
public static Activity? StartActivity(this ActivitySource source, ActivityKind kind, string name, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
using var state = ActivityHelper.ThreadLocalState;
state.AddTags(tags);

return source.StartActivity(kind, tags: state.Tags, name: name);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Altinn.Authorization.ServiceDefaults.Npgsql.Yuniql;
using Altinn.Authorization.ServiceDefaults.Telemetry;
using System.Collections.Concurrent;
using System.Diagnostics;

namespace Altinn.Authorization.ServiceDefaults.Npgsql.Tests;
namespace Altinn.Authorization.ServiceDefaults.Tests;

public class ActivityHelperTests
{
Expand Down

0 comments on commit df081df

Please sign in to comment.