@@ -16,6 +16,7 @@ package extraction
16
16
import (
17
17
"fmt"
18
18
"io"
19
+ "math"
19
20
20
21
dto "github.com/prometheus/client_model/go"
21
22
@@ -85,24 +86,23 @@ func extractCounter(out Ingester, o *ProcessOptions, f *dto.MetricFamily) error
85
86
continue
86
87
}
87
88
88
- sample := new (model.Sample )
89
+ sample := & model.Sample {
90
+ Metric : model.Metric {},
91
+ Value : model .SampleValue (m .Counter .GetValue ()),
92
+ }
89
93
samples = append (samples , sample )
90
94
91
95
if m .TimestampMs != nil {
92
96
sample .Timestamp = model .TimestampFromUnixNano (* m .TimestampMs * 1000000 )
93
97
} else {
94
98
sample .Timestamp = o .Timestamp
95
99
}
96
- sample .Metric = model.Metric {}
97
- metric := sample .Metric
98
100
101
+ metric := sample .Metric
99
102
for _ , p := range m .Label {
100
103
metric [model .LabelName (p .GetName ())] = model .LabelValue (p .GetValue ())
101
104
}
102
-
103
105
metric [model .MetricNameLabel ] = model .LabelValue (f .GetName ())
104
-
105
- sample .Value = model .SampleValue (m .Counter .GetValue ())
106
106
}
107
107
108
108
return out .Ingest (samples )
@@ -116,24 +116,23 @@ func extractGauge(out Ingester, o *ProcessOptions, f *dto.MetricFamily) error {
116
116
continue
117
117
}
118
118
119
- sample := new (model.Sample )
119
+ sample := & model.Sample {
120
+ Metric : model.Metric {},
121
+ Value : model .SampleValue (m .Gauge .GetValue ()),
122
+ }
120
123
samples = append (samples , sample )
121
124
122
125
if m .TimestampMs != nil {
123
126
sample .Timestamp = model .TimestampFromUnixNano (* m .TimestampMs * 1000000 )
124
127
} else {
125
128
sample .Timestamp = o .Timestamp
126
129
}
127
- sample .Metric = model.Metric {}
128
- metric := sample .Metric
129
130
131
+ metric := sample .Metric
130
132
for _ , p := range m .Label {
131
133
metric [model .LabelName (p .GetName ())] = model .LabelValue (p .GetValue ())
132
134
}
133
-
134
135
metric [model .MetricNameLabel ] = model .LabelValue (f .GetName ())
135
-
136
- sample .Value = model .SampleValue (m .Gauge .GetValue ())
137
136
}
138
137
139
138
return out .Ingest (samples )
@@ -153,48 +152,50 @@ func extractSummary(out Ingester, o *ProcessOptions, f *dto.MetricFamily) error
153
152
}
154
153
155
154
for _ , q := range m .Summary .Quantile {
156
- sample := new (model.Sample )
155
+ sample := & model.Sample {
156
+ Metric : model.Metric {},
157
+ Value : model .SampleValue (q .GetValue ()),
158
+ Timestamp : timestamp ,
159
+ }
157
160
samples = append (samples , sample )
158
161
159
- sample .Timestamp = timestamp
160
- sample .Metric = model.Metric {}
161
162
metric := sample .Metric
162
-
163
163
for _ , p := range m .Label {
164
164
metric [model .LabelName (p .GetName ())] = model .LabelValue (p .GetValue ())
165
165
}
166
166
// BUG(matt): Update other names to "quantile".
167
- metric [model .LabelName ("quantile" )] = model .LabelValue (fmt .Sprint (q .GetQuantile ()))
168
-
167
+ metric [model .LabelName (model .QuantileLabel )] = model .LabelValue (fmt .Sprint (q .GetQuantile ()))
169
168
metric [model .MetricNameLabel ] = model .LabelValue (f .GetName ())
170
-
171
- sample .Value = model .SampleValue (q .GetValue ())
172
169
}
173
170
174
171
if m .Summary .SampleSum != nil {
175
- sum := new (model.Sample )
176
- sum .Timestamp = timestamp
177
- metric := model.Metric {}
172
+ sum := & model.Sample {
173
+ Metric : model.Metric {},
174
+ Value : model .SampleValue (m .Summary .GetSampleSum ()),
175
+ Timestamp : timestamp ,
176
+ }
177
+ samples = append (samples , sum )
178
+
179
+ metric := sum .Metric
178
180
for _ , p := range m .Label {
179
181
metric [model .LabelName (p .GetName ())] = model .LabelValue (p .GetValue ())
180
182
}
181
183
metric [model .MetricNameLabel ] = model .LabelValue (f .GetName () + "_sum" )
182
- sum .Metric = metric
183
- sum .Value = model .SampleValue (m .Summary .GetSampleSum ())
184
- samples = append (samples , sum )
185
184
}
186
185
187
186
if m .Summary .SampleCount != nil {
188
- count := new (model.Sample )
189
- count .Timestamp = timestamp
190
- metric := model.Metric {}
187
+ count := & model.Sample {
188
+ Metric : model.Metric {},
189
+ Value : model .SampleValue (m .Summary .GetSampleCount ()),
190
+ Timestamp : timestamp ,
191
+ }
192
+ samples = append (samples , count )
193
+
194
+ metric := count .Metric
191
195
for _ , p := range m .Label {
192
196
metric [model .LabelName (p .GetName ())] = model .LabelValue (p .GetValue ())
193
197
}
194
198
metric [model .MetricNameLabel ] = model .LabelValue (f .GetName () + "_count" )
195
- count .Metric = metric
196
- count .Value = model .SampleValue (m .Summary .GetSampleCount ())
197
- samples = append (samples , count )
198
199
}
199
200
}
200
201
@@ -209,24 +210,23 @@ func extractUntyped(out Ingester, o *ProcessOptions, f *dto.MetricFamily) error
209
210
continue
210
211
}
211
212
212
- sample := new (model.Sample )
213
+ sample := & model.Sample {
214
+ Metric : model.Metric {},
215
+ Value : model .SampleValue (m .Untyped .GetValue ()),
216
+ }
213
217
samples = append (samples , sample )
214
218
215
219
if m .TimestampMs != nil {
216
220
sample .Timestamp = model .TimestampFromUnixNano (* m .TimestampMs * 1000000 )
217
221
} else {
218
222
sample .Timestamp = o .Timestamp
219
223
}
220
- sample .Metric = model.Metric {}
221
- metric := sample .Metric
222
224
225
+ metric := sample .Metric
223
226
for _ , p := range m .Label {
224
227
metric [model .LabelName (p .GetName ())] = model .LabelValue (p .GetValue ())
225
228
}
226
-
227
229
metric [model .MetricNameLabel ] = model .LabelValue (f .GetName ())
228
-
229
- sample .Value = model .SampleValue (m .Untyped .GetValue ())
230
230
}
231
231
232
232
return out .Ingest (samples )
@@ -245,49 +245,72 @@ func extractHistogram(out Ingester, o *ProcessOptions, f *dto.MetricFamily) erro
245
245
timestamp = model .TimestampFromUnixNano (* m .TimestampMs * 1000000 )
246
246
}
247
247
248
+ infSeen := false
249
+
248
250
for _ , q := range m .Histogram .Bucket {
249
- sample := new (model.Sample )
251
+ sample := & model.Sample {
252
+ Metric : model.Metric {},
253
+ Value : model .SampleValue (q .GetCumulativeCount ()),
254
+ Timestamp : timestamp ,
255
+ }
250
256
samples = append (samples , sample )
251
257
252
- sample .Timestamp = timestamp
253
- sample .Metric = model.Metric {}
254
258
metric := sample .Metric
255
-
256
259
for _ , p := range m .Label {
257
260
metric [model .LabelName (p .GetName ())] = model .LabelValue (p .GetValue ())
258
261
}
259
- metric [model .LabelName ("le" )] = model .LabelValue (fmt .Sprint (q .GetUpperBound ()))
260
-
262
+ metric [model .LabelName (model .BucketLabel )] = model .LabelValue (fmt .Sprint (q .GetUpperBound ()))
261
263
metric [model .MetricNameLabel ] = model .LabelValue (f .GetName () + "_bucket" )
262
264
263
- sample .Value = model .SampleValue (q .GetCumulativeCount ())
265
+ if math .IsInf (q .GetUpperBound (), + 1 ) {
266
+ infSeen = true
267
+ }
264
268
}
265
- // TODO: If +Inf bucket is missing, add it.
266
269
267
270
if m .Histogram .SampleSum != nil {
268
- sum := new (model.Sample )
269
- sum .Timestamp = timestamp
270
- metric := model.Metric {}
271
+ sum := & model.Sample {
272
+ Metric : model.Metric {},
273
+ Value : model .SampleValue (m .Histogram .GetSampleSum ()),
274
+ Timestamp : timestamp ,
275
+ }
276
+ samples = append (samples , sum )
277
+
278
+ metric := sum .Metric
271
279
for _ , p := range m .Label {
272
280
metric [model .LabelName (p .GetName ())] = model .LabelValue (p .GetValue ())
273
281
}
274
282
metric [model .MetricNameLabel ] = model .LabelValue (f .GetName () + "_sum" )
275
- sum .Metric = metric
276
- sum .Value = model .SampleValue (m .Histogram .GetSampleSum ())
277
- samples = append (samples , sum )
278
283
}
279
284
280
285
if m .Histogram .SampleCount != nil {
281
- count := new (model.Sample )
282
- count .Timestamp = timestamp
283
- metric := model.Metric {}
286
+ count := & model.Sample {
287
+ Metric : model.Metric {},
288
+ Value : model .SampleValue (m .Histogram .GetSampleCount ()),
289
+ Timestamp : timestamp ,
290
+ }
291
+ samples = append (samples , count )
292
+
293
+ metric := count .Metric
284
294
for _ , p := range m .Label {
285
295
metric [model .LabelName (p .GetName ())] = model .LabelValue (p .GetValue ())
286
296
}
287
297
metric [model .MetricNameLabel ] = model .LabelValue (f .GetName () + "_count" )
288
- count .Metric = metric
289
- count .Value = model .SampleValue (m .Histogram .GetSampleCount ())
290
- samples = append (samples , count )
298
+
299
+ if ! infSeen {
300
+ infBucket := & model.Sample {
301
+ Metric : model.Metric {},
302
+ Value : count .Value ,
303
+ Timestamp : timestamp ,
304
+ }
305
+ samples = append (samples , infBucket )
306
+
307
+ metric := infBucket .Metric
308
+ for _ , p := range m .Label {
309
+ metric [model .LabelName (p .GetName ())] = model .LabelValue (p .GetValue ())
310
+ }
311
+ metric [model .LabelName (model .BucketLabel )] = model .LabelValue ("+Inf" )
312
+ metric [model .MetricNameLabel ] = model .LabelValue (f .GetName () + "_bucket" )
313
+ }
291
314
}
292
315
}
293
316
0 commit comments