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 (
|
|
|
|
"context"
|
2021-04-07 01:40:12 +02:00
|
|
|
"fmt"
|
2019-11-26 21:47:15 +02:00
|
|
|
"net/http/httptest"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-03-02 23:54:57 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/metric/prometheus"
|
2020-11-12 17:28:32 +02:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2021-01-14 00:07:44 +02:00
|
|
|
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
|
2020-05-21 10:38:43 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
2019-11-26 21:47:15 +02:00
|
|
|
)
|
|
|
|
|
2021-04-07 01:40:12 +02:00
|
|
|
type expectedMetric struct {
|
|
|
|
kind string
|
|
|
|
name string
|
|
|
|
help string
|
|
|
|
values []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *expectedMetric) lines() []string {
|
|
|
|
ret := []string{
|
|
|
|
fmt.Sprintf("# HELP %s %s", e.name, e.help),
|
|
|
|
fmt.Sprintf("# TYPE %s %s", e.name, e.kind),
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, e.values...)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectCounterWithHelp(name, help, value string) expectedMetric {
|
|
|
|
return expectedMetric{
|
|
|
|
kind: "counter",
|
|
|
|
name: name,
|
|
|
|
help: help,
|
|
|
|
values: []string{value},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectCounter(name, value string) expectedMetric {
|
|
|
|
return expectCounterWithHelp(name, "", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectGauge(name, value string) expectedMetric {
|
|
|
|
return expectedMetric{
|
|
|
|
kind: "gauge",
|
|
|
|
name: name,
|
|
|
|
values: []string{value},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectHistogram(name string, values ...string) expectedMetric {
|
|
|
|
return expectedMetric{
|
|
|
|
kind: "histogram",
|
|
|
|
name: name,
|
|
|
|
values: values,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:47:15 +02:00
|
|
|
func TestPrometheusExporter(t *testing.T) {
|
2020-07-07 21:52:35 +02:00
|
|
|
exporter, err := prometheus.NewExportPipeline(
|
|
|
|
prometheus.Config{
|
|
|
|
DefaultHistogramBoundaries: []float64{-0.5, 1},
|
|
|
|
},
|
2021-01-14 00:07:44 +02:00
|
|
|
controller.WithCollectPeriod(0),
|
2021-02-18 19:59:37 +02:00
|
|
|
controller.WithResource(resource.NewWithAttributes(attribute.String("R", "V"))),
|
2020-07-07 21:52:35 +02:00
|
|
|
)
|
2020-05-19 03:37:41 +02:00
|
|
|
require.NoError(t, err)
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
meter := exporter.MeterProvider().Meter("test")
|
2020-11-12 17:28:32 +02:00
|
|
|
upDownCounter := metric.Must(meter).NewFloat64UpDownCounter("updowncounter")
|
|
|
|
counter := metric.Must(meter).NewFloat64Counter("counter")
|
|
|
|
valuerecorder := metric.Must(meter).NewFloat64ValueRecorder("valuerecorder")
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{
|
|
|
|
attribute.Key("A").String("B"),
|
|
|
|
attribute.Key("C").String("D"),
|
2019-11-26 21:47:15 +02:00
|
|
|
}
|
2020-05-20 19:27:26 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-04-07 01:40:12 +02:00
|
|
|
var expected []expectedMetric
|
2020-05-20 19:27:26 +02:00
|
|
|
|
|
|
|
counter.Add(ctx, 10, labels...)
|
|
|
|
counter.Add(ctx, 5.3, labels...)
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2021-04-07 01:40:12 +02:00
|
|
|
expected = append(expected, expectCounter("counter", `counter{A="B",C="D",R="V"} 15.3`))
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
_ = metric.Must(meter).NewInt64ValueObserver("intobserver", func(_ context.Context, result metric.Int64ObserverResult) {
|
2020-11-09 23:52:05 +02:00
|
|
|
result.Observe(1, labels...)
|
|
|
|
})
|
|
|
|
|
2021-04-07 01:40:12 +02:00
|
|
|
expected = append(expected, expectGauge("intobserver", `intobserver{A="B",C="D",R="V"} 1`))
|
2020-11-09 23:52:05 +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
|
|
|
|
2021-04-07 01:40:12 +02:00
|
|
|
expected = append(expected, expectHistogram("valuerecorder",
|
|
|
|
`valuerecorder_bucket{A="B",C="D",R="V",le="-0.5"} 1`,
|
|
|
|
`valuerecorder_bucket{A="B",C="D",R="V",le="1"} 3`,
|
|
|
|
`valuerecorder_bucket{A="B",C="D",R="V",le="+Inf"} 4`,
|
|
|
|
`valuerecorder_sum{A="B",C="D",R="V"} 19.6`,
|
|
|
|
`valuerecorder_count{A="B",C="D",R="V"} 4`,
|
|
|
|
))
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-11-09 23:52:05 +02:00
|
|
|
upDownCounter.Add(ctx, 10, labels...)
|
|
|
|
upDownCounter.Add(ctx, -3.2, labels...)
|
|
|
|
|
2021-04-07 01:40:12 +02:00
|
|
|
expected = append(expected, expectGauge("updowncounter", `updowncounter{A="B",C="D",R="V"} 6.8`))
|
2020-11-09 23:52:05 +02:00
|
|
|
|
2020-05-20 19:27:26 +02:00
|
|
|
compareExport(t, exporter, expected)
|
2020-07-07 21:52:35 +02:00
|
|
|
compareExport(t, exporter, expected)
|
2020-05-20 19:27:26 +02:00
|
|
|
}
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2021-04-07 01:40:12 +02:00
|
|
|
func compareExport(t *testing.T, exporter *prometheus.Exporter, expected []expectedMetric) {
|
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
|
|
|
|
2021-04-07 01:40:12 +02:00
|
|
|
expectedLines := []string{""}
|
|
|
|
for _, v := range expected {
|
|
|
|
expectedLines = append(expectedLines, v.lines()...)
|
2019-11-26 21:47:15 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 01:40:12 +02:00
|
|
|
sort.Strings(lines)
|
|
|
|
sort.Strings(expectedLines)
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2021-04-07 01:40:12 +02:00
|
|
|
require.Equal(t, expectedLines, lines)
|
2019-11-26 21:47:15 +02:00
|
|
|
}
|
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{},
|
2021-01-14 00:07:44 +02:00
|
|
|
controller.WithCollectPeriod(0),
|
2021-02-15 22:28:37 +02:00
|
|
|
controller.WithResource(resource.Empty()),
|
2020-05-20 19:27:26 +02:00
|
|
|
)
|
2020-05-19 03:37:41 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
meter := exporter.MeterProvider().Meter("test")
|
2020-05-19 03:37:41 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
counter := metric.Must(meter).NewInt64Counter(
|
2020-05-19 03:37:41 +02:00
|
|
|
"a.counter",
|
2020-11-12 17:28:32 +02:00
|
|
|
metric.WithDescription("Counts things"),
|
2020-05-19 03:37:41 +02:00
|
|
|
)
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
counter.Add(ctx, 100, attribute.String("key", "value"))
|
2020-05-19 03:37:41 +02:00
|
|
|
|
2021-04-07 01:40:12 +02:00
|
|
|
compareExport(t, exporter, []expectedMetric{
|
|
|
|
expectCounterWithHelp("a_counter", "Counts things", `a_counter{key="value"} 100`),
|
|
|
|
})
|
2020-05-19 03:37:41 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
counter.Add(ctx, 100, attribute.String("key", "value"))
|
2020-05-19 03:37:41 +02:00
|
|
|
|
2021-04-07 01:40:12 +02:00
|
|
|
compareExport(t, exporter, []expectedMetric{
|
|
|
|
expectCounterWithHelp("a_counter", "Counts things", `a_counter{key="value"} 200`),
|
|
|
|
})
|
2020-05-19 03:37:41 +02:00
|
|
|
}
|