Skip to content

Commit

Permalink
Add Activity tag enricher
Browse files Browse the repository at this point in the history
  • Loading branch information
Kahbazi committed Aug 4, 2021
1 parent 48f6d47 commit bae8ac5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 89 deletions.
11 changes: 0 additions & 11 deletions Source/Serilog.Enrichers.Span/ActivityEnricher.cs
Expand Up @@ -33,25 +33,15 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
AddSpanId(logEvent, activity);
AddTraceId(logEvent, activity);
AddParentId(logEvent, activity);
AddTags(logEvent, activity);
#else
logEvent.AddPropertyIfAbsent(new LogEventProperty(SpanId, new ScalarValue(activity.GetSpanId())));
logEvent.AddPropertyIfAbsent(new LogEventProperty(TraceId, new ScalarValue(activity.GetTraceId())));
logEvent.AddPropertyIfAbsent(new LogEventProperty(ParentId, new ScalarValue(activity.GetParentId())));
#endif
}
}

#if NET5_0_OR_GREATER

private static void AddTags(LogEvent logEvent, Activity activity)
{
foreach (var item in activity.Tags)
{
logEvent.AddPropertyIfAbsent(new LogEventProperty(item.Key, new ScalarValue(item.Value)));
}
}

private static void AddSpanId(LogEvent logEvent, Activity activity)
{
var property = activity.GetCustomProperty(SpanIdKey);
Expand Down Expand Up @@ -87,7 +77,6 @@ private static void AddParentId(LogEvent logEvent, Activity activity)

logEvent.AddPropertyIfAbsent(logEventProperty);
}

#endif
}
}
31 changes: 31 additions & 0 deletions Source/Serilog.Enrichers.Span/ActivityTagEnricher.cs
@@ -0,0 +1,31 @@
namespace Serilog.Enrichers.Span
{
using System.Diagnostics;
using System.Linq;
using Serilog.Core;
using Serilog.Events;

/// <summary>
/// A log event enricher which adds tags from the current <see cref="Activity"/>.
/// </summary>
public class ActivityTagEnricher : ILogEventEnricher
{
private const string ActivityTags = "Attributes";

/// <summary>
/// Enrich the log event.
/// </summary>
/// <param name="logEvent">The log event to enrich.</param>
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param>
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var activity = Activity.Current;

if (activity is not null)
{
var tags = activity.Tags.Select(tag => new LogEventProperty(tag.Key, new ScalarValue(tag.Value)));
logEvent.AddPropertyIfAbsent(new LogEventProperty(ActivityTags, new StructureValue(tags)));
}
}
}
}
Expand Up @@ -15,12 +15,31 @@ public static class LoggerEnrichmentConfigurationExtensions
/// <param name="loggerEnrichmentConfiguration">The enrichment configuration.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration WithSpan(this LoggerEnrichmentConfiguration loggerEnrichmentConfiguration)
=> loggerEnrichmentConfiguration.WithSpan(new SpanOptions());

/// <summary>
/// Enrich logger output with span information from the current <see cref="Activity"/>.
/// </summary>
/// <param name="loggerEnrichmentConfiguration">The enrichment configuration.</param>
/// <param name="spanOptions"><see cref="SpanOptions"/> to use for enriching Activity.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration WithSpan(this LoggerEnrichmentConfiguration loggerEnrichmentConfiguration, SpanOptions spanOptions)
{
if (loggerEnrichmentConfiguration is null)
{
throw new ArgumentNullException(nameof(loggerEnrichmentConfiguration));
}

if (spanOptions is null)
{
throw new ArgumentNullException(nameof(spanOptions));
}

if (spanOptions.IncludeTags)
{
loggerEnrichmentConfiguration.With<ActivityTagEnricher>();
}

return loggerEnrichmentConfiguration.With<ActivityEnricher>();
}
}
Expand Down
13 changes: 13 additions & 0 deletions Source/Serilog.Enrichers.Span/SpanOptions.cs
@@ -0,0 +1,13 @@
namespace Serilog.Enrichers.Span
{
/// <summary>
/// Options.
/// </summary>
public class SpanOptions
{
/// <summary>
/// Gets or sets a value indicating whether to include tags in log or not. Default is false.
/// </summary>
public bool IncludeTags { get; set; }
}
}
44 changes: 0 additions & 44 deletions Tests/Serilog.Enrichers.Span.Test/Class1Test.cs
@@ -1,8 +1,6 @@
namespace Serilog.Enrichers.Span.Test
{
using System.Diagnostics;
using Serilog.Enrichers.Span;
using Serilog.Events;
using Xunit;

public class Class1Test
Expand All @@ -14,47 +12,5 @@ public void Given_When_Then()

Assert.NotNull(class1);
}

[Fact]
public void AddTags()
{
LogEvent? evt = null;
var log = new LoggerConfiguration()
.Enrich.WithSpan()
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

ActivitySource.AddActivityListener(new ActivityListener
{
ActivityStarted = activity => { },
ActivityStopped = activity => { },
ShouldListenTo = activitySource => true,
SampleUsingParentId = (ref ActivityCreationOptions<string> activityOptions) => ActivitySamplingResult.AllDataAndRecorded,
Sample = (ref ActivityCreationOptions<ActivityContext> activityOptions) => ActivitySamplingResult.AllDataAndRecorded,
});
var activitySource = new ActivitySource("Test");


using (var activity = activitySource.StartActivity("AddTags"))
{
activity?.AddTag("Name", "Chris Shim");
activity?.AddTag("NullString", null);

log.Information(@"Has an EnvironmentUserName property with [domain\]userName");
}

Assert.NotNull(evt);

Assert.Equal("Chris Shim", evt?.Properties["Name"].LiteralValue());
Assert.Null(evt?.Properties["NullString"].LiteralValue());
}
}

internal static class Extensions
{
public static object LiteralValue(this LogEventPropertyValue @this)
{
return ((ScalarValue)@this).Value;
}
}
}
34 changes: 0 additions & 34 deletions Tests/Serilog.Enrichers.Span.Test/DelegatingSink.cs

This file was deleted.

0 comments on commit bae8ac5

Please sign in to comment.