diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/api/metrics.go | 66 | ||||
| -rw-r--r-- | internal/api/metrics/metrics.go | 60 | ||||
| -rw-r--r-- | internal/config/config.go | 12 | ||||
| -rw-r--r-- | internal/config/defaults.go | 9 | ||||
| -rw-r--r-- | internal/config/helpers.gen.go | 150 | ||||
| -rw-r--r-- | internal/observability/metrics.go | 32 | ||||
| -rw-r--r-- | internal/observability/otel.go (renamed from internal/api/metrics/no_metrics.go) | 23 | ||||
| -rw-r--r-- | internal/observability/tracing.go | 134 |
8 files changed, 103 insertions, 383 deletions
diff --git a/internal/api/metrics.go b/internal/api/metrics.go deleted file mode 100644 index add14b0bb..000000000 --- a/internal/api/metrics.go +++ /dev/null @@ -1,66 +0,0 @@ -// GoToSocial -// Copyright (C) GoToSocial Authors admin@gotosocial.org -// SPDX-License-Identifier: AGPL-3.0-or-later -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see <http://www.gnu.org/licenses/>. - -package api - -import ( - "code.superseriousbusiness.org/gotosocial/internal/api/metrics" - "code.superseriousbusiness.org/gotosocial/internal/config" - "code.superseriousbusiness.org/gotosocial/internal/middleware" - "code.superseriousbusiness.org/gotosocial/internal/router" - "github.com/gin-gonic/gin" -) - -type Metrics struct { - metrics *metrics.Module -} - -func (mt *Metrics) Route(r *router.Router, m ...gin.HandlerFunc) { - if !config.GetMetricsEnabled() { - // Noop: metrics - // not enabled. - return - } - - // Create new group on top level "metrics" prefix. - metricsGroup := r.AttachGroup("metrics") - metricsGroup.Use(m...) - metricsGroup.Use( - middleware.CacheControl(middleware.CacheControlConfig{ - // Never cache metrics responses. - Directives: []string{"no-store"}, - }), - ) - - // Attach basic auth if enabled. - if config.GetMetricsAuthEnabled() { - var ( - username = config.GetMetricsAuthUsername() - password = config.GetMetricsAuthPassword() - accounts = gin.Accounts{username: password} - ) - metricsGroup.Use(gin.BasicAuth(accounts)) - } - - mt.metrics.Route(metricsGroup.Handle) -} - -func NewMetrics() *Metrics { - return &Metrics{ - metrics: metrics.New(), - } -} diff --git a/internal/api/metrics/metrics.go b/internal/api/metrics/metrics.go deleted file mode 100644 index 5161a3b71..000000000 --- a/internal/api/metrics/metrics.go +++ /dev/null @@ -1,60 +0,0 @@ -// GoToSocial -// Copyright (C) GoToSocial Authors admin@gotosocial.org -// SPDX-License-Identifier: AGPL-3.0-or-later -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see <http://www.gnu.org/licenses/>. - -//go:build !nootel - -package metrics - -import ( - "net/http" - - "github.com/gin-gonic/gin" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" -) - -type Module struct { - handler http.Handler -} - -func New() *Module { - // Let prometheus use "identity", ie., no compression, - // or "gzip", to match our own gzip compression middleware. - opts := promhttp.HandlerOpts{ - OfferedCompressions: []promhttp.Compression{ - promhttp.Identity, - promhttp.Gzip, - }, - } - - // Instrument handler itself. - handler := promhttp.InstrumentMetricHandler( - prometheus.DefaultRegisterer, - promhttp.HandlerFor(prometheus.DefaultGatherer, opts), - ) - - return &Module{ - handler: handler, - } -} - -func (m *Module) Route(attachHandler func(method string, path string, f ...gin.HandlerFunc) gin.IRoutes) { - attachHandler(http.MethodGet, "", func(c *gin.Context) { - // Defer all "/metrics" handling to prom. - m.handler.ServeHTTP(c.Writer, c.Request) - }) -} diff --git a/internal/config/config.go b/internal/config/config.go index 88083d491..2c4035ecf 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -145,16 +145,8 @@ type Configuration struct { OIDCLinkExisting bool `name:"oidc-link-existing" usage:"link existing user accounts to OIDC logins based on the stored email value"` OIDCAllowedGroups []string `name:"oidc-allowed-groups" usage:"Membership of one of the listed groups allows access to GtS. If this is empty, all groups are allowed."` OIDCAdminGroups []string `name:"oidc-admin-groups" usage:"Membership of one of the listed groups makes someone a GtS admin"` - - TracingEnabled bool `name:"tracing-enabled" usage:"Enable OTLP Tracing"` - TracingTransport string `name:"tracing-transport" usage:"grpc or http"` - TracingEndpoint string `name:"tracing-endpoint" usage:"Endpoint of your trace collector. Eg., 'localhost:4317' for gRPC, 'localhost:4318' for http"` - TracingInsecureTransport bool `name:"tracing-insecure-transport" usage:"Disable TLS for the gRPC or HTTP transport protocol"` - - MetricsEnabled bool `name:"metrics-enabled" usage:"Enable OpenTelemetry based metrics support."` - MetricsAuthEnabled bool `name:"metrics-auth-enabled" usage:"Enable HTTP Basic Authentication for Prometheus metrics endpoint"` - MetricsAuthUsername string `name:"metrics-auth-username" usage:"Username for Prometheus metrics endpoint"` - MetricsAuthPassword string `name:"metrics-auth-password" usage:"Password for Prometheus metrics endpoint"` + TracingEnabled bool `name:"tracing-enabled" usage:"Enable OTLP Tracing"` + MetricsEnabled bool `name:"metrics-enabled" usage:"Enable OpenTelemetry based metrics support."` SMTPHost string `name:"smtp-host" usage:"Host of the smtp server. Eg., 'smtp.eu.mailgun.org'"` SMTPPort int `name:"smtp-port" usage:"Port of the smtp server. Eg., 587"` diff --git a/internal/config/defaults.go b/internal/config/defaults.go index 4ef183aa6..7e1d234f3 100644 --- a/internal/config/defaults.go +++ b/internal/config/defaults.go @@ -122,13 +122,8 @@ var Defaults = Configuration{ SMTPFrom: "", SMTPDiscloseRecipients: false, - TracingEnabled: false, - TracingTransport: "grpc", - TracingEndpoint: "", - TracingInsecureTransport: false, - - MetricsEnabled: false, - MetricsAuthEnabled: false, + TracingEnabled: false, + MetricsEnabled: false, SyslogEnabled: false, SyslogProtocol: "udp", diff --git a/internal/config/helpers.gen.go b/internal/config/helpers.gen.go index 8fc4475b7..b039d26a5 100644 --- a/internal/config/helpers.gen.go +++ b/internal/config/helpers.gen.go @@ -2306,81 +2306,6 @@ func GetTracingEnabled() bool { return global.GetTracingEnabled() } // SetTracingEnabled safely sets the value for global configuration 'TracingEnabled' field func SetTracingEnabled(v bool) { global.SetTracingEnabled(v) } -// GetTracingTransport safely fetches the Configuration value for state's 'TracingTransport' field -func (st *ConfigState) GetTracingTransport() (v string) { - st.mutex.RLock() - v = st.config.TracingTransport - st.mutex.RUnlock() - return -} - -// SetTracingTransport safely sets the Configuration value for state's 'TracingTransport' field -func (st *ConfigState) SetTracingTransport(v string) { - st.mutex.Lock() - defer st.mutex.Unlock() - st.config.TracingTransport = v - st.reloadToViper() -} - -// TracingTransportFlag returns the flag name for the 'TracingTransport' field -func TracingTransportFlag() string { return "tracing-transport" } - -// GetTracingTransport safely fetches the value for global configuration 'TracingTransport' field -func GetTracingTransport() string { return global.GetTracingTransport() } - -// SetTracingTransport safely sets the value for global configuration 'TracingTransport' field -func SetTracingTransport(v string) { global.SetTracingTransport(v) } - -// GetTracingEndpoint safely fetches the Configuration value for state's 'TracingEndpoint' field -func (st *ConfigState) GetTracingEndpoint() (v string) { - st.mutex.RLock() - v = st.config.TracingEndpoint - st.mutex.RUnlock() - return -} - -// SetTracingEndpoint safely sets the Configuration value for state's 'TracingEndpoint' field -func (st *ConfigState) SetTracingEndpoint(v string) { - st.mutex.Lock() - defer st.mutex.Unlock() - st.config.TracingEndpoint = v - st.reloadToViper() -} - -// TracingEndpointFlag returns the flag name for the 'TracingEndpoint' field -func TracingEndpointFlag() string { return "tracing-endpoint" } - -// GetTracingEndpoint safely fetches the value for global configuration 'TracingEndpoint' field -func GetTracingEndpoint() string { return global.GetTracingEndpoint() } - -// SetTracingEndpoint safely sets the value for global configuration 'TracingEndpoint' field -func SetTracingEndpoint(v string) { global.SetTracingEndpoint(v) } - -// GetTracingInsecureTransport safely fetches the Configuration value for state's 'TracingInsecureTransport' field -func (st *ConfigState) GetTracingInsecureTransport() (v bool) { - st.mutex.RLock() - v = st.config.TracingInsecureTransport - st.mutex.RUnlock() - return -} - -// SetTracingInsecureTransport safely sets the Configuration value for state's 'TracingInsecureTransport' field -func (st *ConfigState) SetTracingInsecureTransport(v bool) { - st.mutex.Lock() - defer st.mutex.Unlock() - st.config.TracingInsecureTransport = v - st.reloadToViper() -} - -// TracingInsecureTransportFlag returns the flag name for the 'TracingInsecureTransport' field -func TracingInsecureTransportFlag() string { return "tracing-insecure-transport" } - -// GetTracingInsecureTransport safely fetches the value for global configuration 'TracingInsecureTransport' field -func GetTracingInsecureTransport() bool { return global.GetTracingInsecureTransport() } - -// SetTracingInsecureTransport safely sets the value for global configuration 'TracingInsecureTransport' field -func SetTracingInsecureTransport(v bool) { global.SetTracingInsecureTransport(v) } - // GetMetricsEnabled safely fetches the Configuration value for state's 'MetricsEnabled' field func (st *ConfigState) GetMetricsEnabled() (v bool) { st.mutex.RLock() @@ -2406,81 +2331,6 @@ func GetMetricsEnabled() bool { return global.GetMetricsEnabled() } // SetMetricsEnabled safely sets the value for global configuration 'MetricsEnabled' field func SetMetricsEnabled(v bool) { global.SetMetricsEnabled(v) } -// GetMetricsAuthEnabled safely fetches the Configuration value for state's 'MetricsAuthEnabled' field -func (st *ConfigState) GetMetricsAuthEnabled() (v bool) { - st.mutex.RLock() - v = st.config.MetricsAuthEnabled - st.mutex.RUnlock() - return -} - -// SetMetricsAuthEnabled safely sets the Configuration value for state's 'MetricsAuthEnabled' field -func (st *ConfigState) SetMetricsAuthEnabled(v bool) { - st.mutex.Lock() - defer st.mutex.Unlock() - st.config.MetricsAuthEnabled = v - st.reloadToViper() -} - -// MetricsAuthEnabledFlag returns the flag name for the 'MetricsAuthEnabled' field -func MetricsAuthEnabledFlag() string { return "metrics-auth-enabled" } - -// GetMetricsAuthEnabled safely fetches the value for global configuration 'MetricsAuthEnabled' field -func GetMetricsAuthEnabled() bool { return global.GetMetricsAuthEnabled() } - -// SetMetricsAuthEnabled safely sets the value for global configuration 'MetricsAuthEnabled' field -func SetMetricsAuthEnabled(v bool) { global.SetMetricsAuthEnabled(v) } - -// GetMetricsAuthUsername safely fetches the Configuration value for state's 'MetricsAuthUsername' field -func (st *ConfigState) GetMetricsAuthUsername() (v string) { - st.mutex.RLock() - v = st.config.MetricsAuthUsername - st.mutex.RUnlock() - return -} - -// SetMetricsAuthUsername safely sets the Configuration value for state's 'MetricsAuthUsername' field -func (st *ConfigState) SetMetricsAuthUsername(v string) { - st.mutex.Lock() - defer st.mutex.Unlock() - st.config.MetricsAuthUsername = v - st.reloadToViper() -} - -// MetricsAuthUsernameFlag returns the flag name for the 'MetricsAuthUsername' field -func MetricsAuthUsernameFlag() string { return "metrics-auth-username" } - -// GetMetricsAuthUsername safely fetches the value for global configuration 'MetricsAuthUsername' field -func GetMetricsAuthUsername() string { return global.GetMetricsAuthUsername() } - -// SetMetricsAuthUsername safely sets the value for global configuration 'MetricsAuthUsername' field -func SetMetricsAuthUsername(v string) { global.SetMetricsAuthUsername(v) } - -// GetMetricsAuthPassword safely fetches the Configuration value for state's 'MetricsAuthPassword' field -func (st *ConfigState) GetMetricsAuthPassword() (v string) { - st.mutex.RLock() - v = st.config.MetricsAuthPassword - st.mutex.RUnlock() - return -} - -// SetMetricsAuthPassword safely sets the Configuration value for state's 'MetricsAuthPassword' field -func (st *ConfigState) SetMetricsAuthPassword(v string) { - st.mutex.Lock() - defer st.mutex.Unlock() - st.config.MetricsAuthPassword = v - st.reloadToViper() -} - -// MetricsAuthPasswordFlag returns the flag name for the 'MetricsAuthPassword' field -func MetricsAuthPasswordFlag() string { return "metrics-auth-password" } - -// GetMetricsAuthPassword safely fetches the value for global configuration 'MetricsAuthPassword' field -func GetMetricsAuthPassword() string { return global.GetMetricsAuthPassword() } - -// SetMetricsAuthPassword safely sets the value for global configuration 'MetricsAuthPassword' field -func SetMetricsAuthPassword(v string) { global.SetMetricsAuthPassword(v) } - // GetSMTPHost safely fetches the Configuration value for state's 'SMTPHost' field func (st *ConfigState) GetSMTPHost() (v string) { st.mutex.RLock() diff --git a/internal/observability/metrics.go b/internal/observability/metrics.go index 97ddd088f..785397834 100644 --- a/internal/observability/metrics.go +++ b/internal/observability/metrics.go @@ -21,46 +21,32 @@ package observability import ( "context" - "errors" + "fmt" "code.superseriousbusiness.org/gotosocial/internal/config" "code.superseriousbusiness.org/gotosocial/internal/db" "github.com/gin-gonic/gin" "github.com/technologize/otel-go-contrib/otelginmetrics" + "go.opentelemetry.io/contrib/exporters/autoexport" "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/exporters/prometheus" "go.opentelemetry.io/otel/metric" sdk "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/metric/exemplar" - "go.opentelemetry.io/otel/sdk/resource" - semconv "go.opentelemetry.io/otel/semconv/v1.24.0" ) -const ( - serviceName = "GoToSocial" -) - -func InitializeMetrics(db db.DB) error { +func InitializeMetrics(ctx context.Context, db db.DB) error { if !config.GetMetricsEnabled() { return nil } - if config.GetMetricsAuthEnabled() { - if config.GetMetricsAuthPassword() == "" || config.GetMetricsAuthUsername() == "" { - return errors.New("metrics-auth-username and metrics-auth-password must be set when metrics-auth-enabled is true") - } + r, err := Resource() + if err != nil { + // this can happen if semconv versioning is out-of-sync + return fmt.Errorf("building tracing resource: %w", err) } - r, _ := resource.Merge( - resource.Default(), - resource.NewSchemaless( - semconv.ServiceName(serviceName), - semconv.ServiceVersion(config.GetSoftwareVersion()), - ), - ) - - prometheusExporter, err := prometheus.New() + mt, err := autoexport.NewMetricReader(ctx) if err != nil { return err } @@ -68,7 +54,7 @@ func InitializeMetrics(db db.DB) error { meterProvider := sdk.NewMeterProvider( sdk.WithExemplarFilter(exemplar.AlwaysOffFilter), sdk.WithResource(r), - sdk.WithReader(prometheusExporter), + sdk.WithReader(mt), ) otel.SetMeterProvider(meterProvider) diff --git a/internal/api/metrics/no_metrics.go b/internal/observability/otel.go index 5aa7b722d..53ec3ae6f 100644 --- a/internal/api/metrics/no_metrics.go +++ b/internal/observability/otel.go @@ -15,17 +15,26 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. -//go:build nootel +//go:build !nootel -package metrics +package observability import ( - "github.com/gin-gonic/gin" + "code.superseriousbusiness.org/gotosocial/internal/config" + "go.opentelemetry.io/otel/sdk/resource" + semconv "go.opentelemetry.io/otel/semconv/v1.24.0" ) -type Module struct{} - -func New() *Module { return &Module{} } +const ( + serviceName = "GoToSocial" +) -func (m *Module) Route(attachHandler func(method string, path string, f ...gin.HandlerFunc) gin.IRoutes) { +func Resource() (*resource.Resource, error) { + return resource.Merge( + resource.Default(), + resource.NewSchemaless( + semconv.ServiceName(serviceName), + semconv.ServiceVersion(config.GetSoftwareVersion()), + ), + ) } diff --git a/internal/observability/tracing.go b/internal/observability/tracing.go index 65fd1ac11..25d5e27e8 100644 --- a/internal/observability/tracing.go +++ b/internal/observability/tracing.go @@ -22,19 +22,21 @@ package observability import ( "context" "fmt" + "net" + "net/http" + "strconv" "codeberg.org/gruf/go-kv" + "github.com/gin-gonic/gin" + + "go.opentelemetry.io/contrib/exporters/autoexport" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/sdk/resource" - "go.opentelemetry.io/otel/sdk/trace" - "go.opentelemetry.io/otel/semconv/v1.20.0/httpconv" + sdktrace "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.24.0" - oteltrace "go.opentelemetry.io/otel/trace" + "go.opentelemetry.io/otel/trace" "code.superseriousbusiness.org/gotosocial/internal/config" "code.superseriousbusiness.org/gotosocial/internal/gtscontext" @@ -46,66 +48,35 @@ const ( tracerName = "code.superseriousbusiness.org/gotosocial/internal/observability" ) -func InitializeTracing() error { +func InitializeTracing(ctx context.Context) error { if !config.GetTracingEnabled() { return nil } - insecure := config.GetTracingInsecureTransport() - - var tpo trace.TracerProviderOption - switch config.GetTracingTransport() { - case "grpc": - opts := []otlptracegrpc.Option{ - otlptracegrpc.WithEndpoint(config.GetTracingEndpoint()), - } - if insecure { - opts = append(opts, otlptracegrpc.WithInsecure()) - } - exp, err := otlptracegrpc.New(context.Background(), opts...) - if err != nil { - return fmt.Errorf("building tracing exporter: %w", err) - } - tpo = trace.WithBatcher(exp) - case "http": - opts := []otlptracehttp.Option{ - otlptracehttp.WithEndpoint(config.GetTracingEndpoint()), - } - if insecure { - opts = append(opts, otlptracehttp.WithInsecure()) - } - exp, err := otlptracehttp.New(context.Background(), opts...) - if err != nil { - return fmt.Errorf("building tracing exporter: %w", err) - } - tpo = trace.WithBatcher(exp) - default: - return fmt.Errorf("invalid tracing transport: %s", config.GetTracingTransport()) - } - r, err := resource.Merge( - resource.Default(), - resource.NewSchemaless( - semconv.ServiceName("GoToSocial"), - semconv.ServiceVersion(config.GetSoftwareVersion()), - ), - ) + r, err := Resource() if err != nil { // this can happen if semconv versioning is out-of-sync return fmt.Errorf("building tracing resource: %w", err) } - tp := trace.NewTracerProvider( - tpo, - trace.WithResource(r), + se, err := autoexport.NewSpanExporter(ctx) + if err != nil { + return err + } + + tp := sdktrace.NewTracerProvider( + sdktrace.WithResource(r), + sdktrace.WithBatcher(se), ) + otel.SetTracerProvider(tp) - propagator := propagation.NewCompositeTextMapPropagator( + otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( propagation.TraceContext{}, propagation.Baggage{}, - ) - otel.SetTextMapPropagator(propagator) + )) + log.Hook(func(ctx context.Context, kvs []kv.Field) []kv.Field { - span := oteltrace.SpanFromContext(ctx) + span := trace.SpanFromContext(ctx) if span != nil && span.SpanContext().HasTraceID() { return append(kvs, kv.Field{K: "traceID", V: span.SpanContext().TraceID().String()}) } @@ -121,7 +92,7 @@ func TracingMiddleware() gin.HandlerFunc { provider := otel.GetTracerProvider() tracer := provider.Tracer( tracerName, - oteltrace.WithInstrumentationVersion(config.GetSoftwareVersion()), + trace.WithInstrumentationVersion(config.GetSoftwareVersion()), ) propagator := otel.GetTextMapPropagator() return func(c *gin.Context) { @@ -140,16 +111,16 @@ func TracingMiddleware() gin.HandlerFunc { c.Request = c.Request.WithContext(savedCtx) }() ctx := propagator.Extract(savedCtx, propagation.HeaderCarrier(c.Request.Header)) - opts := []oteltrace.SpanStartOption{ - oteltrace.WithAttributes(httpconv.ServerRequest(config.GetHost(), c.Request)...), - oteltrace.WithSpanKind(oteltrace.SpanKindServer), + opts := []trace.SpanStartOption{ + trace.WithAttributes(ServerRequestAttributes(c.Request)...), + trace.WithSpanKind(trace.SpanKindServer), } rAttr := semconv.HTTPRoute(spanName) - opts = append(opts, oteltrace.WithAttributes(rAttr)) + opts = append(opts, trace.WithAttributes(rAttr)) id := gtscontext.RequestID(c.Request.Context()) if id != "" { - opts = append(opts, oteltrace.WithAttributes(attribute.String("requestID", id))) + opts = append(opts, trace.WithAttributes(attribute.String("requestID", id))) } ctx, span := tracer.Start(ctx, spanName, opts...) defer span.End() @@ -161,7 +132,6 @@ func TracingMiddleware() gin.HandlerFunc { c.Next() status := c.Writer.Status() - span.SetStatus(httpconv.ServerStatus(status)) if status > 0 { span.SetAttributes(semconv.HTTPResponseStatusCode(status)) } @@ -175,8 +145,52 @@ func InjectRequestID() gin.HandlerFunc { return func(c *gin.Context) { id := gtscontext.RequestID(c.Request.Context()) if id != "" { - span := oteltrace.SpanFromContext(c.Request.Context()) + span := trace.SpanFromContext(c.Request.Context()) span.SetAttributes(attribute.String("requestID", id)) } } } + +func ServerRequestAttributes(req *http.Request) []attribute.KeyValue { + attrs := make([]attribute.KeyValue, 0, 8) + attrs = append(attrs, method(req.Method)) + attrs = append(attrs, semconv.URLFull(req.URL.RequestURI())) + attrs = append(attrs, semconv.URLScheme(req.URL.Scheme)) + attrs = append(attrs, semconv.UserAgentOriginal(req.UserAgent())) + attrs = append(attrs, semconv.NetworkProtocolName("http")) + attrs = append(attrs, semconv.NetworkProtocolVersion(fmt.Sprintf("%d:%d", req.ProtoMajor, req.ProtoMinor))) + + if ip, port, err := net.SplitHostPort(req.RemoteAddr); err == nil { + iport, _ := strconv.Atoi(port) + attrs = append(attrs, + semconv.NetworkPeerAddress(ip), + semconv.NetworkPeerPort(iport), + ) + } else if req.RemoteAddr != "" { + attrs = append(attrs, + semconv.NetworkPeerAddress(req.RemoteAddr), + ) + } + + return attrs +} + +func method(m string) attribute.KeyValue { + var methodLookup = map[string]attribute.KeyValue{ + http.MethodConnect: semconv.HTTPRequestMethodConnect, + http.MethodDelete: semconv.HTTPRequestMethodDelete, + http.MethodGet: semconv.HTTPRequestMethodGet, + http.MethodHead: semconv.HTTPRequestMethodHead, + http.MethodOptions: semconv.HTTPRequestMethodOptions, + http.MethodPatch: semconv.HTTPRequestMethodPatch, + http.MethodPost: semconv.HTTPRequestMethodPost, + http.MethodPut: semconv.HTTPRequestMethodPut, + http.MethodTrace: semconv.HTTPRequestMethodTrace, + } + + if kv, ok := methodLookup[m]; ok { + return kv + } + + return semconv.HTTPRequestMethodGet +} |
