@@ -12,14 +12,13 @@ import (
1212
1313 "go.opentelemetry.io/otel/attribute"
1414 "go.opentelemetry.io/otel/metric"
15- otelunit "go.opentelemetry.io/otel/metric/unit"
1615 "golang.org/x/exp/event"
1716)
1817
1918// MetricHandler is an event.Handler for OpenTelemetry metrics.
2019// Its Event method handles Metric events and ignores all others.
2120type MetricHandler struct {
22- meter metric.MeterMust
21+ meter metric.Meter
2322 mu sync.Mutex
2423 // A map from event.Metrics to, effectively, otel Meters.
2524 // But since the only thing we need from the Meter is recording a value, we
@@ -34,7 +33,7 @@ var _ event.Handler = (*MetricHandler)(nil)
3433// NewMetricHandler creates a new MetricHandler.
3534func NewMetricHandler (m metric.Meter ) * MetricHandler {
3635 return & MetricHandler {
37- meter : metric . Must ( m ) ,
36+ meter : m ,
3837 recordFuncs : map [event.Metric ]recordFunc {},
3938 }
4039}
@@ -68,39 +67,56 @@ func (m *MetricHandler) getRecordFunc(em event.Metric) recordFunc {
6867 if f , ok := m .recordFuncs [em ]; ok {
6968 return f
7069 }
71- f := m .newRecordFunc (em )
70+ f , err := m .newRecordFunc (em )
71+ if err != nil {
72+ panic (err )
73+ }
7274 m .recordFuncs [em ] = f
7375 return f
7476}
7577
76- func (m * MetricHandler ) newRecordFunc (em event.Metric ) recordFunc {
78+ func (m * MetricHandler ) newRecordFunc (em event.Metric ) ( recordFunc , error ) {
7779 opts := em .Options ()
7880 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- }
8381 switch em .(type ) {
8482 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 )),
8886 }
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
8994
9095 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
94101 }
102+ return func (ctx context.Context , l event.Label , attrs []attribute.KeyValue ) {
103+ g .Add (ctx , l .Float64 (), metric .WithAttributes (attrs ... ))
104+ }, nil
95105
96106 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
100112 }
101113
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+
102118 default :
103- return nil
119+ return nil , nil
104120 }
105121}
106122
0 commit comments