While running the metrics example for the opentelemetry prometheus exporter got following expected
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
Ealier few dummy value was added in []label.KeyValue so i got metrics but my intention to get Method name and host name in metrics.So i added one anonymous function and assigned return value in variable. As you can see below source code.
// 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")
}
But getting error
Cannot use 'commonLabels' (type func(rw http.ResponseWriter, r *http.Request) []label.KeyValue) as type []label.keyValue`
Can anyone please help me to resolve this issue.
Based on your description, I think it's a grammar problem.
Try this:
commonLabels := []label.KeyValue{
lemonsKey.Int(10),
label.String("Method", r.Method),
label.String("Host", r.URL.Host),
label.String("C", "test"),
}
Or if you need request
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)
This will define and invoke an functor which returns []label.KeyValue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.