Skip to content

Commit 8334b5d

Browse files
committed
Added OpenTelemetry TraceId and SpanId
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).
1 parent a268478 commit 8334b5d

File tree

11 files changed

+178
-2
lines changed

11 files changed

+178
-2
lines changed

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
<PackageVersion Include="Moq" Version="4.18.2" />
2424
<PackageVersion Include="xunit" Version="2.4.2" />
2525
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
26-
<PackageVersion Include="Serilog" Version="2.12.0" />
26+
<PackageVersion Include="Serilog" Version="3.1.1" />
2727
<PackageVersion Include="Serilog.Extensions.Hosting" Version="5.0.1" />
2828
<PackageVersion Include="Serilog.Settings.Configuration" Version="3.4.0" />
2929
<PackageVersion Include="Serilog.Sinks.PeriodicBatching" Version="3.1.0" />
3030
</ItemGroup>
31-
</Project>
31+
</Project>

src/Serilog.Sinks.MSSqlServer/Configuration/Implementations/Microsoft.Extensions.Configuration/MicrosoftExtensionsColumnOptionsProvider.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ private static void ReadStandardColumns(IConfigurationSection config, ColumnOpti
147147
section = config.GetSection("messageTemplate");
148148
if (section != null)
149149
SetCommonColumnOptions(section, columnOptions.MessageTemplate);
150+
151+
section = config.GetSection("traceId");
152+
if (section != null)
153+
{
154+
SetCommonColumnOptions(section, columnOptions.TraceId);
155+
}
156+
157+
section = config.GetSection("spanId");
158+
if (section != null)
159+
{
160+
SetCommonColumnOptions(section, columnOptions.SpanId);
161+
}
150162
}
151163

152164
private static void ReadMiscColumnOptions(IConfigurationSection config, ColumnOptions columnOptions)

src/Serilog.Sinks.MSSqlServer/Configuration/Implementations/System.Configuration/MSSqlServerConfigurationSection.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ public StandardColumnConfigLevel Level
106106
get => (StandardColumnConfigLevel)base[nameof(Level)];
107107
}
108108

109+
[ConfigurationProperty(nameof(TraceId))]
110+
public StandardColumnConfigTraceId TraceId
111+
{
112+
get => (StandardColumnConfigTraceId)base[nameof(TraceId)];
113+
}
114+
115+
[ConfigurationProperty(nameof(SpanId))]
116+
public StandardColumnConfigSpanId SpanId
117+
{
118+
get => (StandardColumnConfigSpanId)base[nameof(SpanId)];
119+
}
120+
109121
[ConfigurationProperty(nameof(LogEvent))]
110122
public StandardColumnConfigLogEvent LogEvent
111123
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Configuration;
2+
3+
// Disable XML comment warnings for internal config classes which are required to have public members
4+
#pragma warning disable 1591
5+
6+
namespace Serilog.Sinks.MSSqlServer
7+
{
8+
public class StandardColumnConfigSpanId : ColumnConfig
9+
{
10+
public StandardColumnConfigSpanId() : base()
11+
{ }
12+
13+
// override to set IsRequired = false
14+
[ConfigurationProperty("ColumnName", IsRequired = false, IsKey = true)]
15+
public override string ColumnName
16+
{
17+
get => base.ColumnName;
18+
set => base.ColumnName = value;
19+
}
20+
}
21+
}
22+
23+
#pragma warning restore 1591
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Configuration;
2+
3+
// Disable XML comment warnings for internal config classes which are required to have public members
4+
#pragma warning disable 1591
5+
6+
namespace Serilog.Sinks.MSSqlServer
7+
{
8+
public class StandardColumnConfigTraceId : ColumnConfig
9+
{
10+
public StandardColumnConfigTraceId() : base()
11+
{ }
12+
13+
// override to set IsRequired = false
14+
[ConfigurationProperty("ColumnName", IsRequired = false, IsKey = true)]
15+
public override string ColumnName
16+
{
17+
get => base.ColumnName;
18+
set => base.ColumnName = value;
19+
}
20+
}
21+
}
22+
23+
#pragma warning restore 1591
24+

src/Serilog.Sinks.MSSqlServer/Configuration/Implementations/System.Configuration/SystemConfigurationColumnOptionsProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ private static void ReadStandardColumns(MSSqlServerConfigurationSection config,
105105
SetCommonColumnOptions(config.Id, columnOptions.Id);
106106
SetCommonColumnOptions(config.Level, columnOptions.Level);
107107
SetCommonColumnOptions(config.LogEvent, columnOptions.LogEvent);
108+
SetCommonColumnOptions(config.TraceId, columnOptions.TraceId);
109+
SetCommonColumnOptions(config.SpanId, columnOptions.SpanId);
108110
SetCommonColumnOptions(config.Message, columnOptions.Message);
109111
SetCommonColumnOptions(config.MessageTemplate, columnOptions.MessageTemplate);
110112
SetCommonColumnOptions(config.PropertiesColumn, columnOptions.Properties);

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/ColumnOptions/ColumnOptions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public ColumnOptions()
2020
// Apply any defaults in the individual Standard Column constructors.
2121
Id = new IdColumnOptions();
2222
Level = new LevelColumnOptions();
23+
TraceId = new TraceIdColumnOptions();
24+
SpanId = new SpanIdColumnOptions();
2325
Properties = new PropertiesColumnOptions();
2426
Message = new MessageColumnOptions();
2527
MessageTemplate = new MessageTemplateColumnOptions();
@@ -113,6 +115,16 @@ public ICollection<StandardColumn> Store
113115
/// </summary>
114116
public LevelColumnOptions Level { get; private set; }
115117

118+
/// <summary>
119+
/// Options for the TraceId column.
120+
/// </summary>
121+
public TraceIdColumnOptions TraceId { get; private set; }
122+
123+
/// <summary>
124+
/// Options for the SpanId column.
125+
/// </summary>
126+
public SpanIdColumnOptions SpanId { get; private set; }
127+
116128
/// <summary>
117129
/// Options for the Properties column.
118130
/// </summary>
@@ -152,6 +164,8 @@ internal SqlColumn GetStandardColumnOptions(StandardColumn standardColumn)
152164
{
153165
case StandardColumn.Id: return Id;
154166
case StandardColumn.Level: return Level;
167+
case StandardColumn.TraceId: return TraceId;
168+
case StandardColumn.SpanId: return SpanId;
155169
case StandardColumn.TimeStamp: return TimeStamp;
156170
case StandardColumn.LogEvent: return LogEvent;
157171
case StandardColumn.Message: return Message;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Data;
3+
4+
namespace Serilog.Sinks.MSSqlServer
5+
{
6+
public partial class ColumnOptions // Standard Column options are inner classes for backwards-compatibility.
7+
{
8+
/// <summary>
9+
/// Options for the SpanId column.
10+
/// </summary>
11+
public class SpanIdColumnOptions : SqlColumn
12+
{
13+
/// <summary>
14+
/// Constructor.
15+
/// </summary>
16+
public SpanIdColumnOptions() : base()
17+
{
18+
StandardColumnIdentifier = StandardColumn.SpanId;
19+
DataType = SqlDbType.NVarChar;
20+
}
21+
22+
/// <summary>
23+
/// The SpanId column defaults to NVarChar and must be of a character-storage data type.
24+
/// </summary>
25+
public new SqlDbType DataType
26+
{
27+
get => base.DataType;
28+
set
29+
{
30+
if (!SqlDataTypes.VariableCharacterColumnTypes.Contains(value))
31+
throw new ArgumentException("The Standard Column \"SpanId\" must be NVarChar or VarChar.");
32+
base.DataType = value;
33+
}
34+
}
35+
}
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Data;
3+
4+
namespace Serilog.Sinks.MSSqlServer
5+
{
6+
public partial class ColumnOptions // Standard Column options are inner classes for backwards-compatibility.
7+
{
8+
/// <summary>
9+
/// Options for the TraceId column.
10+
/// </summary>
11+
public class TraceIdColumnOptions : SqlColumn
12+
{
13+
/// <summary>
14+
/// Constructor.
15+
/// </summary>
16+
public TraceIdColumnOptions() : base()
17+
{
18+
StandardColumnIdentifier = StandardColumn.TraceId;
19+
DataType = SqlDbType.NVarChar;
20+
}
21+
22+
/// <summary>
23+
/// The TraceId column defaults to NVarChar and must be of a character-storage data type.
24+
/// </summary>
25+
public new SqlDbType DataType
26+
{
27+
get => base.DataType;
28+
set
29+
{
30+
if (!SqlDataTypes.VariableCharacterColumnTypes.Contains(value))
31+
throw new ArgumentException("The Standard Column \"TraceId\" must be NVarChar or VarChar.");
32+
base.DataType = value;
33+
}
34+
}
35+
}
36+
}
37+
}

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Output/StandardColumnDataGenerator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public KeyValuePair<string, object> GetStandardColumnNameAndValue(StandardColumn
5050
return new KeyValuePair<string, object>(_columnOptions.MessageTemplate.ColumnName, logEvent.MessageTemplate.Text.TruncateOutput(_columnOptions.MessageTemplate.DataLength));
5151
case StandardColumn.Level:
5252
return new KeyValuePair<string, object>(_columnOptions.Level.ColumnName, _columnOptions.Level.StoreAsEnum ? (object)logEvent.Level : logEvent.Level.ToString());
53+
case StandardColumn.TraceId:
54+
return new KeyValuePair<string, object>(_columnOptions.TraceId.ColumnName, logEvent.TraceId);
55+
case StandardColumn.SpanId:
56+
return new KeyValuePair<string, object>(_columnOptions.SpanId.ColumnName, logEvent.SpanId);
5357
case StandardColumn.TimeStamp:
5458
return GetTimeStampStandardColumnNameAndValue(logEvent);
5559
case StandardColumn.Exception:

0 commit comments

Comments
 (0)