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