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

Commit d17cdd6

Browse files
author
Paulo Janotti
authored
Use enums to replace attribute's runtime prefix function
Instead of always dynamically creating a prefix function to determine the prefix for Zipkin attributes, we use enumerations of the direction and create strings at compile time. This is similar to the work that we did on the Zipkin exporter
1 parent 2e6b582 commit d17cdd6

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

receiver/zipkin/trace_receiver.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"io/ioutil"
2727
"net"
2828
"net/http"
29+
"strconv"
2930
"strings"
3031
"sync"
3132

@@ -392,7 +393,7 @@ func nodeFromZipkinEndpoints(zs *zipkinmodel.SpanModel) *commonpb.Node {
392393
node.ServiceInfo = &commonpb.ServiceInfo{
393394
Name: lep.ServiceName,
394395
}
395-
node.Attributes = zipkinEndpointIntoAttributes(lep, node.Attributes, func(s string) string { return s })
396+
node.Attributes = zipkinEndpointIntoAttributes(lep, node.Attributes, isLocalEndpoint)
396397
}
397398

398399
// Retrieve and make use of the remote endpoint
@@ -404,30 +405,44 @@ func nodeFromZipkinEndpoints(zs *zipkinmodel.SpanModel) *commonpb.Node {
404405
// "zipkin.remoteEndpoint.port": "9000"
405406
// "zipkin.remoteEndpoint.serviceName": "backend",
406407
// }
407-
node.Attributes = zipkinEndpointIntoAttributes(rep, node.Attributes, func(s string) string {
408-
return "zipkin.remoteEndpoint." + s
409-
})
408+
node.Attributes = zipkinEndpointIntoAttributes(rep, node.Attributes, isRemoteEndpoint)
410409
}
411410
return node
412411
}
413412

413+
type zipkinDirection bool
414+
415+
const (
416+
isLocalEndpoint zipkinDirection = true
417+
isRemoteEndpoint zipkinDirection = false
418+
)
419+
414420
var blankIP net.IP
415421

416-
func zipkinEndpointIntoAttributes(ep *zipkinmodel.Endpoint, into map[string]string, prefixFunc func(string) string) map[string]string {
422+
func zipkinEndpointIntoAttributes(ep *zipkinmodel.Endpoint, into map[string]string, endpointType zipkinDirection) map[string]string {
417423
if into == nil {
418424
into = make(map[string]string)
419425
}
426+
427+
var ipv4Key, ipv6Key, portKey, serviceNameKey string
428+
if endpointType == isLocalEndpoint {
429+
ipv4Key, ipv6Key = "ipv4", "ipv6"
430+
portKey, serviceNameKey = "port", "serviceName"
431+
} else {
432+
ipv4Key, ipv6Key = "zipkin.remoteEndpoint.ipv4", "zipkin.remoteEndpoint.ipv6"
433+
portKey, serviceNameKey = "zipkin.remoteEndpoint.port", "zipkin.remoteEndpoint.serviceName"
434+
}
420435
if ep.IPv4 != nil && !ep.IPv4.Equal(blankIP) {
421-
into[prefixFunc("ipv4")] = ep.IPv4.String()
436+
into[ipv4Key] = ep.IPv4.String()
422437
}
423438
if ep.IPv6 != nil && !ep.IPv6.Equal(blankIP) {
424-
into[prefixFunc("ipv6")] = ep.IPv6.String()
439+
into[ipv6Key] = ep.IPv6.String()
425440
}
426441
if ep.Port > 0 {
427-
into[prefixFunc("port")] = fmt.Sprintf("%d", ep.Port)
442+
into[portKey] = strconv.Itoa(int(ep.Port))
428443
}
429444
if serviceName := ep.ServiceName; serviceName != "" {
430-
into[prefixFunc("serviceName")] = serviceName
445+
into[serviceNameKey] = serviceName
431446
}
432447
return into
433448
}

0 commit comments

Comments
 (0)