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"
|
2019-10-10 01:40:19 -06:00
|
|
|
"testing"
|
2019-10-23 18:34:23 -06:00
|
|
|
"time"
|
2019-10-10 01:40:19 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/telemetry"
|
|
|
|
"golang.org/x/tools/internal/telemetry/metric"
|
|
|
|
)
|
|
|
|
|
2019-11-22 15:51:22 -07:00
|
|
|
func TestEncodeMetric(t *testing.T) {
|
2019-11-22 16:25:02 -07:00
|
|
|
end, _ := time.Parse(time.RFC3339Nano, "1970-01-01T00:00:30Z")
|
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
|
|
|
|
data telemetry.MetricData
|
|
|
|
want string
|
2019-10-10 01:40:19 -06:00
|
|
|
}{
|
|
|
|
{
|
2019-11-22 15:51:22 -07:00
|
|
|
name: "nil data",
|
2019-11-26 22:19:20 -07:00
|
|
|
want: prefix + `null` + suffix,
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:51:22 -07:00
|
|
|
name: "Int64Data cumulative",
|
|
|
|
data: &metric.Int64Data{
|
2019-10-10 01:40:19 -06:00
|
|
|
Info: &metric.Scalar{
|
|
|
|
Name: "int",
|
|
|
|
Description: "int metric",
|
|
|
|
Keys: []interface{}{"hello"},
|
|
|
|
},
|
2019-11-22 15:51:22 -07:00
|
|
|
Rows: []int64{
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
2019-11-22 15:51:22 -07:00
|
|
|
EndTime: &end,
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
2019-11-26 22:19:20 -07:00
|
|
|
want: prefix + `{
|
2019-11-22 16:25:02 -07:00
|
|
|
"metric_descriptor": {
|
|
|
|
"name": "int",
|
|
|
|
"description": "int metric",
|
|
|
|
"type": 4,
|
|
|
|
"label_keys": [
|
|
|
|
{
|
|
|
|
"key": "hello"
|
|
|
|
}
|
|
|
|
]
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
2019-11-22 16:25:02 -07:00
|
|
|
"timeseries": [
|
|
|
|
{
|
|
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
|
|
"points": [
|
|
|
|
{
|
|
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
|
|
"int64Value": 1
|
|
|
|
}
|
|
|
|
]
|
2019-10-10 19:41:28 -06:00
|
|
|
},
|
2019-11-22 16:25:02 -07:00
|
|
|
{
|
|
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
|
|
"points": [
|
|
|
|
{
|
|
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
|
|
"int64Value": 2
|
|
|
|
}
|
|
|
|
]
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
2019-11-22 16:25:02 -07:00
|
|
|
{
|
|
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
|
|
"points": [
|
|
|
|
{
|
|
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
|
|
"int64Value": 3
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2019-11-26 22:19:20 -07:00
|
|
|
}` + suffix,
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:51:22 -07:00
|
|
|
name: "Int64Data gauge",
|
|
|
|
data: &metric.Int64Data{
|
2019-10-10 01:40:19 -06:00
|
|
|
Info: &metric.Scalar{
|
2019-11-22 15:51:22 -07:00
|
|
|
Name: "int-gauge",
|
|
|
|
Description: "int metric gauge",
|
|
|
|
Keys: []interface{}{"hello"},
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
2019-11-22 15:51:22 -07:00
|
|
|
IsGauge: true,
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
2019-11-26 22:19:20 -07:00
|
|
|
want: prefix + `{
|
2019-11-22 16:25:02 -07:00
|
|
|
"metric_descriptor": {
|
|
|
|
"name": "int-gauge",
|
|
|
|
"description": "int metric gauge",
|
|
|
|
"type": 1,
|
|
|
|
"label_keys": [
|
|
|
|
{
|
|
|
|
"key": "hello"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2019-11-26 22:19:20 -07:00
|
|
|
}` + suffix,
|
2019-10-10 19:41:28 -06:00
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:51:22 -07:00
|
|
|
name: "Float64Data cumulative",
|
|
|
|
data: &metric.Float64Data{
|
|
|
|
Info: &metric.Scalar{
|
|
|
|
Name: "float",
|
|
|
|
Description: "float metric",
|
|
|
|
Keys: []interface{}{"world"},
|
2019-10-10 19:41:28 -06:00
|
|
|
},
|
2019-11-22 15:51:22 -07:00
|
|
|
Rows: []float64{
|
|
|
|
1.5,
|
|
|
|
4.5,
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
2019-10-23 18:34:23 -06:00
|
|
|
EndTime: &end,
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
2019-11-26 22:19:20 -07:00
|
|
|
want: prefix + `{
|
2019-11-22 16:25:02 -07:00
|
|
|
"metric_descriptor": {
|
|
|
|
"name": "float",
|
|
|
|
"description": "float metric",
|
|
|
|
"type": 5,
|
|
|
|
"label_keys": [
|
|
|
|
{
|
|
|
|
"key": "world"
|
|
|
|
}
|
|
|
|
]
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
2019-11-22 16:25:02 -07:00
|
|
|
"timeseries": [
|
|
|
|
{
|
|
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
|
|
"points": [
|
|
|
|
{
|
|
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
|
|
"doubleValue": 1.5
|
|
|
|
}
|
|
|
|
]
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
2019-11-22 16:25:02 -07:00
|
|
|
{
|
|
|
|
"start_timestamp": "1970-01-01T00:00:00Z",
|
|
|
|
"points": [
|
|
|
|
{
|
|
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
|
|
"doubleValue": 4.5
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2019-11-26 22:19:20 -07:00
|
|
|
}` + suffix,
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:51:22 -07:00
|
|
|
name: "Float64Data gauge",
|
|
|
|
data: &metric.Float64Data{
|
|
|
|
Info: &metric.Scalar{
|
|
|
|
Name: "float-gauge",
|
|
|
|
Description: "float metric gauge",
|
|
|
|
Keys: []interface{}{"world"},
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
2019-11-22 15:51:22 -07:00
|
|
|
IsGauge: true,
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
2019-11-26 22:19:20 -07:00
|
|
|
want: prefix + `{
|
2019-11-22 16:25:02 -07:00
|
|
|
"metric_descriptor": {
|
|
|
|
"name": "float-gauge",
|
|
|
|
"description": "float metric gauge",
|
|
|
|
"type": 2,
|
|
|
|
"label_keys": [
|
|
|
|
{
|
|
|
|
"key": "world"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2019-11-26 22:19:20 -07:00
|
|
|
}` + suffix,
|
2019-10-10 01:40:19 -06:00
|
|
|
},
|
2019-10-10 19:41:28 -06:00
|
|
|
{
|
2019-11-22 15:51:22 -07:00
|
|
|
name: "HistogramInt64",
|
|
|
|
data: &metric.HistogramInt64Data{
|
|
|
|
Info: &metric.HistogramInt64{
|
|
|
|
Name: "histogram int",
|
|
|
|
Description: "histogram int metric",
|
|
|
|
Keys: []interface{}{"hello"},
|
|
|
|
Buckets: []int64{
|
|
|
|
0, 5, 10,
|
|
|
|
},
|
|
|
|
},
|
2019-10-10 19:41:28 -06:00
|
|
|
Rows: []*metric.HistogramInt64Row{
|
|
|
|
{
|
|
|
|
Count: 6,
|
|
|
|
Sum: 40,
|
|
|
|
Values: []int64{
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-10-23 18:34:23 -06:00
|
|
|
EndTime: &end,
|
2019-10-10 19:41:28 -06:00
|
|
|
},
|
2019-11-26 22:19:20 -07:00
|
|
|
want: prefix + `{
|
2019-11-22 16:25:02 -07:00
|
|
|
"metric_descriptor": {
|
|
|
|
"name": "histogram int",
|
|
|
|
"description": "histogram int metric",
|
|
|
|
"type": 6,
|
|
|
|
"label_keys": [
|
|
|
|
{
|
|
|
|
"key": "hello"
|
|
|
|
}
|
|
|
|
]
|
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": [
|
|
|
|
{
|
|
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
|
|
"distributionValue": {
|
|
|
|
"count": 6,
|
|
|
|
"sum": 40,
|
|
|
|
"bucket_options": {
|
|
|
|
"explicit": {
|
|
|
|
"bounds": [
|
|
|
|
0,
|
|
|
|
5,
|
|
|
|
10
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"buckets": [
|
|
|
|
{
|
|
|
|
"count": 1
|
2019-10-10 19:41:28 -06:00
|
|
|
},
|
2019-11-22 16:25:02 -07:00
|
|
|
{
|
|
|
|
"count": 2
|
2019-10-10 19:41:28 -06:00
|
|
|
},
|
2019-11-22 16:25:02 -07:00
|
|
|
{
|
|
|
|
"count": 3
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2019-11-26 22:19:20 -07:00
|
|
|
}` + suffix,
|
2019-10-10 19:41:28 -06:00
|
|
|
},
|
|
|
|
{
|
2019-11-22 15:51:22 -07:00
|
|
|
name: "HistogramFloat64",
|
|
|
|
data: &metric.HistogramFloat64Data{
|
|
|
|
Info: &metric.HistogramFloat64{
|
|
|
|
Name: "histogram float",
|
|
|
|
Description: "histogram float metric",
|
|
|
|
Keys: []interface{}{"hello"},
|
|
|
|
Buckets: []float64{
|
|
|
|
0, 5,
|
|
|
|
},
|
|
|
|
},
|
2019-10-10 19:41:28 -06:00
|
|
|
Rows: []*metric.HistogramFloat64Row{
|
|
|
|
{
|
|
|
|
Count: 3,
|
|
|
|
Sum: 10,
|
|
|
|
Values: []int64{
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-10-23 18:34:23 -06:00
|
|
|
EndTime: &end,
|
2019-10-10 19:41:28 -06:00
|
|
|
},
|
2019-11-26 22:19:20 -07:00
|
|
|
want: prefix + `{
|
2019-11-22 16:25:02 -07:00
|
|
|
"metric_descriptor": {
|
|
|
|
"name": "histogram float",
|
|
|
|
"description": "histogram float metric",
|
|
|
|
"type": 6,
|
|
|
|
"label_keys": [
|
|
|
|
{
|
|
|
|
"key": "hello"
|
|
|
|
}
|
|
|
|
]
|
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": [
|
|
|
|
{
|
|
|
|
"timestamp": "1970-01-01T00:00:30Z",
|
|
|
|
"distributionValue": {
|
|
|
|
"count": 3,
|
|
|
|
"sum": 10,
|
|
|
|
"bucket_options": {
|
|
|
|
"explicit": {
|
|
|
|
"bounds": [
|
|
|
|
0,
|
|
|
|
5
|
|
|
|
]
|
|
|
|
}
|
2019-11-22 15:51:22 -07:00
|
|
|
},
|
2019-11-22 16:25:02 -07:00
|
|
|
"buckets": [
|
|
|
|
{
|
|
|
|
"count": 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"count": 2
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
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) {
|
2019-11-27 09:24:29 -07:00
|
|
|
exporter.Metric(ctx, tt.data)
|
|
|
|
exporter.Flush()
|
|
|
|
got := sent.get("/v1/metrics")
|
2019-11-22 16:25:02 -07:00
|
|
|
checkJSON(t, got, []byte(tt.want))
|
2019-10-10 01:40:19 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|