
OpenTelemetry Collector processorhelper 内部遥测指标详解incoming/duration/outgoing 三件套的采集机制与观测实践【免费下载链接】opentelemetry-collectorOpenTelemetry Collector项目地址: https://gitcode.com/GitHub_Trending/op/opentelemetry-collectorprocessorhelper 是 OpenTelemetry Collector 提供给所有 processor处理器组件的通用封装层它在转发数据、管理生命周期之外还自动为每个处理器暴露一组标准化内部遥测指标。本文以 processorhelper/documentation.md 为核心结合 metadata.yaml、obsreport.go 等源码完整解析otelcol_processor_incoming_items、otelcol_processor_internal_duration、otelcol_processor_outgoing_items三项指标的含义、单位、语义与采集链路并说明如何通过服务级遥测级别控制其导出帮助你在实际部署中利用这些指标观测处理器吞吐与性能。一、指标总览processorhelper 暴露的 3 项内部遥测processorhelper 是 OpenTelemetry Collector 中编写 processor 的脚手架包稳定级别为 beta覆盖 traces/metrics/logs 三种信号见 metadata.yaml。凡是通过NewTraces、NewMetrics、NewLogs三个构造器创建的自定义处理器都会自动接入内部遥测无需在业务代码中手动打点。根据官方生成的 documentation.md该组件共暴露以下 3 项指标全部注册在 Meter 作用域go.opentelemetry.io/collector/processor/processorhelper下见 internal/metadata/generated_telemetry.go1.otelcol_processor_incoming_items描述传入处理器的数据项数量Number of items passed to the processor。指标属性UnitMetric TypeValue TypeMonotonicStability{item}SumInttrueAlpha该指标是单调递增的 Int 类型 Counter以“个数据项”为计量单位用于统计进入处理器的数据规模。2.otelcol_processor_internal_duration描述处理器处理一批遥测数据所消耗的时间Duration of time taken to process a batch of telemetry data through the processor。指标属性UnitMetric TypeValue TypeStabilitysHistogramDoubleAlpha该指标是秒为单位的直方图反映处理器内部单批处理耗时分布可用于观察 P50/P95/P99 延迟。3.otelcol_processor_outgoing_items描述处理器发出的数据项数量Number of items emitted from the processor。指标属性UnitMetric TypeValue TypeMonotonicStability{item}SumInttrueAlpha同样是单调递增的 Int Counter统计处理器实际输出、向后继续传递的数据项数量。值得注意的是三项指标本身的稳定级别为Alpha意味着其名称、语义和属性在未来版本中仍可能调整而processorhelper包整体beta与具体处理器组件如 memorylimiterprocessor、batchprocessor的稳定级别相互独立。二、指标元数据从 mdatagen 声明到生成代码这三项指标并非手写埋点而是通过 mdatagen 工具从声明式配置自动生成。核心元数据位于 metadata.yamltelemetry: metrics: processor_incoming_items: enabled: true stability: alpha description: Number of items passed to the processor. unit: {item} sum: value_type: int monotonic: true processor_internal_duration: enabled: true stability: alpha description: Duration of time taken to process a batch of telemetry data through the processor. unit: s histogram: async: false value_type: double processor_outgoing_items: enabled: true stability: alpha description: Number of items emitted from the processor. unit: {item} sum: value_type: int monotonic: true在源码根目录通过go:generate mdatagen metadata.yaml指令见 processor.go 文件头生成 internal/metadata/generated_telemetry.go。生成代码中三个指标被封装进TelemetryBuilderProcessorIncomingItemsmetric.Int64Counter注册名otelcol_processor_incoming_items单位{item}ProcessorInternalDurationmetric.Float64Histogram注册名otelcol_processor_internal_duration单位sProcessorOutgoingItemsmetric.Int64Counter注册名otelcol_processor_outgoing_items单位{item}。这正是文档表格中 Metric Type、Value Type 与 Monotonic 标记在代码层面的直接对应两个 SumCounter对应Int64Counter一个 Histogram 对应Float64Histogram。三、采集原理obsreport 报告器如何打点指标的实际写入集中在 obsreport.go。newObsReport创建报告器时为所有指标统一附加了一组标签属性otelAttrs: metric.WithAttributeSet(attribute.NewSet( attribute.String(internal.ProcessorKey, set.ID.String()), // 属性键 processor attribute.String(signalKey, signal.String()), // 属性键 otel.signal )),即每条数据序列都带有两个固定维度属性键取值含义processor处理器组件 ID如batch、memory_limiter区分具体是哪个处理器otel.signaltraces/metrics/logs区分遥测信号类型记录逻辑分两个方法func (or *obsReport) recordInOut(ctx context.Context, incoming, outgoing int) { or.telemetryBuilder.ProcessorIncomingItems.Add(ctx, int64(incoming), or.otelAttrs) or.telemetryBuilder.ProcessorOutgoingItems.Add(ctx, int64(outgoing), or.otelAttrs) } func (or *obsReport) recordInternalDuration(ctx context.Context, startTime time.Time) { duration : time.Since(startTime) or.telemetryBuilder.ProcessorInternalDuration.Record(ctx, duration.Seconds(), or.otelAttrs) }由此可以确认三条重要事实三个指标必须带上processor与otel.signal两个属性查询才有意义否则无法区分是哪个处理器的数据incoming_items与outgoing_items成对记录一次消费调用同时累加两者internal_duration记录的是time.Since(startTime)的秒数startTime取自处理函数调用前一刻。四、三种信号下的“数据项”计量语义“数据项”item在不同信号下代表不同的最小单位这是理解该指标的关键。三个构造器traces.go、metrics.go、logs.go分别通过 pdata 的计数方法统计输入输出规模信号构造器输入计数方法输出计数方法“数据项”含义tracesNewTracestd.SpanCount()td.SpanCount()Span 数metricsNewMetricsmd.DataPointCount()md.DataPointCount()数据点DataPoint数logsNewLogsld.LogRecordCount()ld.LogRecordCount()LogRecord 数以 traces 为例traces.go 中的核心处理流程为spansIn : td.SpanCount() // 处理前统计输入 Span 数 td, errFunc tracesFunc(ctx, td) // 调用用户处理逻辑 obs.recordInternalDuration(ctx, startTime) if errFunc ! nil { obs.recordInOut(ctx, spansIn, 0) // 出错时输出记 0 ... return errFunc } spansOut : td.SpanCount() // 处理后统计输出 Span 数 obs.recordInOut(ctx, spansIn, spansOut) return nextConsumer.ConsumeTraces(ctx, td)这意味着incoming_items与outgoing_items的差值可以直观反映处理器“丢弃/合并”了多少数据例如采样、去重、聚合类处理器天然会造成差值。五、错误处理与 sentinelErrSkipProcessingData 的特殊语义处理器返回错误时指标记录遵循明确规则处理函数返回普通错误recordInOut(ctx, in, 0)即outgoing_items记 0错误继续向上传播处理函数返回哨兵错误ErrSkipProcessingData同样输出记 0但错误被吞掉、不向上传播表现为数据被“静默丢弃”。ErrSkipProcessingData定义在 processor.govar ErrSkipProcessingData errors.New(sentinel error to skip processing data from the remainder of the pipeline)其设计意图是当处理器判定某批数据无关紧要如时间戳过期、内容不符合过滤条件时可以主动丢弃且不污染上层日志——这也是 filter 等处理器“丢弃数据”的标准做法。观测时注意这类被丢弃的数据会计入incoming_items而不再计入outgoing_items两者差值即是被 sentinel 静默丢弃的量。六、观测实践如何查询这三个指标processorhelper 的指标经 Collector 自身遥测service::telemetry导出可被 Prometheus 抓取。典型查询方式# 各处理器处理吞吐每秒 rate(otelcol_processor_incoming_items[1m]) rate(otelcol_processor_outgoing_items[1m]) # 各处理器处理延迟分布 histogram_quantile(0.95, sum(rate(otelcol_processor_internal_duration_bucket[5m])) by (le, processor, otel.signal)) # 丢弃率outgoing 与 incoming 的比值 otelcol_processor_outgoing_items / otelcol_processor_incoming_items重要前提——遥测级别otelcol_processor_internal_duration并非在所有遥测级别下都会导出。在 service/internal/metricviews/views.go 中DefaultViews定义了默认的指标视图裁剪规则if level configtelemetry.LevelDetailed { // Drop duration metric if the level is not detailed dropViewOption(config.ViewSelector{ MeterName: new(go.opentelemetry.io/collector/processor/processorhelper), InstrumentName: new(otelcol_processor_internal_duration), }), }也就是说只有将 Collector 的遥测级别配置为detailed通过service::telemetry::metrics::level或--metrics-leveldetailed时otelcol_processor_internal_duration才会被导出incoming_items与outgoing_items两个计数指标则不受该限制。若发现延迟直方图缺失请优先检查遥测级别配置。七、并发安全与测试验证processorhelper 的指标记录是并发安全的TelemetryBuilder内部使用sync.Mutex保护异步仪器注册见 generated_telemetry.go而 Counter/Histogram 本身即线程安全类型。对应测试集中在 metrics_test.goTestMetricsConcurrency10 个 goroutine 各并发消费 10000 批数据验证高并发下无竞态TestMetrics_RecordInOut输入 2 个 DataPoint、处理函数输出 3 个断言incoming_items2、outgoing_items3并验证属性为processorprocessorhelper、otel.signalmetricsTestMetrics_RecordIn_ErrorOut处理返回错误时断言incoming_items2、outgoing_items0印证第五节“出错输出记 0”的语义TestMetrics_ProcessInternalDuration断言直方图Count1验证每次消费都记录一次耗时分布。如果你编写自定义处理器并依赖 processorhelper这些测试同时是processor/otel.signal属性值以及计数语义的最佳参考样例。八、小结otelcol_processor_incoming_items、otelcol_processor_internal_duration、otelcol_processor_outgoing_items构成了 processorhelper 统一的处理器可观测性模型吞吐维度由两个单调 Counter 提供配合processor、otel.signal属性可精确到“某个处理器、某类信号”的进出流量性能维度由秒级直方图提供但需以detailed遥测级别为前提计量口径按信号区分Span / DataPoint / LogRecord解读差值时应结合处理器业务语义过滤、采样、聚合所有指标当前稳定级别为 Alpha接口演进需关注版本升级说明见 CHANGELOG.md 中相关记录。建议在部署 Collector 后将上述 PromQL 查询接入统一监控看板即可获得每个处理器的实时吞吐、丢弃量与延迟画像为容量评估和调优提供数据支撑。【免费下载链接】opentelemetry-collectorOpenTelemetry Collector项目地址: https://gitcode.com/GitHub_Trending/op/opentelemetry-collector创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考