@@ -19,6 +19,7 @@ package opentelemetry
1919
2020import (
2121 "context"
22+ "fmt"
2223 "strings"
2324 "time"
2425
@@ -31,8 +32,6 @@ import (
3132 "go.opentelemetry.io/otel"
3233 "go.opentelemetry.io/otel/attribute"
3334 "go.opentelemetry.io/otel/metric"
34- sdkmetric "go.opentelemetry.io/otel/sdk/metric"
35- "go.opentelemetry.io/otel/sdk/resource"
3635)
3736
3837const defaultExpiryTime = time .Duration (2 * time .Minute )
@@ -41,9 +40,6 @@ const flpMeterName = "flp_meter"
4140type EncodeOtlpMetrics struct {
4241 cfg api.EncodeOtlpMetrics
4342 ctx context.Context
44- res * resource.Resource
45- mp * sdkmetric.MeterProvider
46- meter metric.Meter
4743 metricCommon * encode.MetricsCommonStruct
4844}
4945
@@ -57,12 +53,14 @@ func (e *EncodeOtlpMetrics) Encode(metricRecord config.GenericMap) {
5753 e .metricCommon .MetricCommonEncode (e , metricRecord )
5854}
5955
60- func (e * EncodeOtlpMetrics ) ProcessCounter (m interface {}, labels map [string ]string , value float64 ) error {
61- counter := m .(metric.Float64Counter )
62- // set attributes using the labels
63- attributes := obtainAttributesFromLabels (labels )
64- counter .Add (e .ctx , value , metric .WithAttributes (attributes ... ))
65- return nil
56+ func (e * EncodeOtlpMetrics ) ProcessCounter (m interface {}, name string , labels map [string ]string , value float64 ) error {
57+ if counter , ok := m .(metric.Float64Counter ); ok {
58+ // set attributes using the labels
59+ attributes := obtainAttributesFromLabels (labels )
60+ counter .Add (e .ctx , value , metric .WithAttributes (attributes ... ))
61+ return nil
62+ }
63+ return fmt .Errorf ("wrong Otlp Counter type for %s: %T; expecting Float64Counter" , name , m )
6664}
6765
6866func createKey (name string , keys []string ) string {
@@ -77,30 +75,36 @@ func createKey(name string, keys []string) string {
7775}
7876
7977func (e * EncodeOtlpMetrics ) ProcessGauge (m interface {}, name string , labels map [string ]string , value float64 , lvs []string ) error {
80- obs := m .(Float64Gauge )
81- // set attributes using the labels
82- attributes := obtainAttributesFromLabels (labels )
83- key := createKey (name , lvs )
84- obs .Set (key , value , attributes )
85- return nil
78+ if obs , ok := m .(Float64Gauge ); ok {
79+ // set attributes using the labels
80+ attributes := obtainAttributesFromLabels (labels )
81+ key := createKey (name , lvs )
82+ obs .Set (key , value , attributes )
83+ return nil
84+ }
85+ return fmt .Errorf ("wrong Otlp Gauge type for %s: %T; expecting Float64Gauge" , name , m )
8686}
8787
88- func (e * EncodeOtlpMetrics ) ProcessHist (m interface {}, labels map [string ]string , value float64 ) error {
89- histo := m .(metric.Float64Histogram )
90- // set attributes using the labels
91- attributes := obtainAttributesFromLabels (labels )
92- histo .Record (e .ctx , value , metric .WithAttributes (attributes ... ))
93- return nil
88+ func (e * EncodeOtlpMetrics ) ProcessHist (m interface {}, name string , labels map [string ]string , value float64 ) error {
89+ if histo , ok := m .(metric.Float64Histogram ); ok {
90+ // set attributes using the labels
91+ attributes := obtainAttributesFromLabels (labels )
92+ histo .Record (e .ctx , value , metric .WithAttributes (attributes ... ))
93+ return nil
94+ }
95+ return fmt .Errorf ("wrong Otlp Histogram type for %s: %T; expecting Float64Histogram" , name , m )
9496}
9597
96- func (e * EncodeOtlpMetrics ) ProcessAggHist (m interface {}, labels map [string ]string , values []float64 ) error {
97- histo := m .(metric.Float64Histogram )
98- // set attributes using the labels
99- attributes := obtainAttributesFromLabels (labels )
100- for _ , v := range values {
101- histo .Record (e .ctx , v , metric .WithAttributes (attributes ... ))
98+ func (e * EncodeOtlpMetrics ) ProcessAggHist (m interface {}, name string , labels map [string ]string , values []float64 ) error {
99+ if histo , ok := m .(metric.Float64Histogram ); ok {
100+ // set attributes using the labels
101+ attributes := obtainAttributesFromLabels (labels )
102+ for _ , v := range values {
103+ histo .Record (e .ctx , v , metric .WithAttributes (attributes ... ))
104+ }
105+ return nil
102106 }
103- return nil
107+ return fmt . Errorf ( "wrong Otlp Histogram type for %s: %T; expecting Float64Histogram" , name , m )
104108}
105109
106110func (e * EncodeOtlpMetrics ) GetCacheEntry (entryLabels map [string ]string , _ interface {}) interface {} {
@@ -118,30 +122,28 @@ func NewEncodeOtlpMetrics(opMetrics *operational.Metrics, params config.StagePar
118122 ctx := context .Background ()
119123 res := newResource ()
120124
121- mp , err := NewOtlpMetricsProvider (ctx , params , res )
125+ mp , err := NewOtlpMetricsProvider (ctx , & cfg , res )
122126 if err != nil {
123127 return nil , err
124128 }
125- meter := mp .Meter (
126- flpMeterName ,
127- )
129+ meter := mp .Meter (flpMeterName )
130+ return newEncodeOtlpMetricsWithMeter (ctx , params .Name , opMetrics , & cfg , meter )
131+ }
132+
133+ func newEncodeOtlpMetricsWithMeter (ctx context.Context , stageName string , opMetrics * operational.Metrics , cfg * api.EncodeOtlpMetrics , meter metric.Meter ) (encode.Encoder , error ) {
134+ meterFactory := otel .Meter (flpMeterName )
128135
129136 expiryTime := cfg .ExpiryTime
130137 if expiryTime .Duration == 0 {
131138 expiryTime .Duration = defaultExpiryTime
132139 }
133140
134- meterFactory := otel .Meter (flpMeterName )
135-
136141 w := & EncodeOtlpMetrics {
137- cfg : cfg ,
138- ctx : ctx ,
139- res : res ,
140- mp : mp ,
141- meter : meterFactory ,
142+ cfg : * cfg ,
143+ ctx : ctx ,
142144 }
143145
144- metricCommon := encode .NewMetricsCommonStruct (opMetrics , 0 , params . Name , expiryTime , nil )
146+ metricCommon := encode .NewMetricsCommonStruct (opMetrics , 0 , stageName , expiryTime , nil )
145147 w .metricCommon = metricCommon
146148
147149 for i := range cfg .Metrics {
@@ -172,6 +174,7 @@ func NewEncodeOtlpMetrics(opMetrics *operational.Metrics, params config.StagePar
172174 metricCommon .AddGauge (fullMetricName , gauge , mInfo )
173175 case api .MetricHistogram :
174176 var histo metric.Float64Histogram
177+ var err error
175178 if len (mCfg .Buckets ) == 0 {
176179 histo , err = meter .Float64Histogram (fullMetricName )
177180 } else {
0 commit comments