-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
otelmetric_meter_counter_performer.go
65 lines (57 loc) · 1.99 KB
/
otelmetric_meter_counter_performer.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
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package otelmetric
import (
"context"
"go.opentelemetry.io/otel/metric"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gmetric"
)
// localCounterPerformer is an implementer for interface gmetric.CounterPerformer.
type localCounterPerformer struct {
gmetric.MeterOption
gmetric.MetricOption
metric.Float64Counter
constOption metric.MeasurementOption
}
// newCounterPerformer creates and returns a CounterPerformer that truly takes action to implement Counter.
func (l *localMeterPerformer) newCounterPerformer(
meter metric.Meter,
metricName string,
metricOption gmetric.MetricOption,
) (gmetric.CounterPerformer, error) {
var (
options = []metric.Float64CounterOption{
metric.WithDescription(metricOption.Help),
metric.WithUnit(metricOption.Unit),
}
)
counter, err := meter.Float64Counter(metricName, options...)
if err != nil {
return nil, gerror.WrapCodef(
gcode.CodeInternalError,
err,
`create Float64Counter "%s" failed with option: %s`,
metricName, gjson.MustEncodeString(metricOption),
)
}
return &localCounterPerformer{
MetricOption: metricOption,
MeterOption: l.MeterOption,
Float64Counter: counter,
constOption: genConstOptionForMetric(l.MeterOption, metricOption),
}, nil
}
// Inc increments the counter by 1.
func (l *localCounterPerformer) Inc(ctx context.Context, option ...gmetric.Option) {
l.Add(ctx, 1, option...)
}
// Add adds the given value to the counter. It panics if the value is < 0.
func (l *localCounterPerformer) Add(ctx context.Context, increment float64, option ...gmetric.Option) {
l.Float64Counter.Add(ctx, increment, generateAddOptions(l.MeterOption, l.constOption, option...)...)
}