Vector internal_metrics 源详解:采集并转发 Vector 自身运行指标

发布时间:2026/9/13 19:19:54
Vector internal_metrics 源详解:采集并转发 Vector 自身运行指标
Vector internal_metrics 源详解采集并转发 Vector 自身运行指标【免费下载链接】vectorA high-performance observability data pipeline.项目地址: https://gitcode.com/GitHub_Trending/vect/vectorVector 是高性能的可观测性数据管道而internal_metrics源source是它对自身做“自我观测”的核心入口它把 Vector 实例运行时产生的全部内部指标按周期抓取出来作为普通的 metrics 事件注入用户配置的管道从而可以被过滤、转换、路由到任意 metrics 型 sink。读完本文你将掌握internal_metrics源的完整配置参数namespace、scrape_interval_secs、tags.host_key、tags.pid_key及其默认值理解其底层“定时快照全局指标注册表”的实现机制并学会在多实例部署中避免指标序列冲突。组件定位与能力概览internal_metrics源暴露 Vector 自身产生的内部指标让你像处理其他业务指标一样去采集、处理和路由它们。从组件元数据CUE 定义可以看到它的关键属性属性值含义交付语义deliveryat_least_once每个采集周期产出一批指标下游通道关闭时该批会失败部署角色deployment_rolesaggregator、daemon、sidecar三种典型部署形态均适用开发状态developmentstable稳定组件出口方式egress_methodbatch以批量方式向下游发送是否有状态statefulfalse源本身不保存任何状态状态在全局指标注册表中是否支持确认acknowledgementsfalse源码中can_acknowledge()固定返回false输出类型metrics只产生 metrics 输出这些能力声明与 源码实现 完全对应SourceConfig实现的outputs()返回vec![SourceOutput::new_metrics()]can_acknowledge()返回false且该组件在 src/sources/mod.rs 中由sources-internal_metrics特性开关feature flag控制编译。配置参考internal_metrics源的所有配置参数定义在 src/sources/internal_metrics.rs 的InternalMetricsConfig结构体中结构体标注了#[serde(deny_unknown_fields, default)]即所有字段均可省略、未知字段会导致校验失败且全部字段都有默认值。最小配置仓库生成的最小示例配置minimal.yaml只有两行sources: my_source_id: type: internal_metrics完整配置参数参数类型默认值说明scrape_interval_secsfloat秒1.0两次指标抓取之间的间隔。底层以Duration存储序列化时通过DurationSecondsWithFracf64以秒支持小数表达namespacestringvector覆盖该源发出的指标的默认命名空间tags.host_keystring值路径全局log_schema.host_key默认host覆盖用于附加主机名的 tag 名设为可抑制该 tagtags.pid_keystring未设置不附加指定附加当前进程 PID 的 tag 名例如pid完整的参数示例advanced.yamlsources: my_source_id: type: internal_metrics namespace: vector scrape_interval_secs: 1.0参数行为细节结合源码scrape_interval_secs: 0的告警在build()中若间隔为 0Vector 会输出警告日志提示可能导致高 CPU 占用建议间隔不小于 1 秒——但 0 仍然生效并非校验错误。namespace的覆盖逻辑内部指标本身默认就带vector命名空间只有当你显式配置了非vector的namespace时抓取到的每个 metric 才会被with_namespace()重写。源码中的注释明确写道“A metric starts out with a default vector namespace, but will be overridden if an explicit namespace is provided to this source”。tags.host_key的默认来源build()中取tags.host_key为空则回退到全局log_schema().host_key()配置成空串时不附加主机 tag。tags.pid_key默认不附加 PID配置后如pid_key: pid每个指标都会带上该实例的进程 ID。工作原理定时快照全局指标注册表从源码结构看internal_metrics源并不直接读取某个组件的指标而是对进程级全局指标控制器做周期性快照。核心调用链在 src/sources/internal_metrics.rs 的InternalMetrics::run()中定时器用tokio::time::interval(self.interval)构造IntervalStream并take_until(self.shutdown)与进程关闭信号联动——每到一个scrape_interval_secs周期唤醒一次。抓取快照Controller::get()?取得全局Controller单例指标注册表调用capture_metrics()一次性收集当前所有 gauge / counter / histogram。这也是为什么该源“无状态”——真正的状态保存在注册表中源只负责搬运。自我打点每次抓取都会向自身 emit 两条内部事件——EventsReceived事件数 估算 JSON 大小和BytesReceived::from(Protocol::INTERNAL)原始字节数即该源也在上报自己的吞吐。改写指标对快照中的每个 metric 依次应用命名空间覆盖、host_key主机名 tagmetric.replace_tag(host_key, hostname)、pid_key进程 ID tag。批量发送通过self.out.send_batch(batch).await一次性发往下游若下游通道已关闭则 emitStreamClosedError并结束。同文件内的单元测试tests模块验证了上述行为captures_internal_metrics用gauge!/counter!/histogram!宏写入指标后经capture_metrics()断言 gauge 取最新值、counter 累加、histogram 桶计数正确default_namespace断言默认命名空间为vectorsets_tags断言自定义host_key/pid_key生效only_host_tags_by_default断言默认只附加host而不附加pidnamespace断言自定义命名空间被写入。这些测试路径都位于 src/sources/internal_metrics.rs。输出的指标有哪些该源输出的是 Vector 整个进程内部指标注册表中的全部指标指标清单以 website/cue/reference/components/sources/internal_metrics.cue 中的定义为准默认命名空间为vector按主题可分为几类实例级“进程”指标tag 为实例级 tag 组指标类型说明started_total/stopped_total/reloaded_total/quit_totalcounter实例启动/停止/热重载/退出次数uptime_secondsgauge实例运行总秒数build_infogauge固定值 1.0tag 携带debug、version、rust_version、arch、revision构建信息connection_established_total/connection_shutdown_total/connection_send_errors_totalcounter连接建立/关闭/发送错误总数open_connectionsgauge当前到 Vector 的打开连接数checkpoints_totalcounter完成 checkpoint 的文件总数组件级指标tag 组为_component_tags包含component_kind、component_id、component_type以及实例级 tag指标类型说明component_received_events_total/component_received_bytes_totalcounter组件接收的事件/字节数tag 可含file、uri、pod_name、peer_addr、mode等来源信息component_sent_events_total/component_sent_bytes_totalcounter组件发出的事件/字节数tag 含output、endpoint、protocol、region等component_errors_totalcounter组件错误总数tag 含error_type如parse_failed、http_error、stream_closed等枚举值与stagereceiving/processing/sendingcomponent_discarded_events_totalcounter被丢弃事件数intentionaltag 区分是filter等主动丢弃还是错误丢弃component_latency_secondshistogram事件在单个 transform 中的耗时含输入缓冲排队时间component_latency_mean_secondsgauge上述耗时的 EWMA 平滑均值component_cpu_usage_ns_totalcounter组件 CPU 时间纳秒仅 transform 可用仅 Linux/macOS/Windowsutilizationgauge组件负载比 0~10 表示纯空闲等待输入1 表示从不空闲每 5 秒更新component_timed_out_events_total/component_timed_out_requests_totalcounter源组件超时事件/请求数events_discarded_totalcounter被丢弃事件reasontag 取out_of_order或oversized源缓冲source buffer指标source_buffer_max_size_bytes、source_buffer_max_size_eventsgaugesource_buffer_utilizationhistogram、source_buffer_utilization_level、source_buffer_utilization_meanEWMA 均值 gaugetag 含output另有source_lag_time_seconds事件时间戳与摄取时间之差、source_send_latency_seconds、source_send_batch_latency_seconds等背压相关直方图。旧名source_buffer_max_byte_size、source_buffer_max_event_size已标记废弃。Transform 缓冲指标transform_buffer_max_size_bytes、transform_buffer_max_size_events、transform_buffer_utilization、transform_buffer_utilization_level、transform_buffer_utilization_mean以及废弃的旧名transform_buffer_max_byte_size/transform_buffer_max_event_size。缓冲区buffer管道指标tag 组含buffer_id、stagebuffer_size_bytes、buffer_size_events、buffer_received_events_total、buffer_sent_events_total、buffer_discarded_events_total含intentionaltag、buffer_errors_totalerror_code枚举含deser_failed、checksum_mismatch、decode_failed、incompatible_record_version、partial_write、buffer_send_duration_seconds等。协议/组件特定指标HTTP 客户端与服务器http_client_rtt_seconds、http_client_responses_total、http_server_handler_duration_seconds等、Kafkakafka_consumer_lag、kafka_produced_messages_total、kafka_queue_messages等、S3/SQS对象处理耗时、消息接收/删除计数、gRPCgrpc_server_handler_duration_seconds等、Lualua_memory_used_bytes、adaptive concurrencyadaptive_concurrency_limit、adaptive_concurrency_observed_rtt等、tag_cardinality_limitvalue_limit_reached_total、tag_value_limit_exceeded_total、Kubernetes 日志k8s_docker_format_parse_failures_total等、Dorisdoris_rows_loaded_total等、Windows 服务windows_service_start_total等。此外internal_metrics_cardinalitygauge记录注册表当前指标总数旧名internal_metrics_cardinality_totalcounter已废弃。实战多实例场景下避免指标序列冲突当多个 Vector 实例把internal_metrics发送到同一目的地时若不带能区分实例的 tag各实例的指标序列会互相冲突。文档给出的方案是使用tags.host_key默认即会附加hosttag主机名天然区分不同主机上的实例或者在下游加一个remaptransform从环境中读取一个实例唯一标识再打 tag。示例sources: metrics: type: internal_metrics scrape_interval_secs: 5 tags: host_key: host # 显式指定默认也是 host pid_key: pid # 同机多实例时用 PID 区分 transforms: add_env: type: remap inputs: [metrics] source: | .env production sinks: out: type: prometheus_exporter inputs: [add_env] address: 0.0.0.0:9090在 regression/cases 下的多个回归测试配置中也能看到该源的实际用法模式例如 regression/cases/file_to_blackhole/vector/vector.yaml 中即以internal_metrics作为观测自身吞吐的配套源。适用前提与限制internal_metrics源只输出 metrics不能产出 log 或 trace 输出它不支持 acknowledgementcan_acknowledge恒为false。抓取间隔过小包括 0会持续快照全量指标注册表CPU 开销显著增加官方建议间隔 ≥ 1 秒。每次抓取的是全量快照而非增量counter 类指标在 Vector 侧以累计值记录下游若需要速率需自行求 rate。namespace仅在该源配置的命名空间不等于默认的vector时才会改写指标命名空间若显式配置namespace: vector行为与默认一致。该组件由 Cargo featuresources-internal_metrics控制属于默认构建的一部分无需额外依赖CUE 中requirements为空。【免费下载链接】vectorA high-performance observability data pipeline.项目地址: https://gitcode.com/GitHub_Trending/vect/vector创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考