-
Notifications
You must be signed in to change notification settings - Fork 73
/
metrics.go
97 lines (82 loc) · 3.16 KB
/
metrics.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2022 Authors of spidernet-io
// SPDX-License-Identifier: Apache-2.0
package metrics
import (
"fmt"
"net/http"
"time"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
selector "go.opentelemetry.io/otel/sdk/metric/selector/simple"
)
// meter is a global creator of metric instruments.
var meter metric.Meter
// InitMetricController will set up meter with the input param(required) and create a prometheus exporter.
// returns http handler and error
func InitMetricController(meterName string) (http.Handler, error) {
if len(meterName) == 0 {
return nil, fmt.Errorf("Failed to init metric controller, meter name is asked to be set!")
}
config := prometheus.Config{
DefaultHistogramBoundaries: []float64{1, 5, 7, 10, 15, 20, 30},
}
c := controller.New(
processor.NewFactory(
selector.NewWithHistogramDistribution(
histogram.WithExplicitBoundaries(config.DefaultHistogramBoundaries)),
aggregation.CumulativeTemporalitySelector(),
processor.WithMemory(true),
),
)
exporter, err := prometheus.New(config, c)
if nil != err {
return nil, err
}
global.SetMeterProvider(exporter.MeterProvider())
m := global.Meter(meterName)
meter = m
return exporter, nil
}
// NewMetricInt64Counter will create otel Int64Counter metric.
// The first param metricName is required and the second param is optional.
func NewMetricInt64Counter(metricName string, description string) (syncint64.Counter, error) {
if len(metricName) == 0 {
return nil, fmt.Errorf("Failed to create metric Int64Counter, metric name is asked to be set.")
}
return meter.SyncInt64().Counter(metricName, instrument.WithDescription(description))
}
// NewMetricFloat64Histogram will create otel Float64Histogram metric.
// The first param metricName is required and the second param is optional.
func NewMetricFloat64Histogram(metricName string, description string) (syncfloat64.Histogram, error) {
if len(metricName) == 0 {
return nil, fmt.Errorf("Failed to create metric Float64Histogram, metric name is asked to be set.")
}
return meter.SyncFloat64().Histogram(metricName, instrument.WithDescription(description))
}
var _ TimeRecorder = &timeRecorder{}
// timeRecorder owns a field to record start time.
type timeRecorder struct {
startTime time.Time
}
// TimeRecorder will help you to compute time duration.
type TimeRecorder interface {
SinceInSeconds() float64
}
// NewTimeRecorder will create TimeRecorder and record the current time.
func NewTimeRecorder() TimeRecorder {
t := timeRecorder{}
t.startTime = time.Now()
return &t
}
// SinceInSeconds returns the duration of time since the start time as a float64.
func (t *timeRecorder) SinceInSeconds() float64 {
return float64(time.Since(t.startTime)) / 1e9
}