@@ -61,31 +61,29 @@ func testHandler(t testing.TB) {
61
61
62
62
varintBuf := make ([]byte , binary .MaxVarintLen32 )
63
63
64
- externalMetricFamily := []* dto.MetricFamily {
65
- {
66
- Name : proto .String ("externalname" ),
67
- Help : proto .String ("externaldocstring" ),
68
- Type : dto .MetricType_COUNTER .Enum (),
69
- Metric : []* dto.Metric {
70
- {
71
- Label : []* dto.LabelPair {
72
- {
73
- Name : proto .String ("externallabelname" ),
74
- Value : proto .String ("externalval1" ),
75
- },
76
- {
77
- Name : proto .String ("externalconstname" ),
78
- Value : proto .String ("externalconstvalue" ),
79
- },
64
+ externalMetricFamily := & dto.MetricFamily {
65
+ Name : proto .String ("externalname" ),
66
+ Help : proto .String ("externaldocstring" ),
67
+ Type : dto .MetricType_COUNTER .Enum (),
68
+ Metric : []* dto.Metric {
69
+ {
70
+ Label : []* dto.LabelPair {
71
+ {
72
+ Name : proto .String ("externallabelname" ),
73
+ Value : proto .String ("externalval1" ),
80
74
},
81
- Counter : & dto.Counter {
82
- Value : proto .Float64 (1 ),
75
+ {
76
+ Name : proto .String ("externalconstname" ),
77
+ Value : proto .String ("externalconstvalue" ),
83
78
},
84
79
},
80
+ Counter : & dto.Counter {
81
+ Value : proto .Float64 (1 ),
82
+ },
85
83
},
86
84
},
87
85
}
88
- marshaledExternalMetricFamily , err := proto .Marshal (externalMetricFamily [ 0 ] )
86
+ marshaledExternalMetricFamily , err := proto .Marshal (externalMetricFamily )
89
87
if err != nil {
90
88
t .Fatal (err )
91
89
}
@@ -216,16 +214,42 @@ metric: <
216
214
expectedMetricFamilyAsProtoCompactText := []byte (`name:"name" help:"docstring" type:COUNTER metric:<label:<name:"constname" value:"constvalue" > label:<name:"labelname" value:"val1" > counter:<value:1 > > metric:<label:<name:"constname" value:"constvalue" > label:<name:"labelname" value:"val2" > counter:<value:1 > >
217
215
` )
218
216
217
+ externalMetricFamilyWithSameName := & dto.MetricFamily {
218
+ Name : proto .String ("name" ),
219
+ Help : proto .String ("inconsistent help string does not matter here" ),
220
+ Type : dto .MetricType_COUNTER .Enum (),
221
+ Metric : []* dto.Metric {
222
+ {
223
+ Label : []* dto.LabelPair {
224
+ {
225
+ Name : proto .String ("constname" ),
226
+ Value : proto .String ("constvalue" ),
227
+ },
228
+ {
229
+ Name : proto .String ("labelname" ),
230
+ Value : proto .String ("different_val" ),
231
+ },
232
+ },
233
+ Counter : & dto.Counter {
234
+ Value : proto .Float64 (42 ),
235
+ },
236
+ },
237
+ },
238
+ }
239
+
240
+ expectedMetricFamilyMergedWithExternalAsProtoCompactText := []byte (`name:"name" help:"docstring" type:COUNTER metric:<label:<name:"constname" value:"constvalue" > label:<name:"labelname" value:"different_val" > counter:<value:42 > > metric:<label:<name:"constname" value:"constvalue" > label:<name:"labelname" value:"val1" > counter:<value:1 > > metric:<label:<name:"constname" value:"constvalue" > label:<name:"labelname" value:"val2" > counter:<value:1 > >
241
+ ` )
242
+
219
243
type output struct {
220
244
headers map [string ]string
221
245
body []byte
222
246
}
223
247
224
248
var scenarios = []struct {
225
- headers map [string ]string
226
- out output
227
- withCounter bool
228
- withExternalMF bool
249
+ headers map [string ]string
250
+ out output
251
+ collector Collector
252
+ externalMF [] * dto. MetricFamily
229
253
}{
230
254
{ // 0
231
255
headers : map [string ]string {
@@ -281,7 +305,7 @@ metric: <
281
305
},
282
306
body : expectedMetricFamilyAsText ,
283
307
},
284
- withCounter : true ,
308
+ collector : metricVec ,
285
309
},
286
310
{ // 5
287
311
headers : map [string ]string {
@@ -293,7 +317,7 @@ metric: <
293
317
},
294
318
body : expectedMetricFamilyAsBytes ,
295
319
},
296
- withCounter : true ,
320
+ collector : metricVec ,
297
321
},
298
322
{ // 6
299
323
headers : map [string ]string {
@@ -305,7 +329,7 @@ metric: <
305
329
},
306
330
body : externalMetricFamilyAsText ,
307
331
},
308
- withExternalMF : true ,
332
+ externalMF : [] * dto. MetricFamily { externalMetricFamily } ,
309
333
},
310
334
{ // 7
311
335
headers : map [string ]string {
@@ -317,7 +341,7 @@ metric: <
317
341
},
318
342
body : externalMetricFamilyAsBytes ,
319
343
},
320
- withExternalMF : true ,
344
+ externalMF : [] * dto. MetricFamily { externalMetricFamily } ,
321
345
},
322
346
{ // 8
323
347
headers : map [string ]string {
@@ -335,8 +359,8 @@ metric: <
335
359
[]byte {},
336
360
),
337
361
},
338
- withCounter : true ,
339
- withExternalMF : true ,
362
+ collector : metricVec ,
363
+ externalMF : [] * dto. MetricFamily { externalMetricFamily } ,
340
364
},
341
365
{ // 9
342
366
headers : map [string ]string {
@@ -359,7 +383,7 @@ metric: <
359
383
},
360
384
body : expectedMetricFamilyAsText ,
361
385
},
362
- withCounter : true ,
386
+ collector : metricVec ,
363
387
},
364
388
{ // 11
365
389
headers : map [string ]string {
@@ -377,8 +401,8 @@ metric: <
377
401
[]byte {},
378
402
),
379
403
},
380
- withCounter : true ,
381
- withExternalMF : true ,
404
+ collector : metricVec ,
405
+ externalMF : [] * dto. MetricFamily { externalMetricFamily } ,
382
406
},
383
407
{ // 12
384
408
headers : map [string ]string {
@@ -396,8 +420,8 @@ metric: <
396
420
[]byte {},
397
421
),
398
422
},
399
- withCounter : true ,
400
- withExternalMF : true ,
423
+ collector : metricVec ,
424
+ externalMF : [] * dto. MetricFamily { externalMetricFamily } ,
401
425
},
402
426
{ // 13
403
427
headers : map [string ]string {
@@ -415,8 +439,8 @@ metric: <
415
439
[]byte {},
416
440
),
417
441
},
418
- withCounter : true ,
419
- withExternalMF : true ,
442
+ collector : metricVec ,
443
+ externalMF : [] * dto. MetricFamily { externalMetricFamily } ,
420
444
},
421
445
{ // 14
422
446
headers : map [string ]string {
@@ -434,20 +458,42 @@ metric: <
434
458
[]byte {},
435
459
),
436
460
},
437
- withCounter : true ,
438
- withExternalMF : true ,
461
+ collector : metricVec ,
462
+ externalMF : []* dto.MetricFamily {externalMetricFamily },
463
+ },
464
+ { // 15
465
+ headers : map [string ]string {
466
+ "Accept" : "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=compact-text" ,
467
+ },
468
+ out : output {
469
+ headers : map [string ]string {
470
+ "Content-Type" : `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=compact-text` ,
471
+ },
472
+ body : bytes .Join (
473
+ [][]byte {
474
+ externalMetricFamilyAsProtoCompactText ,
475
+ expectedMetricFamilyMergedWithExternalAsProtoCompactText ,
476
+ },
477
+ []byte {},
478
+ ),
479
+ },
480
+ collector : metricVec ,
481
+ externalMF : []* dto.MetricFamily {
482
+ externalMetricFamily ,
483
+ externalMetricFamilyWithSameName ,
484
+ },
439
485
},
440
486
}
441
487
for i , scenario := range scenarios {
442
488
registry := newRegistry ()
443
489
registry .collectChecksEnabled = true
444
490
445
- if scenario .withCounter {
446
- registry .Register (metricVec )
491
+ if scenario .collector != nil {
492
+ registry .Register (scenario . collector )
447
493
}
448
- if scenario .withExternalMF {
494
+ if scenario .externalMF != nil {
449
495
registry .metricFamilyInjectionHook = func () []* dto.MetricFamily {
450
- return externalMetricFamily
496
+ return scenario . externalMF
451
497
}
452
498
}
453
499
writer := & fakeResponseWriter {
0 commit comments