1
2
3
4
5
6
7 package semconvutil
8
9 import (
10 "net"
11 "strconv"
12 "strings"
13
14 "go.opentelemetry.io/otel/attribute"
15 semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
16 )
17
18
19
20
21 func NetTransport(network string) attribute.KeyValue {
22 return nc.Transport(network)
23 }
24
25
26
27 type netConv struct {
28 NetHostNameKey attribute.Key
29 NetHostPortKey attribute.Key
30 NetPeerNameKey attribute.Key
31 NetPeerPortKey attribute.Key
32 NetProtocolName attribute.Key
33 NetProtocolVersion attribute.Key
34 NetSockFamilyKey attribute.Key
35 NetSockPeerAddrKey attribute.Key
36 NetSockPeerPortKey attribute.Key
37 NetSockHostAddrKey attribute.Key
38 NetSockHostPortKey attribute.Key
39 NetTransportOther attribute.KeyValue
40 NetTransportTCP attribute.KeyValue
41 NetTransportUDP attribute.KeyValue
42 NetTransportInProc attribute.KeyValue
43 }
44
45 var nc = &netConv{
46 NetHostNameKey: semconv.NetHostNameKey,
47 NetHostPortKey: semconv.NetHostPortKey,
48 NetPeerNameKey: semconv.NetPeerNameKey,
49 NetPeerPortKey: semconv.NetPeerPortKey,
50 NetProtocolName: semconv.NetProtocolNameKey,
51 NetProtocolVersion: semconv.NetProtocolVersionKey,
52 NetSockFamilyKey: semconv.NetSockFamilyKey,
53 NetSockPeerAddrKey: semconv.NetSockPeerAddrKey,
54 NetSockPeerPortKey: semconv.NetSockPeerPortKey,
55 NetSockHostAddrKey: semconv.NetSockHostAddrKey,
56 NetSockHostPortKey: semconv.NetSockHostPortKey,
57 NetTransportOther: semconv.NetTransportOther,
58 NetTransportTCP: semconv.NetTransportTCP,
59 NetTransportUDP: semconv.NetTransportUDP,
60 NetTransportInProc: semconv.NetTransportInProc,
61 }
62
63 func (c *netConv) Transport(network string) attribute.KeyValue {
64 switch network {
65 case "tcp", "tcp4", "tcp6":
66 return c.NetTransportTCP
67 case "udp", "udp4", "udp6":
68 return c.NetTransportUDP
69 case "unix", "unixgram", "unixpacket":
70 return c.NetTransportInProc
71 default:
72
73 return c.NetTransportOther
74 }
75 }
76
77
78 func (c *netConv) Host(address string) []attribute.KeyValue {
79 h, p := splitHostPort(address)
80 var n int
81 if h != "" {
82 n++
83 if p > 0 {
84 n++
85 }
86 }
87
88 if n == 0 {
89 return nil
90 }
91
92 attrs := make([]attribute.KeyValue, 0, n)
93 attrs = append(attrs, c.HostName(h))
94 if p > 0 {
95 attrs = append(attrs, c.HostPort(int(p)))
96 }
97 return attrs
98 }
99
100 func (c *netConv) HostName(name string) attribute.KeyValue {
101 return c.NetHostNameKey.String(name)
102 }
103
104 func (c *netConv) HostPort(port int) attribute.KeyValue {
105 return c.NetHostPortKey.Int(port)
106 }
107
108 func family(network, address string) string {
109 switch network {
110 case "unix", "unixgram", "unixpacket":
111 return "unix"
112 default:
113 if ip := net.ParseIP(address); ip != nil {
114 if ip.To4() == nil {
115 return "inet6"
116 }
117 return "inet"
118 }
119 }
120 return ""
121 }
122
123
124 func (c *netConv) Peer(address string) []attribute.KeyValue {
125 h, p := splitHostPort(address)
126 var n int
127 if h != "" {
128 n++
129 if p > 0 {
130 n++
131 }
132 }
133
134 if n == 0 {
135 return nil
136 }
137
138 attrs := make([]attribute.KeyValue, 0, n)
139 attrs = append(attrs, c.PeerName(h))
140 if p > 0 {
141 attrs = append(attrs, c.PeerPort(int(p)))
142 }
143 return attrs
144 }
145
146 func (c *netConv) PeerName(name string) attribute.KeyValue {
147 return c.NetPeerNameKey.String(name)
148 }
149
150 func (c *netConv) PeerPort(port int) attribute.KeyValue {
151 return c.NetPeerPortKey.Int(port)
152 }
153
154 func (c *netConv) SockPeerAddr(addr string) attribute.KeyValue {
155 return c.NetSockPeerAddrKey.String(addr)
156 }
157
158 func (c *netConv) SockPeerPort(port int) attribute.KeyValue {
159 return c.NetSockPeerPortKey.Int(port)
160 }
161
162
163
164
165
166
167
168
169 func splitHostPort(hostport string) (host string, port int) {
170 port = -1
171
172 if strings.HasPrefix(hostport, "[") {
173 addrEnd := strings.LastIndex(hostport, "]")
174 if addrEnd < 0 {
175
176 return
177 }
178 if i := strings.LastIndex(hostport[addrEnd:], ":"); i < 0 {
179 host = hostport[1:addrEnd]
180 return
181 }
182 } else {
183 if i := strings.LastIndex(hostport, ":"); i < 0 {
184 host = hostport
185 return
186 }
187 }
188
189 host, pStr, err := net.SplitHostPort(hostport)
190 if err != nil {
191 return
192 }
193
194 p, err := strconv.ParseUint(pStr, 10, 16)
195 if err != nil {
196 return
197 }
198 return host, int(p)
199 }
200
201 func netProtocol(proto string) (name string, version string) {
202 name, version, _ = strings.Cut(proto, "/")
203 name = strings.ToLower(name)
204 return name, version
205 }
206
View as plain text