如何在 Prometheus 中找到或正确导出 OpenTelemetry 指标的资源标签?

How can find or correctly export resource labels of OpenTelemetry metrics in Prometeus?

我正在使用 .Net 和 OpenTelemetry 创建一个应用程序来收集指标、跟踪。 .Net 应用程序使用 Otlp 导出器。 此外,我还配置了一个收集器,将 Otlp 作为接收器,将 Prometheus 作为指标的导出器。

我在我的 .Net 应用程序中设置了 ServiceName(例如“OpenTelemetrySample”)并设置了一些属性(例如 attributeA、attributeB)

services.AddOpenTelemetryMetrics(builder =>{ 
     builder.SetResourceBuilder(ResourceBuilder.CreateDefault()
            .AddService("OpenTelemetrySample")
            .AddAttributes(new KeyValuePair<string, object>[]
            {new("attributeA", "AAA"), new("attributeB", "BBB")}));
     builder.AddMeter("opentelemetry-meter");
     builder.AddOtlpExporter(otlpOptions =>
            {otlpOptions.Endpoint = new Uri("http://localhost:4317");   
            });
          });

然后我在代码中使用了一些计数器:

static Meter myMeter = new("opentelemetry-meter", "1.0.0");
static Counter<int> myCounter = myMeter.CreateCounter<int>(name: $"test-counter",
            unit: "Counter",
            description: "The number of something");
...
myCounter.Add(2, new("custom.tagA", "tagValueA"), new("custom.tagB", "tagValueB"));

我在 PrometeusUI 中看到的结果是: here 我希望在某处看到 service.name 和资源属性,但在 Prometheus UI.

中找不到它们

例如,相同的过程,当我通过收集器的控制台导出指标时,在 资源标签

部分中显示提到的属性

控制台输出:

collector            | 2022-02-15T13:01:40.968Z INFO    loggingexporter/logging_exporter.go:56  MetricsExporter {"#metrics": 1}
collector            | 2022-02-15T13:01:40.968Z DEBUG   loggingexporter/logging_exporter.go:66  ResourceMetrics #0
collector            | Resource labels:
collector            |      -> attributeA: STRING(AAA)
collector            |      -> attributeB: STRING(BBB)
collector            |      -> service.name: STRING(OpenTelemetrySample)
collector            |      -> service.instance.id: STRING(0f08e609-66e0-49a5-ad6d-44c8e3520bad)
collector            | InstrumentationLibraryMetrics #0
collector            | InstrumentationLibrary opentelemetry-meter 1.0.0
collector            | Metric #0
collector            | Descriptor:
collector            |      -> Name: test-counter
collector            |      -> Description: The number of something
collector            |      -> Unit: Counter
collector            |      -> DataType: Sum
collector            |      -> IsMonotonic: true
collector            |      -> AggregationTemporality: AGGREGATION_TEMPORALITY_CUMULATIVE
collector            | NumberDataPoints #0
collector            | Data point attributes:
collector            |      -> custom.tagA: STRING(tagValueA)
collector            |      -> custom.tagB: STRING(tagValueB)
collector            | StartTimestamp: 2022-02-15 12:26:55.3992028 +0000 UTC
collector            | Timestamp: 2022-02-15 13:01:40.5841487 +0000 UTC
collector            | Value: 2

otel-collector-config.yaml

receivers:
  otlp:
    protocols:
      grpc:
exporters:
    logging:
      loglevel: debug
    jaeger:
      endpoint: jaeger:14250
      insecure: true
    prometheus:
      endpoint: "0.0.0.0:8889"
extensions:
  health_check:
  pprof:
  zpages:
processors:
    batch:
service:
    extensions: [health_check,pprof,zpages]
    pipelines:
        traces:
            receivers: [otlp]
            exporters: [logging, jaeger]
            processors: [batch]
        metrics:
            receivers: [otlp]          
            exporters: [logging, prometheus]
            processors: [batch]            
        logs:
            receivers: [otlp]
            exporters: [logging]
            processors: [batch]

prometheus.yaml

scrape_configs:
  - job_name: 'otel-collector'
    scrape_interval: 5s
    static_configs:
      - targets: ['collector:8889']

想知道如何正确查找或导出指标的资源标签,谢谢

默认情况下禁用资源属性到指标标签的转换。请更新otel配置如下,查看资源属性为prom UI.

receivers:
  otlp:
    protocols:
      grpc:
exporters:
    logging:
      loglevel: debug
    jaeger:
      endpoint: jaeger:14250
      insecure: true
    prometheus:
      endpoint: "0.0.0.0:8889"
      resource_to_telemetry_conversion:
        enabled: true
extensions:
  health_check:
  pprof:
  zpages:
processors:
    batch:
service:
    extensions: [health_check,pprof,zpages]
    pipelines:
        traces:
            receivers: [otlp]
            exporters: [logging, jaeger]
            processors: [batch]
        metrics:
            receivers: [otlp]          
            exporters: [logging, prometheus]
            processors: [batch]            
        logs:
            receivers: [otlp]
            exporters: [logging]
            processors: [batch]