mirror of
https://github.com/golang/go
synced 2024-11-06 05:36:13 -07:00
9dec35b5f8
Rather than building cusom events and driving the exporter, the test now registers a custom exporter and then uses the normal higher level methods to deliver events to it. This means we are testing the actual information that would arise in a user program, and also means we no longer have to expose internal features just for tests. Metrics are not fully converted yet. Change-Id: I63248a480fb1b3e6274dce0c2e1d66904d055978 Reviewed-on: https://go-review.googlesource.com/c/tools/+/222849 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
338 lines
6.4 KiB
Go
338 lines
6.4 KiB
Go
package ocagent_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"golang.org/x/tools/internal/telemetry/event"
|
|
"golang.org/x/tools/internal/telemetry/metric"
|
|
)
|
|
|
|
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: "nil data",
|
|
want: prefix + `null` + suffix,
|
|
run: func(ctx context.Context) {
|
|
exporter.Metric(ctx, nil)
|
|
},
|
|
},
|
|
{
|
|
name: "Int64Data cumulative",
|
|
run: func(ctx context.Context) {
|
|
exporter.Metric(ctx, &metric.Int64Data{
|
|
Info: &metric.Scalar{
|
|
Name: "int",
|
|
Description: "int metric",
|
|
Keys: []*event.Key{keyHello},
|
|
},
|
|
Rows: []int64{
|
|
1,
|
|
2,
|
|
3,
|
|
},
|
|
EndTime: &exporter.start,
|
|
})
|
|
},
|
|
want: prefix + `{
|
|
"metric_descriptor": {
|
|
"name": "int",
|
|
"description": "int metric",
|
|
"type": 4,
|
|
"label_keys": [
|
|
{
|
|
"key": "hello"
|
|
}
|
|
]
|
|
},
|
|
"timeseries": [
|
|
{
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
"points": [
|
|
{
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
"int64Value": 1
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
"points": [
|
|
{
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
"int64Value": 2
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
"points": [
|
|
{
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
"int64Value": 3
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}` + suffix,
|
|
},
|
|
{
|
|
name: "Int64Data gauge",
|
|
run: func(ctx context.Context) {
|
|
exporter.Metric(ctx, &metric.Int64Data{
|
|
Info: &metric.Scalar{
|
|
Name: "int-gauge",
|
|
Description: "int metric gauge",
|
|
Keys: []*event.Key{keyHello},
|
|
},
|
|
IsGauge: true,
|
|
})
|
|
},
|
|
want: prefix + `{
|
|
"metric_descriptor": {
|
|
"name": "int-gauge",
|
|
"description": "int metric gauge",
|
|
"type": 1,
|
|
"label_keys": [
|
|
{
|
|
"key": "hello"
|
|
}
|
|
]
|
|
}
|
|
}` + suffix,
|
|
},
|
|
{
|
|
name: "Float64Data cumulative",
|
|
run: func(ctx context.Context) {
|
|
exporter.Metric(ctx, &metric.Float64Data{
|
|
Info: &metric.Scalar{
|
|
Name: "float",
|
|
Description: "float metric",
|
|
Keys: []*event.Key{keyWorld},
|
|
},
|
|
Rows: []float64{
|
|
1.5,
|
|
4.5,
|
|
},
|
|
EndTime: &exporter.start,
|
|
})
|
|
},
|
|
want: prefix + `{
|
|
"metric_descriptor": {
|
|
"name": "float",
|
|
"description": "float metric",
|
|
"type": 5,
|
|
"label_keys": [
|
|
{
|
|
"key": "world"
|
|
}
|
|
]
|
|
},
|
|
"timeseries": [
|
|
{
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
"points": [
|
|
{
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
"doubleValue": 1.5
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
"points": [
|
|
{
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
"doubleValue": 4.5
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}` + suffix,
|
|
},
|
|
{
|
|
name: "Float64Data gauge",
|
|
run: func(ctx context.Context) {
|
|
exporter.Metric(ctx, &metric.Float64Data{
|
|
Info: &metric.Scalar{
|
|
Name: "float-gauge",
|
|
Description: "float metric gauge",
|
|
Keys: []*event.Key{keyWorld},
|
|
},
|
|
IsGauge: true,
|
|
})
|
|
},
|
|
want: prefix + `{
|
|
"metric_descriptor": {
|
|
"name": "float-gauge",
|
|
"description": "float metric gauge",
|
|
"type": 2,
|
|
"label_keys": [
|
|
{
|
|
"key": "world"
|
|
}
|
|
]
|
|
}
|
|
}` + suffix,
|
|
},
|
|
{
|
|
name: "HistogramInt64",
|
|
run: func(ctx context.Context) {
|
|
exporter.Metric(ctx, &metric.HistogramInt64Data{
|
|
Info: &metric.HistogramInt64{
|
|
Name: "histogram int",
|
|
Description: "histogram int metric",
|
|
Keys: []*event.Key{keyHello},
|
|
Buckets: []int64{
|
|
0, 5, 10,
|
|
},
|
|
},
|
|
Rows: []*metric.HistogramInt64Row{
|
|
{
|
|
Count: 6,
|
|
Sum: 40,
|
|
Values: []int64{
|
|
1,
|
|
2,
|
|
3,
|
|
},
|
|
},
|
|
},
|
|
EndTime: &exporter.start,
|
|
})
|
|
},
|
|
want: prefix + `{
|
|
"metric_descriptor": {
|
|
"name": "histogram int",
|
|
"description": "histogram int metric",
|
|
"type": 6,
|
|
"label_keys": [
|
|
{
|
|
"key": "hello"
|
|
}
|
|
]
|
|
},
|
|
"timeseries": [
|
|
{
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
"points": [
|
|
{
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
"distributionValue": {
|
|
"count": 6,
|
|
"sum": 40,
|
|
"bucket_options": {
|
|
"explicit": {
|
|
"bounds": [
|
|
0,
|
|
5,
|
|
10
|
|
]
|
|
}
|
|
},
|
|
"buckets": [
|
|
{
|
|
"count": 1
|
|
},
|
|
{
|
|
"count": 2
|
|
},
|
|
{
|
|
"count": 3
|
|
}
|
|
]
|
|
}
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}` + suffix,
|
|
},
|
|
{
|
|
name: "HistogramFloat64",
|
|
run: func(ctx context.Context) {
|
|
exporter.Metric(ctx, &metric.HistogramFloat64Data{
|
|
Info: &metric.HistogramFloat64{
|
|
Name: "histogram float",
|
|
Description: "histogram float metric",
|
|
Keys: []*event.Key{keyHello},
|
|
Buckets: []float64{
|
|
0, 5,
|
|
},
|
|
},
|
|
Rows: []*metric.HistogramFloat64Row{
|
|
{
|
|
Count: 3,
|
|
Sum: 10,
|
|
Values: []int64{
|
|
1,
|
|
2,
|
|
},
|
|
},
|
|
},
|
|
EndTime: &exporter.start,
|
|
})
|
|
},
|
|
want: prefix + `{
|
|
"metric_descriptor": {
|
|
"name": "histogram float",
|
|
"description": "histogram float metric",
|
|
"type": 6,
|
|
"label_keys": [
|
|
{
|
|
"key": "hello"
|
|
}
|
|
]
|
|
},
|
|
"timeseries": [
|
|
{
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
"points": [
|
|
{
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
"distributionValue": {
|
|
"count": 3,
|
|
"sum": 10,
|
|
"bucket_options": {
|
|
"explicit": {
|
|
"bounds": [
|
|
0,
|
|
5
|
|
]
|
|
}
|
|
},
|
|
"buckets": [
|
|
{
|
|
"count": 1
|
|
},
|
|
{
|
|
"count": 2
|
|
}
|
|
]
|
|
}
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}` + 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))
|
|
})
|
|
}
|
|
}
|