Debian Codegraph @

instrumentation/github.com/gin-gonic/gin/otelgin/config.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Based on https://github.com/DataDog/dd-trace-go/blob/8fb554ff7cf694267f9077ae35e27ce4689ed8b6/contrib/gin-gonic/gin/option.go
5
6 package otelgin // import "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
7
8 import (
9 "net/http"
10 "slices"
11 "strings"
12
13 "github.com/gin-gonic/gin"
14 "go.opentelemetry.io/otel/attribute"
15 "go.opentelemetry.io/otel/metric"
16 "go.opentelemetry.io/otel/propagation"
17 oteltrace "go.opentelemetry.io/otel/trace"
18 )
19
20 type config struct {
21 TracerProvider oteltrace.TracerProvider
22 Propagators propagation.TextMapPropagator
23 SpanStartOptions []oteltrace.SpanStartOption
24 Filters []Filter
25 GinFilters []GinFilter
26 SpanNameFormatter SpanNameFormatter
27 MeterProvider metric.MeterProvider
28 MetricAttributeFn MetricAttributeFn
29 GinMetricAttributeFn GinMetricAttributeFn
30 }
31
32 // defaultSpanNameFormatter is the default span name formatter.
33 var defaultSpanNameFormatter SpanNameFormatter = func(c *gin.Context) string {
34 method := strings.ToUpper(c.Request.Method)
35 if !slices.Contains([]string{
36 http.MethodGet, http.MethodHead,
37 http.MethodPost, http.MethodPut,
38 http.MethodPatch, http.MethodDelete,
39 http.MethodConnect, http.MethodOptions,
40 http.MethodTrace,
41 }, method) {
42 method = "HTTP"
43 }
44
45 if path := c.FullPath(); path != "" {
46 return method + " " + path
47 }
48
49 return method
50 }
51
52 // Filter is a predicate used to determine whether a given http.request should
53 // be traced. A Filter must return true if the request should be traced.
54 type Filter func(*http.Request) bool
55
56 // GinFilter filters an [net/http.Request] based on content of a [gin.Context].
57 type GinFilter func(*gin.Context) bool
58
59 // SpanNameFormatter is used by `WithSpanNameFormatter` to customize the request's span name.
60 type SpanNameFormatter func(*gin.Context) string
61
62 // MetricAttributeFn is used to extract additional attributes from the http.Request
63 // and return them as a slice of attribute.KeyValue.
64 type MetricAttributeFn func(*http.Request) []attribute.KeyValue
65
66 // GinMetricAttributeFn is used to extract additional attributes from the gin.Context
67 // and return them as a slice of attribute.KeyValue.
68 type GinMetricAttributeFn func(*gin.Context) []attribute.KeyValue
69
70 // Option specifies instrumentation configuration options.
71 type Option interface {
72 apply(*config)
73 }
74
75 type optionFunc func(*config)
76
77 func (o optionFunc) apply(c *config) {
78 o(c)
79 }
80
81 // WithPropagators specifies propagators to use for extracting
82 // information from the HTTP requests. If none are specified, global
83 // ones will be used.
84 func WithPropagators(propagators propagation.TextMapPropagator) Option {
85 return optionFunc(func(cfg *config) {
86 if propagators != nil {
87 cfg.Propagators = propagators
88 }
89 })
90 }
91
92 // WithSpanStartOptions configures an additional set of
93 // trace.SpanStartOptions, which are applied to each new span.
94 func WithSpanStartOptions(opts ...oteltrace.SpanStartOption) Option {
95 return optionFunc(func(c *config) {
96 c.SpanStartOptions = append(c.SpanStartOptions, opts...)
97 })
98 }
99
100 // WithTracerProvider specifies a tracer provider to use for creating a tracer.
101 // If none is specified, the global provider is used.
102 func WithTracerProvider(provider oteltrace.TracerProvider) Option {
103 return optionFunc(func(cfg *config) {
104 if provider != nil {
105 cfg.TracerProvider = provider
106 }
107 })
108 }
109
110 // WithFilter adds a filter to the list of filters used by the handler.
111 // If any filter indicates to exclude a request then the request will not be
112 // traced. All gin and net/http filters must allow a request to be traced for a Span to be created.
113 // If no filters are provided then all requests are traced.
114 // Filters will be invoked for each processed request, it is advised to make them
115 // simple and fast.
116 func WithFilter(f ...Filter) Option {
117 return optionFunc(func(c *config) {
118 c.Filters = append(c.Filters, f...)
119 })
120 }
121
122 // WithGinFilter adds a gin filter to the list of filters used by the handler.
123 func WithGinFilter(f ...GinFilter) Option {
124 return optionFunc(func(c *config) {
125 c.GinFilters = append(c.GinFilters, f...)
126 })
127 }
128
129 // WithSpanNameFormatter takes a function that will be called on every
130 // request and the returned string will become the Span Name.
131 func WithSpanNameFormatter(f SpanNameFormatter) Option {
132 return optionFunc(func(c *config) {
133 c.SpanNameFormatter = f
134 })
135 }
136
137 // WithMeterProvider specifies a meter provider to use for creating a meter.
138 // If none is specified, the global provider is used.
139 func WithMeterProvider(mp metric.MeterProvider) Option {
140 return optionFunc(func(c *config) {
141 c.MeterProvider = mp
142 })
143 }
144
145 // WithMetricAttributeFn specifies a function that extracts additional attributes from the http.Request
146 // and returns them as a slice of attribute.KeyValue.
147 //
148 // If attributes are duplicated between this method and `WithGinMetricAttributeFn`, the attributes in this method will be overridden.
149 func WithMetricAttributeFn(f MetricAttributeFn) Option {
150 return optionFunc(func(c *config) {
151 c.MetricAttributeFn = f
152 })
153 }
154
155 // WithGinMetricAttributeFn specifies a function that extracts additional attributes from the gin.Context
156 // and returns them as a slice of attribute.KeyValue.
157 //
158 // If attributes are duplicated between this method and `WithMetricAttributeFn`, the attributes in this method will be used.
159 func WithGinMetricAttributeFn(f GinMetricAttributeFn) Option {
160 return optionFunc(func(c *config) {
161 c.GinMetricAttributeFn = f
162 })
163 }