prometheus/otlptranslator

GitHub: prometheus/otlptranslator

一个 Go 库,用于将 OpenTelemetry Protocol(OTLP)的指标和属性名称转换为符合 Prometheus 命名规范的格式。

Stars: 15 | Forks: 11

# OTLP Prometheus 翻译器 一个用于将 [OpenTelemetry Protocol (OTLP)](https://opentelemetry.io/docs/specs/otlp/) 指标和属性名称转换为符合 [Prometheus](https://prometheus.io/) 格式的 Go 库。这是 Prometheus 和 OpenTelemetry 的内部库,不对外部使用提供任何稳定性保证。 它是 [Prometheus](https://prometheus.io/) 生态系统的一部分,遵循 [OpenTelemetry 到 Prometheus 的兼容性规范](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/compatibility/prometheus_and_openmetrics.md)。 ## 功能 - **指标名称和标签转换**:将 OTLP 指标名称和属性转换为符合 Prometheus 格式的名称 - **单位处理**:将 OTLP 单位转换为 Prometheus 单位约定 - **类型感知后缀**:根据指标类型可选地附加 `_total`、`_ratio` - **命名空间支持**:添加可配置的命名空间前缀 - **UTF-8 支持**:在符合 Prometheus 旧版方案的指标/标签名称 (`[a-zA-Z0-9:_]`) 或未转换的指标/标签名称之间进行选择 - **转换策略配置**:使用一组标准字符串选择转换策略。 ## 安装 ``` go get github.com/prometheus/otlptranslator ``` ## 快速入门 ``` package main import ( "fmt" "github.com/prometheus/otlptranslator" ) func main() { // Create a metric namer using traditional Prometheus name translation, with suffixes added and UTF-8 disallowed. strategy := otlptranslator.UnderscoreEscapingWithSuffixes namer := otlptranslator.NewMetricNamer("myapp", strategy) // Translate OTLP metric to Prometheus format metric := otlptranslator.Metric{ Name: "http.server.request.duration", Unit: "s", Type: otlptranslator.MetricTypeHistogram, } metricName, err := namer.Build(metric) if err != nil { panic(err) } fmt.Println(metricName) // Output: myapp_http_server_request_duration_seconds // Translate label names labelNamer := otlptranslator.LabelNamer{UTF8Allowed: false} labelName, err := labelNamer.Build("http.method") if err != nil { panic(err) } fmt.Println(labelName) // Output: http_method } ``` ## 使用示例 ### 指标名称转换 ``` namer := otlptranslator.MetricNamer{WithMetricSuffixes: true, UTF8Allowed: false} // Counter gets _total suffix counter := otlptranslator.Metric{ Name: "requests.count", Unit: "1", Type: otlptranslator.MetricTypeMonotonicCounter, } counterName, _ := namer.Build(counter) fmt.Println(counterName) // requests_count_total // Gauge with unit conversion gauge := otlptranslator.Metric{ Name: "memory.usage", Unit: "By", Type: otlptranslator.MetricTypeGauge, } gaugeName, _ := namer.Build(gauge) fmt.Println(gaugeName) // memory_usage_bytes // Dimensionless gauge gets _ratio suffix ratio := otlptranslator.Metric{ Name: "cpu.utilization", Unit: "1", Type: otlptranslator.MetricTypeGauge, } ratioName, _ := namer.Build(ratio) fmt.Println(ratioName) // cpu_utilization_ratio ``` ### 标签转换 ``` labelNamer := otlptranslator.LabelNamer{UTF8Allowed: false} labelNamer.Build("http.method") // http_method, nil labelNamer.Build("123invalid") // key_123invalid, nil labelNamer.Build("_private") // _private, nil labelNamer.Build("__reserved__") // __reserved__, nil labelNamer.Build("label@with$symbols") // label_with_symbols, nil ``` ### 单位转换 ``` unitNamer := otlptranslator.UnitNamer{UTF8Allowed: false} unitNamer.Build("s") // seconds unitNamer.Build("By") // bytes unitNamer.Build("requests/s") // requests_per_second unitNamer.Build("1") // "" (dimensionless) ``` ### 配置选项 ``` // Prometheus-compliant mode - supports [a-zA-Z0-9:_] compliantNamer := otlptranslator.MetricNamer{UTF8Allowed: false, WithMetricSuffixes: true} // Transparent pass-through mode, aka "NoTranslation" utf8Namer := otlptranslator.MetricNamer{UTF8Allowed: true, WithMetricSuffixes: false} utf8Namer = otlptranslator.NewMetricNamer("", otlptranslator.NoTranslation) // With namespace and suffixes productionNamer := otlptranslator.MetricNamer{ Namespace: "myservice", WithMetricSuffixes: true, UTF8Allowed: false, } ``` ## 许可证 基于 Apache License 2.0 授权 - 详见 [LICENSE](LICENSE) 文件。
标签:API集成, EVTX分析, GET参数, Go, OpenTelemetry, Ruby工具, 可观测性, 开发库, 数据转换, 日志审计, 用户代理, 自定义请求头