Skip to content

Commit 6864b6a

Browse files
yurishkuroIsaac Hier
authored andcommitted
Handle ports > 32k in Zipkin JSON (jaegertracing#488)
1 parent f0765d1 commit 6864b6a

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

cmd/collector/app/zipkin/http_handler_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,23 @@ func TestViaClient(t *testing.T) {
8383

8484
tracer.StartSpan("root").Finish()
8585

86+
waitForSpans(t, handler.zipkinSpansHandler.(*mockZipkinHandler), 1)
87+
}
88+
89+
func waitForSpans(t *testing.T, handler *mockZipkinHandler, expecting int) {
8690
deadline := time.Now().Add(2 * time.Second)
8791
for {
8892
if time.Now().After(deadline) {
8993
t.Error("never received a span")
9094
return
9195
}
92-
if want, have := 1, len(handler.zipkinSpansHandler.(*mockZipkinHandler).getSpans()); want != have {
96+
if have := len(handler.getSpans()); expecting != have {
9397
time.Sleep(time.Millisecond)
9498
continue
9599
}
96100
break
97101
}
98-
99-
assert.Equal(t, 1, len(handler.zipkinSpansHandler.(*mockZipkinHandler).getSpans()))
102+
assert.Len(t, handler.getSpans(), expecting)
100103
}
101104

102105
func TestThriftFormat(t *testing.T) {
@@ -120,7 +123,7 @@ func TestJsonFormat(t *testing.T) {
120123
server, handler := initializeTestServer(nil)
121124
defer server.Close()
122125

123-
endpJSON := createEndpoint("foo", "127.0.0.1", "2001:db8::c001", 66)
126+
endpJSON := createEndpoint("foo", "127.0.0.1", "2001:db8::c001", 65535)
124127
annoJSON := createAnno("cs", 1515, endpJSON)
125128
binAnnoJSON := createBinAnno("http.status_code", "200", endpJSON)
126129
spanJSON := createSpan("bar", "1234567891234565", "1234567891234567", "1234567891234568", 156, 15145, false,
@@ -129,6 +132,11 @@ func TestJsonFormat(t *testing.T) {
129132
assert.NoError(t, err)
130133
assert.EqualValues(t, http.StatusAccepted, statusCode)
131134
assert.EqualValues(t, "", resBodyStr)
135+
waitForSpans(t, handler.zipkinSpansHandler.(*mockZipkinHandler), 1)
136+
recdSpan := handler.zipkinSpansHandler.(*mockZipkinHandler).getSpans()[0]
137+
require.Len(t, recdSpan.Annotations, 1)
138+
require.NotNil(t, recdSpan.Annotations[0].Host)
139+
assert.EqualValues(t, -1, recdSpan.Annotations[0].Host.Port, "Port 65535 must be represented as -1 in zipkin.thrift")
132140

133141
endpErrJSON := createEndpoint("", "127.0.0.A", "", 80)
134142

cmd/collector/app/zipkin/json.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type endpoint struct {
2727
ServiceName string `json:"serviceName"`
2828
IPv4 string `json:"ipv4"`
2929
IPv6 string `json:"ipv6"`
30-
Port int16 `json:"port"`
30+
Port int32 `json:"port"`
3131
}
3232
type annotation struct {
3333
Endpoint endpoint `json:"endpoint"`
@@ -147,10 +147,15 @@ func endpointToThrift(e endpoint) (*zipkincore.Endpoint, error) {
147147
if err != nil {
148148
return nil, err
149149
}
150+
port := e.Port
151+
if port >= (1 << 15) {
152+
// Zipkin.thrift defines port as i16, so values between (2^15 and 2^16-1) must be encoded as negative
153+
port = port - (1 << 16)
154+
}
150155

151156
return &zipkincore.Endpoint{
152157
ServiceName: e.ServiceName,
153-
Port: e.Port,
158+
Port: int16(port),
154159
Ipv4: ipv4,
155160
Ipv6: []byte(e.IPv6),
156161
}, nil

0 commit comments

Comments
 (0)