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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,5 @@ FodyWeavers.xsd
.idea
.DS_Store

.mcp.json
.claude/*
2 changes: 2 additions & 0 deletions seq-app-opentelemetry.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Otlp/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
4 changes: 2 additions & 2 deletions src/Seq.App.OpenTelemetry/Batching/BatchPump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Seq.App.OpenTelemetry.Batching;

class BatchPump : IAsyncDisposable
{
readonly IOtlpExporter _exporter;
readonly OtlpExporter _exporter;
readonly int _maxBatchSize;
readonly int _maxQueueSize;
readonly int _flushIntervalMs;
Expand All @@ -21,7 +21,7 @@ class BatchPump : IAsyncDisposable
volatile bool _backpressureActive;
volatile bool _disposed;

public BatchPump(IOtlpExporter exporter, int maxBatchSize, int maxQueueSize, int flushIntervalMs, ILogger log)
public BatchPump(OtlpExporter exporter, int maxBatchSize, int maxQueueSize, int flushIntervalMs, ILogger log)
{
_exporter = exporter;
_maxBatchSize = maxBatchSize;
Expand Down
2 changes: 1 addition & 1 deletion src/Seq.App.OpenTelemetry/Conversion/LogRecordConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static LogRecord Convert(ClefEvent evt)
{
record.Attributes.Add(new KeyValue
{
Key = "seq.message.template",
Key = "message_template.text",
Value = new AnyValue { StringValue = evt.MessageTemplate }
});
}
Expand Down
14 changes: 6 additions & 8 deletions src/Seq.App.OpenTelemetry/Conversion/ResourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ static class ResourceBuilder
public static Resource Build(ClefEvent evt, IReadOnlyList<KeyValue>? configuredAttributes)
{
// Collect attributes in priority order; higher-priority sources overwrite lower.
var attrs = new Dictionary<string, AnyValue>();

// 1. Defaults (lowest priority)
attrs["telemetry.sdk.name"] = new AnyValue { StringValue = "seq" };
attrs["telemetry.sdk.language"] = new AnyValue { StringValue = "dotnet" };
var attrs = new Dictionary<string, AnyValue>
{
["telemetry.sdk.name"] = new() { StringValue = "seq-app-opentelemetry" },
["telemetry.sdk.language"] = new() { StringValue = "dotnet" }
};

// 2. User-configured resource attributes
if (configuredAttributes != null)
{
foreach (var attr in configuredAttributes)
attrs[attr.Key] = attr.Value;
}

// 3. Event-level @ra (highest priority, from OTLP ingestion)
if (evt.ResourceAttributes is { } ra && ra.ValueKind == JsonValueKind.Object)
if (evt.ResourceAttributes is { ValueKind: JsonValueKind.Object } ra)
{
foreach (var prop in ra.EnumerateObject())
{
Expand Down
21 changes: 12 additions & 9 deletions src/Seq.App.OpenTelemetry/Export/GrpcExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Seq.App.OpenTelemetry.Export;

class GrpcExporter : IOtlpExporter
class GrpcExporter : OtlpExporter
{
readonly GrpcChannel _channel;
readonly LogsService.LogsServiceClient _logsClient;
Expand All @@ -12,7 +12,10 @@ class GrpcExporter : IOtlpExporter

public GrpcExporter(string endpoint, IReadOnlyList<KeyValuePair<string, string>>? headers)
{
_channel = GrpcChannel.ForAddress(endpoint);
_channel = GrpcChannel.ForAddress(endpoint, new GrpcChannelOptions
{
ThrowOperationCanceledOnCancellation = true
});
_logsClient = new LogsService.LogsServiceClient(_channel);
_tracesClient = new TraceService.TraceServiceClient(_channel);

Expand All @@ -24,29 +27,29 @@ public GrpcExporter(string endpoint, IReadOnlyList<KeyValuePair<string, string>>
}
}

public async Task ExportLogsAsync(ExportLogsServiceRequest request, CancellationToken ct)
public override async Task ExportLogsAsync(ExportLogsServiceRequest request, CancellationToken ct)
{
try
{
await _logsClient.ExportAsync(request, _metadata, cancellationToken: ct);
await _logsClient.ExportAsync(request, _metadata, DateTime.UtcNow + RequestTimeout, ct);
}
catch (RpcException ex) when (IsRetryableStatus(ex.StatusCode))
{
await Task.Delay(1000, ct);
await _logsClient.ExportAsync(request, _metadata, cancellationToken: ct);
await _logsClient.ExportAsync(request, _metadata, DateTime.UtcNow + RequestTimeout, ct);
}
}

public async Task ExportTracesAsync(ExportTraceServiceRequest request, CancellationToken ct)
public override async Task ExportTracesAsync(ExportTraceServiceRequest request, CancellationToken ct)
{
try
{
await _tracesClient.ExportAsync(request, _metadata, cancellationToken: ct);
await _tracesClient.ExportAsync(request, _metadata, DateTime.UtcNow + RequestTimeout, ct);
}
catch (RpcException ex) when (IsRetryableStatus(ex.StatusCode))
{
await Task.Delay(1000, ct);
await _tracesClient.ExportAsync(request, _metadata, cancellationToken: ct);
await _tracesClient.ExportAsync(request, _metadata, DateTime.UtcNow + RequestTimeout, ct);
}
}

Expand All @@ -55,7 +58,7 @@ static bool IsRetryableStatus(StatusCode code)
return code is StatusCode.Unavailable or StatusCode.DeadlineExceeded;
}

public ValueTask DisposeAsync()
public override ValueTask DisposeAsync()
{
_channel.Dispose();
return default;
Expand Down
10 changes: 5 additions & 5 deletions src/Seq.App.OpenTelemetry/Export/HttpProtobufExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Seq.App.OpenTelemetry.Export;

class HttpProtobufExporter : IOtlpExporter
class HttpProtobufExporter : OtlpExporter
{
readonly HttpClient _httpClient;
readonly Uri _logsEndpoint;
Expand All @@ -15,20 +15,20 @@ public HttpProtobufExporter(string endpoint, IReadOnlyList<KeyValuePair<string,
_logsEndpoint = new Uri($"{baseUri}/v1/logs");
_tracesEndpoint = new Uri($"{baseUri}/v1/traces");

_httpClient = new HttpClient();
_httpClient = new HttpClient { Timeout = RequestTimeout };
if (headers != null)
{
foreach (var header in headers)
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value);
}
}

public async Task ExportLogsAsync(ExportLogsServiceRequest request, CancellationToken ct)
public override async Task ExportLogsAsync(ExportLogsServiceRequest request, CancellationToken ct)
{
await SendAsync(request, _logsEndpoint, ct);
}

public async Task ExportTracesAsync(ExportTraceServiceRequest request, CancellationToken ct)
public override async Task ExportTracesAsync(ExportTraceServiceRequest request, CancellationToken ct)
{
await SendAsync(request, _tracesEndpoint, ct);
}
Expand Down Expand Up @@ -67,7 +67,7 @@ or HttpStatusCode.ServiceUnavailable
or HttpStatusCode.GatewayTimeout;
}

public ValueTask DisposeAsync()
public override ValueTask DisposeAsync()
{
_httpClient.Dispose();
return default;
Expand Down
7 changes: 0 additions & 7 deletions src/Seq.App.OpenTelemetry/Export/IOtlpExporter.cs

This file was deleted.

10 changes: 10 additions & 0 deletions src/Seq.App.OpenTelemetry/Export/OtlpExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Seq.App.OpenTelemetry.Export;

abstract class OtlpExporter : IAsyncDisposable
{
protected static readonly TimeSpan RequestTimeout = TimeSpan.FromSeconds(10);

public abstract Task ExportLogsAsync(ExportLogsServiceRequest request, CancellationToken ct);
public abstract Task ExportTracesAsync(ExportTraceServiceRequest request, CancellationToken ct);
public abstract ValueTask DisposeAsync();
}
4 changes: 2 additions & 2 deletions src/Seq.App.OpenTelemetry/OpenTelemetryApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace Seq.App.OpenTelemetry;
Description = "Forwards Seq events to an OTLP-compatible endpoint as logs and traces.")]
public class OpenTelemetryApp : SeqApp, ISubscribeToJsonAsync, IAsyncDisposable
{
IOtlpExporter? _exporter;
OtlpExporter? _exporter;
BatchPump? _pump;
IReadOnlyList<KeyValue>? _configuredResourceAttributes;

public OpenTelemetryApp() { }

internal OpenTelemetryApp(IOtlpExporter exporter)
internal OpenTelemetryApp(OtlpExporter exporter)
{
_exporter = exporter;
}
Expand Down
78 changes: 60 additions & 18 deletions test/Seq.App.OpenTelemetry.SmokeTest/Program.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,68 @@
using Seq.Apps.Testing.Hosting;
using System.Globalization;
using System.Reflection;
using Seq.App.OpenTelemetry;
using Seq.App.OpenTelemetry.SmokeTest.Support;
using Seq.Apps;
using Serilog;

var app = new Seq.App.OpenTelemetry.OpenTelemetryApp();
var host = new TestAppHost();
var settings = (IDictionary<string, string>)host.App.Settings;
var app = new OpenTelemetryApp();

// Read settings from SEQ_APP_SETTING_* environment variables
var settingNames = new[]
foreach (var property in typeof(OpenTelemetryApp).GetProperties())
{
"Endpoint", "Protocol", "Headers", "ResourceAttributes",
"MaxBatchSize", "MaxQueueSize", "FlushIntervalMs"
};
if (property.GetCustomAttribute<SeqAppSettingAttribute>() == null)
continue;

foreach (var name in settingNames)
{
var envValue = Environment.GetEnvironmentVariable($"SEQ_APP_SETTING_{name.ToUpperInvariant()}");
if (envValue != null)
settings[name] = envValue;
var envValue = Environment.GetEnvironmentVariable($"SEQ_APP_SETTING_{property.Name.ToUpperInvariant()}");
if (envValue == null)
continue;

var targetType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
var value = targetType.IsEnum
? Enum.Parse(targetType, envValue, ignoreCase: true)
: Convert.ChangeType(envValue, targetType, CultureInfo.InvariantCulture);

property.SetValue(app, value);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This addresses a feature gap in TestAppHost, which we ship separately. We'll need to come up with an API that makes attaching the app a responsibility of the host, and have the host do the reflection we're otherwise doing manually, here.

Console.WriteLine($"{property.Name} = {envValue}");
}

var errors = new ErrorCountingSink();
await using var logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.Sink(errors)
.CreateLogger();

try
{
app.Attach(host);
app.Attach(new SmokeTestAppHost(logger));
Console.WriteLine("App attached successfully.");

var sampleLog = """{"@t":"2024-01-15T10:30:00Z","@l":"Information","@mt":"Smoke test event from {Source}","Source":"Seq.App.OpenTelemetry.SmokeTest"}""";
var traceId = RandomHex(16);
var spanId = RandomHex(8);
var spanEnd = DateTime.UtcNow;
var spanStart = spanEnd - TimeSpan.FromMilliseconds(120);

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

var sampleSpan = $$"""
{"@t":"{{Timestamp(spanEnd)}}","@mt":"Smoke test span","@tr":"{{traceId}}","@sp":"{{spanId}}","@st":"{{Timestamp(spanStart)}}","@sk":"Internal"}
""";
await app.OnAsync(sampleSpan);
Console.WriteLine("Sample span sent.");

await app.DisposeAsync();
Console.WriteLine("App disposed. Smoke test passed.");
Console.WriteLine("App disposed.");

if (errors.ErrorCount > 0)
{
Console.Error.WriteLine($"Smoke test failed: {errors.ErrorCount} error(s) were logged during export.");
return 1;
}

Console.WriteLine($"Smoke test passed. Check the target server for the log event and span with trace id {traceId}.");
}
catch (Exception ex)
{
Expand All @@ -38,3 +71,12 @@
}

return 0;

static string Timestamp(DateTime instant) => instant.ToString("O", CultureInfo.InvariantCulture);

static string RandomHex(int bytes)
{
var buffer = new byte[bytes];
Random.Shared.NextBytes(buffer);
return Convert.ToHexStringLower(buffer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="Seq.Apps.Testing" Version="*" />
<PackageReference Include="Serilog.Sinks.Console" Version="*" />
</ItemGroup>

<ItemGroup>
Expand Down
17 changes: 17 additions & 0 deletions test/Seq.App.OpenTelemetry.SmokeTest/Support/ErrorCountingSink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Serilog.Core;
using Serilog.Events;

namespace Seq.App.OpenTelemetry.SmokeTest.Support;

class ErrorCountingSink : ILogEventSink
{
int _errorCount;

public int ErrorCount => _errorCount;

public void Emit(LogEvent logEvent)
{
if (logEvent.Level >= LogEventLevel.Error)
Interlocked.Increment(ref _errorCount);
}
}
15 changes: 15 additions & 0 deletions test/Seq.App.OpenTelemetry.SmokeTest/Support/SmokeTestAppHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Seq.Apps;
using Seq.Apps.Testing.Hosting;
using Serilog;

namespace Seq.App.OpenTelemetry.SmokeTest.Support;

class SmokeTestAppHost(ILogger logger) : IAppHost
{
readonly TestAppHost _defaults = new();

public Apps.App App => _defaults.App;
public Host Host => _defaults.Host;
public ILogger Logger { get; } = logger;
public string StoragePath => _defaults.StoragePath;
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void AddsMessageTemplateAttribute()
using var evt = ClefParser.Parse("""{"@t":"2024-01-15T10:30:00Z","@mt":"Hello {Name}"}""");
var record = LogRecordConverter.Convert(evt);

var attr = record.Attributes.FirstOrDefault(a => a.Key == "seq.message.template");
var attr = record.Attributes.FirstOrDefault(a => a.Key == "message_template.text");
Assert.NotNull(attr);
Assert.Equal("Hello {Name}", attr.Value.StringValue);
}
Expand Down
8 changes: 4 additions & 4 deletions test/Seq.App.OpenTelemetry.Tests/Support/TestExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Seq.App.OpenTelemetry.Tests.Support;

sealed class TestExporter : IOtlpExporter
sealed class TestExporter : OtlpExporter
{
public List<ExportLogsServiceRequest> LogRequests { get; } = [];
public List<ExportTraceServiceRequest> TraceRequests { get; } = [];
Expand All @@ -11,7 +11,7 @@ sealed class TestExporter : IOtlpExporter
public int FailCount { get; set; }
public TimeSpan ExportDelay { get; set; }

public async Task ExportLogsAsync(ExportLogsServiceRequest request, CancellationToken ct)
public override async Task ExportLogsAsync(ExportLogsServiceRequest request, CancellationToken ct)
{
if (ExportDelay > TimeSpan.Zero)
await Task.Delay(ExportDelay, ct);
Expand All @@ -23,7 +23,7 @@ public async Task ExportLogsAsync(ExportLogsServiceRequest request, Cancellation
LogRequests.Add(request);
}

public async Task ExportTracesAsync(ExportTraceServiceRequest request, CancellationToken ct)
public override async Task ExportTracesAsync(ExportTraceServiceRequest request, CancellationToken ct)
{
if (ExportDelay > TimeSpan.Zero)
await Task.Delay(ExportDelay, ct);
Expand All @@ -35,5 +35,5 @@ public async Task ExportTracesAsync(ExportTraceServiceRequest request, Cancellat
TraceRequests.Add(request);
}

public ValueTask DisposeAsync() => default;
public override ValueTask DisposeAsync() => default;
}
Loading