Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit 0283713

Browse files
author
Paulo Janotti
authored
Prefer BinaryAnnotation.host over Zipkin "lc" annotation for service name (#394)
Per zipkinCore.Thrift comments for "lc" (LOCAL_COMPONENT) it should be used to provide the "component" of a local span, however, the service context should come from BinnaryAnnotation.host. The recent changes were overwriting binary annotation host information if the "lc" was available, however, "lc" should be the last resort. This change only uses "lc" if no endpoint or binary annotation host is available. Fix #393
1 parent 05f5589 commit 0283713

6 files changed

Lines changed: 77 additions & 12 deletions

translator/trace/testdata/zipkin_v1_local_component.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,25 @@
1313
"value": "myLocalComponent"
1414
}
1515
]
16+
},
17+
{
18+
"traceId": "0ed2e63cbe71f5a8",
19+
"name": "sendOrder",
20+
"id": "fe351a053fbcac2f",
21+
"parentId": "0ed2e63cbe71f5a8",
22+
"timestamp": 1544805927453925,
23+
"duration": 3740,
24+
"annotations": [],
25+
"binaryAnnotations": [
26+
{
27+
"key": "lc",
28+
"value": "myLocalComponent",
29+
"endpoint": {
30+
"ipv4": "172.31.0.7",
31+
"port": 0,
32+
"serviceName": "myServiceName"
33+
}
34+
}
35+
]
1636
}
1737
]

translator/trace/testdata/zipkin_v1_thrift_local_component.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,26 @@
1414
"value": "bXlMb2NhbENvbXBvbmVudA=="
1515
}
1616
]
17+
},
18+
{
19+
"trace_id": 1068169210207794600,
20+
"name": "sendOrder",
21+
"id": -129168404463703007,
22+
"parent_id": 1068169210207794600,
23+
"timestamp": 1544805927453925,
24+
"duration": 3740,
25+
"annotations": [],
26+
"binary_annotations": [
27+
{
28+
"key": "lc",
29+
"annotation_type": "STRING",
30+
"value": "bXlMb2NhbENvbXBvbmVudA==",
31+
"host": {
32+
"ipv4": -1407254521,
33+
"port": 0,
34+
"service_name": "myServiceName"
35+
}
36+
}
37+
]
1738
}
1839
]

translator/trace/zipkinv1_thrift_to_protospan.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ func toTranslatorEndpoint(e *zipkincore.Endpoint) *endpoint {
137137

138138
var trueByteSlice = []byte{1}
139139

140-
func zipkinV1ThriftBinAnnotationsToOCAttributes(ztBinAnnotations []*zipkincore.BinaryAnnotation) (attributes *tracepb.Span_Attributes, localComponent string) {
140+
func zipkinV1ThriftBinAnnotationsToOCAttributes(ztBinAnnotations []*zipkincore.BinaryAnnotation) (attributes *tracepb.Span_Attributes, fallbackServiceName string) {
141141
if len(ztBinAnnotations) == 0 {
142142
return nil, ""
143143
}
144144

145-
var fallbackServiceName string
145+
var localComponent string
146146
attributeMap := make(map[string]*tracepb.AttributeValue)
147147
for _, binaryAnnotation := range ztBinAnnotations {
148148
pbAttrib := &tracepb.AttributeValue{}
@@ -200,14 +200,14 @@ func zipkinV1ThriftBinAnnotationsToOCAttributes(ztBinAnnotations []*zipkincore.B
200200
attributeMap[key] = pbAttrib
201201
}
202202

203-
if localComponent == "" && fallbackServiceName != "" {
204-
localComponent = fallbackServiceName
203+
if fallbackServiceName == "" {
204+
fallbackServiceName = localComponent
205205
}
206206

207207
attributes = &tracepb.Span_Attributes{
208208
AttributeMap: attributeMap,
209209
}
210-
return attributes, localComponent
210+
return attributes, fallbackServiceName
211211
}
212212

213213
var errNotEnoughBytes = errors.New("not enough bytes representing the number")

translator/trace/zipkinv1_thrift_to_protospan_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,20 @@ func TestZipkinThriftFallbackToLocalComponent(t *testing.T) {
3939
t.Fatalf("failed to translate zipkinv1 thrift to OC proto: %v", err)
4040
}
4141

42+
if len(reqs) != 2 {
43+
t.Fatalf("got %d trace service request(s), want 2", len(reqs))
44+
}
45+
46+
// First span didn't have a host/endpoint to give service name, use the local component.
4247
got := reqs[0].Node.ServiceInfo.Name
43-
const want = "myLocalComponent"
48+
want := "myLocalComponent"
49+
if got != want {
50+
t.Fatalf("got %q for service name, want %q", got, want)
51+
}
52+
53+
// Second span have a host/endpoint to give service name, do not use local component.
54+
got = reqs[1].Node.ServiceInfo.Name
55+
want = "myServiceName"
4456
if got != want {
4557
t.Fatalf("got %q for service name, want %q", got, want)
4658
}

translator/trace/zipkinv1_to_protospan.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ func zipkinV1ToOCSpan(zSpan *zipkinV1Span) (*tracepb.Span, *annotationParseResul
175175
return ocSpan, parsedAnnotations, nil
176176
}
177177

178-
func zipkinV1BinAnnotationsToOCAttributes(binAnnotations []*binaryAnnotation) (attributes *tracepb.Span_Attributes, localComponent string) {
178+
func zipkinV1BinAnnotationsToOCAttributes(binAnnotations []*binaryAnnotation) (attributes *tracepb.Span_Attributes, fallbackServiceName string) {
179179
if len(binAnnotations) == 0 {
180180
return nil, ""
181181
}
182182

183-
var fallbackServiceName string
183+
var localComponent string
184184
attributeMap := make(map[string]*tracepb.AttributeValue)
185185
for _, binAnnotation := range binAnnotations {
186186
if binAnnotation.Endpoint != nil && binAnnotation.Endpoint.ServiceName != "" {
@@ -209,15 +209,15 @@ func zipkinV1BinAnnotationsToOCAttributes(binAnnotations []*binaryAnnotation) (a
209209
return nil, ""
210210
}
211211

212-
if localComponent == "" && fallbackServiceName != "" {
213-
localComponent = fallbackServiceName
212+
if fallbackServiceName == "" {
213+
fallbackServiceName = localComponent
214214
}
215215

216216
attributes = &tracepb.Span_Attributes{
217217
AttributeMap: attributeMap,
218218
}
219219

220-
return attributes, localComponent
220+
return attributes, fallbackServiceName
221221
}
222222

223223
// annotationParseResult stores the results of examining the original annotations,

translator/trace/zipkinv1_to_protospan_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,20 @@ func TestZipkinJSONFallbackToLocalComponent(t *testing.T) {
141141
t.Fatalf("failed to translate zipkinv1 to OC proto: %v", err)
142142
}
143143

144+
if len(reqs) != 2 {
145+
t.Fatalf("got %d trace service request(s), want 2", len(reqs))
146+
}
147+
148+
// First span didn't have a host/endpoint to give service name, use the local component.
144149
got := reqs[0].Node.ServiceInfo.Name
145-
const want = "myLocalComponent"
150+
want := "myLocalComponent"
151+
if got != want {
152+
t.Fatalf("got %q for service name, want %q", got, want)
153+
}
154+
155+
// Second span have a host/endpoint to give service name, do not use local component.
156+
got = reqs[1].Node.ServiceInfo.Name
157+
want = "myServiceName"
146158
if got != want {
147159
t.Fatalf("got %q for service name, want %q", got, want)
148160
}

0 commit comments

Comments
 (0)