如何解决更新 opentelemetry prometheus 导出器中的标签
在运行 opentelemetry prometheus 导出器的指标示例时得到了预期
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",ex_com_lemons="10",le="+Inf"} 1
ex_com_two_sum{A="1",ex_com_lemons="10"} 2
ex_com_two_count{A="1",ex_com_lemons="10"} 1
在 []label.keyvalue 中添加了一些虚拟值,所以我得到了指标,但我打算得到方法 指标中的名称和主机名。所以我添加了一个匿名函数并在变量中分配了返回值。 正如您在下面的源代码中看到的那样。
// 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",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,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),}
}(rw,r)
这将定义并调用一个返回 []label.KeyValue
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。