2013-04-03 20:33:32 +04:00
|
|
|
// Copyright (c) 2013, Prometheus Team
|
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"
|
2013-04-19 16:11:01 +04:00
|
|
|
"encoding/json"
|
2013-01-19 17:48:30 +04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2013-06-27 20:46:16 +04:00
|
|
|
|
|
|
|
"code.google.com/p/goprotobuf/proto"
|
2014-03-14 04:41:19 +04:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/model"
|
2013-01-19 17:48:30 +04:00
|
|
|
)
|
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
func testRegister(t tester) {
|
2013-01-19 17:48:30 +04:00
|
|
|
var oldState = struct {
|
|
|
|
abortOnMisuse bool
|
|
|
|
debugRegistration bool
|
|
|
|
useAggressiveSanityChecks bool
|
|
|
|
}{
|
2013-06-27 20:46:16 +04:00
|
|
|
abortOnMisuse: *abortOnMisuse,
|
|
|
|
debugRegistration: *debugRegistration,
|
|
|
|
useAggressiveSanityChecks: *useAggressiveSanityChecks,
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
defer func() {
|
2013-06-27 20:46:16 +04:00
|
|
|
abortOnMisuse = &(oldState.abortOnMisuse)
|
|
|
|
debugRegistration = &(oldState.debugRegistration)
|
|
|
|
useAggressiveSanityChecks = &(oldState.useAggressiveSanityChecks)
|
2013-01-19 17:48:30 +04:00
|
|
|
}()
|
|
|
|
|
|
|
|
type input struct {
|
|
|
|
name string
|
|
|
|
baseLabels map[string]string
|
|
|
|
}
|
|
|
|
|
2013-09-11 19:22:00 +04:00
|
|
|
validLabels := map[string]string{"label": "value"}
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
var scenarios = []struct {
|
|
|
|
inputs []input
|
|
|
|
outputs []bool
|
|
|
|
}{
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
inputs: []input{
|
|
|
|
{
|
|
|
|
name: "my_name_without_labels",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
outputs: []bool{
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inputs: []input{
|
|
|
|
{
|
|
|
|
name: "my_name_without_labels",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "another_name_without_labels",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
outputs: []bool{
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inputs: []input{
|
|
|
|
{
|
|
|
|
name: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
outputs: []bool{
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inputs: []input{
|
|
|
|
{
|
|
|
|
name: "valid_name",
|
2014-03-14 04:41:19 +04:00
|
|
|
baseLabels: map[string]string{model.ReservedLabelPrefix + "internal": "illegal_internal_name"},
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
outputs: []bool{
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inputs: []input{
|
|
|
|
{
|
|
|
|
name: "duplicate_names",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "duplicate_names",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
outputs: []bool{
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inputs: []input{
|
|
|
|
{
|
|
|
|
name: "duplicate_names_with_identical_labels",
|
|
|
|
baseLabels: map[string]string{"label": "value"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "duplicate_names_with_identical_labels",
|
|
|
|
baseLabels: map[string]string{"label": "value"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
outputs: []bool{
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
2013-09-11 19:22:00 +04:00
|
|
|
{
|
|
|
|
inputs: []input{
|
|
|
|
{
|
|
|
|
name: "metric_1_with_identical_labels",
|
|
|
|
baseLabels: validLabels,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "metric_2_with_identical_labels",
|
|
|
|
baseLabels: validLabels,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
outputs: []bool{
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
},
|
2013-01-19 17:48:30 +04:00
|
|
|
{
|
|
|
|
inputs: []input{
|
|
|
|
{
|
|
|
|
name: "duplicate_names_with_dissimilar_labels",
|
|
|
|
baseLabels: map[string]string{"label": "foo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "duplicate_names_with_dissimilar_labels",
|
|
|
|
baseLabels: map[string]string{"label": "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
outputs: []bool{
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
|
|
|
if len(scenario.inputs) != len(scenario.outputs) {
|
2013-03-29 00:03:01 +04:00
|
|
|
t.Fatalf("%d. expected scenario output length %d, got %d", i, len(scenario.inputs), len(scenario.outputs))
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
abortOnMisuse = proto.Bool(false)
|
|
|
|
debugRegistration = proto.Bool(false)
|
|
|
|
useAggressiveSanityChecks = proto.Bool(true)
|
2013-01-19 17:48:30 +04:00
|
|
|
|
|
|
|
registry := NewRegistry()
|
|
|
|
|
|
|
|
for j, input := range scenario.inputs {
|
|
|
|
actual := registry.Register(input.name, "", input.baseLabels, nil)
|
|
|
|
if scenario.outputs[j] != (actual == nil) {
|
|
|
|
t.Errorf("%d.%d. expected %s, got %s", i, j, scenario.outputs[j], actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegister(t *testing.T) {
|
|
|
|
testRegister(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRegister(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testRegister(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
}
|
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
func testDecorateWriter(t tester) {
|
2013-01-19 17:48:30 +04:00
|
|
|
type input struct {
|
|
|
|
headers map[string]string
|
|
|
|
body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type output struct {
|
|
|
|
headers map[string]string
|
|
|
|
body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
var scenarios = []struct {
|
|
|
|
in input
|
|
|
|
out output
|
|
|
|
}{
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
headers: map[string]string{
|
|
|
|
"Accept-Encoding": "gzip,deflate,sdch",
|
|
|
|
},
|
|
|
|
body: []byte("Hi, mom!"),
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{
|
|
|
|
"Content-Encoding": "gzip",
|
|
|
|
},
|
|
|
|
body: []byte("\x1f\x8b\b\x00\x00\tn\x88\x00\xff\xf2\xc8\xd4Q\xc8\xcd\xcfU\x04\x04\x00\x00\xff\xff9C&&\b\x00\x00\x00"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
|
|
|
headers: map[string]string{
|
|
|
|
"Accept-Encoding": "foo",
|
|
|
|
},
|
|
|
|
body: []byte("Hi, mom!"),
|
|
|
|
},
|
|
|
|
out: output{
|
|
|
|
headers: map[string]string{},
|
|
|
|
body: []byte("Hi, mom!"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
|
|
|
request, _ := http.NewRequest("GET", "/", nil)
|
|
|
|
|
|
|
|
for key, value := range scenario.in.headers {
|
|
|
|
request.Header.Add(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
baseWriter := &fakeResponseWriter{
|
|
|
|
header: make(http.Header),
|
|
|
|
}
|
|
|
|
|
|
|
|
writer := decorateWriter(request, baseWriter)
|
|
|
|
|
|
|
|
for key, value := range scenario.out.headers {
|
|
|
|
if baseWriter.Header().Get(key) != value {
|
|
|
|
t.Errorf("%d. expected %s for header %s, got %s", i, value, key, baseWriter.Header().Get(key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Write(scenario.in.body)
|
|
|
|
|
|
|
|
if closer, ok := writer.(io.Closer); ok {
|
|
|
|
closer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(scenario.out.body, baseWriter.body.Bytes()) {
|
|
|
|
t.Errorf("%d. expected %s for body, got %s", i, scenario.out.body, baseWriter.body.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecorateWriter(t *testing.T) {
|
|
|
|
testDecorateWriter(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDecorateWriter(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testDecorateWriter(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
func testDumpToWriter(t tester) {
|
2013-01-19 17:48:30 +04:00
|
|
|
type input struct {
|
2013-04-03 20:33:32 +04:00
|
|
|
metrics map[string]Metric
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var scenarios = []struct {
|
|
|
|
in input
|
|
|
|
out []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
out: []byte("[]"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
2013-04-03 20:33:32 +04:00
|
|
|
metrics: map[string]Metric{
|
|
|
|
"foo": NewCounter(),
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
2014-03-14 04:41:19 +04:00
|
|
|
out: []byte(`[{"baseLabels":{"__name__":"foo","label_foo":"foo"},"docstring":"metric foo","metric":{"type":"counter","value":[]}}]`),
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
in: input{
|
2013-04-03 20:33:32 +04:00
|
|
|
metrics: map[string]Metric{
|
|
|
|
"foo": NewCounter(),
|
|
|
|
"bar": NewCounter(),
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
},
|
2014-03-14 04:41:19 +04:00
|
|
|
out: []byte(`[{"baseLabels":{"__name__":"bar","label_bar":"bar"},"docstring":"metric bar","metric":{"type":"counter","value":[]}},{"baseLabels":{"__name__":"foo","label_foo":"foo"},"docstring":"metric foo","metric":{"type":"counter","value":[]}}]`),
|
2013-01-19 17:48:30 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
2013-06-27 20:46:16 +04:00
|
|
|
registry := NewRegistry().(*registry)
|
2013-01-19 17:48:30 +04:00
|
|
|
|
|
|
|
for name, metric := range scenario.in.metrics {
|
|
|
|
err := registry.Register(name, fmt.Sprintf("metric %s", name), map[string]string{fmt.Sprintf("label_%s", name): name}, metric)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%d. encountered error while registering metric %s", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-19 16:11:01 +04:00
|
|
|
actual, err := json.Marshal(registry)
|
2013-01-19 17:48:30 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%d. encountered error while dumping %s", i, err)
|
|
|
|
}
|
|
|
|
|
2013-04-19 16:11:01 +04:00
|
|
|
if !bytes.Equal(scenario.out, actual) {
|
|
|
|
t.Errorf("%d. expected %q for dumping, got %q", i, scenario.out, actual)
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDumpToWriter(t *testing.T) {
|
|
|
|
testDumpToWriter(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDumpToWriter(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testDumpToWriter(b)
|
|
|
|
}
|
|
|
|
}
|