| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package internal // import "go.opentelemetry.io/otel/semconv/internal/v3" |
| 5 |
|
| 6 |
import ( |
| 7 |
"net" |
| 8 |
"strconv" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/attribute" |
| 12 |
) |
| 13 |
|
| 14 |
// NetConv are the network semantic convention attributes defined for a version |
| 15 |
// of the OpenTelemetry specification. |
| 16 |
type NetConv struct { |
| 17 |
NetHostNameKey attribute.Key |
| 18 |
NetHostPortKey attribute.Key |
| 19 |
NetPeerNameKey attribute.Key |
| 20 |
NetPeerPortKey attribute.Key |
| 21 |
NetSockFamilyKey attribute.Key |
| 22 |
NetSockPeerAddrKey attribute.Key |
| 23 |
NetSockPeerPortKey attribute.Key |
| 24 |
NetSockHostAddrKey attribute.Key |
| 25 |
NetSockHostPortKey attribute.Key |
| 26 |
NetTransportOther attribute.KeyValue |
| 27 |
NetTransportTCP attribute.KeyValue |
| 28 |
NetTransportUDP attribute.KeyValue |
| 29 |
NetTransportInProc attribute.KeyValue |
| 30 |
} |
| 31 |
|
| 32 |
func (c *NetConv) Transport(network string) attribute.KeyValue { |
| 33 |
switch network { |
| 34 |
case "tcp", "tcp4", "tcp6": |
| 35 |
return c.NetTransportTCP |
| 36 |
case "udp", "udp4", "udp6": |
| 37 |
return c.NetTransportUDP |
| 38 |
case "unix", "unixgram", "unixpacket": |
| 39 |
return c.NetTransportInProc |
| 40 |
default: |
| 41 |
// "ip:*", "ip4:*", and "ip6:*" all are considered other. |
| 42 |
return c.NetTransportOther |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// Host returns attributes for a network host address. |
| 47 |
func (c *NetConv) Host(address string) []attribute.KeyValue { |
| 48 |
h, p := splitHostPort(address) |
| 49 |
var n int |
| 50 |
if h != "" { |
| 51 |
n++ |
| 52 |
if p > 0 { |
| 53 |
n++ |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if n == 0 { |
| 58 |
return nil |
| 59 |
} |
| 60 |
|
| 61 |
attrs := make([]attribute.KeyValue, 0, n) |
| 62 |
attrs = append(attrs, c.HostName(h)) |
| 63 |
if p > 0 { |
| 64 |
attrs = append(attrs, c.HostPort(p)) |
| 65 |
} |
| 66 |
return attrs |
| 67 |
} |
| 68 |
|
| 69 |
// Server returns attributes for a network listener listening at address. See |
| 70 |
// net.Listen for information about acceptable address values, address should |
| 71 |
// be the same as the one used to create ln. If ln is nil, only network host |
| 72 |
// attributes will be returned that describe address. Otherwise, the socket |
| 73 |
// level information about ln will also be included. |
| 74 |
func (c *NetConv) Server(address string, ln net.Listener) []attribute.KeyValue { |
| 75 |
if ln == nil { |
| 76 |
return c.Host(address) |
| 77 |
} |
| 78 |
|
| 79 |
lAddr := ln.Addr() |
| 80 |
if lAddr == nil { |
| 81 |
return c.Host(address) |
| 82 |
} |
| 83 |
|
| 84 |
hostName, hostPort := splitHostPort(address) |
| 85 |
sockHostAddr, sockHostPort := splitHostPort(lAddr.String()) |
| 86 |
network := lAddr.Network() |
| 87 |
sockFamily := family(network, sockHostAddr) |
| 88 |
|
| 89 |
n := nonZeroStr(hostName, network, sockHostAddr, sockFamily) |
| 90 |
n += positiveInt(hostPort, sockHostPort) |
| 91 |
attr := make([]attribute.KeyValue, 0, n) |
| 92 |
if hostName != "" { |
| 93 |
attr = append(attr, c.HostName(hostName)) |
| 94 |
if hostPort > 0 { |
| 95 |
// Only if net.host.name is set should net.host.port be. |
| 96 |
attr = append(attr, c.HostPort(hostPort)) |
| 97 |
} |
| 98 |
} |
| 99 |
if network != "" { |
| 100 |
attr = append(attr, c.Transport(network)) |
| 101 |
} |
| 102 |
if sockFamily != "" { |
| 103 |
attr = append(attr, c.NetSockFamilyKey.String(sockFamily)) |
| 104 |
} |
| 105 |
if sockHostAddr != "" { |
| 106 |
attr = append(attr, c.NetSockHostAddrKey.String(sockHostAddr)) |
| 107 |
if sockHostPort > 0 { |
| 108 |
// Only if net.sock.host.addr is set should net.sock.host.port be. |
| 109 |
attr = append(attr, c.NetSockHostPortKey.Int(sockHostPort)) |
| 110 |
} |
| 111 |
} |
| 112 |
return attr |
| 113 |
} |
| 114 |
|
| 115 |
func (c *NetConv) HostName(name string) attribute.KeyValue { |
| 116 |
return c.NetHostNameKey.String(name) |
| 117 |
} |
| 118 |
|
| 119 |
func (c *NetConv) HostPort(port int) attribute.KeyValue { |
| 120 |
return c.NetHostPortKey.Int(port) |
| 121 |
} |
| 122 |
|
| 123 |
// Client returns attributes for a client network connection to address. See |
| 124 |
// net.Dial for information about acceptable address values, address should be |
| 125 |
// the same as the one used to create conn. If conn is nil, only network peer |
| 126 |
// attributes will be returned that describe address. Otherwise, the socket |
| 127 |
// level information about conn will also be included. |
| 128 |
func (c *NetConv) Client(address string, conn net.Conn) []attribute.KeyValue { |
| 129 |
if conn == nil { |
| 130 |
return c.Peer(address) |
| 131 |
} |
| 132 |
|
| 133 |
lAddr, rAddr := conn.LocalAddr(), conn.RemoteAddr() |
| 134 |
|
| 135 |
var network string |
| 136 |
switch { |
| 137 |
case lAddr != nil: |
| 138 |
network = lAddr.Network() |
| 139 |
case rAddr != nil: |
| 140 |
network = rAddr.Network() |
| 141 |
default: |
| 142 |
return c.Peer(address) |
| 143 |
} |
| 144 |
|
| 145 |
peerName, peerPort := splitHostPort(address) |
| 146 |
var ( |
| 147 |
sockFamily string |
| 148 |
sockPeerAddr string |
| 149 |
sockPeerPort int |
| 150 |
sockHostAddr string |
| 151 |
sockHostPort int |
| 152 |
) |
| 153 |
|
| 154 |
if lAddr != nil { |
| 155 |
sockHostAddr, sockHostPort = splitHostPort(lAddr.String()) |
| 156 |
} |
| 157 |
|
| 158 |
if rAddr != nil { |
| 159 |
sockPeerAddr, sockPeerPort = splitHostPort(rAddr.String()) |
| 160 |
} |
| 161 |
|
| 162 |
switch { |
| 163 |
case sockHostAddr != "": |
| 164 |
sockFamily = family(network, sockHostAddr) |
| 165 |
case sockPeerAddr != "": |
| 166 |
sockFamily = family(network, sockPeerAddr) |
| 167 |
} |
| 168 |
|
| 169 |
n := nonZeroStr(peerName, network, sockPeerAddr, sockHostAddr, sockFamily) |
| 170 |
n += positiveInt(peerPort, sockPeerPort, sockHostPort) |
| 171 |
attr := make([]attribute.KeyValue, 0, n) |
| 172 |
if peerName != "" { |
| 173 |
attr = append(attr, c.PeerName(peerName)) |
| 174 |
if peerPort > 0 { |
| 175 |
// Only if net.peer.name is set should net.peer.port be. |
| 176 |
attr = append(attr, c.PeerPort(peerPort)) |
| 177 |
} |
| 178 |
} |
| 179 |
if network != "" { |
| 180 |
attr = append(attr, c.Transport(network)) |
| 181 |
} |
| 182 |
if sockFamily != "" { |
| 183 |
attr = append(attr, c.NetSockFamilyKey.String(sockFamily)) |
| 184 |
} |
| 185 |
if sockPeerAddr != "" { |
| 186 |
attr = append(attr, c.NetSockPeerAddrKey.String(sockPeerAddr)) |
| 187 |
if sockPeerPort > 0 { |
| 188 |
// Only if net.sock.peer.addr is set should net.sock.peer.port be. |
| 189 |
attr = append(attr, c.NetSockPeerPortKey.Int(sockPeerPort)) |
| 190 |
} |
| 191 |
} |
| 192 |
if sockHostAddr != "" { |
| 193 |
attr = append(attr, c.NetSockHostAddrKey.String(sockHostAddr)) |
| 194 |
if sockHostPort > 0 { |
| 195 |
// Only if net.sock.host.addr is set should net.sock.host.port be. |
| 196 |
attr = append(attr, c.NetSockHostPortKey.Int(sockHostPort)) |
| 197 |
} |
| 198 |
} |
| 199 |
return attr |
| 200 |
} |
| 201 |
|
| 202 |
func family(network, address string) string { |
| 203 |
switch network { |
| 204 |
case "unix", "unixgram", "unixpacket": |
| 205 |
return "unix" |
| 206 |
default: |
| 207 |
if ip := net.ParseIP(address); ip != nil { |
| 208 |
if ip.To4() == nil { |
| 209 |
return "inet6" |
| 210 |
} |
| 211 |
return "inet" |
| 212 |
} |
| 213 |
} |
| 214 |
return "" |
| 215 |
} |
| 216 |
|
| 217 |
func nonZeroStr(strs ...string) int { |
| 218 |
var n int |
| 219 |
for _, str := range strs { |
| 220 |
if str != "" { |
| 221 |
n++ |
| 222 |
} |
| 223 |
} |
| 224 |
return n |
| 225 |
} |
| 226 |
|
| 227 |
func positiveInt(ints ...int) int { |
| 228 |
var n int |
| 229 |
for _, i := range ints { |
| 230 |
if i > 0 { |
| 231 |
n++ |
| 232 |
} |
| 233 |
} |
| 234 |
return n |
| 235 |
} |
| 236 |
|
| 237 |
// Peer returns attributes for a network peer address. |
| 238 |
func (c *NetConv) Peer(address string) []attribute.KeyValue { |
| 239 |
h, p := splitHostPort(address) |
| 240 |
var n int |
| 241 |
if h != "" { |
| 242 |
n++ |
| 243 |
if p > 0 { |
| 244 |
n++ |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
if n == 0 { |
| 249 |
return nil |
| 250 |
} |
| 251 |
|
| 252 |
attrs := make([]attribute.KeyValue, 0, n) |
| 253 |
attrs = append(attrs, c.PeerName(h)) |
| 254 |
if p > 0 { |
| 255 |
attrs = append(attrs, c.PeerPort(p)) |
| 256 |
} |
| 257 |
return attrs |
| 258 |
} |
| 259 |
|
| 260 |
func (c *NetConv) PeerName(name string) attribute.KeyValue { |
| 261 |
return c.NetPeerNameKey.String(name) |
| 262 |
} |
| 263 |
|
| 264 |
func (c *NetConv) PeerPort(port int) attribute.KeyValue { |
| 265 |
return c.NetPeerPortKey.Int(port) |
| 266 |
} |
| 267 |
|
| 268 |
func (c *NetConv) SockPeerAddr(addr string) attribute.KeyValue { |
| 269 |
return c.NetSockPeerAddrKey.String(addr) |
| 270 |
} |
| 271 |
|
| 272 |
func (c *NetConv) SockPeerPort(port int) attribute.KeyValue { |
| 273 |
return c.NetSockPeerPortKey.Int(port) |
| 274 |
} |
| 275 |
|
| 276 |
// splitHostPort splits a network address hostport of the form "host", |
| 277 |
// "host%zone", "[host]", "[host%zone], "host:port", "host%zone:port", |
| 278 |
// "[host]:port", "[host%zone]:port", or ":port" into host or host%zone and |
| 279 |
// port. |
| 280 |
// |
| 281 |
// An empty host is returned if it is not provided or unparsable. A negative |
| 282 |
// port is returned if it is not provided or unparsable. |
| 283 |
func splitHostPort(hostport string) (host string, port int) { |
| 284 |
port = -1 |
| 285 |
|
| 286 |
if strings.HasPrefix(hostport, "[") { |
| 287 |
addrEnd := strings.LastIndex(hostport, "]") |
| 288 |
if addrEnd < 0 { |
| 289 |
// Invalid hostport. |
| 290 |
return host, port |
| 291 |
} |
| 292 |
if i := strings.LastIndex(hostport[addrEnd:], ":"); i < 0 { |
| 293 |
host = hostport[1:addrEnd] |
| 294 |
return host, port |
| 295 |
} |
| 296 |
} else { |
| 297 |
if i := strings.LastIndex(hostport, ":"); i < 0 { |
| 298 |
host = hostport |
| 299 |
return host, port |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
host, pStr, err := net.SplitHostPort(hostport) |
| 304 |
if err != nil { |
| 305 |
return host, port |
| 306 |
} |
| 307 |
|
| 308 |
p, err := strconv.ParseUint(pStr, 10, 16) |
| 309 |
if err != nil { |
| 310 |
return host, port |
| 311 |
} |
| 312 |
return host, int(p) // nolint: gosec // Bit size of 16 checked above. |
| 313 |
} |