2015-02-02 17:14:36 +03:00
|
|
|
// Copyright 2014 The Prometheus Authors
|
2014-05-07 22:08:33 +04:00
|
|
|
// 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.
|
|
|
|
|
2015-02-02 17:14:36 +03:00
|
|
|
// Copyright (c) 2013, The Prometheus Authors
|
2013-01-19 17:48:30 +04:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
2013-02-12 05:36:06 +04:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be found
|
|
|
|
// in the LICENSE file.
|
2013-01-19 17:48:30 +04:00
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
package prometheus
|
2013-01-19 17:48:30 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-04-02 15:31:22 +04:00
|
|
|
"encoding/binary"
|
2013-01-19 17:48:30 +04:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
2013-06-27 20:46:16 +04:00
|
|
|
|
2015-02-27 18:12:59 +03:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
2013-01-19 17:48:30 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type fakeResponseWriter struct {
|
|
|
|
header http.Header
|
|
|
|
body bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *fakeResponseWriter) Header() http.Header {
|
|
|
|
return r.header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *fakeResponseWriter) Write(d []byte) (l int, err error) {
|
|
|
|
return r.body.Write(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *fakeResponseWriter) WriteHeader(c int) {
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
func testHandler(t testing.TB) {
|
2014-04-02 15:31:22 +04:00
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
metricVec := NewCounterVec(
|
|
|
|
CounterOpts{
|
|
|
|
Name: "name",
|
|
|
|
Help: "docstring",
|
|
|
|
ConstLabels: Labels{"constname": "constvalue"},
|
|
|
|
},
|
|
|
|
[]string{"labelname"},
|
|
|
|
)
|
|
|
|
|
|
|
|
metricVec.WithLabelValues("val1").Inc()
|
|
|
|
metricVec.WithLabelValues("val2").Inc()
|
2014-04-02 15:31:22 +04:00
|
|
|
|
|
|
|
varintBuf := make([]byte, binary.MaxVarintLen32)
|
|
|
|
|
|
|
|
externalMetricFamily := []*dto.MetricFamily{
|
2014-04-14 20:45:16 +04:00
|
|
|
{
|
2014-04-02 15:31:22 +04:00
|
|
|
Name: proto.String("externalname"),
|
|
|
|
Help: proto.String("externaldocstring"),
|
|
|
|
Type: dto.MetricType_COUNTER.Enum(),
|
|
|
|
Metric: []*dto.Metric{
|
2014-04-14 20:45:16 +04:00
|
|
|
{
|
2014-04-02 15:31:22 +04:00
|
|
|
Label: []*dto.LabelPair{
|
2014-04-14 20:45:16 +04:00
|
|
|
{
|
2014-04-02 15:31:22 +04:00
|
|
|
Name: proto.String("externallabelname"),
|
|
|
|
Value: proto.String("externalval1"),
|
|
|
|
},
|
2014-04-14 20:45:16 +04:00
|
|
|
{
|
2014-05-07 22:08:33 +04:00
|
|
|
Name: proto.String("externalconstname"),
|
|
|
|
Value: proto.String("externalconstvalue"),
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Counter: &dto.Counter{
|
|
|
|
Value: proto.Float64(1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
marshaledExternalMetricFamily, err := proto.Marshal(externalMetricFamily[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var externalBuf bytes.Buffer
|
|
|
|
l := binary.PutUvarint(varintBuf, uint64(len(marshaledExternalMetricFamily)))
|
|
|
|
_, err = externalBuf.Write(varintBuf[:l])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = externalBuf.Write(marshaledExternalMetricFamily)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
externalMetricFamilyAsBytes := externalBuf.Bytes()
|
2014-04-23 15:40:37 +04:00
|
|
|
externalMetricFamilyAsText := []byte(`# HELP externalname externaldocstring
|
|
|
|
# TYPE externalname counter
|
2014-05-07 22:08:33 +04:00
|
|
|
externalname{externallabelname="externalval1",externalconstname="externalconstvalue"} 1
|
2014-04-23 15:40:37 +04:00
|
|
|
`)
|
|
|
|
externalMetricFamilyAsProtoText := []byte(`name: "externalname"
|
|
|
|
help: "externaldocstring"
|
|
|
|
type: COUNTER
|
|
|
|
metric: <
|
|
|
|
label: <
|
|
|
|
name: "externallabelname"
|
|
|
|
value: "externalval1"
|
|
|
|
>
|
|
|
|
label: <
|
2014-05-07 22:08:33 +04:00
|
|
|
name: "externalconstname"
|
|
|
|
value: "externalconstvalue"
|
2014-04-23 15:40:37 +04:00
|
|
|
>
|
|
|
|
counter: <
|
|
|
|
value: 1
|
|
|
|
>
|
|
|
|
>
|
|
|
|
|
|
|
|
`)
|
2014-05-07 22:08:33 +04:00
|
|
|
externalMetricFamilyAsProtoCompactText := []byte(`name:"externalname" help:"externaldocstring" type:COUNTER metric:<label:<name:"externallabelname" value:"externalval1" > label:<name:"externalconstname" value:"externalconstvalue" > counter:<value:1 > >
|
2014-04-23 15:40:37 +04:00
|
|
|
`)
|
2014-04-02 15:31:22 +04:00
|
|
|
|
|
|
|
expectedMetricFamily := &dto.MetricFamily{
|
|
|
|
Name: proto.String("name"),
|
|
|
|
Help: proto.String("docstring"),
|
|
|
|
Type: dto.MetricType_COUNTER.Enum(),
|
|
|
|
Metric: []*dto.Metric{
|
2014-04-14 20:45:16 +04:00
|
|
|
{
|
2014-04-02 15:31:22 +04:00
|
|
|
Label: []*dto.LabelPair{
|
2014-04-14 20:45:16 +04:00
|
|
|
{
|
2014-05-07 22:08:33 +04:00
|
|
|
Name: proto.String("constname"),
|
|
|
|
Value: proto.String("constvalue"),
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
2014-04-14 20:45:16 +04:00
|
|
|
{
|
2014-05-07 22:08:33 +04:00
|
|
|
Name: proto.String("labelname"),
|
|
|
|
Value: proto.String("val1"),
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Counter: &dto.Counter{
|
|
|
|
Value: proto.Float64(1),
|
|
|
|
},
|
|
|
|
},
|
2014-04-14 20:45:16 +04:00
|
|
|
{
|
2014-04-02 15:31:22 +04:00
|
|
|
Label: []*dto.LabelPair{
|
2014-04-14 20:45:16 +04:00
|
|
|
{
|
2014-05-07 22:08:33 +04:00
|
|
|
Name: proto.String("constname"),
|
|
|
|
Value: proto.String("constvalue"),
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
2014-04-14 20:45:16 +04:00
|
|
|
{
|
2014-05-07 22:08:33 +04:00
|
|
|
Name: proto.String("labelname"),
|
|
|
|
Value: proto.String("val2"),
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Counter: &dto.Counter{
|
|
|
|
Value: proto.Float64(1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
marshaledExpectedMetricFamily, err := proto.Marshal(expectedMetricFamily)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
l = binary.PutUvarint(varintBuf, uint64(len(marshaledExpectedMetricFamily)))
|
|
|
|
_, err = buf.Write(varintBuf[:l])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = buf.Write(marshaledExpectedMetricFamily)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expectedMetricFamilyAsBytes := buf.Bytes()
|
2014-04-23 15:40:37 +04:00
|
|
|
expectedMetricFamilyAsText := []byte(`# HELP name docstring
|
|
|
|
# TYPE name counter
|
2014-05-07 22:08:33 +04:00
|
|
|
name{constname="constvalue",labelname="val1"} 1
|
|
|
|
name{constname="constvalue",labelname="val2"} 1
|
2014-04-23 15:40:37 +04:00
|
|
|
`)
|
|
|
|
expectedMetricFamilyAsProtoText := []byte(`name: "name"
|
|
|
|
help: "docstring"
|
|
|
|
type: COUNTER
|
|
|
|
metric: <
|
|
|
|
label: <
|
2014-05-07 22:08:33 +04:00
|
|
|
name: "constname"
|
|
|
|
value: "constvalue"
|
2014-04-23 15:40:37 +04:00
|
|
|
>
|
|
|
|
label: <
|
2014-05-07 22:08:33 +04:00
|
|
|
name: "labelname"
|
|
|
|
value: "val1"
|
2014-04-23 15:40:37 +04:00
|
|
|
>
|
|
|
|
counter: <
|
|
|
|
value: 1
|
|
|
|
>
|
|
|
|
>
|
|
|
|
metric: <
|
|
|
|
label: <
|
2014-05-07 22:08:33 +04:00
|
|
|
name: "constname"
|
|
|
|
value: "constvalue"
|
2014-04-23 15:40:37 +04:00
|
|
|
>
|
|
|
|
label: <
|
2014-05-07 22:08:33 +04:00
|
|
|
name: "labelname"
|
|
|
|
value: "val2"
|
2014-04-23 15:40:37 +04:00
|
|
|
>
|
|
|
|
counter: <
|
|
|
|
value: 1
|
|
|
|
>
|
|
|
|
>
|
|
|
|
|
|
|
|
`)
|
2014-05-07 22:08:33 +04:00
|
|
|
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 > >
|
2014-04-23 15:40:37 +04:00
|
|
|
`)
|
2014-04-02 15:31:22 +04:00
|
|
|
|
|
|
|
type output struct {
|
|
|
|
headers map[string]string
|
|
|
|
body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
var scenarios = []struct {
|
|
|
|
headers map[string]string
|
|
|
|
out output
|
|
|
|
withCounter bool
|
|
|
|
withExternalMF bool
|
|
|
|
}{
|
2014-04-23 15:40:37 +04:00
|
|
|
{ // 0
|
2014-04-02 15:31:22 +04:00
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "foo/bar;q=0.2, dings/bums;q=0.8",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
2014-05-07 22:08:33 +04:00
|
|
|
"Content-Type": `text/plain; version=0.0.4`,
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
2014-05-07 22:08:33 +04:00
|
|
|
body: []byte{},
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-04-23 15:40:37 +04:00
|
|
|
{ // 1
|
2014-04-02 15:31:22 +04:00
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "foo/bar;q=0.2, application/quark;q=0.8",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
2014-05-07 22:08:33 +04:00
|
|
|
"Content-Type": `text/plain; version=0.0.4`,
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
2014-05-07 22:08:33 +04:00
|
|
|
body: []byte{},
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-04-23 15:40:37 +04:00
|
|
|
{ // 2
|
2014-04-02 15:31:22 +04:00
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "foo/bar;q=0.2, application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=bla;q=0.8",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
2014-05-07 22:08:33 +04:00
|
|
|
"Content-Type": `text/plain; version=0.0.4`,
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
2014-05-07 22:08:33 +04:00
|
|
|
body: []byte{},
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-04-23 15:40:37 +04:00
|
|
|
{ // 3
|
2014-04-02 15:31:22 +04:00
|
|
|
headers: map[string]string{
|
2014-04-23 15:40:37 +04:00
|
|
|
"Accept": "text/plain;q=0.2, application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.8",
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
2014-07-15 17:34:52 +04:00
|
|
|
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
body: []byte{},
|
|
|
|
},
|
|
|
|
},
|
2014-04-23 15:40:37 +04:00
|
|
|
{ // 4
|
2014-04-02 15:31:22 +04:00
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "application/json",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
2014-05-07 22:08:33 +04:00
|
|
|
"Content-Type": `text/plain; version=0.0.4`,
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
2014-05-07 22:08:33 +04:00
|
|
|
body: expectedMetricFamilyAsText,
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
withCounter: true,
|
|
|
|
},
|
2014-04-23 15:40:37 +04:00
|
|
|
{ // 5
|
2014-04-02 15:31:22 +04:00
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
2014-07-15 17:34:52 +04:00
|
|
|
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
body: expectedMetricFamilyAsBytes,
|
|
|
|
},
|
|
|
|
withCounter: true,
|
|
|
|
},
|
2014-04-23 15:40:37 +04:00
|
|
|
{ // 6
|
2014-04-02 15:31:22 +04:00
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "application/json",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
2014-05-07 22:08:33 +04:00
|
|
|
"Content-Type": `text/plain; version=0.0.4`,
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
2014-05-07 22:08:33 +04:00
|
|
|
body: externalMetricFamilyAsText,
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
withExternalMF: true,
|
|
|
|
},
|
2014-04-23 15:40:37 +04:00
|
|
|
{ // 7
|
2014-04-02 15:31:22 +04:00
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
2014-07-15 17:34:52 +04:00
|
|
|
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
body: externalMetricFamilyAsBytes,
|
|
|
|
},
|
|
|
|
withExternalMF: true,
|
|
|
|
},
|
2014-04-23 15:40:37 +04:00
|
|
|
{ // 8
|
2014-04-02 15:31:22 +04:00
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
2014-07-15 17:34:52 +04:00
|
|
|
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
body: bytes.Join(
|
|
|
|
[][]byte{
|
|
|
|
externalMetricFamilyAsBytes,
|
2014-05-07 22:08:33 +04:00
|
|
|
expectedMetricFamilyAsBytes,
|
2014-04-02 15:31:22 +04:00
|
|
|
},
|
|
|
|
[]byte{},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
withCounter: true,
|
|
|
|
withExternalMF: true,
|
|
|
|
},
|
2014-04-23 15:40:37 +04:00
|
|
|
{ // 9
|
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "text/plain",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
|
|
|
"Content-Type": `text/plain; version=0.0.4`,
|
|
|
|
},
|
|
|
|
body: []byte{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // 10
|
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=bla;q=0.2, text/plain;q=0.5",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
|
|
|
"Content-Type": `text/plain; version=0.0.4`,
|
|
|
|
},
|
|
|
|
body: expectedMetricFamilyAsText,
|
|
|
|
},
|
|
|
|
withCounter: true,
|
|
|
|
},
|
|
|
|
{ // 11
|
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=bla;q=0.2, text/plain;q=0.5;version=0.0.4",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
|
|
|
"Content-Type": `text/plain; version=0.0.4`,
|
|
|
|
},
|
|
|
|
body: bytes.Join(
|
|
|
|
[][]byte{
|
|
|
|
externalMetricFamilyAsText,
|
2014-05-07 22:08:33 +04:00
|
|
|
expectedMetricFamilyAsText,
|
2014-04-23 15:40:37 +04:00
|
|
|
},
|
|
|
|
[]byte{},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
withCounter: true,
|
|
|
|
withExternalMF: true,
|
|
|
|
},
|
|
|
|
{ // 12
|
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.2, text/plain;q=0.5;version=0.0.2",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
2014-07-15 17:34:52 +04:00
|
|
|
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
|
2014-04-23 15:40:37 +04:00
|
|
|
},
|
|
|
|
body: bytes.Join(
|
|
|
|
[][]byte{
|
|
|
|
externalMetricFamilyAsBytes,
|
2014-05-07 22:08:33 +04:00
|
|
|
expectedMetricFamilyAsBytes,
|
2014-04-23 15:40:37 +04:00
|
|
|
},
|
|
|
|
[]byte{},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
withCounter: true,
|
|
|
|
withExternalMF: true,
|
|
|
|
},
|
|
|
|
{ // 13
|
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=text;q=0.5, application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.4",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
2014-07-15 17:34:52 +04:00
|
|
|
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=text`,
|
2014-04-23 15:40:37 +04:00
|
|
|
},
|
|
|
|
body: bytes.Join(
|
|
|
|
[][]byte{
|
|
|
|
externalMetricFamilyAsProtoText,
|
2014-05-07 22:08:33 +04:00
|
|
|
expectedMetricFamilyAsProtoText,
|
2014-04-23 15:40:37 +04:00
|
|
|
},
|
|
|
|
[]byte{},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
withCounter: true,
|
|
|
|
withExternalMF: true,
|
|
|
|
},
|
|
|
|
{ // 14
|
|
|
|
headers: map[string]string{
|
|
|
|
"Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=compact-text",
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
2014-07-15 17:34:52 +04:00
|
|
|
"Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=compact-text`,
|
2014-04-23 15:40:37 +04:00
|
|
|
},
|
|
|
|
body: bytes.Join(
|
|
|
|
[][]byte{
|
|
|
|
externalMetricFamilyAsProtoCompactText,
|
2014-05-07 22:08:33 +04:00
|
|
|
expectedMetricFamilyAsProtoCompactText,
|
2014-04-23 15:40:37 +04:00
|
|
|
},
|
|
|
|
[]byte{},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
withCounter: true,
|
|
|
|
withExternalMF: true,
|
|
|
|
},
|
2014-04-02 15:31:22 +04:00
|
|
|
}
|
|
|
|
for i, scenario := range scenarios {
|
2014-05-07 22:08:33 +04:00
|
|
|
registry := newRegistry()
|
|
|
|
registry.collectChecksEnabled = true
|
|
|
|
|
2014-04-02 15:31:22 +04:00
|
|
|
if scenario.withCounter {
|
2014-05-07 22:08:33 +04:00
|
|
|
registry.Register(metricVec)
|
2014-04-02 15:31:22 +04:00
|
|
|
}
|
|
|
|
if scenario.withExternalMF {
|
2014-05-07 22:08:33 +04:00
|
|
|
registry.metricFamilyInjectionHook = func() []*dto.MetricFamily {
|
|
|
|
return externalMetricFamily
|
|
|
|
}
|
2014-04-02 15:31:22 +04:00
|
|
|
}
|
|
|
|
writer := &fakeResponseWriter{
|
|
|
|
header: http.Header{},
|
|
|
|
}
|
2014-05-07 22:08:33 +04:00
|
|
|
handler := InstrumentHandler("prometheus", registry)
|
2014-04-02 15:31:22 +04:00
|
|
|
request, _ := http.NewRequest("GET", "/", nil)
|
|
|
|
for key, value := range scenario.headers {
|
|
|
|
request.Header.Add(key, value)
|
|
|
|
}
|
|
|
|
handler(writer, request)
|
|
|
|
|
|
|
|
for key, value := range scenario.out.headers {
|
|
|
|
if writer.Header().Get(key) != value {
|
|
|
|
t.Errorf(
|
|
|
|
"%d. expected %q for header %q, got %q",
|
|
|
|
i, value, key, writer.Header().Get(key),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(scenario.out.body, writer.body.Bytes()) {
|
|
|
|
t.Errorf(
|
|
|
|
"%d. expected %q for body, got %q",
|
|
|
|
i, scenario.out.body, writer.body.Bytes(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
func TestHandler(t *testing.T) {
|
2014-04-02 15:31:22 +04:00
|
|
|
testHandler(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHandler(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testHandler(b)
|
|
|
|
}
|
|
|
|
}
|