forked from openzipkin-contrib/zipkin-go-opentracing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipkin-endpoint.go
72 lines (62 loc) · 1.64 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
61
62
63
64
65
66
67
68
69
70
71
72
package zipkintracer
import (
"encoding/binary"
"net"
"strconv"
"strings"
"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) (ep *zipkincore.Endpoint) {
ep = zipkincore.NewEndpoint()
// Set the ServiceName
ep.ServiceName = serviceName
if strings.IndexByte(hostport, ':') < 0 {
// "<host>" becomes "<host>:0"
hostport = hostport + ":0"
}
// try to parse provided "<host>:<port>"
host, port, err := net.SplitHostPort(hostport)
if err != nil {
// if unparsable, return as "undefined:0"
return
}
// try to set port number
p, _ := strconv.ParseUint(port, 10, 16)
ep.Port = int16(p)
// if <host> is a domain name, look it up
addrs, err := net.LookupIP(host)
if err != nil {
// return as "undefined:<port>"
return
}
var addr4, addr16 net.IP
for i := range addrs {
addr := addrs[i].To4()
if addr == nil {
// IPv6
if addr16 == nil {
addr16 = addrs[i].To16() // IPv6 - 16 bytes
}
} else {
// IPv4
if addr4 == nil {
addr4 = addr // IPv4 - 4 bytes
}
}
if addr16 != nil && addr4 != nil {
// IPv4 & IPv6 have been set, we can stop looking further
break
}
}
// default to 0 filled 4 byte array for IPv4 if IPv6 only host was found
if addr4 == nil {
addr4 = make([]byte, 4)
}
// set IPv4 and IPv6 addresses
ep.Ipv4 = (int32)(binary.BigEndian.Uint32(addr4))
ep.Ipv6 = []byte(addr16)
return
}