2019-11-22 16:25:02 -07:00
|
|
|
package ocagent_test
|
2019-10-10 01:40:19 -06:00
|
|
|
|
|
|
|
import (
|
2019-11-27 09:24:29 -07:00
|
|
|
"context"
|
2020-03-17 14:00:16 -06:00
|
|
|
"errors"
|
2019-10-10 01:40:19 -06:00
|
|
|
"testing"
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
"golang.org/x/tools/internal/telemetry/event"
|
2019-10-10 01:40:19 -06:00
|
|
|
)
|
|
|
|
|
2019-11-22 15:51:22 -07:00
|
|
|
func TestEncodeMetric(t *testing.T) {
|
2020-03-10 19:43:28 -06:00
|
|
|
exporter := registerExporter()
|
2019-11-26 22:19:20 -07:00
|
|
|
const prefix = testNodeStr + `
|
|
|
|
"metrics":[`
|
|
|
|
const suffix = `]}`
|
2019-10-10 01:40:19 -06:00
|
|
|
tests := []struct {
|
2019-11-26 22:19:20 -07:00
|
|
|
name string
|
2020-03-10 19:43:28 -06:00
|
|
|
run func(ctx context.Context)
|
2019-11-26 22:19:20 -07:00
|
|
|
want string
|
2019-10-10 01:40:19 -06:00
|
|
|
}{
|
|
|
|
{
|
2020-03-17 14:00:16 -06:00
|
|
|
name: "HistogramFloat64, HistogramInt64",
|
2020-03-10 19:43:28 -06:00
|
|
|
run: func(ctx context.Context) {
|
2020-03-17 14:00:16 -06:00
|
|
|
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)
|
2020-03-10 19:43:28 -06:00
|
|
|
},
|
2020-03-17 14:00:16 -06:00
|
|
|
want: prefix + `
|
|
|
|
{
|
2019-11-22 16:25:02 -07:00
|
|
|
"metric_descriptor": {
|
2020-03-17 14:00:16 -06:00
|
|
|
"name": "latency_ms",
|
|
|
|
"description": "The latency of calls in milliseconds",
|
|
|
|
"type": 6,
|
2019-11-22 16:25:02 -07:00
|
|
|
"label_keys": [
|
|
|
|
{
|
2020-03-17 14:00:16 -06:00
|
|
|
"key": "method"
|
2019-10-10 19:41:28 -06:00
|
|
|
},
|
2019-11-22 16:25:02 -07:00
|
|
|
{
|
2020-03-17 14:00:16 -06:00
|
|
|
"key": "route"
|
2019-11-22 16:25:02 -07:00
|
|
|
}
|
|
|
|
]
|
2019-11-22 15:51:22 -07:00
|
|
|
},
|
2019-11-22 16:25:02 -07:00
|
|
|
"timeseries": [
|
|
|
|
{
|
|
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
|
|
"points": [
|
|
|
|
{
|
2020-03-17 14:00:16 -06:00
|
|
|
"timestamp": "1970-01-01T00:00:40Z",
|
2019-11-22 16:25:02 -07:00
|
|
|
"distributionValue": {
|
2020-03-17 14:00:16 -06:00
|
|
|
"count": 1,
|
|
|
|
"sum": 96.58,
|
2019-11-22 16:25:02 -07:00
|
|
|
"bucket_options": {
|
|
|
|
"explicit": {
|
|
|
|
"bounds": [
|
|
|
|
0,
|
|
|
|
5,
|
2020-03-17 14:00:16 -06:00
|
|
|
10,
|
|
|
|
25,
|
|
|
|
50
|
2019-11-22 16:25:02 -07:00
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"buckets": [
|
2020-03-17 14:00:16 -06:00
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{}
|
2019-11-22 16:25:02 -07:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2019-10-10 19:41:28 -06:00
|
|
|
},
|
2020-03-17 14:00:16 -06:00
|
|
|
{
|
2019-11-22 16:25:02 -07:00
|
|
|
"metric_descriptor": {
|
2020-03-17 14:00:16 -06:00
|
|
|
"name": "latency_ms",
|
|
|
|
"description": "The latency of calls in milliseconds",
|
2019-11-22 16:25:02 -07:00
|
|
|
"type": 6,
|
|
|
|
"label_keys": [
|
|
|
|
{
|
2020-03-17 14:00:16 -06:00
|
|
|
"key": "method"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key": "route"
|
2019-11-22 16:25:02 -07:00
|
|
|
}
|
|
|
|
]
|
2019-10-10 19:41:28 -06:00
|
|
|
},
|
2019-11-22 16:25:02 -07:00
|
|
|
"timeseries": [
|
|
|
|
{
|
|
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
|
|
"points": [
|
|
|
|
{
|
2020-03-17 14:00:16 -06:00
|
|
|
"timestamp": "1970-01-01T00:00:40Z",
|
2019-11-22 16:25:02 -07:00
|
|
|
"distributionValue": {
|
2020-03-17 14:00:16 -06:00
|
|
|
"count": 1,
|
|
|
|
"sum": 9700,
|
2019-11-22 16:25:02 -07:00
|
|
|
"bucket_options": {
|
|
|
|
"explicit": {
|
|
|
|
"bounds": [
|
|
|
|
0,
|
2020-03-17 14:00:16 -06:00
|
|
|
10,
|
|
|
|
50,
|
|
|
|
100,
|
|
|
|
500,
|
|
|
|
1000,
|
|
|
|
2000
|
2019-11-22 16:25:02 -07:00
|
|
|
]
|
|
|
|
}
|
2019-11-22 15:51:22 -07:00
|
|
|
},
|
2019-11-22 16:25:02 -07:00
|
|
|
"buckets": [
|
2020-03-17 14:00:16 -06:00
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{}
|
2019-11-22 16:25:02 -07:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2019-11-26 22:19:20 -07:00
|
|
|
}` + suffix,
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-27 09:24:29 -07:00
|
|
|
ctx := context.TODO()
|
2019-10-10 01:40:19 -06:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-03-10 19:43:28 -06:00
|
|
|
tt.run(ctx)
|
|
|
|
got := exporter.Output("/v1/metrics")
|
2019-11-22 16:25:02 -07:00
|
|
|
checkJSON(t, got, []byte(tt.want))
|
2019-10-10 01:40:19 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|