-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
65 lines (58 loc) · 1.67 KB
/
prometheus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package server
import (
"github.com/hopeio/cherry/context/httpctx"
"github.com/hopeio/cherry/utils/log"
prometheus1 "github.com/hopeio/cherry/utils/net/http/prometheus"
prometheus2 "github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
"time"
)
/*func init() {
sink, _ := prometheus.NewPrometheusSink()
conf := metrics.DefaultConfig(initialize.GlobalConfig.Module)
metrics1, _ := metrics.New(conf, sink)
metrics1.EnableHostnameLabel = true
http.Handle("/metrics", promhttp.Handler())
}*/
var (
meter = otel.Meter("service-meter")
apiCounter metric.Int64Counter
histogram metric.Float64Histogram
)
func init() {
var err error
apiCounter, err = meter.Int64Counter(
"api.counter",
metric.WithDescription("Number of API calls."),
metric.WithUnit("{call}"),
)
if err != nil {
log.Fatal(err)
}
histogram, err = meter.Float64Histogram(
"task.duration",
metric.WithDescription("The duration of task execution."),
metric.WithUnit("s"),
)
if err != nil {
log.Fatal(err)
}
}
type MetricsRecord = func(ctxi *httpctx.Context, uri, method string, code int)
var defaultMetricsRecord = func(ctxi *httpctx.Context, uri, method string, code int) {
labels := prometheus2.Labels{
"method": method,
"uri": uri,
}
t := time.Now().Sub(ctxi.RequestAt.Time)
prometheus1.AccessCounter.With(labels).Add(1)
prometheus1.QueueGauge.With(labels).Set(1)
prometheus1.HttpDurationsHistogram.With(labels).Observe(float64(t) / 1000)
prometheus1.HttpDurations.With(labels).Observe(float64(t) / 1000)
}
func SetMetricsRecord(metricsRecord MetricsRecord) {
if metricsRecord != nil {
defaultMetricsRecord = metricsRecord
}
}