2019-08-03 17:13:38 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-11-18 08:30:03 -07:00
|
|
|
package ocagent_test
|
2019-08-03 17:13:38 -06:00
|
|
|
|
|
|
|
import (
|
2019-11-22 11:41:57 -07:00
|
|
|
"bytes"
|
2019-08-15 19:10:50 -06:00
|
|
|
"context"
|
2019-08-03 17:13:38 -06:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry"
|
2019-11-18 08:30:03 -07:00
|
|
|
"golang.org/x/tools/internal/telemetry/export/ocagent"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/tag"
|
2019-08-03 17:13:38 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConvert_annotation(t *testing.T) {
|
|
|
|
tests := []struct {
|
2019-08-14 10:51:42 -06:00
|
|
|
name string
|
2019-08-15 19:10:50 -06:00
|
|
|
event func(ctx context.Context) telemetry.Event
|
2019-08-15 18:59:12 -06:00
|
|
|
want string
|
2019-08-03 17:13:38 -06:00
|
|
|
}{
|
|
|
|
{
|
2019-08-15 19:10:50 -06:00
|
|
|
name: "no tags",
|
|
|
|
event: func(ctx context.Context) telemetry.Event { return telemetry.Event{} },
|
|
|
|
want: "null",
|
2019-08-03 17:13:38 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description no error",
|
2019-08-15 19:10:50 -06:00
|
|
|
event: func(ctx context.Context) telemetry.Event {
|
|
|
|
return telemetry.Event{
|
|
|
|
Message: "cache miss",
|
|
|
|
Tags: telemetry.TagList{
|
|
|
|
tag.Of("db", "godb"),
|
|
|
|
},
|
|
|
|
}
|
2019-08-03 17:13:38 -06:00
|
|
|
},
|
2019-08-15 18:59:12 -06:00
|
|
|
want: `{
|
|
|
|
"description": {
|
|
|
|
"value": "cache miss"
|
|
|
|
},
|
|
|
|
"attributes": {
|
|
|
|
"attributeMap": {
|
|
|
|
"db": {
|
|
|
|
"stringValue": {
|
|
|
|
"value": "godb"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
2019-08-03 17:13:38 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "description and error",
|
2019-08-15 19:10:50 -06:00
|
|
|
event: func(ctx context.Context) telemetry.Event {
|
|
|
|
return telemetry.Event{
|
|
|
|
Message: "cache miss",
|
|
|
|
Error: errors.New("no network connectivity"),
|
|
|
|
Tags: telemetry.TagList{
|
|
|
|
tag.Of("db", "godb"),
|
|
|
|
},
|
|
|
|
}
|
2019-08-03 17:13:38 -06:00
|
|
|
},
|
2019-08-15 18:59:12 -06:00
|
|
|
want: `{
|
|
|
|
"description": {
|
|
|
|
"value": "cache miss"
|
|
|
|
},
|
|
|
|
"attributes": {
|
|
|
|
"attributeMap": {
|
|
|
|
"Error": {
|
|
|
|
"stringValue": {
|
|
|
|
"value": "no network connectivity"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"db": {
|
|
|
|
"stringValue": {
|
|
|
|
"value": "godb"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
2019-08-03 17:13:38 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no description, but error",
|
2019-08-15 19:10:50 -06:00
|
|
|
event: func(ctx context.Context) telemetry.Event {
|
|
|
|
return telemetry.Event{
|
|
|
|
Error: errors.New("no network connectivity"),
|
|
|
|
Tags: telemetry.TagList{
|
|
|
|
tag.Of("db", "godb"),
|
|
|
|
},
|
|
|
|
}
|
2019-08-03 17:13:38 -06:00
|
|
|
},
|
2019-08-15 18:59:12 -06:00
|
|
|
want: `{
|
|
|
|
"description": {
|
|
|
|
"value": "no network connectivity"
|
|
|
|
},
|
|
|
|
"attributes": {
|
|
|
|
"attributeMap": {
|
|
|
|
"db": {
|
|
|
|
"stringValue": {
|
|
|
|
"value": "godb"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
2019-08-03 17:13:38 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "enumerate all attribute types",
|
2019-08-15 19:10:50 -06:00
|
|
|
event: func(ctx context.Context) telemetry.Event {
|
|
|
|
return telemetry.Event{
|
|
|
|
Message: "cache miss",
|
|
|
|
Tags: telemetry.TagList{
|
|
|
|
tag.Of("db", "godb"),
|
2019-08-03 17:13:38 -06:00
|
|
|
|
2019-08-15 19:10:50 -06:00
|
|
|
tag.Of("age", 0.456), // Constant converted into "float64"
|
|
|
|
tag.Of("ttl", float32(5000)),
|
|
|
|
tag.Of("expiry_ms", float64(1e3)),
|
2019-08-03 17:13:38 -06:00
|
|
|
|
2019-08-15 19:10:50 -06:00
|
|
|
tag.Of("retry", false),
|
|
|
|
tag.Of("stale", true),
|
2019-08-03 17:13:38 -06:00
|
|
|
|
2019-08-15 19:10:50 -06:00
|
|
|
tag.Of("max", 0x7fff), // Constant converted into "int"
|
|
|
|
tag.Of("opcode", int8(0x7e)),
|
|
|
|
tag.Of("base", int16(1<<9)),
|
|
|
|
tag.Of("checksum", int32(0x11f7e294)),
|
|
|
|
tag.Of("mode", int64(0644)),
|
2019-08-03 17:13:38 -06:00
|
|
|
|
2019-08-15 19:10:50 -06:00
|
|
|
tag.Of("min", uint(1)),
|
|
|
|
tag.Of("mix", uint8(44)),
|
|
|
|
tag.Of("port", uint16(55678)),
|
|
|
|
tag.Of("min_hops", uint32(1<<9)),
|
|
|
|
tag.Of("max_hops", uint64(0xffffff)),
|
|
|
|
},
|
|
|
|
}
|
2019-08-03 17:13:38 -06:00
|
|
|
},
|
2019-08-15 18:59:12 -06:00
|
|
|
want: `{
|
|
|
|
"description": {
|
|
|
|
"value": "cache miss"
|
|
|
|
},
|
|
|
|
"attributes": {
|
|
|
|
"attributeMap": {
|
|
|
|
"age": {
|
|
|
|
"doubleValue": 0.456
|
|
|
|
},
|
|
|
|
"base": {
|
|
|
|
"intValue": 512
|
|
|
|
},
|
|
|
|
"checksum": {
|
|
|
|
"intValue": 301458068
|
|
|
|
},
|
|
|
|
"db": {
|
|
|
|
"stringValue": {
|
|
|
|
"value": "godb"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"expiry_ms": {
|
|
|
|
"doubleValue": 1000
|
|
|
|
},
|
|
|
|
"max": {
|
|
|
|
"intValue": 32767
|
|
|
|
},
|
|
|
|
"max_hops": {
|
|
|
|
"intValue": 16777215
|
|
|
|
},
|
|
|
|
"min": {
|
|
|
|
"intValue": 1
|
|
|
|
},
|
|
|
|
"min_hops": {
|
|
|
|
"intValue": 512
|
|
|
|
},
|
|
|
|
"mix": {
|
|
|
|
"intValue": 44
|
|
|
|
},
|
|
|
|
"mode": {
|
|
|
|
"intValue": 420
|
|
|
|
},
|
|
|
|
"opcode": {
|
|
|
|
"intValue": 126
|
|
|
|
},
|
|
|
|
"port": {
|
|
|
|
"intValue": 55678
|
|
|
|
},
|
|
|
|
"retry": {},
|
|
|
|
"stale": {
|
|
|
|
"boolValue": true
|
|
|
|
},
|
|
|
|
"ttl": {
|
|
|
|
"doubleValue": 5000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
2019-08-03 17:13:38 -06:00
|
|
|
},
|
|
|
|
}
|
2019-08-15 19:10:50 -06:00
|
|
|
ctx := context.TODO()
|
2019-08-03 17:13:38 -06:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-11-18 08:30:03 -07:00
|
|
|
got, err := ocagent.EncodeAnnotation(tt.event(ctx))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-08-03 17:13:38 -06:00
|
|
|
}
|
2019-11-18 08:30:03 -07:00
|
|
|
checkJSON(t, got, []byte(tt.want))
|
2019-08-03 17:13:38 -06:00
|
|
|
})
|
|
|
|
}
|
2019-11-22 11:41:57 -07:00
|
|
|
|
2019-08-03 17:13:38 -06:00
|
|
|
}
|
|
|
|
|
2019-11-18 08:30:03 -07:00
|
|
|
func checkJSON(t *testing.T, got, want []byte) {
|
2019-11-22 11:41:57 -07:00
|
|
|
// compare the compact form, to allow for formatting differences
|
|
|
|
g := &bytes.Buffer{}
|
|
|
|
if err := json.Compact(g, got); err != nil {
|
2019-11-18 08:30:03 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-22 11:41:57 -07:00
|
|
|
w := &bytes.Buffer{}
|
|
|
|
if err := json.Compact(w, want); err != nil {
|
2019-11-18 08:30:03 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-22 11:41:57 -07:00
|
|
|
if g.String() != w.String() {
|
2019-11-18 08:30:03 -07:00
|
|
|
t.Fatalf("Got:\n%s\nWant:\n%s", got, want)
|
|
|
|
}
|
2019-08-03 17:13:38 -06:00
|
|
|
}
|