Skip to content

Commit 102d6cf

Browse files
committed
remote endpoint, refactor
Signed-off-by: Pavol Loffay <ploffay@redhat.com>
1 parent 509145a commit 102d6cf

File tree

4 files changed

+82
-44
lines changed

4 files changed

+82
-44
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ THRIFT_GO_ARGS=thrift_import="github.com/apache/thrift/lib/go/thrift"
3434
THRIFT_GEN=$(shell which thrift-gen)
3535
THRIFT_GEN_DIR=thrift-gen
3636

37-
SWAGGER_IMG_VER=0.12.0
38-
SWAGGER_IMAGE=quay.io/goswagger/swagger:$(SWAGGER_IMG_VER)
37+
SWAGGER_VER=0.12.0
38+
SWAGGER_IMAGE=quay.io/goswagger/swagger:$(SWAGGER_VER)
3939
SWAGGER=docker run --rm -it -u ${shell id -u} -v "${PWD}:/go/src/${PROJECT_ROOT}" -w /go/src/${PROJECT_ROOT} $(SWAGGER_IMAGE)
4040
SWAGGER_GEN_DIR=swagger-gen
4141

cmd/collector/app/zipkin/json.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ func cutLongID(id string) string {
152152
return id
153153
}
154154

155-
func endpointToThrift(ip4 string, ip6 string, p int32, service string) (*zipkincore.Endpoint, error) {
155+
func endpointToThrift(e endpoint) (*zipkincore.Endpoint, error) {
156+
return eToThrift(e.IPv4, e.IPv6, e.Port, e.ServiceName)
157+
}
158+
159+
func eToThrift(ip4 string, ip6 string, p int32, service string) (*zipkincore.Endpoint, error) {
156160
ipv4, err := parseIpv4(ip4)
157161
if err != nil {
158162
return nil, err
@@ -179,7 +183,7 @@ func port(p int32) int32 {
179183
}
180184

181185
func annoToThrift(a annotation) (*zipkincore.Annotation, error) {
182-
endpoint, err := endpointToThrift(a.Endpoint.IPv4, a.Endpoint.IPv6, a.Endpoint.Port, a.Endpoint.ServiceName)
186+
endpoint, err := endpointToThrift(a.Endpoint)
183187
if err != nil {
184188
return nil, err
185189
}
@@ -192,7 +196,7 @@ func annoToThrift(a annotation) (*zipkincore.Annotation, error) {
192196
}
193197

194198
func binAnnoToThrift(ba binaryAnnotation) (*zipkincore.BinaryAnnotation, error) {
195-
endpoint, err := endpointToThrift(ba.Endpoint.IPv4, ba.Endpoint.IPv6, ba.Endpoint.Port, ba.Endpoint.ServiceName)
199+
endpoint, err := endpointToThrift(ba.Endpoint)
196200
if err != nil {
197201
return nil, err
198202
}

cmd/collector/app/zipkin/json_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func TestEndpointToThrift(t *testing.T) {
217217
Port: 80,
218218
IPv4: "127.0.0.1",
219219
}
220-
tEndpoint, err := endpointToThrift(endp.IPv4, endp.IPv6, endp.Port, endp.ServiceName)
220+
tEndpoint, err := endpointToThrift(endp)
221221
require.NoError(t, err)
222222
assert.Equal(t, "foo", tEndpoint.ServiceName)
223223
assert.Equal(t, int16(80), tEndpoint.Port)
@@ -228,7 +228,7 @@ func TestEndpointToThrift(t *testing.T) {
228228
Port: 80,
229229
IPv4: "",
230230
}
231-
tEndpoint, err = endpointToThrift(endp.IPv4, endp.IPv6, endp.Port, endp.ServiceName)
231+
tEndpoint, err = endpointToThrift(endp)
232232
require.NoError(t, err)
233233
assert.Equal(t, "foo", tEndpoint.ServiceName)
234234
assert.Equal(t, int16(80), tEndpoint.Port)
@@ -239,7 +239,7 @@ func TestEndpointToThrift(t *testing.T) {
239239
Port: 80,
240240
IPv4: "127.0.0.A",
241241
}
242-
tEndpoint, err = endpointToThrift(endp.IPv4, endp.IPv6, endp.Port, endp.ServiceName)
242+
tEndpoint, err = endpointToThrift(endp)
243243
require.Error(t, err)
244244
assert.Equal(t, errWrongIpv4, err)
245245
assert.Nil(t, tEndpoint)
@@ -249,7 +249,7 @@ func TestEndpointToThrift(t *testing.T) {
249249
Port: 80,
250250
IPv6: "::R",
251251
}
252-
tEndpoint, err = endpointToThrift(endp.IPv4, endp.IPv6, endp.Port, endp.ServiceName)
252+
tEndpoint, err = endpointToThrift(endp)
253253
require.Error(t, err)
254254
assert.Equal(t, errWrongIpv6, err)
255255
assert.Nil(t, tEndpoint)

cmd/collector/app/zipkin/jsonv2.go

+69-35
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
package zipkin
1616

1717
import (
18-
"fmt"
1918
"github.com/jaegertracing/jaeger/model"
2019
"github.com/jaegertracing/jaeger/swagger-gen/models"
2120
"github.com/jaegertracing/jaeger/thrift-gen/zipkincore"
21+
"github.com/opentracing/opentracing-go/ext"
2222
)
2323

2424
func spansV2ToThrift(spans *models.ListOfSpans) ([]*zipkincore.Span, error) {
@@ -42,7 +42,6 @@ func spanV2ToThrift(s models.Span) (*zipkincore.Span, error) {
4242
if err != nil {
4343
return nil, err
4444
}
45-
4645
tSpan := &zipkincore.Span{
4746
ID: int64(id),
4847
TraceID: int64(traceID.Low),
@@ -61,68 +60,103 @@ func spanV2ToThrift(s models.Span) (*zipkincore.Span, error) {
6160
tSpan.ParentID = &signed
6261
}
6362

64-
localE, err := endpointToThrift(string(s.LocalEndpoint.IPV4),
65-
string(s.LocalEndpoint.IPV6),
66-
int32(s.LocalEndpoint.Port),
67-
s.LocalEndpoint.ServiceName)
68-
if err != nil {
69-
return nil, err
70-
}
63+
localE, err := endpointV2ToThrift(s.LocalEndpoint)
7164

7265
for _, a := range s.Annotations {
73-
tA := annToThrift(a, *localE)
66+
tA := annoV2ToThrift(a, *localE)
7467
tSpan.Annotations = append(tSpan.Annotations, tA)
7568
}
7669

77-
for k, v := range s.Tags {
78-
ba := &zipkincore.BinaryAnnotation{
79-
Key: k,
80-
Value: []byte(v),
81-
AnnotationType: zipkincore.AnnotationType_STRING,
82-
Host: localE,
70+
tSpan.BinaryAnnotations = append(tSpan.BinaryAnnotations, tagsToThrift(s.Tags, *localE)...)
71+
tSpan.Annotations = append(tSpan.Annotations, kindToThrift(s, localE)...)
72+
73+
if s.RemoteEndpoint != nil {
74+
if bAnno, err := rEndpToThrift(s.RemoteEndpoint, s.Kind); err != nil {
75+
tSpan.BinaryAnnotations = append(tSpan.BinaryAnnotations, bAnno)
76+
} else {
77+
return nil, err
8378
}
84-
tSpan.BinaryAnnotations = append(tSpan.BinaryAnnotations, ba)
8579
}
80+
return tSpan, nil
81+
}
8682

87-
if s.Kind == models.SpanKindCLIENT {
88-
tSpan.Annotations = append(tSpan.Annotations, &zipkincore.Annotation{
89-
Value: zipkincore.CLIENT_SEND,
83+
func rEndpToThrift(e *models.Endpoint, kind string) (*zipkincore.BinaryAnnotation, error) {
84+
rEndp, err := endpointV2ToThrift(e)
85+
if err != nil {
86+
return nil, err
87+
}
88+
89+
var key string
90+
switch kind {
91+
case models.SpanKindCLIENT:
92+
key = zipkincore.SERVER_ADDR
93+
case models.SpanKindSERVER:
94+
key = zipkincore.CLIENT_ADDR
95+
default:
96+
key = string(ext.PeerAddress)
97+
}
98+
99+
return &zipkincore.BinaryAnnotation{
100+
Key: key,
101+
Host: rEndp,
102+
AnnotationType: zipkincore.AnnotationType_BOOL,
103+
}, nil
104+
}
105+
106+
func kindToThrift(s models.Span, localE *zipkincore.Endpoint) []*zipkincore.Annotation {
107+
var annos []*zipkincore.Annotation
108+
switch s.Kind {
109+
case models.SpanKindCLIENT:
110+
annos = append(annos, &zipkincore.Annotation{
111+
Value: zipkincore.SERVER_RECV,
90112
Host: localE,
91113
Timestamp: s.Timestamp,
92114
})
93-
tSpan.Annotations = append(tSpan.Annotations, &zipkincore.Annotation{
94-
Value: zipkincore.CLIENT_RECV,
115+
annos = append(annos, &zipkincore.Annotation{
116+
Value: zipkincore.SERVER_SEND,
95117
Host: localE,
96118
Timestamp: s.Timestamp + s.Duration,
97119
})
98-
} else if s.Kind == models.SpanKindSERVER {
99-
tSpan.Annotations = append(tSpan.Annotations, &zipkincore.Annotation{
100-
Value: zipkincore.SERVER_RECV,
120+
case models.SpanKindSERVER:
121+
annos = append(annos, &zipkincore.Annotation{
122+
Value: zipkincore.CLIENT_SEND,
101123
Host: localE,
102124
Timestamp: s.Timestamp,
103125
})
104-
tSpan.Annotations = append(tSpan.Annotations, &zipkincore.Annotation{
105-
Value: zipkincore.SERVER_SEND,
126+
annos = append(annos, &zipkincore.Annotation{
127+
Value: zipkincore.CLIENT_RECV,
106128
Host: localE,
107129
Timestamp: s.Timestamp + s.Duration,
108130
})
131+
// TODO support for producer/consumer once idl supports it
109132
}
110-
// TODO consumer producer
111133

112-
// TODO
113-
//s.RemoteEndpoint
114-
115-
fmt.Println("V2 span")
116-
fmt.Println(tSpan)
134+
return annos
135+
}
117136

118-
return tSpan, nil
137+
func endpointV2ToThrift(e *models.Endpoint) (*zipkincore.Endpoint, error) {
138+
return eToThrift(string(e.IPV4), string(e.IPV6), int32(e.Port), e.ServiceName)
119139
}
120140

121-
func annToThrift(a *models.Annotation, e zipkincore.Endpoint) *zipkincore.Annotation {
141+
func annoV2ToThrift(a *models.Annotation, e zipkincore.Endpoint) *zipkincore.Annotation {
122142
ta := &zipkincore.Annotation{
123143
Value: a.Value,
124144
Timestamp: a.Timestamp,
125145
Host: &e,
126146
}
127147
return ta
128148
}
149+
150+
func tagsToThrift(tags models.Tags, localE zipkincore.Endpoint) []*zipkincore.BinaryAnnotation {
151+
var bAnnos []*zipkincore.BinaryAnnotation
152+
for k, v := range tags {
153+
ba := &zipkincore.BinaryAnnotation{
154+
Key: k,
155+
Value: []byte(v),
156+
AnnotationType: zipkincore.AnnotationType_STRING,
157+
Host: &localE,
158+
}
159+
bAnnos = append(bAnnos, ba)
160+
}
161+
return bAnnos
162+
}

0 commit comments

Comments
 (0)