0

在运行 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",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 中添加了几个虚拟值,所以我得到了指标,但我打算在指标中获取方法名称和主机名。所以我添加了一个匿名函数并在变量中分配了返回值。正如您在下面的源代码中看到的。

// 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`

谁能帮我解决这个问题。

4

1 回答 1

2

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

尝试这个 :

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

于 2021-01-27T06:17:33.493 回答