Skip to content

Commit c0285b2

Browse files
authored
Merge pull request #174 from nblumhardt/record-structure-type-tag
Add an option to include `StructureValue.TypeTag` as `$type` when converting to OTLP maps
2 parents ffbd232 + 635865e commit c0285b2

File tree

6 files changed

+149
-123
lines changed

6 files changed

+149
-123
lines changed

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "8.0.401",
3+
"version": "9.0.200",
44
"allowPrerelease": false,
55
"rollForward": "latestFeature"
66
}

src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/IncludedData.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// limitations under the License.
1414

1515
using System.Diagnostics;
16+
using OpenTelemetry.Proto.Common.V1;
17+
using Serilog.Events;
1618

1719
namespace Serilog.Sinks.OpenTelemetry;
1820

@@ -82,5 +84,11 @@ public enum IncludedData
8284
/// Preserve the value of the <c>SourceContext</c> property, in addition to using it as the OTLP <c>InstrumentationScope</c> name. If
8385
/// not specified, the <c>SourceContext</c> property will be omitted from the individual log record attributes.
8486
/// </summary>
85-
SourceContextAttribute = 128
87+
SourceContextAttribute = 128,
88+
89+
/// <summary>
90+
/// Include <see cref="StructureValue.TypeTag"/> as <c>$type</c> when converting event properties to
91+
/// OTLP <see cref="AnyValue.KvlistValue"/> values.
92+
/// </summary>
93+
StructureValueTypeTags = 256,
8694
}

src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/OtlpEventBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ ParentSpanIdPropertyName or
120120
continue;
121121
}
122122

123-
var v = PrimitiveConversions.ToOpenTelemetryAnyValue(property.Value);
123+
var v = PrimitiveConversions.ToOpenTelemetryAnyValue(property.Value, includedData);
124124
addAttribute(PrimitiveConversions.NewAttribute(property.Key, v));
125125
}
126126
}

src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/ProtocolHelpers/PrimitiveConversions.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static AnyValue ToOpenTelemetryScalar(ScalarValue scalar)
136136
return ToOpenTelemetryPrimitive(scalar.Value);
137137
}
138138

139-
public static AnyValue ToOpenTelemetryMap(StructureValue value)
139+
public static AnyValue ToOpenTelemetryMap(StructureValue value, IncludedData includedData)
140140
{
141141
var map = new AnyValue();
142142
var kvList = new KeyValueList();
@@ -145,14 +145,27 @@ public static AnyValue ToOpenTelemetryMap(StructureValue value)
145145
// Per the OTLP protos, attribute keys MUST be unique.
146146
var seen = new HashSet<string>();
147147

148+
if ((includedData & IncludedData.StructureValueTypeTags) == IncludedData.StructureValueTypeTags && !string.IsNullOrEmpty(value.TypeTag))
149+
{
150+
kvList.Values.Add(new KeyValue
151+
{
152+
Key = "$type",
153+
Value = new()
154+
{
155+
StringValue = value.TypeTag
156+
}
157+
});
158+
}
159+
148160
foreach (var prop in value.Properties)
149161
{
150-
if (seen.Contains(prop.Name))
162+
if (!seen.Add(prop.Name))
163+
{
164+
// Already present
151165
continue;
166+
}
152167

153-
seen.Add(prop.Name);
154-
155-
var v = ToOpenTelemetryAnyValue(prop.Value);
168+
var v = ToOpenTelemetryAnyValue(prop.Value, includedData);
156169
var kv = new KeyValue
157170
{
158171
Key = prop.Name,
@@ -164,7 +177,7 @@ public static AnyValue ToOpenTelemetryMap(StructureValue value)
164177
return map;
165178
}
166179

167-
public static AnyValue ToOpenTelemetryMap(DictionaryValue value)
180+
public static AnyValue ToOpenTelemetryMap(DictionaryValue value, IncludedData includedData)
168181
{
169182
var map = new AnyValue();
170183
var kvList = new KeyValueList();
@@ -173,7 +186,7 @@ public static AnyValue ToOpenTelemetryMap(DictionaryValue value)
173186
foreach (var element in value.Elements)
174187
{
175188
var k = element.Key.Value?.ToString() ?? "null";
176-
var v = ToOpenTelemetryAnyValue(element.Value);
189+
var v = ToOpenTelemetryAnyValue(element.Value, includedData);
177190
kvList.Values.Add(new KeyValue
178191
{
179192
Key = k,
@@ -184,27 +197,27 @@ public static AnyValue ToOpenTelemetryMap(DictionaryValue value)
184197
return map;
185198
}
186199

187-
public static AnyValue ToOpenTelemetryArray(SequenceValue value)
200+
public static AnyValue ToOpenTelemetryArray(SequenceValue value, IncludedData includedData)
188201
{
189202
var array = new AnyValue();
190203
var values = new ArrayValue();
191204
array.ArrayValue = values;
192205
foreach (var element in value.Elements)
193206
{
194-
var v = ToOpenTelemetryAnyValue(element);
207+
var v = ToOpenTelemetryAnyValue(element, includedData);
195208
values.Values.Add(v);
196209
}
197210
return array;
198211
}
199212

200-
internal static AnyValue ToOpenTelemetryAnyValue(LogEventPropertyValue value)
213+
internal static AnyValue ToOpenTelemetryAnyValue(LogEventPropertyValue value, IncludedData includedData)
201214
{
202215
return value switch
203216
{
204217
ScalarValue scalar => ToOpenTelemetryScalar(scalar),
205-
StructureValue structure => ToOpenTelemetryMap(structure),
206-
SequenceValue sequence => ToOpenTelemetryArray(sequence),
207-
DictionaryValue dictionary => ToOpenTelemetryMap(dictionary),
218+
StructureValue structure => ToOpenTelemetryMap(structure, includedData),
219+
SequenceValue sequence => ToOpenTelemetryArray(sequence, includedData),
220+
DictionaryValue dictionary => ToOpenTelemetryMap(dictionary, includedData),
208221
_ => ToOpenTelemetryPrimitive(value.ToString()),
209222
};
210223
}
@@ -213,7 +226,7 @@ internal static string OnlyHexDigits(string s)
213226
{
214227
try
215228
{
216-
return Regex.Replace(s, @"[^0-9a-fA-F]", "", RegexOptions.None, TimeSpan.FromSeconds(1.5));
229+
return Regex.Replace(s, "[^0-9a-fA-F]", "", RegexOptions.None, TimeSpan.FromSeconds(1.5));
217230
}
218231
catch (RegexMatchTimeoutException)
219232
{

0 commit comments

Comments
 (0)