Skip to content

Commit

Permalink
Skip ActivityData capture for NLog, since doing Layout capture (#379)
Browse files Browse the repository at this point in the history
* Skip ActivityData capture for NLog, since doing Layout capture

* address formatting

---------

Co-authored-by: Martijn Laarman <[email protected]>
  • Loading branch information
snakefoot and Mpdreamz authored May 28, 2024
1 parent 49a7d32 commit 6d61edb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
51 changes: 51 additions & 0 deletions src/Elastic.CommonSchema.NLog/ActivityExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Diagnostics;

namespace Elastic.CommonSchema.NLog
{
/// <summary>
/// Helpers for getting the right values from Activity no matter the format (w3c or hierarchical)
/// </summary>
internal static class ActivityExtensions
{
private static readonly ActivitySpanId EmptySpanId = default;
private static readonly ActivityTraceId EmptyTraceId = default;

public static string GetSpanId(this Activity activity) =>
activity.IdFormat == ActivityIdFormat.W3C ?
SpanIdToHexString(activity.SpanId) :
activity.Id;

public static string GetTraceId(this Activity activity) =>
activity.IdFormat == ActivityIdFormat.W3C ?
TraceIdToHexString(activity.TraceId) :
activity.RootId;

public static string GetParentId(this Activity activity) =>
activity.IdFormat == ActivityIdFormat.W3C ?
SpanIdToHexString(activity.ParentSpanId) :
activity.ParentId;

private static string SpanIdToHexString(ActivitySpanId spanId)
{
if (EmptySpanId.Equals(spanId))
return string.Empty;

var spanHexString = spanId.ToHexString();
if (ReferenceEquals(spanHexString, EmptySpanId.ToHexString()))
return string.Empty;

return spanHexString;
}

private static string TraceIdToHexString(ActivityTraceId traceId)
{
if (EmptyTraceId.Equals(traceId))
return string.Empty;

var traceHexString = traceId.ToHexString();
return ReferenceEquals(traceHexString, EmptyTraceId.ToHexString())
? string.Empty
: traceHexString;
}
}
}
10 changes: 8 additions & 2 deletions src/Elastic.CommonSchema.NLog/EcsLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Text;
using System.Text.Json.Serialization;
Expand All @@ -21,6 +22,7 @@ internal class NlogEcsDocumentCreationOptions : IEcsDocumentCreationOptions
public bool IncludeHost { get; set; } = false;
public bool IncludeProcess { get; set; } = false;
public bool IncludeUser { get; set; } = false;
public bool IncludeTraceId { get; set; } = false;
}

/// <summary> An NLOG layout implementation that renders logs as ECS json</summary>
Expand Down Expand Up @@ -146,11 +148,11 @@ private static bool NLogWeb4Registered() =>

// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
/// <inheritdoc cref="BaseFieldSet.TraceId"/>
public Layout ApmTraceId { get; set; }
public Layout ApmTraceId { get; set; } = FromMethod(_ => ResolveTraceId());
/// <inheritdoc cref="BaseFieldSet.TransactionId"/>
public Layout ApmTransactionId { get; set; }
/// <inheritdoc cref="BaseFieldSet.SpanId"/>
public Layout ApmSpanId { get; set; }
public Layout ApmSpanId { get; set; } = FromMethod(_ => ResolveSpanId());

/// <inheritdoc cref="ServiceFieldSet.Name"/>
public Layout ApmServiceName { get; set; }
Expand Down Expand Up @@ -775,6 +777,10 @@ private static void Populate(IDictionary<string, string> propertyBag, string key
propertyBag.Add(usedKey, value);
}

private static string ResolveTraceId() => Activity.Current?.GetTraceId();

private static string ResolveSpanId() => Activity.Current?.GetSpanId();

/// <summary>
/// A subclass of <see cref="EcsDocument"/> that adds additional properties related to Extensions logging.
/// <para>For instance it adds scope information to each logged event</para>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class EcsTextFormatterConfiguration<TEcsDocument> : IEcsTextFormatterConf
/// <inheritdoc cref="IEcsDocumentCreationOptions.IncludeUser"/>
public bool IncludeUser { get; set; } = true;

/// <inheritdoc cref="IEcsDocumentCreationOptions.IncludeTraceId"/>
public bool IncludeTraceId { get; set; } = true;

/// <inheritdoc cref="IEcsTextFormatterConfiguration.MapHttpAdapter"/>
public IHttpAdapter? MapHttpAdapter { get; set; }

Expand Down
8 changes: 7 additions & 1 deletion src/Elastic.CommonSchema/EcsDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public interface IEcsDocumentCreationOptions
/// Gets or sets a flag indicating whether user details should be included in the message. Defaults to <c>true</c>.
/// </summary>
bool IncludeUser { get; set; }

/// <summary>
/// Gets or sets a flag indicating whether TraceId/SpanId should be included in the message. Defaults to <c>true</c>.
/// </summary>
bool IncludeTraceId { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -85,11 +90,12 @@ public static TEcsDocument CreateNewWithDefaults<TEcsDocument>(
Error = GetError(exception),
Service = GetService(initialCache)
};
SetActivityData(doc);

if (options?.IncludeHost is null or true) doc.Host = GetHost(initialCache);
if (options?.IncludeProcess is null or true) doc.Process = GetProcess(initialCache);
if (options?.IncludeUser is null or true) doc.User = GetUser();
if (options?.IncludeTraceId is null or true)
SetActivityData(doc);

return doc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class ElasticsearchLoggerOptions : IEcsDocumentCreationOptions
/// </summary>
public bool IncludeUser { get; set; } = true;

/// <summary>
/// Gets or sets a flag indicating whether TraceId/SpanId should be included in the message. Defaults to <c>true</c>.
/// </summary>
public bool IncludeTraceId { get; set; } = true;

/// <summary>
/// The data stream to log into, defaults to <c>logs-generic-default</c> if neither <see cref="DataStream"/> or <see cref="Index"/> is set.
/// </summary>
Expand Down

0 comments on commit 6d61edb

Please sign in to comment.