1
0
mirror of https://github.com/golang/go synced 2024-11-06 13:36:12 -07:00
go/internal/telemetry/export/ocagent/metrics_test.go
Ian Cottrell 326edff2a4 internal/telemetry: switch metrics to use only the public API
This also modifies the test data based on a comment in
https://go-review.googlesource.com/c/tools/+/222849

Change-Id: Ib0db60846566b40408b12f84240b58356065d319
Reviewed-on: https://go-review.googlesource.com/c/tools/+/223928
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2020-03-23 14:44:17 +00:00

141 lines
2.5 KiB
Go

package ocagent_test
import (
"context"
"errors"
"testing"
"golang.org/x/tools/internal/telemetry/event"
)
func TestEncodeMetric(t *testing.T) {
exporter := registerExporter()
const prefix = testNodeStr + `
"metrics":[`
const suffix = `]}`
tests := []struct {
name string
run func(ctx context.Context)
want string
}{
{
name: "HistogramFloat64, HistogramInt64",
run: func(ctx context.Context) {
ctx = event.Label(ctx, keyMethod.Of("godoc.ServeHTTP"))
latencyMs.Record(ctx, 96.58)
//event.Record(ctx, latencyMs.Of(96.58))
ctx = event.Label(ctx, event.Err.Of(errors.New("panic: fatal signal")))
bytesIn.Record(ctx, 97e2)
},
want: prefix + `
{
"metric_descriptor": {
"name": "latency_ms",
"description": "The latency of calls in milliseconds",
"type": 6,
"label_keys": [
{
"key": "method"
},
{
"key": "route"
}
]
},
"timeseries": [
{
"start_timestamp": "1970-01-01T00:00:00Z",
"points": [
{
"timestamp": "1970-01-01T00:00:40Z",
"distributionValue": {
"count": 1,
"sum": 96.58,
"bucket_options": {
"explicit": {
"bounds": [
0,
5,
10,
25,
50
]
}
},
"buckets": [
{},
{},
{},
{},
{}
]
}
}
]
}
]
},
{
"metric_descriptor": {
"name": "latency_ms",
"description": "The latency of calls in milliseconds",
"type": 6,
"label_keys": [
{
"key": "method"
},
{
"key": "route"
}
]
},
"timeseries": [
{
"start_timestamp": "1970-01-01T00:00:00Z",
"points": [
{
"timestamp": "1970-01-01T00:00:40Z",
"distributionValue": {
"count": 1,
"sum": 9700,
"bucket_options": {
"explicit": {
"bounds": [
0,
10,
50,
100,
500,
1000,
2000
]
}
},
"buckets": [
{},
{},
{},
{},
{},
{},
{}
]
}
}
]
}
]
}` + suffix,
},
}
ctx := context.TODO()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.run(ctx)
got := exporter.Output("/v1/metrics")
checkJSON(t, got, []byte(tt.want))
})
}
}