config.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package otelhttp // import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
  15. import (
  16. "context"
  17. "net/http"
  18. "net/http/httptrace"
  19. "go.opentelemetry.io/otel"
  20. "go.opentelemetry.io/otel/metric"
  21. "go.opentelemetry.io/otel/propagation"
  22. "go.opentelemetry.io/otel/trace"
  23. )
  24. // ScopeName is the instrumentation scope name.
  25. const ScopeName = "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
  26. // config represents the configuration options available for the http.Handler
  27. // and http.Transport types.
  28. type config struct {
  29. ServerName string
  30. Tracer trace.Tracer
  31. Meter metric.Meter
  32. Propagators propagation.TextMapPropagator
  33. SpanStartOptions []trace.SpanStartOption
  34. PublicEndpoint bool
  35. PublicEndpointFn func(*http.Request) bool
  36. ReadEvent bool
  37. WriteEvent bool
  38. Filters []Filter
  39. SpanNameFormatter func(string, *http.Request) string
  40. ClientTrace func(context.Context) *httptrace.ClientTrace
  41. TracerProvider trace.TracerProvider
  42. MeterProvider metric.MeterProvider
  43. }
  44. // Option interface used for setting optional config properties.
  45. type Option interface {
  46. apply(*config)
  47. }
  48. type optionFunc func(*config)
  49. func (o optionFunc) apply(c *config) {
  50. o(c)
  51. }
  52. // newConfig creates a new config struct and applies opts to it.
  53. func newConfig(opts ...Option) *config {
  54. c := &config{
  55. Propagators: otel.GetTextMapPropagator(),
  56. MeterProvider: otel.GetMeterProvider(),
  57. }
  58. for _, opt := range opts {
  59. opt.apply(c)
  60. }
  61. // Tracer is only initialized if manually specified. Otherwise, can be passed with the tracing context.
  62. if c.TracerProvider != nil {
  63. c.Tracer = newTracer(c.TracerProvider)
  64. }
  65. c.Meter = c.MeterProvider.Meter(
  66. ScopeName,
  67. metric.WithInstrumentationVersion(Version()),
  68. )
  69. return c
  70. }
  71. // WithTracerProvider specifies a tracer provider to use for creating a tracer.
  72. // If none is specified, the global provider is used.
  73. func WithTracerProvider(provider trace.TracerProvider) Option {
  74. return optionFunc(func(cfg *config) {
  75. if provider != nil {
  76. cfg.TracerProvider = provider
  77. }
  78. })
  79. }
  80. // WithMeterProvider specifies a meter provider to use for creating a meter.
  81. // If none is specified, the global provider is used.
  82. func WithMeterProvider(provider metric.MeterProvider) Option {
  83. return optionFunc(func(cfg *config) {
  84. if provider != nil {
  85. cfg.MeterProvider = provider
  86. }
  87. })
  88. }
  89. // WithPublicEndpoint configures the Handler to link the span with an incoming
  90. // span context. If this option is not provided, then the association is a child
  91. // association instead of a link.
  92. func WithPublicEndpoint() Option {
  93. return optionFunc(func(c *config) {
  94. c.PublicEndpoint = true
  95. })
  96. }
  97. // WithPublicEndpointFn runs with every request, and allows conditionnally
  98. // configuring the Handler to link the span with an incoming span context. If
  99. // this option is not provided or returns false, then the association is a
  100. // child association instead of a link.
  101. // Note: WithPublicEndpoint takes precedence over WithPublicEndpointFn.
  102. func WithPublicEndpointFn(fn func(*http.Request) bool) Option {
  103. return optionFunc(func(c *config) {
  104. c.PublicEndpointFn = fn
  105. })
  106. }
  107. // WithPropagators configures specific propagators. If this
  108. // option isn't specified, then the global TextMapPropagator is used.
  109. func WithPropagators(ps propagation.TextMapPropagator) Option {
  110. return optionFunc(func(c *config) {
  111. if ps != nil {
  112. c.Propagators = ps
  113. }
  114. })
  115. }
  116. // WithSpanOptions configures an additional set of
  117. // trace.SpanOptions, which are applied to each new span.
  118. func WithSpanOptions(opts ...trace.SpanStartOption) Option {
  119. return optionFunc(func(c *config) {
  120. c.SpanStartOptions = append(c.SpanStartOptions, opts...)
  121. })
  122. }
  123. // WithFilter adds a filter to the list of filters used by the handler.
  124. // If any filter indicates to exclude a request then the request will not be
  125. // traced. All filters must allow a request to be traced for a Span to be created.
  126. // If no filters are provided then all requests are traced.
  127. // Filters will be invoked for each processed request, it is advised to make them
  128. // simple and fast.
  129. func WithFilter(f Filter) Option {
  130. return optionFunc(func(c *config) {
  131. c.Filters = append(c.Filters, f)
  132. })
  133. }
  134. type event int
  135. // Different types of events that can be recorded, see WithMessageEvents.
  136. const (
  137. ReadEvents event = iota
  138. WriteEvents
  139. )
  140. // WithMessageEvents configures the Handler to record the specified events
  141. // (span.AddEvent) on spans. By default only summary attributes are added at the
  142. // end of the request.
  143. //
  144. // Valid events are:
  145. // - ReadEvents: Record the number of bytes read after every http.Request.Body.Read
  146. // using the ReadBytesKey
  147. // - WriteEvents: Record the number of bytes written after every http.ResponeWriter.Write
  148. // using the WriteBytesKey
  149. func WithMessageEvents(events ...event) Option {
  150. return optionFunc(func(c *config) {
  151. for _, e := range events {
  152. switch e {
  153. case ReadEvents:
  154. c.ReadEvent = true
  155. case WriteEvents:
  156. c.WriteEvent = true
  157. }
  158. }
  159. })
  160. }
  161. // WithSpanNameFormatter takes a function that will be called on every
  162. // request and the returned string will become the Span Name.
  163. func WithSpanNameFormatter(f func(operation string, r *http.Request) string) Option {
  164. return optionFunc(func(c *config) {
  165. c.SpanNameFormatter = f
  166. })
  167. }
  168. // WithClientTrace takes a function that returns client trace instance that will be
  169. // applied to the requests sent through the otelhttp Transport.
  170. func WithClientTrace(f func(context.Context) *httptrace.ClientTrace) Option {
  171. return optionFunc(func(c *config) {
  172. c.ClientTrace = f
  173. })
  174. }
  175. // WithServerName returns an Option that sets the name of the (virtual) server
  176. // handling requests.
  177. func WithServerName(server string) Option {
  178. return optionFunc(func(c *config) {
  179. c.ServerName = server
  180. })
  181. }