更新 opentelemetry prometheus 导出器中的标签

Posted

技术标签:

【中文标题】更新 opentelemetry prometheus 导出器中的标签【英文标题】:update label in opentelemetry prometheus exporter 【发布时间】:2021-04-30 22:33:08 【问题描述】:

在为 opentelemetry prometheus 导出器运行指标示例时,得到了预期的结果

    prometheus metrics:
    # HELP ex_com_one A ValueObserver set to 1.0
    # TYPE ex_com_one histogram
    ex_com_one_bucketex_com_lemons="13",le="+Inf" 1
    ex_com_one_sumex_com_lemons="13" 1
    ex_com_one_countex_com_lemons="13" 1
    # HELP ex_com_three 
    # TYPE ex_com_three counter
    ex_com_threeex_com_lemons="13" 22
    ex_com_threeA="1",B="2",C="3",ex_com_lemons="10" 12
    # HELP ex_com_two 
    # TYPE ex_com_two histogram
    ex_com_two_bucketex_com_lemons="13",le="+Inf" 1
    ex_com_two_sumex_com_lemons="13" 2
    ex_com_two_countex_com_lemons="13" 1
    ex_com_two_bucketA="1",B="2",C="3",ex_com_lemons="10",le="+Inf" 1
    ex_com_two_sumA="1",B="2",C="3",ex_com_lemons="10" 2
    ex_com_two_countA="1",B="2",C="3",ex_com_lemons="10" 1

在 []label.KeyValue 中添加了几个虚拟值,所以我得到了指标,但我打算得到 Method 指标中的名称和主机名。所以我添加了一个匿名函数并在变量中分配了返回值。 如下源代码所示。

// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metrics

import (
    "context"
    "fmt"
    "log"
    "net/http"
    "sync"
    "time"

    "go.opentelemetry.io/otel"
    "go.opentelemetry.io/otel/exporters/metric/prometheus"
    "go.opentelemetry.io/otel/label"
    "go.opentelemetry.io/otel/metric"
)

type MyResponseWriter struct 
    rw http.ResponseWriter
    r *http.Request

var (
    lemonsKey = label.Key("ex.com/lemons")
    //commonLabels = [...]label.KeyValue
    
)

func initMeter() 
    exporter, err := prometheus.InstallNewPipeline(prometheus.Config)
    
    if err != nil 
        log.Panicf("failed to initialize prometheus exporter %v", err)
    
    http.HandleFunc("/", exporter.ServeHTTP)
    go func() 
        http.ListenAndServe(":2222", nil)
    ()

    fmt.Println("Prometheus server running on :2222")


func init() 
    initMeter()
    
    meter := otel.Meter("prometheus")

    //meter.
    observerLock := new(sync.RWMutex)
    observerValueToReport := new(float64)
    observerLabelsToReport := new([]label.KeyValue)
    cb := func(_ context.Context, result metric.Float64ObserverResult) 
        (*observerLock).RLock()
        value := *observerValueToReport
        labels := *observerLabelsToReport
        (*observerLock).RUnlock()
        result.Observe(value, labels...)
    
    _ = metric.Must(meter).NewFloat64ValueObserver("mem_usage_per_app", cb,
        metric.WithDescription("CPU"),
    )
    _ = metric.Must(meter).NewFloat64ValueObserver("cpu_usage_per_app", cb,
        metric.WithDescription("Memory"),
    )

    valuerecorder := metric.Must(meter).NewFloat64ValueRecorder("http_request_duration_seconds_bucket")
    counter := metric.Must(meter).NewFloat64Counter("http_request_duration_seconds_count")
    


    commonLabels:=func(rw http.ResponseWriter,r *http.Request)[]label.KeyValue
        return []label.KeyValuelemonsKey.Int(10), label.String("Method","a"), label.String("B", "2"), label.String("C", "3")
    
    
    notSoCommonLabels := []label.KeyValuelemonsKey.Int(13)

    ctx := context.Background()

    (*observerLock).Lock()
    *observerValueToReport = 1.0
    *observerLabelsToReport = commonLabels
    (*observerLock).Unlock()
    
    meter.RecordBatch(
        ctx,
        commonLabels,
        valuerecorder.Measurement(2.0),
        counter.Measurement(12.0),
    )

    time.Sleep(5 * time.Second)

    (*observerLock).Lock()
    *observerValueToReport = 1.0
    *observerLabelsToReport = notSoCommonLabels
    (*observerLock).Unlock()
    meter.RecordBatch(
        ctx,
        notSoCommonLabels,
        valuerecorder.Measurement(2.0),
        counter.Measurement(22.0),
    )

    fmt.Println("Example finished updating, please visit :2222")

    

但出现错误

Cannot use 'commonLabels' (type func(rw http.ResponseWriter, r *http.Request) []label.KeyValue) as type []label.keyValue`

谁能帮我解决这个问题。

【问题讨论】:

你的问题在形成和描述方面很薄弱,兄弟。我认为这是一个语法问题?你能提供 commonLabels 是如何使用的吗? 对不起,我是新来的,有些编队问题。我将 commonLabels 传递给meter.RecordBatch( ctx, commonLabels, valuerecorder.Measurement(2.0), counter.Measurement(12.0), ) 【参考方案1】:

根据你的描述,我认为是语法问题。

试试这个:

commonLabels := []label.KeyValue
    lemonsKey.Int(10), 
    label.String("Method", r.Method),
    label.String("Host", r.URL.Host), 
    label.String("C", "test"),


或者如果您需要请求


commonLabels := func(rw http.ResponseWriter,r *http.Request)[]label.KeyValue
        return []label.KeyValue
        lemonsKey.Int(10), 
        label.String("Method", r.Method),
        label.String("Host", r.URL.Host), 
        label.String("C", "test"),
        
(rw, r)

这将定义并调用一个返回[]label.KeyValue的函子

【讨论】:

如果我不在方法内部声明那么我将如何获取http请求对象来调用方法和主机。 @user3595144,我猜你对 functor/lambda 函数有点不熟悉。为了帮助您解决问题,您应该使用完整的使用代码而不是其中的一部分来编辑您的问题。 @user3595144 回复你。

以上是关于更新 opentelemetry prometheus 导出器中的标签的主要内容,如果未能解决你的问题,请参考以下文章

OpenTelemetry - 云原生的观测技术框架

OpenTelemetry - 云原生的观测技术框架

OpenTelemetry - 云原生的观测技术框架

OpenTelemetry项目中的Observability

OpenTelemetry 实现方案

OpenTelemetry 收集器都有哪些用例?