netconv.go

  1// Code created by gotmpl. DO NOT MODIFY.
  2// source: internal/shared/semconvutil/netconv.go.tmpl
  3
  4// Copyright The OpenTelemetry Authors
  5// SPDX-License-Identifier: Apache-2.0
  6
  7package semconvutil // import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/internal/semconvutil"
  8
  9import (
 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// NetTransport returns a trace attribute describing the transport protocol of the
 19// passed network. See the net.Dial for information about acceptable network
 20// values.
 21func NetTransport(network string) attribute.KeyValue {
 22	return nc.Transport(network)
 23}
 24
 25// netConv are the network semantic convention attributes defined for a version
 26// of the OpenTelemetry specification.
 27type 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
 45var 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
 63func (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		// "ip:*", "ip4:*", and "ip6:*" all are considered other.
 73		return c.NetTransportOther
 74	}
 75}
 76
 77// Host returns attributes for a network host address.
 78func (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(p))
 96	}
 97	return attrs
 98}
 99
100func (c *netConv) HostName(name string) attribute.KeyValue {
101	return c.NetHostNameKey.String(name)
102}
103
104func (c *netConv) HostPort(port int) attribute.KeyValue {
105	return c.NetHostPortKey.Int(port)
106}
107
108func 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// Peer returns attributes for a network peer address.
124func (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(p))
142	}
143	return attrs
144}
145
146func (c *netConv) PeerName(name string) attribute.KeyValue {
147	return c.NetPeerNameKey.String(name)
148}
149
150func (c *netConv) PeerPort(port int) attribute.KeyValue {
151	return c.NetPeerPortKey.Int(port)
152}
153
154func (c *netConv) SockPeerAddr(addr string) attribute.KeyValue {
155	return c.NetSockPeerAddrKey.String(addr)
156}
157
158func (c *netConv) SockPeerPort(port int) attribute.KeyValue {
159	return c.NetSockPeerPortKey.Int(port)
160}
161
162// splitHostPort splits a network address hostport of the form "host",
163// "host%zone", "[host]", "[host%zone], "host:port", "host%zone:port",
164// "[host]:port", "[host%zone]:port", or ":port" into host or host%zone and
165// port.
166//
167// An empty host is returned if it is not provided or unparsable. A negative
168// port is returned if it is not provided or unparsable.
169func 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			// Invalid hostport.
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) // nolint: gosec  // Bitsize checked to be 16 above.
199}
200
201func netProtocol(proto string) (name string, version string) {
202	name, version, _ = strings.Cut(proto, "/")
203	name = strings.ToLower(name)
204	return name, version
205}