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"
|
2019-11-27 09:24:29 -07:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
2019-08-03 17:13:38 -06:00
|
|
|
"testing"
|
2019-11-26 22:19:20 -07:00
|
|
|
"time"
|
2019-08-03 17:13:38 -06:00
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry"
|
2020-03-01 16:35:55 -07:00
|
|
|
"golang.org/x/tools/internal/telemetry/export"
|
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
|
|
|
)
|
|
|
|
|
2019-11-26 22:19:20 -07:00
|
|
|
var (
|
2020-02-28 12:08:49 -07:00
|
|
|
exporter *ocagent.Exporter
|
2019-11-27 09:24:29 -07:00
|
|
|
sent fakeSender
|
|
|
|
start time.Time
|
|
|
|
at time.Time
|
|
|
|
end time.Time
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cfg := ocagent.Config{
|
2019-11-26 22:19:20 -07:00
|
|
|
Host: "tester",
|
|
|
|
Process: 1,
|
|
|
|
Service: "ocagent-tests",
|
2019-11-27 09:24:29 -07:00
|
|
|
Client: &http.Client{Transport: &sent},
|
2019-11-26 22:19:20 -07:00
|
|
|
}
|
|
|
|
cfg.Start, _ = time.Parse(time.RFC3339Nano, "1970-01-01T00:00:00Z")
|
2019-11-27 09:24:29 -07:00
|
|
|
exporter = ocagent.Connect(&cfg)
|
2019-11-26 22:19:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const testNodeStr = `{
|
|
|
|
"node":{
|
|
|
|
"identifier":{
|
|
|
|
"host_name":"tester",
|
|
|
|
"pid":1,
|
|
|
|
"start_timestamp":"1970-01-01T00:00:00Z"
|
|
|
|
},
|
|
|
|
"library_info":{
|
|
|
|
"language":4,
|
|
|
|
"exporter_version":"0.0.1",
|
|
|
|
"core_library_version":"x/tools"
|
|
|
|
},
|
|
|
|
"service_info":{
|
|
|
|
"name":"ocagent-tests"
|
|
|
|
}
|
|
|
|
},`
|
|
|
|
|
|
|
|
func TestEvents(t *testing.T) {
|
|
|
|
start, _ := time.Parse(time.RFC3339Nano, "1970-01-01T00:00:30Z")
|
|
|
|
at, _ := time.Parse(time.RFC3339Nano, "1970-01-01T00:00:40Z")
|
|
|
|
end, _ := time.Parse(time.RFC3339Nano, "1970-01-01T00:00:50Z")
|
|
|
|
const prefix = testNodeStr + `
|
|
|
|
"spans":[{
|
|
|
|
"trace_id":"AAAAAAAAAAAAAAAAAAAAAA==",
|
|
|
|
"span_id":"AAAAAAAAAAA=",
|
|
|
|
"parent_span_id":"AAAAAAAAAAA=",
|
|
|
|
"name":{"value":"event span"},
|
|
|
|
"start_time":"1970-01-01T00:00:30Z",
|
|
|
|
"end_time":"1970-01-01T00:00:50Z",
|
|
|
|
"time_events":{
|
|
|
|
`
|
|
|
|
const suffix = `
|
|
|
|
},
|
|
|
|
"same_process_as_parent_span":true
|
|
|
|
}]
|
|
|
|
}`
|
2019-08-03 17:13:38 -06:00
|
|
|
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-11-26 22:19:20 -07:00
|
|
|
name: "no tags",
|
|
|
|
event: func(ctx context.Context) telemetry.Event {
|
|
|
|
return telemetry.Event{
|
|
|
|
At: at,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
want: prefix + `
|
|
|
|
"timeEvent":[{"time":"1970-01-01T00:00:40Z"}]
|
|
|
|
` + suffix,
|
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{
|
2019-11-26 22:19:20 -07:00
|
|
|
At: at,
|
2019-08-15 19:10:50 -06:00
|
|
|
Message: "cache miss",
|
|
|
|
Tags: telemetry.TagList{
|
|
|
|
tag.Of("db", "godb"),
|
|
|
|
},
|
|
|
|
}
|
2019-08-03 17:13:38 -06:00
|
|
|
},
|
2019-11-26 22:19:20 -07:00
|
|
|
want: prefix + `"timeEvent":[{"time":"1970-01-01T00:00:40Z","annotation":{
|
2019-11-27 20:08:18 -07:00
|
|
|
"description": { "value": "cache miss" },
|
2019-08-15 18:59:12 -06:00
|
|
|
"attributes": {
|
|
|
|
"attributeMap": {
|
2019-11-27 20:08:18 -07:00
|
|
|
"db": { "stringValue": { "value": "godb" } }
|
2019-08-15 18:59:12 -06:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 22:19:20 -07:00
|
|
|
}}]` + suffix,
|
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{
|
2019-11-26 22:19:20 -07:00
|
|
|
At: at,
|
2019-08-15 19:10:50 -06:00
|
|
|
Message: "cache miss",
|
|
|
|
Error: errors.New("no network connectivity"),
|
|
|
|
Tags: telemetry.TagList{
|
2019-11-27 20:08:18 -07:00
|
|
|
tag.Of("db", "godb"), // must come before e
|
2019-08-15 19:10:50 -06:00
|
|
|
},
|
|
|
|
}
|
2019-08-03 17:13:38 -06:00
|
|
|
},
|
2019-11-26 22:19:20 -07:00
|
|
|
want: prefix + `"timeEvent":[{"time":"1970-01-01T00:00:40Z","annotation":{
|
2019-11-27 20:08:18 -07:00
|
|
|
"description": { "value": "cache miss" },
|
2019-08-15 18:59:12 -06:00
|
|
|
"attributes": {
|
|
|
|
"attributeMap": {
|
2019-11-27 20:08:18 -07:00
|
|
|
"db": { "stringValue": { "value": "godb" } },
|
|
|
|
"error": { "stringValue": { "value": "no network connectivity" } }
|
2019-08-15 18:59:12 -06:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 22:19:20 -07:00
|
|
|
}}]` + suffix,
|
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{
|
2019-11-26 22:19:20 -07:00
|
|
|
At: at,
|
2019-08-15 19:10:50 -06:00
|
|
|
Error: errors.New("no network connectivity"),
|
|
|
|
Tags: telemetry.TagList{
|
|
|
|
tag.Of("db", "godb"),
|
|
|
|
},
|
|
|
|
}
|
2019-08-03 17:13:38 -06:00
|
|
|
},
|
2019-11-26 22:19:20 -07:00
|
|
|
want: prefix + `"timeEvent":[{"time":"1970-01-01T00:00:40Z","annotation":{
|
2019-11-27 20:08:18 -07:00
|
|
|
"description": { "value": "no network connectivity" },
|
2019-08-15 18:59:12 -06:00
|
|
|
"attributes": {
|
|
|
|
"attributeMap": {
|
2019-11-27 20:08:18 -07:00
|
|
|
"db": { "stringValue": { "value": "godb" } }
|
2019-08-15 18:59:12 -06:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 22:19:20 -07:00
|
|
|
}}]` + suffix,
|
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{
|
2019-11-26 22:19:20 -07:00
|
|
|
At: at,
|
2019-08-15 19:10:50 -06:00
|
|
|
Message: "cache miss",
|
|
|
|
Tags: telemetry.TagList{
|
2019-11-27 20:08:18 -07:00
|
|
|
tag.Of("1_db", "godb"),
|
2019-08-03 17:13:38 -06:00
|
|
|
|
2019-11-27 20:08:18 -07:00
|
|
|
tag.Of("2a_age", 0.456), // Constant converted into "float64"
|
|
|
|
tag.Of("2b_ttl", float32(5000)),
|
|
|
|
tag.Of("2c_expiry_ms", float64(1e3)),
|
2019-08-03 17:13:38 -06:00
|
|
|
|
2019-11-27 20:08:18 -07:00
|
|
|
tag.Of("3a_retry", false),
|
|
|
|
tag.Of("3b_stale", true),
|
2019-08-03 17:13:38 -06:00
|
|
|
|
2019-11-27 20:08:18 -07:00
|
|
|
tag.Of("4a_max", 0x7fff), // Constant converted into "int"
|
|
|
|
tag.Of("4b_opcode", int8(0x7e)),
|
|
|
|
tag.Of("4c_base", int16(1<<9)),
|
|
|
|
tag.Of("4e_checksum", int32(0x11f7e294)),
|
|
|
|
tag.Of("4f_mode", int64(0644)),
|
2019-08-03 17:13:38 -06:00
|
|
|
|
2019-11-27 20:08:18 -07:00
|
|
|
tag.Of("5a_min", uint(1)),
|
|
|
|
tag.Of("5b_mix", uint8(44)),
|
|
|
|
tag.Of("5c_port", uint16(55678)),
|
|
|
|
tag.Of("5d_min_hops", uint32(1<<9)),
|
|
|
|
tag.Of("5e_max_hops", uint64(0xffffff)),
|
2019-08-15 19:10:50 -06:00
|
|
|
},
|
|
|
|
}
|
2019-08-03 17:13:38 -06:00
|
|
|
},
|
2019-11-26 22:19:20 -07:00
|
|
|
want: prefix + `"timeEvent":[{"time":"1970-01-01T00:00:40Z","annotation":{
|
2019-11-27 20:08:18 -07:00
|
|
|
"description": { "value": "cache miss" },
|
2019-08-15 18:59:12 -06:00
|
|
|
"attributes": {
|
|
|
|
"attributeMap": {
|
2019-11-27 20:08:18 -07:00
|
|
|
"1_db": { "stringValue": { "value": "godb" } },
|
|
|
|
"2a_age": { "doubleValue": 0.456 },
|
|
|
|
"2b_ttl": { "doubleValue": 5000 },
|
|
|
|
"2c_expiry_ms": { "doubleValue": 1000 },
|
|
|
|
"3a_retry": {},
|
|
|
|
"3b_stale": { "boolValue": true },
|
|
|
|
"4a_max": { "intValue": 32767 },
|
|
|
|
"4b_opcode": { "intValue": 126 },
|
|
|
|
"4c_base": { "intValue": 512 },
|
|
|
|
"4e_checksum": { "intValue": 301458068 },
|
|
|
|
"4f_mode": { "intValue": 420 },
|
|
|
|
"5a_min": { "intValue": 1 },
|
|
|
|
"5b_mix": { "intValue": 44 },
|
|
|
|
"5c_port": { "intValue": 55678 },
|
|
|
|
"5d_min_hops": { "intValue": 512 },
|
|
|
|
"5e_max_hops": { "intValue": 16777215 }
|
2019-08-15 18:59:12 -06:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 22:19:20 -07:00
|
|
|
}}]` + suffix,
|
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) {
|
2020-03-01 16:35:55 -07:00
|
|
|
startEvent := telemetry.Event{
|
|
|
|
Type: telemetry.EventStartSpan,
|
|
|
|
Message: "event span",
|
|
|
|
At: start,
|
2019-11-26 22:19:20 -07:00
|
|
|
}
|
2020-03-01 16:35:55 -07:00
|
|
|
endEvent := telemetry.Event{
|
|
|
|
Type: telemetry.EventEndSpan,
|
|
|
|
At: end,
|
|
|
|
}
|
|
|
|
ctx := export.ContextSpan(ctx, startEvent)
|
|
|
|
span := export.GetSpan(ctx)
|
|
|
|
span.ID = export.SpanContext{}
|
|
|
|
span.Events = []telemetry.Event{tt.event(ctx)}
|
|
|
|
exporter.ProcessEvent(ctx, startEvent)
|
|
|
|
export.ContextSpan(ctx, endEvent)
|
|
|
|
exporter.ProcessEvent(ctx, endEvent)
|
2019-11-27 09:24:29 -07:00
|
|
|
exporter.Flush()
|
|
|
|
got := sent.get("/v1/trace")
|
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-26 22:19:20 -07:00
|
|
|
t.Fatalf("Got:\n%s\nWant:\n%s", g, w)
|
2019-11-18 08:30:03 -07:00
|
|
|
}
|
2019-08-03 17:13:38 -06:00
|
|
|
}
|
2019-11-27 09:24:29 -07:00
|
|
|
|
|
|
|
type fakeSender struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
data map[string][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *fakeSender) get(route string) []byte {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
data, found := s.data[route]
|
|
|
|
if found {
|
|
|
|
delete(s.data, route)
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *fakeSender) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if s.data == nil {
|
|
|
|
s.data = make(map[string][]byte)
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path := req.URL.EscapedPath()
|
|
|
|
if _, found := s.data[path]; found {
|
|
|
|
return nil, fmt.Errorf("duplicate delivery to %v", path)
|
|
|
|
}
|
|
|
|
s.data[path] = data
|
|
|
|
return &http.Response{
|
|
|
|
Status: "200 OK",
|
|
|
|
StatusCode: 200,
|
|
|
|
Proto: "HTTP/1.0",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 0,
|
|
|
|
}, nil
|
|
|
|
}
|