gpt4 book ai didi

go - 更新opentelemetry普罗米修斯导出商中的标签

转载 作者:行者123 更新时间:2023-12-03 10:10:30 26 4
gpt4 key购买 nike

在运行opentelemetry普罗米修斯导出商的指标示例时,遵循以下预期

    prometheus metrics:
# HELP ex_com_one A ValueObserver set to 1.0
# TYPE ex_com_one histogram
ex_com_one_bucket{ex_com_lemons="13",le="+Inf"} 1
ex_com_one_sum{ex_com_lemons="13"} 1
ex_com_one_count{ex_com_lemons="13"} 1
# HELP ex_com_three
# TYPE ex_com_three counter
ex_com_three{ex_com_lemons="13"} 22
ex_com_three{A="1",B="2",C="3",ex_com_lemons="10"} 12
# HELP ex_com_two
# TYPE ex_com_two histogram
ex_com_two_bucket{ex_com_lemons="13",le="+Inf"} 1
ex_com_two_sum{ex_com_lemons="13"} 2
ex_com_two_count{ex_com_lemons="13"} 1
ex_com_two_bucket{A="1",B="2",C="3",ex_com_lemons="10",le="+Inf"} 1
ex_com_two_sum{A="1",B="2",C="3",ex_com_lemons="10"} 2
ex_com_two_count{A="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.KeyValue{lemonsKey.Int(10), label.String("Method","a"), label.String("B", "2"), label.String("C", "3")}
}

notSoCommonLabels := []label.KeyValue{lemonsKey.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 := []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

关于go - 更新opentelemetry普罗米修斯导出商中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65913251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com