2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-11-26 21:47:15 +02:00
|
|
|
package prometheus_test
|
|
|
|
|
|
|
|
import (
|
2020-05-19 03:37:41 +02:00
|
|
|
"bytes"
|
2019-11-26 21:47:15 +02:00
|
|
|
"context"
|
2020-05-19 03:37:41 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-11-26 21:47:15 +02:00
|
|
|
"net/http/httptest"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2020-03-19 21:02:46 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
2020-03-02 23:54:57 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/metric/prometheus"
|
2020-05-20 19:27:26 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/controller/pull"
|
2020-05-21 10:38:43 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
2019-11-26 21:47:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPrometheusExporter(t *testing.T) {
|
2020-05-20 19:27:26 +02:00
|
|
|
exporter, err := prometheus.NewExportPipeline(prometheus.Config{
|
2020-05-21 19:29:03 +02:00
|
|
|
DefaultHistogramBoundaries: []float64{-0.5, 1},
|
2020-05-21 10:38:43 +02:00
|
|
|
}, pull.WithResource(resource.New(kv.String("R", "V"))))
|
2020-05-19 03:37:41 +02:00
|
|
|
require.NoError(t, err)
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-05-20 19:27:26 +02:00
|
|
|
meter := exporter.Provider().Meter("test")
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-05-20 19:27:26 +02:00
|
|
|
counter := metric.Must(meter).NewFloat64Counter("counter")
|
|
|
|
valuerecorder := metric.Must(meter).NewFloat64ValueRecorder("valuerecorder")
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
labels := []kv.KeyValue{
|
2020-05-14 01:21:23 +02:00
|
|
|
kv.Key("A").String("B"),
|
|
|
|
kv.Key("C").String("D"),
|
2019-11-26 21:47:15 +02:00
|
|
|
}
|
2020-05-20 19:27:26 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
var expected []string
|
|
|
|
|
|
|
|
counter.Add(ctx, 10, labels...)
|
|
|
|
counter.Add(ctx, 5.3, labels...)
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-05-21 10:38:43 +02:00
|
|
|
expected = append(expected, `counter{A="B",C="D",R="V"} 15.3`)
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-05-20 19:27:26 +02:00
|
|
|
valuerecorder.Record(ctx, -0.6, labels...)
|
|
|
|
valuerecorder.Record(ctx, -0.4, labels...)
|
|
|
|
valuerecorder.Record(ctx, 0.6, labels...)
|
|
|
|
valuerecorder.Record(ctx, 20, labels...)
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-05-21 10:38:43 +02:00
|
|
|
expected = append(expected, `valuerecorder_bucket{A="B",C="D",R="V",le="+Inf"} 4`)
|
|
|
|
expected = append(expected, `valuerecorder_bucket{A="B",C="D",R="V",le="-0.5"} 1`)
|
|
|
|
expected = append(expected, `valuerecorder_bucket{A="B",C="D",R="V",le="1"} 3`)
|
|
|
|
expected = append(expected, `valuerecorder_count{A="B",C="D",R="V"} 4`)
|
|
|
|
expected = append(expected, `valuerecorder_sum{A="B",C="D",R="V"} 19.6`)
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-05-20 19:27:26 +02:00
|
|
|
compareExport(t, exporter, expected)
|
|
|
|
}
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-05-20 19:27:26 +02:00
|
|
|
func compareExport(t *testing.T, exporter *prometheus.Exporter, expected []string) {
|
2019-11-26 21:47:15 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest("GET", "/metrics", nil)
|
|
|
|
exporter.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
output := rec.Body.String()
|
|
|
|
lines := strings.Split(output, "\n")
|
2020-05-20 19:27:26 +02:00
|
|
|
|
2019-11-26 21:47:15 +02:00
|
|
|
var metricsOnly []string
|
|
|
|
for _, line := range lines {
|
|
|
|
if !strings.HasPrefix(line, "#") && line != "" {
|
|
|
|
metricsOnly = append(metricsOnly, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(metricsOnly)
|
|
|
|
sort.Strings(expected)
|
|
|
|
|
|
|
|
require.Equal(t, strings.Join(expected, "\n"), strings.Join(metricsOnly, "\n"))
|
|
|
|
}
|
2020-05-19 03:37:41 +02:00
|
|
|
|
|
|
|
func TestPrometheusStatefulness(t *testing.T) {
|
|
|
|
// Create a meter
|
2020-05-20 19:27:26 +02:00
|
|
|
exporter, err := prometheus.NewExportPipeline(
|
|
|
|
prometheus.Config{},
|
|
|
|
pull.WithCachePeriod(0),
|
|
|
|
)
|
2020-05-19 03:37:41 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-05-20 19:27:26 +02:00
|
|
|
meter := exporter.Provider().Meter("test")
|
2020-05-19 03:37:41 +02:00
|
|
|
|
|
|
|
// GET the HTTP endpoint
|
|
|
|
scrape := func() string {
|
|
|
|
var input bytes.Buffer
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
req, err := http.NewRequest("GET", "/", &input)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
exporter.ServeHTTP(resp, req)
|
|
|
|
data, err := ioutil.ReadAll(resp.Result().Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
counter := metric.Must(meter).NewInt64Counter(
|
|
|
|
"a.counter",
|
|
|
|
metric.WithDescription("Counts things"),
|
|
|
|
)
|
|
|
|
|
|
|
|
counter.Add(ctx, 100, kv.String("key", "value"))
|
|
|
|
|
|
|
|
require.Equal(t, `# HELP a_counter Counts things
|
|
|
|
# TYPE a_counter counter
|
|
|
|
a_counter{key="value"} 100
|
|
|
|
`, scrape())
|
|
|
|
|
|
|
|
counter.Add(ctx, 100, kv.String("key", "value"))
|
|
|
|
|
|
|
|
require.Equal(t, `# HELP a_counter Counts things
|
|
|
|
# TYPE a_counter counter
|
|
|
|
a_counter{key="value"} 200
|
|
|
|
`, scrape())
|
|
|
|
|
|
|
|
}
|