Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions src/Seq.App.OpenTelemetry/Conversion/AnyValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seq.App.OpenTelemetry.Conversion;

static class AnyValueConverter
{
public static AnyValue? Convert(JsonElement element)
public static AnyValue Convert(JsonElement element)
{
switch (element.ValueKind)
{
Expand All @@ -25,33 +25,26 @@ static class AnyValueConverter
case JsonValueKind.Array:
var arrayValue = new ArrayValue();
foreach (var item in element.EnumerateArray())
{
var converted = Convert(item);
if (converted != null)
arrayValue.Values.Add(converted);
}
arrayValue.Values.Add(Convert(item));
return new AnyValue { ArrayValue = arrayValue };

case JsonValueKind.Object:
var kvList = new KeyValueList();
foreach (var prop in element.EnumerateObject())
{
var converted = Convert(prop.Value);
if (converted != null)
kvList.Values.Add(new KeyValue
{
kvList.Values.Add(new KeyValue
{
Key = prop.Name,
Value = converted
});
}
Key = prop.Name,
Value = Convert(prop.Value)
});
}
return new AnyValue { KvlistValue = kvList };

case JsonValueKind.Null:
case JsonValueKind.Undefined:
default:
return null;
// OTLP represents a null/absent value as an AnyValue with no oneof field set.
return new AnyValue();
}
}

Expand Down
65 changes: 65 additions & 0 deletions src/Seq.App.OpenTelemetry/Conversion/AttributeFlattener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Text.Json;

namespace Seq.App.OpenTelemetry.Conversion;

/// <summary>
/// Seq stores scope and resource attributes as nested JSON objects (e.g.
/// <c>{"service":{"name":"x"}}</c>), but OTLP resource and scope attributes are a flat list of
/// dotted keys (e.g. <c>service.name = "x"</c>). This flattens the former into the latter.
/// </summary>
static class AttributeFlattener
{
/// <summary>
/// The maximum object nesting depth that is unpacked into dotted attribute keys. Once a key
/// reaches this many segments, any remaining object structure is emitted as a single structured
/// <see cref="AnyValue"/> rather than being flattened further. This keeps pathological or deeply
/// recursive payloads from producing an unbounded number of attributes.
/// </summary>
public const int MaxDepth = 5;

/// <summary>
/// Flattens the properties of <paramref name="obj"/> into <paramref name="attributes"/>, unpacking
/// nested objects into dotted keys. Later keys overwrite earlier ones, so callers can seed
/// <paramref name="attributes"/> with lower-priority defaults before flattening. Non-object
/// values (and anything below <see cref="MaxDepth"/>) are converted with
/// <see cref="AnyValueConverter"/>, preserving arrays and deep structures as-is.
/// </summary>
public static void Flatten(JsonElement obj, IDictionary<string, AnyValue> attributes)
{
if (obj.ValueKind != JsonValueKind.Object)
return;

foreach (var prop in obj.EnumerateObject())
Flatten(prop.Name, prop.Value, attributes);
}

/// <summary>
/// Flattens a single <paramref name="key"/>/<paramref name="value"/> pair into
/// <paramref name="attributes"/> with the same nested-object unpacking as
/// <see cref="Flatten(JsonElement,IDictionary{string,AnyValue})"/>.
/// </summary>
public static void Flatten(string key, JsonElement value, IDictionary<string, AnyValue> attributes)
{
FlattenInto(key, value, 1, attributes);
}

static void FlattenInto(string key, JsonElement value, int depth, IDictionary<string, AnyValue> attributes)
{
if (value.ValueKind == JsonValueKind.Object && depth < MaxDepth)
{
var empty = true;
foreach (var prop in value.EnumerateObject())
{
empty = false;
FlattenInto($"{key}.{prop.Name}", prop.Value, depth + 1, attributes);
}

// A non-empty object is fully represented by its flattened children. An empty object has
// no children, so fall through and emit it as an empty structured value to keep the key.
if (!empty)
return;
}

attributes[key] = AnyValueConverter.Convert(value);
}
}
14 changes: 5 additions & 9 deletions src/Seq.App.OpenTelemetry/Conversion/LogRecordConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ public static LogRecord Convert(ClefEvent evt)
// User properties → attributes
foreach (var prop in evt.Properties)
{
var converted = AnyValueConverter.Convert(prop.Value);
if (converted != null)
record.Attributes.Add(new KeyValue
{
record.Attributes.Add(new KeyValue
{
Key = prop.Key,
Value = converted
});
}
Key = prop.Key,
Value = AnyValueConverter.Convert(prop.Value)
});
}

// Well-known attributes
Expand All @@ -45,7 +41,7 @@ public static LogRecord Convert(ClefEvent evt)
{
record.Attributes.Add(new KeyValue
{
Key = "seq.event.id",
Key = "seq.event_type",
Value = new AnyValue { StringValue = evt.EventId }
});
}
Expand Down
43 changes: 15 additions & 28 deletions src/Seq.App.OpenTelemetry/Conversion/ResourceBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text.Json;
using Seq.App.OpenTelemetry.Parsing;

namespace Seq.App.OpenTelemetry.Conversion;
Expand All @@ -20,15 +19,8 @@ public static Resource Build(ClefEvent evt, IReadOnlyList<KeyValue>? configuredA
attrs[attr.Key] = attr.Value;
}

if (evt.ResourceAttributes is { ValueKind: JsonValueKind.Object } ra)
{
foreach (var prop in ra.EnumerateObject())
{
var converted = AnyValueConverter.Convert(prop.Value);
if (converted != null)
attrs[prop.Name] = converted;
}
}
foreach (var (key, value) in evt.ResourceAttributes)
AttributeFlattener.Flatten(key, value, attrs);

var resource = new Resource();
foreach (var (key, value) in attrs)
Expand All @@ -41,38 +33,33 @@ public static Resource Build(ClefEvent evt, IReadOnlyList<KeyValue>? configuredA

public static InstrumentationScope? BuildScope(ClefEvent evt)
{
if (evt.Scope is not { } sc || sc.ValueKind != JsonValueKind.Object)
if (evt.ScopeAttributes.Count == 0)
return null;

var scope = new InstrumentationScope();
var attributes = new Dictionary<string, AnyValue>();

foreach (var prop in sc.EnumerateObject())
// `@sa` is flat: `name` and `version` are well-known attributes that sit alongside any other
// scope attributes, which become OTLP scope attributes.
foreach (var (key, value) in evt.ScopeAttributes)
{
switch (prop.Name)
switch (key)
{
case "name":
scope.Name = prop.Value.GetString() ?? "";
scope.Name = value.GetString() ?? "";
break;
case "version":
scope.Version = prop.Value.GetString() ?? "";
scope.Version = value.GetString() ?? "";
break;
case "attributes" when prop.Value.ValueKind == JsonValueKind.Object:
foreach (var attr in prop.Value.EnumerateObject())
{
var converted = AnyValueConverter.Convert(attr.Value);
if (converted != null)
{
scope.Attributes.Add(new KeyValue
{
Key = attr.Name,
Value = converted
});
}
}
default:
AttributeFlattener.Flatten(key, value, attributes);
break;
}
}

foreach (var (key, value) in attributes)
scope.Attributes.Add(new KeyValue { Key = key, Value = value });

return scope;
}

Expand Down
12 changes: 4 additions & 8 deletions src/Seq.App.OpenTelemetry/Conversion/SpanConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ public static Span Convert(ClefEvent evt)
// User properties → attributes
foreach (var prop in evt.Properties)
{
var converted = AnyValueConverter.Convert(prop.Value);
if (converted != null)
span.Attributes.Add(new KeyValue
{
span.Attributes.Add(new KeyValue
{
Key = prop.Key,
Value = converted
});
}
Key = prop.Key,
Value = AnyValueConverter.Convert(prop.Value)
});
}

return span;
Expand Down
8 changes: 5 additions & 3 deletions src/Seq.App.OpenTelemetry/Parsing/ClefEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public ClefEvent(JsonDocument document)
public DateTimeOffset? SpanStart { get; init; }
public string? SpanKind { get; init; }

// Resource and scope
public JsonElement? Scope { get; init; }
public JsonElement? ResourceAttributes { get; init; }
// Resource and scope attributes. Seq sends these as object-valued `@ra`/`@sa` properties, so
// they share the same flat shape as `Properties`. Scope's `name` and `version` are well-known
// attributes that sit alongside the other scope attributes here.
public List<KeyValuePair<string, JsonElement>> ResourceAttributes { get; init; } = [];
public List<KeyValuePair<string, JsonElement>> ScopeAttributes { get; init; } = [];

// All non-reified properties
public List<KeyValuePair<string, JsonElement>> Properties { get; init; } = [];
Expand Down
28 changes: 20 additions & 8 deletions src/Seq.App.OpenTelemetry/Parsing/ClefParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static class ClefParser
static ReadOnlySpan<byte> Prop_ps => "@ps"u8;
static ReadOnlySpan<byte> Prop_st => "@st"u8;
static ReadOnlySpan<byte> Prop_sk => "@sk"u8;
static ReadOnlySpan<byte> Prop_sc => "@sc"u8;
static ReadOnlySpan<byte> Prop_sa => "@sa"u8;
static ReadOnlySpan<byte> Prop_ra => "@ra"u8;

public static ClefEvent Parse(string json)
Expand All @@ -34,8 +34,8 @@ public static ClefEvent Parse(string json)
string? parentSpanId = null;
DateTimeOffset? spanStart = null;
string? spanKind = null;
JsonElement? scope = null;
JsonElement? resourceAttributes = null;
List<KeyValuePair<string, JsonElement>>? scopeAttributes = null;
List<KeyValuePair<string, JsonElement>>? resourceAttributes = null;
var properties = new List<KeyValuePair<string, JsonElement>>();

foreach (var prop in root.EnumerateObject())
Expand Down Expand Up @@ -90,13 +90,17 @@ public static ClefEvent Parse(string json)
{
spanKind = prop.Value.GetString();
}
else if (prop.NameEquals(Prop_sc))
else if (prop.NameEquals(Prop_sa))
{
scope = prop.Value.Clone();
// Seq only ever sends an object-valued `@sa`.
if (prop.Value.ValueKind == JsonValueKind.Object)
scopeAttributes = ReadProperties(prop.Value);
}
else if (prop.NameEquals(Prop_ra))
{
resourceAttributes = prop.Value.Clone();
// Seq only ever sends an object-valued `@ra`.
if (prop.Value.ValueKind == JsonValueKind.Object)
resourceAttributes = ReadProperties(prop.Value);
}
else
{
Expand Down Expand Up @@ -127,9 +131,17 @@ public static ClefEvent Parse(string json)
ParentSpanId = parentSpanId,
SpanStart = spanStart,
SpanKind = spanKind,
Scope = scope,
ResourceAttributes = resourceAttributes,
ScopeAttributes = scopeAttributes ?? [],
ResourceAttributes = resourceAttributes ?? [],
Properties = properties
};
}

static List<KeyValuePair<string, JsonElement>> ReadProperties(JsonElement obj)
{
var properties = new List<KeyValuePair<string, JsonElement>>();
foreach (var prop in obj.EnumerateObject())
properties.Add(new KeyValuePair<string, JsonElement>(prop.Name, prop.Value.Clone()));
return properties;
}
}
7 changes: 5 additions & 2 deletions test/Seq.App.OpenTelemetry.SmokeTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@
var spanEnd = DateTime.UtcNow;
var spanStart = spanEnd - TimeSpan.FromMilliseconds(120);

// Nested resource attributes; the exporter should flatten this to a `service.name` attribute.
var resourceAttributes = "\"@ra\":{\"service\":{\"name\":\"Seq.App.OpenTelemetry.SmokeTest\"}}";

var sampleLog = $$"""
{"@t":"{{Timestamp(spanEnd)}}","@l":"Information","@mt":"Smoke test event from {Source}","@tr":"{{traceId}}","@sp":"{{spanId}}","Source":"Seq.App.OpenTelemetry.SmokeTest"}
{"@t":"{{Timestamp(spanEnd)}}","@l":"Information","@mt":"Smoke test event from {Source}","@tr":"{{traceId}}","@sp":"{{spanId}}",{{resourceAttributes}},"Source":"Seq.App.OpenTelemetry.SmokeTest"}
""";
await app.OnAsync(sampleLog);
Console.WriteLine("Sample log event sent.");

var sampleSpan = $$"""
{"@t":"{{Timestamp(spanEnd)}}","@mt":"Smoke test span","@tr":"{{traceId}}","@sp":"{{spanId}}","@st":"{{Timestamp(spanStart)}}","@sk":"Internal"}
{"@t":"{{Timestamp(spanEnd)}}","@mt":"Smoke test span","@tr":"{{traceId}}","@sp":"{{spanId}}","@st":"{{Timestamp(spanStart)}}","@sk":"Internal",{{resourceAttributes}}}
""";
await app.OnAsync(sampleSpan);
Console.WriteLine("Sample span sent.");
Expand Down
51 changes: 51 additions & 0 deletions test/Seq.App.OpenTelemetry.Tests/AnyValueConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Text.Json;
using Seq.App.OpenTelemetry.Conversion;
using Xunit;

namespace Seq.App.OpenTelemetry.Tests;

public class AnyValueConverterTests
{
static AnyValue Convert(string json)
{
using var document = JsonDocument.Parse(json);
return AnyValueConverter.Convert(document.RootElement);
}

[Fact]
public void NullConvertsToValueWithNoFieldSet()
{
var value = Convert("null");
Assert.Equal(AnyValue.ValueOneofCase.None, value.ValueCase);
}

[Fact]
public void UndefinedConvertsToValueWithNoFieldSet()
{
// A default JsonElement has ValueKind Undefined.
var value = AnyValueConverter.Convert(default);
Assert.Equal(AnyValue.ValueOneofCase.None, value.ValueCase);
}

[Fact]
public void NullArrayElementsArePreservedToKeepPositions()
{
var value = Convert("[1,null,3]");

var values = value.ArrayValue.Values;
Assert.Equal(3, values.Count);
Assert.Equal(1, values[0].IntValue);
Assert.Equal(AnyValue.ValueOneofCase.None, values[1].ValueCase);
Assert.Equal(3, values[2].IntValue);
}

[Fact]
public void NullObjectPropertiesArePreservedAsEmptyValues()
{
var value = Convert("""{"present":"x","absent":null}""");

var entries = value.KvlistValue.Values.ToDictionary(kv => kv.Key, kv => kv.Value);
Assert.Equal("x", entries["present"].StringValue);
Assert.Equal(AnyValue.ValueOneofCase.None, entries["absent"].ValueCase);
}
}
Loading
Loading