@ -30,8 +30,8 @@ import (
dto "github.com/prometheus/prometheus/prompb/io/prometheus/client"
)
func TestProtobufParse ( t * testing . T ) {
tex tMetricFamilies := [ ] string {
func createTestProtoBuf ( t * testing . T ) * bytes . Buffer {
tes tMetricFamilies := [ ] string {
` name : "go_build_info"
help : "Build information about the main Go module."
type : GAUGE
@ -231,7 +231,6 @@ help: "Test float histogram with many buckets removed to keep it manageable in s
type : HISTOGRAM
metric : <
histogram : <
sample_count : 175
sample_count_float : 175.0
sample_sum : 0.0008280461746287094
bucket : <
@ -302,7 +301,6 @@ help: "Like test_float_histogram but as gauge histogram."
type : GAUGE_HISTOGRAM
metric : <
histogram : <
sample_count : 175
sample_count_float : 175.0
sample_sum : 0.0008280461746287094
bucket : <
@ -450,9 +448,9 @@ metric: <
}
varintBuf := make ( [ ] byte , binary . MaxVarintLen32 )
inputB uf := & bytes . Buffer { }
b uf := & bytes . Buffer { }
for _ , tmf := range tex tMetricFamilies {
for _ , tmf := range tes tMetricFamilies {
pb := & dto . MetricFamily { }
// From text to proto message.
require . NoError ( t , proto . UnmarshalText ( tmf , pb ) )
@ -462,11 +460,15 @@ metric: <
// Write first length, then binary protobuf.
varintLength := binary . PutUvarint ( varintBuf , uint64 ( len ( protoBuf ) ) )
inputB uf. Write ( varintBuf [ : varintLength ] )
inputB uf. Write ( protoBuf )
b uf. Write ( varintBuf [ : varintLength ] )
b uf. Write ( protoBuf )
}
exp := [ ] struct {
return buf
}
func TestProtobufParse ( t * testing . T ) {
type parseResult struct {
lset labels . Labels
m string
t int64
@ -478,7 +480,19 @@ metric: <
shs * histogram . Histogram
fhs * histogram . FloatHistogram
e [ ] exemplar . Exemplar
}
inputBuf := createTestProtoBuf ( t )
scenarios := [ ] struct {
name string
parser Parser
expected [ ] parseResult
} {
{
name : "ignore classic buckets of native histograms" ,
parser : NewProtobufParser ( inputBuf . Bytes ( ) , false ) ,
expected : [ ] parseResult {
{
m : "go_build_info" ,
help : "Build information about the main Go module." ,
@ -810,12 +824,587 @@ metric: <
"__name__" , "without_quantiles_sum" ,
) ,
} ,
} ,
} ,
{
name : "parse classic and native buckets" ,
parser : NewProtobufParser ( inputBuf . Bytes ( ) , true ) ,
expected : [ ] parseResult {
{ // 0
m : "go_build_info" ,
help : "Build information about the main Go module." ,
} ,
{ // 1
m : "go_build_info" ,
typ : MetricTypeGauge ,
} ,
{ // 2
m : "go_build_info\xFFchecksum\xFF\xFFpath\xFFgithub.com/prometheus/client_golang\xFFversion\xFF(devel)" ,
v : 1 ,
lset : labels . FromStrings (
"__name__" , "go_build_info" ,
"checksum" , "" ,
"path" , "github.com/prometheus/client_golang" ,
"version" , "(devel)" ,
) ,
} ,
{ // 3
m : "go_memstats_alloc_bytes_total" ,
help : "Total number of bytes allocated, even if freed." ,
} ,
{ // 4
m : "go_memstats_alloc_bytes_total" ,
typ : MetricTypeCounter ,
} ,
{ // 5
m : "go_memstats_alloc_bytes_total" ,
v : 1.546544e+06 ,
lset : labels . FromStrings (
"__name__" , "go_memstats_alloc_bytes_total" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "42" ) , Value : 12 , HasTs : true , Ts : 1625851151233 } ,
} ,
} ,
{ // 6
m : "something_untyped" ,
help : "Just to test the untyped type." ,
} ,
{ // 7
m : "something_untyped" ,
typ : MetricTypeUnknown ,
} ,
{ // 8
m : "something_untyped" ,
t : 1234567 ,
v : 42 ,
lset : labels . FromStrings (
"__name__" , "something_untyped" ,
) ,
} ,
{ // 9
m : "test_histogram" ,
help : "Test histogram with many buckets removed to keep it manageable in size." ,
} ,
{ // 10
m : "test_histogram" ,
typ : MetricTypeHistogram ,
} ,
{ // 11
m : "test_histogram" ,
t : 1234568 ,
shs : & histogram . Histogram {
Count : 175 ,
ZeroCount : 2 ,
Sum : 0.0008280461746287094 ,
ZeroThreshold : 2.938735877055719e-39 ,
Schema : 3 ,
PositiveSpans : [ ] histogram . Span {
{ Offset : - 161 , Length : 1 } ,
{ Offset : 8 , Length : 3 } ,
} ,
NegativeSpans : [ ] histogram . Span {
{ Offset : - 162 , Length : 1 } ,
{ Offset : 23 , Length : 4 } ,
} ,
PositiveBuckets : [ ] int64 { 1 , 2 , - 1 , - 1 } ,
NegativeBuckets : [ ] int64 { 1 , 3 , - 2 , - 1 , 1 } ,
} ,
lset : labels . FromStrings (
"__name__" , "test_histogram" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "59727" ) , Value : - 0.00039 , HasTs : true , Ts : 1625851155146 } ,
{ Labels : labels . FromStrings ( "dummyID" , "5617" ) , Value : - 0.00029 , HasTs : false } ,
} ,
} ,
{ // 12
m : "test_histogram_count" ,
t : 1234568 ,
v : 175 ,
lset : labels . FromStrings (
"__name__" , "test_histogram_count" ,
) ,
} ,
{ // 13
m : "test_histogram_sum" ,
t : 1234568 ,
v : 0.0008280461746287094 ,
lset : labels . FromStrings (
"__name__" , "test_histogram_sum" ,
) ,
} ,
{ // 14
m : "test_histogram_bucket\xffle\xff-0.0004899999999999998" ,
t : 1234568 ,
v : 2 ,
lset : labels . FromStrings (
"__name__" , "test_histogram_bucket" ,
"le" , "-0.0004899999999999998" ,
) ,
} ,
{ // 15
m : "test_histogram_bucket\xffle\xff-0.0003899999999999998" ,
t : 1234568 ,
v : 4 ,
lset : labels . FromStrings (
"__name__" , "test_histogram_bucket" ,
"le" , "-0.0003899999999999998" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "59727" ) , Value : - 0.00039 , HasTs : true , Ts : 1625851155146 } ,
} ,
} ,
{ // 16
m : "test_histogram_bucket\xffle\xff-0.0002899999999999998" ,
t : 1234568 ,
v : 16 ,
lset : labels . FromStrings (
"__name__" , "test_histogram_bucket" ,
"le" , "-0.0002899999999999998" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "5617" ) , Value : - 0.00029 , HasTs : false } ,
} ,
} ,
{ // 17
m : "test_histogram_bucket\xffle\xff+Inf" ,
t : 1234568 ,
v : 175 ,
lset : labels . FromStrings (
"__name__" , "test_histogram_bucket" ,
"le" , "+Inf" ,
) ,
} ,
{ // 18
m : "test_gauge_histogram" ,
help : "Like test_histogram but as gauge histogram." ,
} ,
{ // 19
m : "test_gauge_histogram" ,
typ : MetricTypeGaugeHistogram ,
} ,
{ // 20
m : "test_gauge_histogram" ,
t : 1234568 ,
shs : & histogram . Histogram {
CounterResetHint : histogram . GaugeType ,
Count : 175 ,
ZeroCount : 2 ,
Sum : 0.0008280461746287094 ,
ZeroThreshold : 2.938735877055719e-39 ,
Schema : 3 ,
PositiveSpans : [ ] histogram . Span {
{ Offset : - 161 , Length : 1 } ,
{ Offset : 8 , Length : 3 } ,
} ,
NegativeSpans : [ ] histogram . Span {
{ Offset : - 162 , Length : 1 } ,
{ Offset : 23 , Length : 4 } ,
} ,
PositiveBuckets : [ ] int64 { 1 , 2 , - 1 , - 1 } ,
NegativeBuckets : [ ] int64 { 1 , 3 , - 2 , - 1 , 1 } ,
} ,
lset : labels . FromStrings (
"__name__" , "test_gauge_histogram" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "59727" ) , Value : - 0.00039 , HasTs : true , Ts : 1625851155146 } ,
{ Labels : labels . FromStrings ( "dummyID" , "5617" ) , Value : - 0.00029 , HasTs : false } ,
} ,
} ,
{ // 21
m : "test_gauge_histogram_count" ,
t : 1234568 ,
v : 175 ,
lset : labels . FromStrings (
"__name__" , "test_gauge_histogram_count" ,
) ,
} ,
{ // 22
m : "test_gauge_histogram_sum" ,
t : 1234568 ,
v : 0.0008280461746287094 ,
lset : labels . FromStrings (
"__name__" , "test_gauge_histogram_sum" ,
) ,
} ,
{ // 23
m : "test_gauge_histogram_bucket\xffle\xff-0.0004899999999999998" ,
t : 1234568 ,
v : 2 ,
lset : labels . FromStrings (
"__name__" , "test_gauge_histogram_bucket" ,
"le" , "-0.0004899999999999998" ,
) ,
} ,
{ // 24
m : "test_gauge_histogram_bucket\xffle\xff-0.0003899999999999998" ,
t : 1234568 ,
v : 4 ,
lset : labels . FromStrings (
"__name__" , "test_gauge_histogram_bucket" ,
"le" , "-0.0003899999999999998" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "59727" ) , Value : - 0.00039 , HasTs : true , Ts : 1625851155146 } ,
} ,
} ,
{ // 25
m : "test_gauge_histogram_bucket\xffle\xff-0.0002899999999999998" ,
t : 1234568 ,
v : 16 ,
lset : labels . FromStrings (
"__name__" , "test_gauge_histogram_bucket" ,
"le" , "-0.0002899999999999998" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "5617" ) , Value : - 0.00029 , HasTs : false } ,
} ,
} ,
{ // 26
m : "test_gauge_histogram_bucket\xffle\xff+Inf" ,
t : 1234568 ,
v : 175 ,
lset : labels . FromStrings (
"__name__" , "test_gauge_histogram_bucket" ,
"le" , "+Inf" ,
) ,
} ,
{ // 27
m : "test_float_histogram" ,
help : "Test float histogram with many buckets removed to keep it manageable in size." ,
} ,
{ // 28
m : "test_float_histogram" ,
typ : MetricTypeHistogram ,
} ,
{ // 29
m : "test_float_histogram" ,
t : 1234568 ,
fhs : & histogram . FloatHistogram {
Count : 175.0 ,
ZeroCount : 2.0 ,
Sum : 0.0008280461746287094 ,
ZeroThreshold : 2.938735877055719e-39 ,
Schema : 3 ,
PositiveSpans : [ ] histogram . Span {
{ Offset : - 161 , Length : 1 } ,
{ Offset : 8 , Length : 3 } ,
} ,
NegativeSpans : [ ] histogram . Span {
{ Offset : - 162 , Length : 1 } ,
{ Offset : 23 , Length : 4 } ,
} ,
PositiveBuckets : [ ] float64 { 1.0 , 2.0 , - 1.0 , - 1.0 } ,
NegativeBuckets : [ ] float64 { 1.0 , 3.0 , - 2.0 , - 1.0 , 1.0 } ,
} ,
lset : labels . FromStrings (
"__name__" , "test_float_histogram" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "59727" ) , Value : - 0.00039 , HasTs : true , Ts : 1625851155146 } ,
{ Labels : labels . FromStrings ( "dummyID" , "5617" ) , Value : - 0.00029 , HasTs : false } ,
} ,
} ,
{ // 30
m : "test_float_histogram_count" ,
t : 1234568 ,
v : 175 ,
lset : labels . FromStrings (
"__name__" , "test_float_histogram_count" ,
) ,
} ,
{ // 31
m : "test_float_histogram_sum" ,
t : 1234568 ,
v : 0.0008280461746287094 ,
lset : labels . FromStrings (
"__name__" , "test_float_histogram_sum" ,
) ,
} ,
{ // 32
m : "test_float_histogram_bucket\xffle\xff-0.0004899999999999998" ,
t : 1234568 ,
v : 2 ,
lset : labels . FromStrings (
"__name__" , "test_float_histogram_bucket" ,
"le" , "-0.0004899999999999998" ,
) ,
} ,
{ // 33
m : "test_float_histogram_bucket\xffle\xff-0.0003899999999999998" ,
t : 1234568 ,
v : 4 ,
lset : labels . FromStrings (
"__name__" , "test_float_histogram_bucket" ,
"le" , "-0.0003899999999999998" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "59727" ) , Value : - 0.00039 , HasTs : true , Ts : 1625851155146 } ,
} ,
} ,
{ // 34
m : "test_float_histogram_bucket\xffle\xff-0.0002899999999999998" ,
t : 1234568 ,
v : 16 ,
lset : labels . FromStrings (
"__name__" , "test_float_histogram_bucket" ,
"le" , "-0.0002899999999999998" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "5617" ) , Value : - 0.00029 , HasTs : false } ,
} ,
} ,
{ // 35
m : "test_float_histogram_bucket\xffle\xff+Inf" ,
t : 1234568 ,
v : 175 ,
lset : labels . FromStrings (
"__name__" , "test_float_histogram_bucket" ,
"le" , "+Inf" ,
) ,
} ,
{ // 36
m : "test_gauge_float_histogram" ,
help : "Like test_float_histogram but as gauge histogram." ,
} ,
{ // 37
m : "test_gauge_float_histogram" ,
typ : MetricTypeGaugeHistogram ,
} ,
{ // 38
m : "test_gauge_float_histogram" ,
t : 1234568 ,
fhs : & histogram . FloatHistogram {
CounterResetHint : histogram . GaugeType ,
Count : 175.0 ,
ZeroCount : 2.0 ,
Sum : 0.0008280461746287094 ,
ZeroThreshold : 2.938735877055719e-39 ,
Schema : 3 ,
PositiveSpans : [ ] histogram . Span {
{ Offset : - 161 , Length : 1 } ,
{ Offset : 8 , Length : 3 } ,
} ,
NegativeSpans : [ ] histogram . Span {
{ Offset : - 162 , Length : 1 } ,
{ Offset : 23 , Length : 4 } ,
} ,
PositiveBuckets : [ ] float64 { 1.0 , 2.0 , - 1.0 , - 1.0 } ,
NegativeBuckets : [ ] float64 { 1.0 , 3.0 , - 2.0 , - 1.0 , 1.0 } ,
} ,
lset : labels . FromStrings (
"__name__" , "test_gauge_float_histogram" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "59727" ) , Value : - 0.00039 , HasTs : true , Ts : 1625851155146 } ,
{ Labels : labels . FromStrings ( "dummyID" , "5617" ) , Value : - 0.00029 , HasTs : false } ,
} ,
} ,
{ // 39
m : "test_gauge_float_histogram_count" ,
t : 1234568 ,
v : 175 ,
lset : labels . FromStrings (
"__name__" , "test_gauge_float_histogram_count" ,
) ,
} ,
{ // 40
m : "test_gauge_float_histogram_sum" ,
t : 1234568 ,
v : 0.0008280461746287094 ,
lset : labels . FromStrings (
"__name__" , "test_gauge_float_histogram_sum" ,
) ,
} ,
{ // 41
m : "test_gauge_float_histogram_bucket\xffle\xff-0.0004899999999999998" ,
t : 1234568 ,
v : 2 ,
lset : labels . FromStrings (
"__name__" , "test_gauge_float_histogram_bucket" ,
"le" , "-0.0004899999999999998" ,
) ,
} ,
{ // 42
m : "test_gauge_float_histogram_bucket\xffle\xff-0.0003899999999999998" ,
t : 1234568 ,
v : 4 ,
lset : labels . FromStrings (
"__name__" , "test_gauge_float_histogram_bucket" ,
"le" , "-0.0003899999999999998" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "59727" ) , Value : - 0.00039 , HasTs : true , Ts : 1625851155146 } ,
} ,
} ,
{ // 43
m : "test_gauge_float_histogram_bucket\xffle\xff-0.0002899999999999998" ,
t : 1234568 ,
v : 16 ,
lset : labels . FromStrings (
"__name__" , "test_gauge_float_histogram_bucket" ,
"le" , "-0.0002899999999999998" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "5617" ) , Value : - 0.00029 , HasTs : false } ,
} ,
} ,
{ // 44
m : "test_gauge_float_histogram_bucket\xffle\xff+Inf" ,
t : 1234568 ,
v : 175 ,
lset : labels . FromStrings (
"__name__" , "test_gauge_float_histogram_bucket" ,
"le" , "+Inf" ,
) ,
} ,
{ // 45
m : "test_histogram2" ,
help : "Similar histogram as before but now without sparse buckets." ,
} ,
{ // 46
m : "test_histogram2" ,
typ : MetricTypeHistogram ,
} ,
{ // 47
m : "test_histogram2_count" ,
v : 175 ,
lset : labels . FromStrings (
"__name__" , "test_histogram2_count" ,
) ,
} ,
{ // 48
m : "test_histogram2_sum" ,
v : 0.000828 ,
lset : labels . FromStrings (
"__name__" , "test_histogram2_sum" ,
) ,
} ,
{ // 49
m : "test_histogram2_bucket\xffle\xff-0.00048" ,
v : 2 ,
lset : labels . FromStrings (
"__name__" , "test_histogram2_bucket" ,
"le" , "-0.00048" ,
) ,
} ,
{ // 50
m : "test_histogram2_bucket\xffle\xff-0.00038" ,
v : 4 ,
lset : labels . FromStrings (
"__name__" , "test_histogram2_bucket" ,
"le" , "-0.00038" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "59727" ) , Value : - 0.00038 , HasTs : true , Ts : 1625851153146 } ,
} ,
} ,
{ // 51
m : "test_histogram2_bucket\xffle\xff1.0" ,
v : 16 ,
lset : labels . FromStrings (
"__name__" , "test_histogram2_bucket" ,
"le" , "1.0" ,
) ,
e : [ ] exemplar . Exemplar {
{ Labels : labels . FromStrings ( "dummyID" , "5617" ) , Value : - 0.000295 , HasTs : false } ,
} ,
} ,
{ // 52
m : "test_histogram2_bucket\xffle\xff+Inf" ,
v : 175 ,
lset : labels . FromStrings (
"__name__" , "test_histogram2_bucket" ,
"le" , "+Inf" ,
) ,
} ,
{ // 53
m : "rpc_durations_seconds" ,
help : "RPC latency distributions." ,
} ,
{ // 54
m : "rpc_durations_seconds" ,
typ : MetricTypeSummary ,
} ,
{ // 55
m : "rpc_durations_seconds_count\xffservice\xffexponential" ,
v : 262 ,
lset : labels . FromStrings (
"__name__" , "rpc_durations_seconds_count" ,
"service" , "exponential" ,
) ,
} ,
{ // 56
m : "rpc_durations_seconds_sum\xffservice\xffexponential" ,
v : 0.00025551262820703587 ,
lset : labels . FromStrings (
"__name__" , "rpc_durations_seconds_sum" ,
"service" , "exponential" ,
) ,
} ,
{ // 57
m : "rpc_durations_seconds\xffservice\xffexponential\xffquantile\xff0.5" ,
v : 6.442786329648548e-07 ,
lset : labels . FromStrings (
"__name__" , "rpc_durations_seconds" ,
"quantile" , "0.5" ,
"service" , "exponential" ,
) ,
} ,
{ // 58
m : "rpc_durations_seconds\xffservice\xffexponential\xffquantile\xff0.9" ,
v : 1.9435742936658396e-06 ,
lset : labels . FromStrings (
"__name__" , "rpc_durations_seconds" ,
"quantile" , "0.9" ,
"service" , "exponential" ,
) ,
} ,
{ // 59
m : "rpc_durations_seconds\xffservice\xffexponential\xffquantile\xff0.99" ,
v : 4.0471608667037015e-06 ,
lset : labels . FromStrings (
"__name__" , "rpc_durations_seconds" ,
"quantile" , "0.99" ,
"service" , "exponential" ,
) ,
} ,
{ // 60
m : "without_quantiles" ,
help : "A summary without quantiles." ,
} ,
{ // 61
m : "without_quantiles" ,
typ : MetricTypeSummary ,
} ,
{ // 62
m : "without_quantiles_count" ,
v : 42 ,
lset : labels . FromStrings (
"__name__" , "without_quantiles_count" ,
) ,
} ,
{ // 63
m : "without_quantiles_sum" ,
v : 1.234 ,
lset : labels . FromStrings (
"__name__" , "without_quantiles_sum" ,
) ,
} ,
} ,
} ,
}
p := NewProtobufParser ( inputBuf . Bytes ( ) )
i := 0
var res labels . Labels
for _ , scenario := range scenarios {
t . Run ( scenario . name , func ( t * testing . T ) {
var (
i int
res labels . Labels
p = scenario . parser
exp = scenario . expected
)
for {
et , err := p . Next ( )
@ -891,4 +1480,6 @@ metric: <
i ++
}
require . Equal ( t , len ( exp ) , i )
} )
}
}