Skip to content

Commit e1fc249

Browse files
committed
internal/lsp/telemetry/ocagent: add convertAnnotation tests
* Adds tests for 'convertAnnotation'. * Ensures that converting an empty string "" to a truncatable string returns nil, to save bandwidth. However, in the future, we should perhaps allow empty strings to be serialized if say "emptyAllowed" is set. * Caught the case where convertAttribute hadn't type switched on "int" * Provides 40.5% test coverage for ocagent.go More tests for the other functions shall follow in later CLs. Updates CL 186679 Change-Id: Ie9b46b0b320339ed79cd136fff536ccfcfbeb9e7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/188877 Run-TryBot: Emmanuel Odeke <[email protected]> Reviewed-by: Ian Cottrell <[email protected]>
1 parent 2756c52 commit e1fc249

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

internal/lsp/telemetry/ocagent/ocagent.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ func convertTimestamp(t time.Time) wire.Timestamp {
136136
}
137137

138138
func toTruncatableString(s string) *wire.TruncatableString {
139+
if s == "" {
140+
return nil
141+
}
139142
return &wire.TruncatableString{Value: s}
140143
}
141144

@@ -185,6 +188,8 @@ func convertAttribute(v interface{}) wire.Attribute {
185188
return wire.IntAttribute{IntValue: int64(v)}
186189
case int64:
187190
return wire.IntAttribute{IntValue: v}
191+
case int:
192+
return wire.IntAttribute{IntValue: int64(v)}
188193
case uint8:
189194
return wire.IntAttribute{IntValue: int64(v)}
190195
case uint16:
@@ -225,6 +230,9 @@ func convertEvent(event trace.Event) wire.TimeEvent {
225230
}
226231

227232
func convertAnnotation(tags tag.List) *wire.Annotation {
233+
if len(tags) == 0 {
234+
return nil
235+
}
228236
entry := log.ToEntry(nil, time.Time{}, tags)
229237
description := entry.Message
230238
if description == "" && entry.Error != nil {
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package ocagent
6+
7+
import (
8+
"encoding/json"
9+
"errors"
10+
"reflect"
11+
"testing"
12+
13+
"golang.org/x/tools/internal/lsp/telemetry/log"
14+
"golang.org/x/tools/internal/lsp/telemetry/ocagent/wire"
15+
"golang.org/x/tools/internal/lsp/telemetry/tag"
16+
)
17+
18+
func TestConvert_annotation(t *testing.T) {
19+
tests := []struct {
20+
name string
21+
tagList tag.List
22+
want *wire.Annotation
23+
}{
24+
{
25+
name: "no tags",
26+
tagList: nil,
27+
want: nil,
28+
},
29+
{
30+
name: "description no error",
31+
tagList: tag.List{
32+
tag.Of(log.MessageTag, "cache miss"),
33+
tag.Of("db", "godb"),
34+
},
35+
want: &wire.Annotation{
36+
Description: &wire.TruncatableString{Value: "cache miss"},
37+
Attributes: &wire.Attributes{
38+
AttributeMap: map[string]wire.Attribute{
39+
"db": wire.StringAttribute{StringValue: &wire.TruncatableString{Value: "godb"}},
40+
},
41+
},
42+
},
43+
},
44+
45+
{
46+
name: "description and error",
47+
tagList: tag.List{
48+
tag.Of(log.MessageTag, "cache miss"),
49+
tag.Of("db", "godb"),
50+
tag.Of(log.ErrorTag, errors.New("no network connectivity")),
51+
},
52+
want: &wire.Annotation{
53+
Description: &wire.TruncatableString{Value: "cache miss"},
54+
Attributes: &wire.Attributes{
55+
AttributeMap: map[string]wire.Attribute{
56+
"Error": wire.StringAttribute{StringValue: &wire.TruncatableString{Value: "no network connectivity"}},
57+
"db": wire.StringAttribute{StringValue: &wire.TruncatableString{Value: "godb"}},
58+
},
59+
},
60+
},
61+
},
62+
{
63+
name: "no description, but error",
64+
tagList: tag.List{
65+
tag.Of("db", "godb"),
66+
tag.Of(log.ErrorTag, errors.New("no network connectivity")),
67+
},
68+
want: &wire.Annotation{
69+
Description: &wire.TruncatableString{Value: "no network connectivity"},
70+
Attributes: &wire.Attributes{
71+
AttributeMap: map[string]wire.Attribute{
72+
"db": wire.StringAttribute{StringValue: &wire.TruncatableString{Value: "godb"}},
73+
},
74+
},
75+
},
76+
},
77+
{
78+
name: "enumerate all attribute types",
79+
tagList: tag.List{
80+
tag.Of(log.MessageTag, "cache miss"),
81+
tag.Of("db", "godb"),
82+
83+
tag.Of("age", 0.456), // Constant converted into "float64"
84+
tag.Of("ttl", float32(5000)),
85+
tag.Of("expiry_ms", float64(1e3)),
86+
87+
tag.Of("retry", false),
88+
tag.Of("stale", true),
89+
90+
tag.Of("max", 0x7fff), // Constant converted into "int"
91+
tag.Of("opcode", int8(0x7e)),
92+
tag.Of("base", int16(1<<9)),
93+
tag.Of("checksum", int32(0x11f7e294)),
94+
tag.Of("mode", int64(0644)),
95+
96+
tag.Of("min", uint(1)),
97+
tag.Of("mix", uint8(44)),
98+
tag.Of("port", uint16(55678)),
99+
tag.Of("min_hops", uint32(1<<9)),
100+
tag.Of("max_hops", uint64(0xffffff)),
101+
},
102+
want: &wire.Annotation{
103+
Description: &wire.TruncatableString{Value: "cache miss"},
104+
Attributes: &wire.Attributes{
105+
AttributeMap: map[string]wire.Attribute{
106+
"db": wire.StringAttribute{StringValue: &wire.TruncatableString{Value: "godb"}},
107+
108+
"age": wire.DoubleAttribute{DoubleValue: 0.456},
109+
"ttl": wire.DoubleAttribute{DoubleValue: 5000.0},
110+
"expiry_ms": wire.DoubleAttribute{DoubleValue: 1e3},
111+
112+
"retry": wire.BoolAttribute{BoolValue: false},
113+
"stale": wire.BoolAttribute{BoolValue: true},
114+
115+
"max": wire.IntAttribute{IntValue: 0x7fff},
116+
"opcode": wire.IntAttribute{IntValue: 0x7e},
117+
"base": wire.IntAttribute{IntValue: 1 << 9},
118+
"checksum": wire.IntAttribute{IntValue: 0x11f7e294},
119+
"mode": wire.IntAttribute{IntValue: 0644},
120+
121+
"min": wire.IntAttribute{IntValue: 1},
122+
"mix": wire.IntAttribute{IntValue: 44},
123+
"port": wire.IntAttribute{IntValue: 55678},
124+
"min_hops": wire.IntAttribute{IntValue: 1 << 9},
125+
"max_hops": wire.IntAttribute{IntValue: 0xffffff},
126+
},
127+
},
128+
},
129+
},
130+
}
131+
132+
for _, tt := range tests {
133+
t.Run(tt.name, func(t *testing.T) {
134+
got := convertAnnotation(tt.tagList)
135+
if !reflect.DeepEqual(got, tt.want) {
136+
t.Fatalf("Got:\n%s\nWant:\n%s", marshaled(got), marshaled(tt.want))
137+
}
138+
})
139+
}
140+
}
141+
142+
func marshaled(v interface{}) string {
143+
blob, _ := json.MarshalIndent(v, "", " ")
144+
return string(blob)
145+
}

0 commit comments

Comments
 (0)