Debian Codegraph @

instrumentation/github.com/labstack/echo/otelecho/internal/semconv/util.go

raw
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/semconv/util.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 package semconv // import "go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho/internal/semconv"
8
9 import (
10 "net"
11 "net/http"
12 "strconv"
13 "strings"
14 "time"
15
16 "go.opentelemetry.io/otel"
17 "go.opentelemetry.io/otel/attribute"
18 semconvNew "go.opentelemetry.io/otel/semconv/v1.40.0"
19 )
20
21 // SplitHostPort splits a network address hostport of the form "host",
22 // "host%zone", "[host]", "[host%zone], "host:port", "host%zone:port",
23 // "[host]:port", "[host%zone]:port", or ":port" into host or host%zone and
24 // port.
25 //
26 // An empty host is returned if it is not provided or unparsable. A negative
27 // port is returned if it is not provided or unparsable.
28 func SplitHostPort(hostport string) (host string, port int) {
29 port = -1
30
31 if strings.HasPrefix(hostport, "[") {
32 addrEnd := strings.LastIndexByte(hostport, ']')
33 if addrEnd < 0 {
34 // Invalid hostport.
35 return
36 }
37 if i := strings.LastIndexByte(hostport[addrEnd:], ':'); i < 0 {
38 host = hostport[1:addrEnd]
39 return
40 }
41 } else {
42 if i := strings.LastIndexByte(hostport, ':'); i < 0 {
43 host = hostport
44 return
45 }
46 }
47
48 host, pStr, err := net.SplitHostPort(hostport)
49 if err != nil {
50 return
51 }
52
53 p, err := strconv.ParseUint(pStr, 10, 16)
54 if err != nil {
55 return
56 }
57 return host, int(p) //nolint:gosec // Byte size checked 16 above.
58 }
59
60 func requiredHTTPPort(https bool, port int) int { //nolint:revive // ignore linter
61 if https {
62 if port > 0 && port != 443 {
63 return port
64 }
65 } else {
66 if port > 0 && port != 80 {
67 return port
68 }
69 }
70 return -1
71 }
72
73 func serverClientIP(xForwardedFor string) string {
74 if idx := strings.IndexByte(xForwardedFor, ','); idx >= 0 {
75 xForwardedFor = xForwardedFor[:idx]
76 }
77 return xForwardedFor
78 }
79
80 func httpRoute(pattern string) string {
81 if idx := strings.IndexByte(pattern, '/'); idx >= 0 {
82 return pattern[idx:]
83 }
84 return ""
85 }
86
87 func netProtocol(proto string) (name string, version string) {
88 name, version, _ = strings.Cut(proto, "/")
89 switch name {
90 case "HTTP":
91 name = "http"
92 case "QUIC":
93 name = "quic"
94 case "SPDY":
95 name = "spdy"
96 default:
97 name = strings.ToLower(name)
98 }
99 return name, version
100 }
101
102 var methodLookup = map[string]attribute.KeyValue{
103 http.MethodConnect: semconvNew.HTTPRequestMethodConnect,
104 http.MethodDelete: semconvNew.HTTPRequestMethodDelete,
105 http.MethodGet: semconvNew.HTTPRequestMethodGet,
106 http.MethodHead: semconvNew.HTTPRequestMethodHead,
107 http.MethodOptions: semconvNew.HTTPRequestMethodOptions,
108 http.MethodPatch: semconvNew.HTTPRequestMethodPatch,
109 http.MethodPost: semconvNew.HTTPRequestMethodPost,
110 http.MethodPut: semconvNew.HTTPRequestMethodPut,
111 http.MethodTrace: semconvNew.HTTPRequestMethodTrace,
112 }
113
114 func handleErr(err error) {
115 if err != nil {
116 otel.Handle(err)
117 }
118 }
119
120 func standardizeHTTPMethod(method string) string {
121 method = strings.ToUpper(method)
122 switch method {
123 case http.MethodConnect, http.MethodDelete, http.MethodGet, http.MethodHead, http.MethodOptions, http.MethodPatch, http.MethodPost, http.MethodPut, http.MethodTrace:
124 default:
125 method = "_OTHER"
126 }
127 return method
128 }
129
130 func durationToSeconds(d time.Duration) float64 {
131 // Use floating point division here for higher precision (instead of Seconds method).
132 return float64(d) / float64(time.Second)
133 }