forked from openzipkin-contrib/zipkin-go-opentracing
/
zipkin-endpoint.go
60 lines (52 loc) · 1.32 KB
/
zipkin-endpoint.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
50
51
52
53
54
55
56
57
58
59
60
package zipkintracer
import (
"encoding/binary"
"net"
"strconv"
"github.com/openzipkin/zipkin-go-opentracing/_thrift/gen-go/zipkincore"
)
// makeEndpoint takes the hostport and service name that represent this Zipkin
// service, and returns an endpoint that's embedded into the Zipkin core Span
// type. It will return a nil endpoint if the input parameters are malformed.
func makeEndpoint(hostport, serviceName string) *zipkincore.Endpoint {
host, port, err := net.SplitHostPort(hostport)
if err != nil {
return nil
}
portInt, err := strconv.ParseInt(port, 10, 16)
if err != nil {
return nil
}
addrs, err := net.LookupIP(host)
if err != nil {
return nil
}
var addr4, addr16 net.IP
for i := range addrs {
if addr := addrs[i].To4(); addr == nil {
if addr16 == nil {
addr16 = addrs[i].To16() // IPv6 - 16 bytes
}
} else {
if addr4 == nil {
addr4 = addr // IPv4 - 4 bytes
}
}
if addr16 != nil && addr4 != nil {
break
}
}
if addr4 == nil {
if addr16 == nil {
return nil
}
// we have an IPv6 but no IPv4, code IPv4 as 0 (none found)
addr4 = []byte("\x00\x00\x00\x00")
}
endpoint := zipkincore.NewEndpoint()
endpoint.Ipv4 = (int32)(binary.BigEndian.Uint32(addr4))
endpoint.Ipv6 = []byte(addr16)
endpoint.Port = int16(portInt)
endpoint.ServiceName = serviceName
return endpoint
}