.Net Open Telemetry 指标未显示在 Prometheus 中

问题描述 投票:0回答:0

我正在尝试在我的 .Net 6.0 API 项目中使用开放遥测协议配置指标,尽管这些指标被 OTLP 接收器获取,但它们不会出现在 Prometheus 导出器中。奇怪的是,我仍然可以在 Prometheus 中看到来自 OTLP 目标的其他跟踪和指标,只是来自我的应用程序的指标没有成为 Prometheus(但是,它们确实进入了 OTLP)。

我的设置是这样的:

Starup.cs

            services.AddOpenTelemetryMetrics(opts => opts
                .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("otel-collector"))
                .AddMeter(metrics.MetricName)
                .AddAspNetCoreInstrumentation()
                .AddConsoleExporter()
                .AddOtlpExporter()
            );

度量仪表类

    public class OtelMetrics
    {
        private Counter<int> MoveToLoadPositionCounter { get; }
        private Counter<int> MoveToSetupPositionCounter { get; }        
        private Histogram<double> MoveToPositionDelayHistogram { get; }
        public string MetricName { get; }

        public OtelMetrics(string meterName = "RealTimeChannelMeter")
        {
            var meter = new Meter(meterName);
            MetricName = meterName;

            MoveToLoadPositionCounter = meter.CreateCounter<int>("move_to_load", "Movement");
            MoveToSetupPositionCounter = meter.CreateCounter<int>("move_to_setup", "Movement");
            MoveToPositionDelayHistogram = meter.CreateHistogram<double>("move_to_position_delay", "Ms", "Move to position latency.");
        }
        
        public void AddToMoveToLoad() => MoveToLoadPositionCounter.Add(1);
        public void AddToMoveToSetup() => MoveToSetupPositionCounter.Add(1);
        public void MoveToPositionDelay(double delayMs) => MoveToPositionDelayHistogram.Record(delayMs);

    }

触发指标:

        [HttpPost]
        [Route("Load")]
        [ApiConventionMethod(typeof(DefaultApiConventions), nameof(DefaultApiConventions.Post))]
        public async Task<ActionResult<MoveToPositionResponse>> LoadAsync(MoveToPositionRequest request)
        {
            _metrics.AddToMoveToLoad();

            var watch = Stopwatch.StartNew();
            await Task.Delay(5000);
            watch.Stop();

            _metrics.MoveToPositionDelay(watch.ElapsedMilliseconds);

            return CreateActionResult(MovementResult.SUCCESS);
        }

OTLP配置文件:

receivers:
  otlp:
    protocols:
      http:

exporters:
  prometheus:
    endpoint: "0.0.0.0:8888"

processors:
  batch:

extensions:
  health_check:

service:
  extensions: [health_check]
  pipelines:
    metrics:
      receivers: [otlp]
      processors: [batch]
      exporters: [prometheus]

将 OTLP 作为 Docker 容器运行:

docker run -d -p 8888:8888 -p 8889:8889 -p 13133:13133 -p 4317:4317 -p 4318:4318 -v ./config.yaml:/etc/otel-collector-config.yamll otel/opentelemetry-collector:0.60.0

普罗米修斯配置文件:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'RealTimeChannelMeter'
    scrape_interval: 5s
    static_configs:
      - targets: ['host.docker.internal:8889']
      - targets: ['host.docker.internal:8888']

普罗米修斯 Dockerfile:

FROM prom/prometheus:v2.38.0

ADD prometheus.yml /etc/prometheus/

将 Prometheus 作为 Docker 容器运行:

docker build -t prometheus2 . 
docker run -d -p 9000:9000 prometheus2

我在资源管理器中看到的指标如下

enter image description here

当我检查 OTLP 容器内的日志时,我可以看到,例如,使它成为普罗米修斯的指标之一看起来像这样:

Resource SchemaURL: 
Resource attributes:
     -> service.name: STRING(otel-collector)
     -> service.instance.id: STRING(0.0.0.0:8888)
     -> net.host.port: STRING(8888)
     -> http.scheme: STRING(http)
ScopeMetrics #0
ScopeMetrics SchemaURL: 
InstrumentationScope  
Metric #0
Descriptor:
     -> Name: otelcol_exporter_enqueue_failed_metric_points
     -> Description: Number of metric points failed to be added to the sending queue.
     -> Unit: 
     -> DataType: Sum
     -> IsMonotonic: true
     -> AggregationTemporality: AGGREGATION_TEMPORALITY_CUMULATIVE
NumberDataPoints #0
Data point attributes:
     -> exporter: STRING(logging)
     -> service_instance_id: STRING(32709c55-4140-4bb7-98c3-94a09b557f4c)
     -> service_version: STRING(0.60.0)
StartTimestamp: 2023-02-23 16:24:28.395 +0000 UTC
Timestamp: 2023-02-23 16:24:28.395 +0000 UTC
Value: 0.000000

另一方面,来自我在 OTLP 容器中的应用程序但未在 Prometheus 中显示的指标如下所示:

Resource SchemaURL: 
Resource attributes:
     -> service.name: STRING(otel-collector)
     -> service.instance.id: STRING(1e10dd35-13c6-4d1d-9f42-4a82611924da)
ScopeMetrics #0
ScopeMetrics SchemaURL: 
InstrumentationScope RealTimeChannelMeter 
Metric #0
Descriptor:
     -> Name: move-to-load
     -> Description: 
     -> Unit: Movement
     -> DataType: Sum
     -> IsMonotonic: true
     -> AggregationTemporality: AGGREGATION_TEMPORALITY_CUMULATIVE
NumberDataPoints #0
StartTimestamp: 2023-02-23 16:07:04.4076488 +0000 UTC
Timestamp: 2023-02-23 16:08:04.3291068 +0000 UTC
Value: 2

我的假设是它必须与 -> net.host.port: STRING(8888) -> http.scheme: STRING(http) 因为这是我能注意到的唯一区别,但我不确定如何在我的 .Net 应用程序中配置它。但是,如果情况并非如此,我能否对我做错了什么有一些见解。

谢谢!

c# .net prometheus metrics open-telemetry
© www.soinside.com 2019 - 2024. All rights reserved.