-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer_tracing.go
49 lines (40 loc) · 1.12 KB
/
consumer_tracing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package kafka_zipkin_interceptor
import (
"fmt"
"github.com/openzipkin/zipkin-go"
"github.com/openzipkin/zipkin-go/model"
"github.com/segmentio/kafka-go"
"strconv"
)
func ExtractTraceInfo(m kafka.Message, key string, topic string, clientId string, groupId string, t *zipkin.Tracer) zipkin.Span {
traceId := ""
spanId := ""
for _, header := range m.Headers {
if header.Key == "X-B3-TraceId" {
traceId = string(header.Value)
} else if header.Key == "X-B3-SpanId" {
spanId = string(header.Value)
}
}
fmt.Printf("Extracted trace and span id : [%s] [%s]", traceId, spanId)
unitSpanId, _ := strconv.ParseUint(spanId, 0, 64)
traceIdModel, _ := model.TraceIDFromHex(traceId)
spanContext := model.SpanContext{
TraceID: traceIdModel,
ID: model.ID(unitSpanId),
}
tags := map[string]string {
KAFKA_KEY: key,
KAFKA_TOPIC: topic,
KAFKA_CLIENT_ID: clientId,
KAFKA_GROUP_ID: groupId,
}
span := t.StartSpan(
SPAN_NAME_POLL,
zipkin.RemoteEndpoint(&model.Endpoint{ServiceName: REMOTE_SERVICE_NAME_DEFAULT}),
zipkin.Tags(tags),
zipkin.Kind(model.Consumer),
zipkin.Parent(spanContext),
)
return span
}