如何使用OpenTelemetry收集器和.NET SDK捕获日志

eyh26e7m  于 2023-03-31  发布在  .NET
关注(0)|答案(1)|浏览(144)

我试图使用一个简单的例子来设置.NET和OpenTelemetry的日志。

using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;

using var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.ClearProviders();
    builder.AddOpenTelemetry(options =>
    {
        options
            .ConfigureResource(c => c.AddService("GettingStarted"))
            .AddConsoleExporter()
            .AddOtlpExporter(config =>
            {
                config.Endpoint = new Uri("http://localhost:4318");
                config.Protocol = OtlpExportProtocol.HttpProtobuf;
            });
    });
});

var logger = loggerFactory.CreateLogger<Program>();

for (var i = 0; i < 100; i++)
{
    logger.LogError("{count}: Hello World!", i);
    await Task.Delay(1000);
}

收集器配置(otel-collector-config.yaml):

receivers:
  otlp:
    protocols:
      http:
exporters:
  logging:
    loglevel: debug
service:
  telemetry:
    logs:
      level: debug
  pipelines:
    logs:
      receivers: [otlp]
      exporters: [logging]

OpenTelemetry Collector(docker-compose.yml):

services:
  opentelemetry-collector:
    image: otel/opentelemetry-collector:0.67.0
    ports:
      - 4318:4318
    volumes:
      - ./otel-collector-config.yaml:/etc/otel-collector-config.yaml

这些日志会传送到控制台(ConsoleExporter),但我在收集器的日志(stdout)中看不到它。
我哪里做错了?

ffscu2ro

ffscu2ro1#

我的直觉是合成文件中的收集器条目缺少command属性:

services:
  opentelemetry-collector:
    image: otel/opentelemetry-collector:0.67.0
    ports:
      - 4318:4318
    # Add this line here
    command: ["--config=/etc/otel-collector-config.yaml"]
    volumes:
      - ./otel-collector-config.yaml:/etc/otel-collector-config.yaml

因此,您的配置实际上没有应用于收集器。

相关问题