// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
packageexpfmt
import(
"errors"
"io"
"net/http"
"reflect"
"sort"
"strings"
"testing"
"github.com/prometheus/common/model"
)
funcTestTextDecoder(t*testing.T){
var(
ts=model.Now()
in=`
# Only a quite simple scenario with two metric families.
# More complicated tests of the parser itself can be found in the text package.
# TYPE mf2 counter
mf2 3
mf1{label="value1"} -3.14 123456
mf1{label="value2"} 42
mf2 4
`
out=model.Vector{
&model.Sample{
Metric:model.Metric{
model.MetricNameLabel:"mf1",
"label":"value1",
},
Value:-3.14,
Timestamp:123456,
},
&model.Sample{
Metric:model.Metric{
model.MetricNameLabel:"mf1",
"label":"value2",
},
Value:42,
Timestamp:ts,
},
&model.Sample{
Metric:model.Metric{
model.MetricNameLabel:"mf2",
},
Value:3,
Timestamp:ts,
},
&model.Sample{
Metric:model.Metric{
model.MetricNameLabel:"mf2",
},
Value:4,
Timestamp:ts,
},
}
)
dec:=&SampleDecoder{
Dec:&textDecoder{r:strings.NewReader(in)},
Opts:&DecodeOptions{
Timestamp:ts,
},
}
varallmodel.Vector
for{
varsmplsmodel.Vector
err:=dec.Decode(&smpls)
iferr==io.EOF{
break
}
iferr!=nil{
t.Fatal(err)
}
all=append(all,smpls...)
}
sort.Sort(all)
sort.Sort(out)
if!reflect.DeepEqual(all,out){
t.Fatalf("output does not match")
}
}
funcTestProtoDecoder(t*testing.T){
vartestTime=model.Now()
scenarios:=[]struct{
instring
expectedmodel.Vector
}{
{
in:"",
},
{
in:"\x8f\x01\n\rrequest_count\x12\x12Number of requests\x18\x00\"0\n#\n\x0fsome_label_name\x12\x10some_label_value\x1a\t\t\x00\x00\x00\x00\x00\x00E\xc0\"6\n)\n\x12another_label_name\x12\x13another_label_value\x1a\t\t\x00\x00\x00\x00\x00\x00U@",
expected:model.Vector{
&model.Sample{
Metric:model.Metric{
model.MetricNameLabel:"request_count",
"some_label_name":"some_label_value",
},
Value:-42,
Timestamp:testTime,
},
&model.Sample{
Metric:model.Metric{
model.MetricNameLabel:"request_count",
"another_label_name":"another_label_value",
},
Value:84,
Timestamp:testTime,
},
},
},
{
in:"\xb9\x01\n\rrequest_count\x12\x12Number of requests\x18\x02\"O\n#\n\x0fsome_label_name\x12\x10some_label_value\"(\x1a\x12\t\xaeG\xe1z\x14\xae\xef?\x11\x00\x00\x00\x00\x00\x00E\xc0\x1a\x12\t+\x87\x16\xd9\xce\xf7\xef?\x11\x00\x00\x00\x00\x00\x00U\xc0\"A\n)\n\x12another_label_name\x12\x13another_label_value\"\x14\x1a\x12\t\x00\x00\x00\x00\x00\x00\xe0?\x11\x00\x00\x00\x00\x00\x00$@",
# HELP prometheus_local_storage_checkpoint_duration_milliseconds The duration (in milliseconds) it took to checkpoint in-memory metrics and head chunks.
# TYPE prometheus_local_storage_checkpoint_duration_milliseconds gauge
# HELP prometheus_local_storage_invalid_preload_requests_total The total number of preload requests referring to a non-existent series. This is an indication of outdated label indexes.
# TYPE prometheus_local_storage_invalid_preload_requests_total counter
# HELP prometheus_rule_evaluation_failures_total The total number of rule evaluation failures.
# TYPE prometheus_rule_evaluation_failures_total counter
prometheus_rule_evaluation_failures_total 0
# HELP prometheus_samples_queue_capacity Capacity of the queue for unwritten samples.
# TYPE prometheus_samples_queue_capacity gauge
prometheus_samples_queue_capacity 4096
# HELP prometheus_samples_queue_length Current number of items in the queue for unwritten samples. Each item comprises all samples exposed by one target as one metric family (i.e. metrics of the same name).
# TYPE prometheus_samples_queue_length gauge
prometheus_samples_queue_length 0
# HELP prometheus_target_interval_length_seconds Actual intervals between scrapes.
# TYPE prometheus_target_interval_length_seconds summary