@@ -52,7 +52,7 @@ type collector struct {
52
52
53
53
disableTargetInfo bool
54
54
withoutUnits bool
55
- targetInfo * metricData
55
+ targetInfo prometheus. Metric
56
56
createTargetInfoOnce sync.Once
57
57
}
58
58
@@ -105,74 +105,39 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
105
105
}
106
106
}
107
107
108
- for _ , metricData := range c .getMetricData (metrics ) {
109
- if metricData .valueType == prometheus .UntypedValue {
110
- m , err := prometheus .NewConstHistogram (metricData .description , metricData .histogramCount , metricData .histogramSum , metricData .histogramBuckets , metricData .attributeValues ... )
111
- if err != nil {
112
- otel .Handle (err )
113
- continue
114
- }
115
- ch <- m
116
- } else {
117
- m , err := prometheus .NewConstMetric (metricData .description , metricData .valueType , metricData .value , metricData .attributeValues ... )
118
- if err != nil {
119
- otel .Handle (err )
120
- continue
121
- }
122
- ch <- m
123
- }
124
- }
125
- }
126
-
127
- // metricData holds the metadata as well as values for individual data points.
128
- type metricData struct {
129
- // name should include the unit as a suffix (before _total on counters)
130
- // see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/data-model.md#metric-metadata-1
131
- name string
132
- description * prometheus.Desc
133
- attributeValues []string
134
- valueType prometheus.ValueType
135
- value float64
136
- histogramCount uint64
137
- histogramSum float64
138
- histogramBuckets map [float64 ]uint64
139
- }
140
-
141
- func (c * collector ) getMetricData (metrics metricdata.ResourceMetrics ) []* metricData {
142
- allMetrics := make ([]* metricData , 0 )
143
-
144
108
c .createTargetInfoOnce .Do (func () {
145
109
// Resource should be immutable, we don't need to compute again
146
- c .targetInfo = c .createInfoMetricData (targetInfoMetricName , targetInfoDescription , metrics .Resource )
110
+ targetInfo , err := c .createInfoMetric (targetInfoMetricName , targetInfoDescription , metrics .Resource )
111
+ if err != nil {
112
+ // If the target info metric is invalid, disable sending it.
113
+ otel .Handle (err )
114
+ c .disableTargetInfo = true
115
+ }
116
+ c .targetInfo = targetInfo
147
117
})
148
-
149
- if c .targetInfo != nil {
150
- allMetrics = append (allMetrics , c .targetInfo )
118
+ if ! c .disableTargetInfo {
119
+ ch <- c .targetInfo
151
120
}
152
-
153
121
for _ , scopeMetrics := range metrics .ScopeMetrics {
154
122
for _ , m := range scopeMetrics .Metrics {
155
123
switch v := m .Data .(type ) {
156
124
case metricdata.Histogram :
157
- allMetrics = append ( allMetrics , getHistogramMetricData ( v , m , c .getName (m )) ... )
125
+ addHistogramMetric ( ch , v , m , c .getName (m ))
158
126
case metricdata.Sum [int64 ]:
159
- allMetrics = append ( allMetrics , getSumMetricData ( v , m , c .getName (m )) ... )
127
+ addSumMetric ( ch , v , m , c .getName (m ))
160
128
case metricdata.Sum [float64 ]:
161
- allMetrics = append ( allMetrics , getSumMetricData ( v , m , c .getName (m )) ... )
129
+ addSumMetric ( ch , v , m , c .getName (m ))
162
130
case metricdata.Gauge [int64 ]:
163
- allMetrics = append ( allMetrics , getGaugeMetricData ( v , m , c .getName (m )) ... )
131
+ addGaugeMetric ( ch , v , m , c .getName (m ))
164
132
case metricdata.Gauge [float64 ]:
165
- allMetrics = append ( allMetrics , getGaugeMetricData ( v , m , c .getName (m )) ... )
133
+ addGaugeMetric ( ch , v , m , c .getName (m ))
166
134
}
167
135
}
168
136
}
169
-
170
- return allMetrics
171
137
}
172
138
173
- func getHistogramMetricData ( histogram metricdata.Histogram , m metricdata.Metrics , name string ) [] * metricData {
139
+ func addHistogramMetric ( ch chan <- prometheus. Metric , histogram metricdata.Histogram , m metricdata.Metrics , name string ) {
174
140
// TODO(https://github.com/open-telemetry/opentelemetry-go/issues/3163): support exemplars
175
- dataPoints := make ([]* metricData , 0 , len (histogram .DataPoints ))
176
141
for _ , dp := range histogram .DataPoints {
177
142
keys , values := getAttrs (dp .Attributes )
178
143
desc := prometheus .NewDesc (name , m .Description , keys , nil )
@@ -183,60 +148,47 @@ func getHistogramMetricData(histogram metricdata.Histogram, m metricdata.Metrics
183
148
cumulativeCount += dp .BucketCounts [i ]
184
149
buckets [bound ] = cumulativeCount
185
150
}
186
- md := & metricData {
187
- name : m .Name ,
188
- description : desc ,
189
- attributeValues : values ,
190
- valueType : prometheus .UntypedValue ,
191
- histogramCount : dp .Count ,
192
- histogramSum : dp .Sum ,
193
- histogramBuckets : buckets ,
151
+ m , err := prometheus .NewConstHistogram (desc , dp .Count , dp .Sum , buckets , values ... )
152
+ if err != nil {
153
+ otel .Handle (err )
154
+ continue
194
155
}
195
- dataPoints = append ( dataPoints , md )
156
+ ch <- m
196
157
}
197
- return dataPoints
198
158
}
199
159
200
- func getSumMetricData [N int64 | float64 ](sum metricdata.Sum [N ], m metricdata.Metrics , name string ) [] * metricData {
160
+ func addSumMetric [N int64 | float64 ](ch chan <- prometheus. Metric , sum metricdata.Sum [N ], m metricdata.Metrics , name string ) {
201
161
valueType := prometheus .CounterValue
202
162
if ! sum .IsMonotonic {
203
163
valueType = prometheus .GaugeValue
204
164
}
205
- dataPoints := make ([]* metricData , 0 , len (sum .DataPoints ))
206
165
for _ , dp := range sum .DataPoints {
207
166
if sum .IsMonotonic {
208
167
// Add _total suffix for counters
209
168
name += counterSuffix
210
169
}
211
170
keys , values := getAttrs (dp .Attributes )
212
171
desc := prometheus .NewDesc (name , m .Description , keys , nil )
213
- md := & metricData {
214
- name : m .Name ,
215
- description : desc ,
216
- attributeValues : values ,
217
- valueType : valueType ,
218
- value : float64 (dp .Value ),
172
+ m , err := prometheus .NewConstMetric (desc , valueType , float64 (dp .Value ), values ... )
173
+ if err != nil {
174
+ otel .Handle (err )
175
+ continue
219
176
}
220
- dataPoints = append ( dataPoints , md )
177
+ ch <- m
221
178
}
222
- return dataPoints
223
179
}
224
180
225
- func getGaugeMetricData [N int64 | float64 ](gauge metricdata.Gauge [N ], m metricdata.Metrics , name string ) []* metricData {
226
- dataPoints := make ([]* metricData , 0 , len (gauge .DataPoints ))
181
+ func addGaugeMetric [N int64 | float64 ](ch chan <- prometheus.Metric , gauge metricdata.Gauge [N ], m metricdata.Metrics , name string ) {
227
182
for _ , dp := range gauge .DataPoints {
228
183
keys , values := getAttrs (dp .Attributes )
229
184
desc := prometheus .NewDesc (name , m .Description , keys , nil )
230
- md := & metricData {
231
- name : m .Name ,
232
- description : desc ,
233
- attributeValues : values ,
234
- valueType : prometheus .GaugeValue ,
235
- value : float64 (dp .Value ),
185
+ m , err := prometheus .NewConstMetric (desc , prometheus .GaugeValue , float64 (dp .Value ), values ... )
186
+ if err != nil {
187
+ otel .Handle (err )
188
+ continue
236
189
}
237
- dataPoints = append ( dataPoints , md )
190
+ ch <- m
238
191
}
239
- return dataPoints
240
192
}
241
193
242
194
// getAttrs parses the attribute.Set to two lists of matching Prometheus-style
@@ -268,21 +220,10 @@ func getAttrs(attrs attribute.Set) ([]string, []string) {
268
220
return keys , values
269
221
}
270
222
271
- func (c * collector ) createInfoMetricData (name , description string , res * resource.Resource ) * metricData {
272
- if c .disableTargetInfo {
273
- return nil
274
- }
275
-
223
+ func (c * collector ) createInfoMetric (name , description string , res * resource.Resource ) (prometheus.Metric , error ) {
276
224
keys , values := getAttrs (* res .Set ())
277
-
278
225
desc := prometheus .NewDesc (name , description , keys , nil )
279
- return & metricData {
280
- name : name ,
281
- description : desc ,
282
- attributeValues : values ,
283
- valueType : prometheus .GaugeValue ,
284
- value : float64 (1 ),
285
- }
226
+ return prometheus .NewConstMetric (desc , prometheus .GaugeValue , float64 (1 ), values ... )
286
227
}
287
228
288
229
func sanitizeRune (r rune ) rune {
0 commit comments