Debian Codegraph @

internal/shared/otlp/observ/target.go.tmpl

raw
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/otlp/observ/target.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 package {{ .pkg }} // import "{{ .pkg_path }}"
8
9 import (
10 "errors"
11 "fmt"
12 "net"
13 "net/netip"
14 "strconv"
15 "strings"
16 )
17
18 const (
19 schemeUnix = "unix"
20 schemeUnixAbstract = "unix-abstract"
21 )
22
23 // ParseCanonicalTarget parses a target string and returns the extracted host
24 // (domain address or IP), the target port, or an error.
25 //
26 // If no port is specified, -1 is returned.
27 //
28 // If no host is specified, an empty string is returned.
29 //
30 // The target string is expected to always have the form
31 // "<scheme>://[authority]/<endpoint>". For example:
32 // - "dns:///example.com:42"
33 // - "dns://8.8.8.8/example.com:42"
34 // - "unix:///path/to/socket"
35 // - "unix-abstract:///socket-name"
36 // - "passthrough:///192.34.2.1:42"
37 //
38 // The target is expected to come from the CanonicalTarget method of a gRPC
39 // Client.
40 func ParseCanonicalTarget(target string) (string, int, error) {
41 const sep = "://"
42
43 // Find scheme. Do not allocate the string by using url.Parse.
44 idx := strings.Index(target, sep)
45 if idx == -1 {
46 return "", -1, fmt.Errorf("invalid target %q: missing scheme", target)
47 }
48 scheme, endpoint := target[:idx], target[idx+len(sep):]
49
50 // Check for unix schemes.
51 if scheme == schemeUnix || scheme == schemeUnixAbstract {
52 return parseUnix(endpoint)
53 }
54
55 // Strip leading slash and any authority.
56 if i := strings.Index(endpoint, "/"); i != -1 {
57 endpoint = endpoint[i+1:]
58 }
59
60 // DNS, passthrough, and custom resolvers.
61 return parseEndpoint(endpoint)
62 }
63
64 // parseUnix parses unix socket targets.
65 func parseUnix(endpoint string) (string, int, error) {
66 // Format: unix[-abstract]://path
67 //
68 // We should have "/path" (empty authority) if valid.
69 if len(endpoint) >= 1 && endpoint[0] == '/' {
70 // Return the full path including leading slash.
71 return endpoint, -1, nil
72 }
73
74 // If there's no leading slash, it means there might be an authority
75 // Check for authority case (should error): "authority/path"
76 if slashIdx := strings.Index(endpoint, "/"); slashIdx > 0 {
77 return "", -1, fmt.Errorf("invalid (non-empty) authority: %s", endpoint[:slashIdx])
78 }
79
80 return "", -1, errors.New("invalid unix target format")
81 }
82
83 // parseEndpoint parses an endpoint from a gRPC target.
84 //
85 // It supports the following formats:
86 // - "host"
87 // - "host%zone"
88 // - "host:port"
89 // - "host%zone:port"
90 // - "ipv4"
91 // - "ipv4%zone"
92 // - "ipv4:port"
93 // - "ipv4%zone:port"
94 // - "ipv6"
95 // - "ipv6%zone"
96 // - "[ipv6]"
97 // - "[ipv6%zone]"
98 // - "[ipv6]:port"
99 // - "[ipv6%zone]:port"
100 //
101 // It returns the host or host%zone (domain address or IP), the port (or -1 if
102 // not specified), or an error if the input is not a valid.
103 func parseEndpoint(endpoint string) (string, int, error) {
104 // First check if the endpoint is just an IP address.
105 if ip := parseIP(endpoint); ip != "" {
106 return ip, -1, nil
107 }
108
109 // If there's no colon, there is no port (IPv6 with no port checked above).
110 if !strings.Contains(endpoint, ":") {
111 return endpoint, -1, nil
112 }
113
114 host, portStr, err := net.SplitHostPort(endpoint)
115 if err != nil {
116 return "", -1, fmt.Errorf("invalid host:port %q: %w", endpoint, err)
117 }
118
119 const base, bitSize = 10, 16
120 port16, err := strconv.ParseUint(portStr, base, bitSize)
121 if err != nil {
122 return "", -1, fmt.Errorf("invalid port %q: %w", portStr, err)
123 }
124 port := int(port16) // port is guaranteed to be in the range [0, 65535].
125
126 return host, port, nil
127 }
128
129 // parseIP attempts to parse the entire endpoint as an IP address.
130 // It returns the normalized string form of the IP if successful,
131 // or an empty string if parsing fails.
132 func parseIP(ip string) string {
133 // Strip leading and trailing brackets for IPv6 addresses.
134 if len(ip) >= 2 && ip[0] == '[' && ip[len(ip)-1] == ']' {
135 ip = ip[1 : len(ip)-1]
136 }
137 addr, err := netip.ParseAddr(ip)
138 if err != nil {
139 return ""
140 }
141 // Return the normalized string form of the IP.
142 return addr.String()
143 }