serilogopen-telemetry

Serilog HTTPProtobuf protocol doesn't seem to be respected


I am trying to setup the Serilog OpenTelemetry sink to send logs to my Otel Collector using the HttpProtobuf protocol.

My logs don't seem to be arriving, so I turned on Serilog self logging and see the following in the console:

Grpc.Core.RpcException: Status(StatusCode="Unknown", Detail="Bad gRPC response. HTTP status code: 405") at Serilog.Core.Sinks.Batching.BatchingSink.LoopAsync()

This would seem to suggest the protocol is not being supported and the sink is still trying to send as Grpc.

My setup is as follows:

builder.Host.UseSerilog((ctx, serviceProvider, lc) =>
{
    lc = lc.ReadFrom.Configuration(ctx.Configuration)
        .WriteTo.Console();

    var otlpEndpoint = builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"];
    if (!string.IsNullOrWhiteSpace(otlpEndpoint))
    {
        lc.WriteTo.OpenTelemetry(cfg =>
        {
            cfg.Endpoint = $"{otlpEndpoint}/v1/logs";
            cfg.Protocol = OtlpProtocol.HttpProtobuf;         
        });
    }
});

Solution

  • Pass ignoreEnvironment: true to WriteTo.OpenTelemetry() to ignore any environment variable overrides set in your environment:

    builder.Host.UseSerilog((ctx, serviceProvider, lc) =>
    {
        lc = lc.ReadFrom.Configuration(ctx.Configuration)
            .WriteTo.Console();
    
        var otlpEndpoint = builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"];
        if (!string.IsNullOrWhiteSpace(otlpEndpoint))
        {
            lc.WriteTo.OpenTelemetry(cfg =>
            {
                cfg.Endpoint = $"{otlpEndpoint}/v1/logs";
                cfg.Protocol = OtlpProtocol.HttpProtobuf;  
            // Add this arg:       
            }, ignoreEnvironment: true);
        }
    });