Skip to content

Commit

Permalink
Added OpenTelemetry TraceId and SpanId
Browse files Browse the repository at this point in the history
Added the new standard columns TraceId and SpanId for OpenTelemetry tracing. Those columns are by default not enabled and must be added to the ColumnOptions.Store property to use them (for backwards compatibility). If the columns are activated, the corresponding OpenTracing log event properties from Serilog will be written to those columns (serilog/serilog#1955).
  • Loading branch information
ckadluba committed Nov 25, 2023
1 parent a268478 commit 8334b5d
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
<PackageVersion Include="Moq" Version="4.18.2" />
<PackageVersion Include="xunit" Version="2.4.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
<PackageVersion Include="Serilog" Version="2.12.0" />
<PackageVersion Include="Serilog" Version="3.1.1" />
<PackageVersion Include="Serilog.Extensions.Hosting" Version="5.0.1" />
<PackageVersion Include="Serilog.Settings.Configuration" Version="3.4.0" />
<PackageVersion Include="Serilog.Sinks.PeriodicBatching" Version="3.1.0" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ private static void ReadStandardColumns(IConfigurationSection config, ColumnOpti
section = config.GetSection("messageTemplate");
if (section != null)
SetCommonColumnOptions(section, columnOptions.MessageTemplate);

section = config.GetSection("traceId");
if (section != null)
{
SetCommonColumnOptions(section, columnOptions.TraceId);
}

section = config.GetSection("spanId");
if (section != null)
{
SetCommonColumnOptions(section, columnOptions.SpanId);
}
}

private static void ReadMiscColumnOptions(IConfigurationSection config, ColumnOptions columnOptions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ public StandardColumnConfigLevel Level
get => (StandardColumnConfigLevel)base[nameof(Level)];
}

[ConfigurationProperty(nameof(TraceId))]
public StandardColumnConfigTraceId TraceId
{
get => (StandardColumnConfigTraceId)base[nameof(TraceId)];
}

[ConfigurationProperty(nameof(SpanId))]
public StandardColumnConfigSpanId SpanId
{
get => (StandardColumnConfigSpanId)base[nameof(SpanId)];
}

[ConfigurationProperty(nameof(LogEvent))]
public StandardColumnConfigLogEvent LogEvent
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Configuration;

// Disable XML comment warnings for internal config classes which are required to have public members
#pragma warning disable 1591

namespace Serilog.Sinks.MSSqlServer
{
public class StandardColumnConfigSpanId : ColumnConfig
{
public StandardColumnConfigSpanId() : base()
{ }

// override to set IsRequired = false
[ConfigurationProperty("ColumnName", IsRequired = false, IsKey = true)]
public override string ColumnName
{
get => base.ColumnName;
set => base.ColumnName = value;
}
}
}

#pragma warning restore 1591

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Configuration;

// Disable XML comment warnings for internal config classes which are required to have public members
#pragma warning disable 1591

namespace Serilog.Sinks.MSSqlServer
{
public class StandardColumnConfigTraceId : ColumnConfig
{
public StandardColumnConfigTraceId() : base()
{ }

// override to set IsRequired = false
[ConfigurationProperty("ColumnName", IsRequired = false, IsKey = true)]
public override string ColumnName
{
get => base.ColumnName;
set => base.ColumnName = value;
}
}
}

#pragma warning restore 1591

Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ private static void ReadStandardColumns(MSSqlServerConfigurationSection config,
SetCommonColumnOptions(config.Id, columnOptions.Id);
SetCommonColumnOptions(config.Level, columnOptions.Level);
SetCommonColumnOptions(config.LogEvent, columnOptions.LogEvent);
SetCommonColumnOptions(config.TraceId, columnOptions.TraceId);
SetCommonColumnOptions(config.SpanId, columnOptions.SpanId);
SetCommonColumnOptions(config.Message, columnOptions.Message);
SetCommonColumnOptions(config.MessageTemplate, columnOptions.MessageTemplate);
SetCommonColumnOptions(config.PropertiesColumn, columnOptions.Properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public ColumnOptions()
// Apply any defaults in the individual Standard Column constructors.
Id = new IdColumnOptions();
Level = new LevelColumnOptions();
TraceId = new TraceIdColumnOptions();
SpanId = new SpanIdColumnOptions();
Properties = new PropertiesColumnOptions();
Message = new MessageColumnOptions();
MessageTemplate = new MessageTemplateColumnOptions();
Expand Down Expand Up @@ -113,6 +115,16 @@ public ICollection<StandardColumn> Store
/// </summary>
public LevelColumnOptions Level { get; private set; }

/// <summary>
/// Options for the TraceId column.
/// </summary>
public TraceIdColumnOptions TraceId { get; private set; }

/// <summary>
/// Options for the SpanId column.
/// </summary>
public SpanIdColumnOptions SpanId { get; private set; }

/// <summary>
/// Options for the Properties column.
/// </summary>
Expand Down Expand Up @@ -152,6 +164,8 @@ internal SqlColumn GetStandardColumnOptions(StandardColumn standardColumn)
{
case StandardColumn.Id: return Id;
case StandardColumn.Level: return Level;
case StandardColumn.TraceId: return TraceId;
case StandardColumn.SpanId: return SpanId;
case StandardColumn.TimeStamp: return TimeStamp;
case StandardColumn.LogEvent: return LogEvent;
case StandardColumn.Message: return Message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Data;

namespace Serilog.Sinks.MSSqlServer
{
public partial class ColumnOptions // Standard Column options are inner classes for backwards-compatibility.
{
/// <summary>
/// Options for the SpanId column.
/// </summary>
public class SpanIdColumnOptions : SqlColumn
{
/// <summary>
/// Constructor.
/// </summary>
public SpanIdColumnOptions() : base()
{
StandardColumnIdentifier = StandardColumn.SpanId;
DataType = SqlDbType.NVarChar;
}

/// <summary>
/// The SpanId column defaults to NVarChar and must be of a character-storage data type.
/// </summary>
public new SqlDbType DataType
{
get => base.DataType;
set
{
if (!SqlDataTypes.VariableCharacterColumnTypes.Contains(value))
throw new ArgumentException("The Standard Column \"SpanId\" must be NVarChar or VarChar.");
base.DataType = value;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Data;

namespace Serilog.Sinks.MSSqlServer
{
public partial class ColumnOptions // Standard Column options are inner classes for backwards-compatibility.
{
/// <summary>
/// Options for the TraceId column.
/// </summary>
public class TraceIdColumnOptions : SqlColumn
{
/// <summary>
/// Constructor.
/// </summary>
public TraceIdColumnOptions() : base()
{
StandardColumnIdentifier = StandardColumn.TraceId;
DataType = SqlDbType.NVarChar;
}

/// <summary>
/// The TraceId column defaults to NVarChar and must be of a character-storage data type.
/// </summary>
public new SqlDbType DataType
{
get => base.DataType;
set
{
if (!SqlDataTypes.VariableCharacterColumnTypes.Contains(value))
throw new ArgumentException("The Standard Column \"TraceId\" must be NVarChar or VarChar.");
base.DataType = value;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ internal class StandardColumnDataGenerator : IStandardColumnDataGenerator
return new KeyValuePair<string, object>(_columnOptions.MessageTemplate.ColumnName, logEvent.MessageTemplate.Text.TruncateOutput(_columnOptions.MessageTemplate.DataLength));
case StandardColumn.Level:
return new KeyValuePair<string, object>(_columnOptions.Level.ColumnName, _columnOptions.Level.StoreAsEnum ? (object)logEvent.Level : logEvent.Level.ToString());
case StandardColumn.TraceId:
return new KeyValuePair<string, object>(_columnOptions.TraceId.ColumnName, logEvent.TraceId);
case StandardColumn.SpanId:
return new KeyValuePair<string, object>(_columnOptions.SpanId.ColumnName, logEvent.SpanId);
case StandardColumn.TimeStamp:
return GetTimeStampStandardColumnNameAndValue(logEvent);
case StandardColumn.Exception:
Expand Down
10 changes: 10 additions & 0 deletions src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/StandardColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public enum StandardColumn
/// </summary>
Level,

/// <summary>
/// The OpenTelemetry trace id of the event.
/// </summary>
TraceId,

/// <summary>
/// The OpenTelemetry span id of the event.
/// </summary>
SpanId,

/// <summary>
/// The time at which the event occurred.
/// </summary>
Expand Down

0 comments on commit 8334b5d

Please sign in to comment.