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
17 changes: 15 additions & 2 deletions src/Seq.App.OpenTelemetry/Batching/BatchBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void AddLog(LogRecord record, Resource resource, InstrumentationScope? sc
_logGroups[resourceKey] = group;
}

var scopeKey = scope?.Name ?? "";
var scopeKey = GetScopeKey(scope);
if (!group.ScopeGroups.TryGetValue(scopeKey, out var scopeGroup))
{
scopeGroup = new ScopeLogGroup(scope);
Expand All @@ -41,7 +41,7 @@ public void AddSpan(Span span, Resource resource, InstrumentationScope? scope)
_spanGroups[resourceKey] = group;
}

var scopeKey = scope?.Name ?? "";
var scopeKey = GetScopeKey(scope);
if (!group.ScopeGroups.TryGetValue(scopeKey, out var scopeGroup))
{
scopeGroup = new ScopeSpanGroup(scope);
Expand Down Expand Up @@ -113,6 +113,19 @@ static string GetResourceKey(Resource resource)
return string.Join("|", parts);
}

static string GetScopeKey(InstrumentationScope? scope)
{
// Name alone isn't sufficient: same-named scopes with different versions or attributes
// must not be merged, or records would be reported under the wrong scope.
if (scope == null)
return "";

var parts = scope.Attributes
.OrderBy(a => a.Key)
.Select(a => $"{a.Key}={a.Value}");
return $"{scope.Name}\n{scope.Version}\n{string.Join("|", parts)}";
}

sealed class ResourceLogGroup(Resource resource)
{
public Resource Resource { get; } = resource;
Expand Down
72 changes: 45 additions & 27 deletions src/Seq.App.OpenTelemetry/Batching/BatchPump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,61 +32,79 @@ public BatchPump(OtlpExporter exporter, int maxBatchSize, int maxQueueSize, int
_pumpTask = Task.Run(PumpLoop);
}

public async Task EnqueueLogAsync(LogRecord record, Resource resource, InstrumentationScope? scope)
public Task EnqueueLogAsync(LogRecord record, Resource resource, InstrumentationScope? scope)
{
int count;
lock (_sync)
{
_builder.AddLog(record, resource, scope);
count = _builder.Count;
count = AfterAddLocked();
}

if (count >= _maxBatchSize)
_signal.Release();

if (count >= _maxQueueSize)
{
_backpressureActive = true;
await _backpressure.WaitAsync();
}
return AfterAdd(count);
}

public async Task EnqueueSpanAsync(Span span, Resource resource, InstrumentationScope? scope)
public Task EnqueueSpanAsync(Span span, Resource resource, InstrumentationScope? scope)
{
int count;
lock (_sync)
{
_builder.AddSpan(span, resource, scope);
count = _builder.Count;
count = AfterAddLocked();
}

if (count >= _maxBatchSize)
_signal.Release();
return AfterAdd(count);
}

int AfterAddLocked()
{
var count = _builder.Count;

// Setting the flag inside the lock means the pump can't miss it between this add and the
// producer's subsequent wait: any flush that observes the over-full builder also observes
// the flag and releases (or has already released) `_backpressure`.
if (count >= _maxQueueSize)
{
_backpressureActive = true;
await _backpressure.WaitAsync();
}

return count;
}

Task AfterAdd(int count)
{
// Best-effort guard: the host stops delivering events before disposing, but a late
// arrival shouldn't hit the disposed semaphores.
if (_disposed)
return Task.CompletedTask;

// `==` rather than `>=`: producers are serial, so the threshold is crossed exactly once
// per batch, and permits don't accumulate on the semaphore.
if (count == _maxBatchSize)
_signal.Release();

if (count >= _maxQueueSize)
return _backpressure.WaitAsync();

return Task.CompletedTask;
}

async Task PumpLoop()
{
var ct = _cts.Token;
Task? signalTask = null;

while (!ct.IsCancellationRequested)
{
try
{
// Wait for signal or timer
var signalTask = _signal.WaitAsync(ct);
var delayTask = Task.Delay(_flushIntervalMs, ct);
await Task.WhenAny(signalTask, delayTask);
}
catch (OperationCanceledException)
{
// Reuse a pending signal waiter across timer-elapsed iterations; creating a new
// `WaitAsync` each time would queue an abandoned waiter per flush interval.
signalTask ??= _signal.WaitAsync(ct);
var delayTask = Task.Delay(_flushIntervalMs, ct);
var completed = await Task.WhenAny(signalTask, delayTask);

if (ct.IsCancellationRequested)
break;
}

if (completed == signalTask)
signalTask = null;

await FlushAsync();
}
Expand Down
28 changes: 25 additions & 3 deletions src/Seq.App.OpenTelemetry/Conversion/AnyValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@ namespace Seq.App.OpenTelemetry.Conversion;

static class AnyValueConverter
{
/// <summary>
/// Converts to a full OTLP <see cref="AnyValue"/>, preserving objects as structured kvlist
/// values. The OTel spec only permits kvlist attribute values on log records; use
/// <see cref="ConvertStandard"/> for span, resource, and scope attributes.
/// </summary>
public static AnyValue Convert(JsonElement element)
{
return Convert(element, structured: true);
}

/// <summary>
/// Converts to a "standard" OTLP attribute value — primitives and arrays only, the value
/// types permitted on span, resource, and scope attributes. Objects (at any depth) are
/// JSON-encoded as strings, since backends commonly drop kvlist values in these positions.
/// </summary>
public static AnyValue ConvertStandard(JsonElement element)
{
return Convert(element, structured: false);
}

static AnyValue Convert(JsonElement element, bool structured)
{
switch (element.ValueKind)
{
Expand All @@ -25,17 +45,20 @@ public static AnyValue Convert(JsonElement element)
case JsonValueKind.Array:
var arrayValue = new ArrayValue();
foreach (var item in element.EnumerateArray())
arrayValue.Values.Add(Convert(item));
arrayValue.Values.Add(Convert(item, structured));
return new AnyValue { ArrayValue = arrayValue };

case JsonValueKind.Object:
if (!structured)
return new AnyValue { StringValue = element.GetRawText() };

var kvList = new KeyValueList();
foreach (var prop in element.EnumerateObject())
{
kvList.Values.Add(new KeyValue
{
Key = prop.Name,
Value = Convert(prop.Value)
Value = Convert(prop.Value, structured)
});
}
return new AnyValue { KvlistValue = kvList };
Expand All @@ -47,5 +70,4 @@ public static AnyValue Convert(JsonElement element)
return new AnyValue();
}
}

}
17 changes: 9 additions & 8 deletions src/Seq.App.OpenTelemetry/Conversion/AttributeFlattener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ 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
/// reaches this many segments, any remaining object structure is emitted as a single
/// JSON-encoded string 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;
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.
/// <paramref name="attributes"/> with lower-priority defaults before flattening. Leaves are
/// converted with <see cref="AnyValueConverter.ConvertStandard"/> — resource and scope
/// attributes only permit standard (primitive and array) values, so any object remaining at a
/// leaf (below <see cref="MaxDepth"/>, or nested inside an array) becomes a JSON string.
/// </summary>
public static void Flatten(JsonElement obj, IDictionary<string, AnyValue> attributes)
{
Expand Down Expand Up @@ -55,11 +56,11 @@ static void FlattenInto(string key, JsonElement value, int depth, IDictionary<st
}

// 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.
// no children, so fall through and emit it as a JSON string to keep the key.
if (!empty)
return;
}

attributes[key] = AnyValueConverter.Convert(value);
attributes[key] = AnyValueConverter.ConvertStandard(value);
}
}
49 changes: 49 additions & 0 deletions src/Seq.App.OpenTelemetry/Conversion/ExceptionAttributes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Google.Protobuf.Collections;

namespace Seq.App.OpenTelemetry.Conversion;

/// <summary>
/// Maps a CLEF <c>@x</c> exception string onto the OpenTelemetry <c>exception.*</c> semantic
/// convention attributes.
/// </summary>
static class ExceptionAttributes
{
/// <summary>
/// Adds <c>exception.type</c> and <c>exception.message</c> when the first line of
/// <paramref name="exception"/> has the .NET "TypeName: Message" shape, and
/// <c>exception.stacktrace</c> always.
/// </summary>
public static void AddTo(RepeatedField<KeyValue> attributes, string exception)
{
var firstLine = FirstLine(exception);
var colonIndex = firstLine?.IndexOf(": ", StringComparison.Ordinal) ?? -1;
if (firstLine != null && colonIndex > 0)
{
attributes.Add(new KeyValue
{
Key = "exception.type",
Value = new AnyValue { StringValue = firstLine[..colonIndex] }
});
attributes.Add(new KeyValue
{
Key = "exception.message",
Value = new AnyValue { StringValue = firstLine[(colonIndex + 2)..] }
});
}

attributes.Add(new KeyValue
{
Key = "exception.stacktrace",
Value = new AnyValue { StringValue = exception }
});
}

public static string? FirstLine(string? exception)
{
if (string.IsNullOrEmpty(exception))
return null;

var newline = exception.IndexOf('\n');
return (newline >= 0 ? exception[..newline] : exception).TrimEnd('\r');
}
}
20 changes: 19 additions & 1 deletion src/Seq.App.OpenTelemetry/Conversion/HexBytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,34 @@ public static ByteString ToSpanId(string? hex)

static ByteString ToByteString(string hex, int expectedLength)
{
// OTLP treats absent and invalid ids equivalently, so rather than throwing (and losing the
// whole event), unparseable values are omitted.
if (hex.Length > expectedLength * 2)
return Empty;

// Pad with leading zeros if shorter than expected
if (hex.Length < expectedLength * 2)
hex = hex.PadLeft(expectedLength * 2, '0');

var bytes = new byte[expectedLength];
for (var i = 0; i < expectedLength; i++)
{
bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
var hi = HexDigit(hex[i * 2]);
var lo = HexDigit(hex[i * 2 + 1]);
if (hi < 0 || lo < 0)
return Empty;

bytes[i] = (byte)(hi << 4 | lo);
}

return ByteString.CopyFrom(bytes);
}

static int HexDigit(char c) => c switch
{
>= '0' and <= '9' => c - '0',
>= 'a' and <= 'f' => c - 'a' + 10,
>= 'A' and <= 'F' => c - 'A' + 10,
_ => -1
};
}
37 changes: 5 additions & 32 deletions src/Seq.App.OpenTelemetry/Conversion/LogRecordConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,44 +48,17 @@ public static LogRecord Convert(ClefEvent evt)

if (evt.Exception != null)
{
AddExceptionAttributes(record, evt.Exception);
ExceptionAttributes.AddTo(record.Attributes, evt.Exception);
}

return record;
}

static void AddExceptionAttributes(LogRecord record, string exception)
{
// Try to parse "TypeName: Message\n at ..." format
var firstNewline = exception.IndexOf('\n');
if (firstNewline > 0)
{
var firstLine = exception[..firstNewline].TrimEnd('\r');
var colonIndex = firstLine.IndexOf(": ", StringComparison.Ordinal);
if (colonIndex > 0)
{
record.Attributes.Add(new KeyValue
{
Key = "exception.type",
Value = new AnyValue { StringValue = firstLine[..colonIndex] }
});
record.Attributes.Add(new KeyValue
{
Key = "exception.message",
Value = new AnyValue { StringValue = firstLine[(colonIndex + 2)..] }
});
}
}

record.Attributes.Add(new KeyValue
{
Key = "exception.stacktrace",
Value = new AnyValue { StringValue = exception }
});
}

internal static ulong ToUnixNano(DateTimeOffset timestamp)
{
return (ulong)(timestamp.UtcTicks - DateTimeOffset.UnixEpoch.Ticks) * 100;
// OTLP defines zero as "unknown or missing timestamp"; pre-epoch values (including
// `default`, from events with missing or malformed `@t`) would otherwise wrap.
var ticks = timestamp.UtcTicks - DateTimeOffset.UnixEpoch.Ticks;
return ticks <= 0 ? 0ul : (ulong)ticks * 100;
}
}
5 changes: 4 additions & 1 deletion src/Seq.App.OpenTelemetry/Conversion/ResourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ static class ResourceBuilder
{
public static Resource Build(ClefEvent evt, IReadOnlyList<KeyValue>? configuredAttributes)
{
// Collect attributes in priority order; higher-priority sources overwrite lower.
// Collect attributes in priority order; higher-priority sources overwrite lower. The
// `service.name` fallback follows the OTel `unknown_service:` convention; most backends
// group telemetry by `service.name` and behave poorly when it's absent.
var attrs = new Dictionary<string, AnyValue>
{
["service.name"] = new() { StringValue = "unknown_service:seq-app-opentelemetry" },
["telemetry.sdk.name"] = new() { StringValue = "seq-app-opentelemetry" },
["telemetry.sdk.language"] = new() { StringValue = "dotnet" }
};
Expand Down
Loading
Loading