@@ -12,14 +12,13 @@ import (
12
12
13
13
"go.opentelemetry.io/otel/attribute"
14
14
"go.opentelemetry.io/otel/metric"
15
- otelunit "go.opentelemetry.io/otel/metric/unit"
16
15
"golang.org/x/exp/event"
17
16
)
18
17
19
18
// MetricHandler is an event.Handler for OpenTelemetry metrics.
20
19
// Its Event method handles Metric events and ignores all others.
21
20
type MetricHandler struct {
22
- meter metric.MeterMust
21
+ meter metric.Meter
23
22
mu sync.Mutex
24
23
// A map from event.Metrics to, effectively, otel Meters.
25
24
// But since the only thing we need from the Meter is recording a value, we
@@ -34,7 +33,7 @@ var _ event.Handler = (*MetricHandler)(nil)
34
33
// NewMetricHandler creates a new MetricHandler.
35
34
func NewMetricHandler (m metric.Meter ) * MetricHandler {
36
35
return & MetricHandler {
37
- meter : metric . Must ( m ) ,
36
+ meter : m ,
38
37
recordFuncs : map [event.Metric ]recordFunc {},
39
38
}
40
39
}
@@ -68,39 +67,56 @@ func (m *MetricHandler) getRecordFunc(em event.Metric) recordFunc {
68
67
if f , ok := m .recordFuncs [em ]; ok {
69
68
return f
70
69
}
71
- f := m .newRecordFunc (em )
70
+ f , err := m .newRecordFunc (em )
71
+ if err != nil {
72
+ panic (err )
73
+ }
72
74
m .recordFuncs [em ] = f
73
75
return f
74
76
}
75
77
76
- func (m * MetricHandler ) newRecordFunc (em event.Metric ) recordFunc {
78
+ func (m * MetricHandler ) newRecordFunc (em event.Metric ) ( recordFunc , error ) {
77
79
opts := em .Options ()
78
80
name := opts .Namespace + "/" + em .Name ()
79
- otelOpts := []metric.InstrumentOption {
80
- metric .WithDescription (opts .Description ),
81
- metric .WithUnit (otelunit .Unit (opts .Unit )), // cast OK: same strings
82
- }
83
81
switch em .(type ) {
84
82
case * event.Counter :
85
- c := m . meter . NewInt64Counter ( name , otelOpts ... )
86
- return func ( ctx context. Context , l event. Label , attrs []attribute. KeyValue ) {
87
- c . Add ( ctx , l . Int64 (), attrs ... )
83
+ opts := []metric. Int64CounterOption {
84
+ metric . WithDescription ( opts . Description ),
85
+ metric . WithUnit ( string ( opts . Unit )),
88
86
}
87
+ c , err := m .meter .Int64Counter (name , opts ... )
88
+ if err != nil {
89
+ return nil , err
90
+ }
91
+ return func (ctx context.Context , l event.Label , attrs []attribute.KeyValue ) {
92
+ c .Add (ctx , l .Int64 (), metric .WithAttributes (attrs ... ))
93
+ }, nil
89
94
90
95
case * event.FloatGauge :
91
- g := m .meter .NewFloat64UpDownCounter (name , otelOpts ... )
92
- return func (ctx context.Context , l event.Label , attrs []attribute.KeyValue ) {
93
- g .Add (ctx , l .Float64 (), attrs ... )
96
+ g , err := m .meter .Float64UpDownCounter (name ,
97
+ metric .WithDescription (opts .Description ),
98
+ metric .WithUnit (string (opts .Unit )))
99
+ if err != nil {
100
+ return nil , err
94
101
}
102
+ return func (ctx context.Context , l event.Label , attrs []attribute.KeyValue ) {
103
+ g .Add (ctx , l .Float64 (), metric .WithAttributes (attrs ... ))
104
+ }, nil
95
105
96
106
case * event.DurationDistribution :
97
- r := m .meter .NewInt64Histogram (name , otelOpts ... )
98
- return func (ctx context.Context , l event.Label , attrs []attribute.KeyValue ) {
99
- r .Record (ctx , l .Duration ().Nanoseconds (), attrs ... )
107
+ r , err := m .meter .Int64Histogram (name ,
108
+ metric .WithDescription (opts .Description ),
109
+ metric .WithUnit (string (opts .Unit )))
110
+ if err != nil {
111
+ return nil , err
100
112
}
101
113
114
+ return func (ctx context.Context , l event.Label , attrs []attribute.KeyValue ) {
115
+ r .Record (ctx , l .Duration ().Nanoseconds (), metric .WithAttributes (attrs ... ))
116
+ }, nil
117
+
102
118
default :
103
- return nil
119
+ return nil , nil
104
120
}
105
121
}
106
122
0 commit comments