1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:54:40 -07:00

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>
This commit is contained in:
Ian Cottrell 2020-03-17 16:00:16 -04:00
parent b378960d5b
commit 326edff2a4
2 changed files with 85 additions and 250 deletions

View File

@ -2,10 +2,10 @@ package ocagent_test
import (
"context"
"errors"
"testing"
"golang.org/x/tools/internal/telemetry/event"
"golang.org/x/tools/internal/telemetry/metric"
)
func TestEncodeMetric(t *testing.T) {
@ -19,204 +19,26 @@ func TestEncodeMetric(t *testing.T) {
want string
}{
{
name: "nil data",
want: prefix + `null` + suffix,
name: "HistogramFloat64, HistogramInt64",
run: func(ctx context.Context) {
exporter.Metric(ctx, nil)
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)
},
},
{
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 + `{
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",
"name": "latency_ms",
"description": "The latency of calls in milliseconds",
"type": 6,
"label_keys": [
{
"key": "hello"
"key": "method"
},
{
"key": "route"
}
]
},
@ -225,70 +47,45 @@ func TestEncodeMetric(t *testing.T) {
"start_timestamp": "1970-01-01T00:00:00Z",
"points": [
{
"timestamp": "1970-01-01T00:00:30Z",
"timestamp": "1970-01-01T00:00:40Z",
"distributionValue": {
"count": 6,
"sum": 40,
"count": 1,
"sum": 96.58,
"bucket_options": {
"explicit": {
"bounds": [
0,
5,
10
10,
25,
50
]
}
},
"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",
"name": "latency_ms",
"description": "The latency of calls in milliseconds",
"type": 6,
"label_keys": [
{
"key": "hello"
"key": "method"
},
{
"key": "route"
}
]
},
@ -297,25 +94,31 @@ func TestEncodeMetric(t *testing.T) {
"start_timestamp": "1970-01-01T00:00:00Z",
"points": [
{
"timestamp": "1970-01-01T00:00:30Z",
"timestamp": "1970-01-01T00:00:40Z",
"distributionValue": {
"count": 3,
"sum": 10,
"count": 1,
"sum": 9700,
"bucket_options": {
"explicit": {
"bounds": [
0,
5
10,
50,
100,
500,
1000,
2000
]
}
},
"buckets": [
{
"count": 1
},
{
"count": 2
}
{},
{},
{},
{},
{},
{},
{}
]
}
}

View File

@ -19,6 +19,8 @@ import (
"golang.org/x/tools/internal/telemetry/export"
"golang.org/x/tools/internal/telemetry/export/ocagent"
"golang.org/x/tools/internal/telemetry/metric"
"golang.org/x/tools/internal/telemetry/stats"
"golang.org/x/tools/internal/telemetry/unit"
)
const testNodeStr = `{
@ -39,9 +41,9 @@ const testNodeStr = `{
},`
var (
keyDB = event.NewStringKey("db", "the database name")
keyHello = event.NewStringKey("hello", "a metric grouping key")
keyWorld = event.NewStringKey("world", "another metric grouping key")
keyDB = event.NewStringKey("db", "the database name")
keyMethod = event.NewStringKey("method", "a metric grouping key")
keyRoute = event.NewStringKey("route", "another metric grouping key")
key1DB = event.NewStringKey("1_db", "A test string key")
@ -63,6 +65,30 @@ var (
key5cPort = event.NewUInt16Key("5c_port", "A test uint16 key")
key5dMinHops = event.NewUInt32Key("5d_min_hops", "A test uint32 key")
key5eMaxHops = event.NewUInt64Key("5e_max_hops", "A test uint64 key")
recursiveCalls = stats.Int64("recursive_calls", "Number of recursive calls", unit.Dimensionless)
bytesIn = stats.Int64("bytes_in", "Number of bytes in", unit.Bytes)
latencyMs = stats.Float64("latency", "The latency in milliseconds", unit.Milliseconds)
metricLatency = metric.HistogramFloat64{
Name: "latency_ms",
Description: "The latency of calls in milliseconds",
Keys: []event.Key{keyMethod, keyRoute},
Buckets: []float64{0, 5, 10, 25, 50},
}.Record(latencyMs)
metricBytesIn = metric.HistogramInt64{
Name: "latency_ms",
Description: "The latency of calls in milliseconds",
Keys: []event.Key{keyMethod, keyRoute},
Buckets: []int64{0, 10, 50, 100, 500, 1000, 2000},
}.Record(bytesIn)
metricRecursiveCalls = metric.Scalar{
Name: "latency_ms",
Description: "The latency of calls in milliseconds",
Keys: []event.Key{keyMethod, keyRoute},
}.SumInt64(recursiveCalls)
)
type testExporter struct {
@ -112,7 +138,13 @@ func (e *testExporter) ProcessEvent(ctx context.Context, ev event.Event) (contex
func (e *testExporter) Metric(ctx context.Context, data event.MetricData) {
switch data := data.(type) {
case *metric.Int64Data:
data.EndTime = &e.start
data.EndTime = &e.at
case *metric.Float64Data:
data.EndTime = &e.at
case *metric.HistogramInt64Data:
data.EndTime = &e.at
case *metric.HistogramFloat64Data:
data.EndTime = &e.at
}
e.ocagent.Metric(ctx, data)
}