|
| 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 | + |
| 15 | +package http |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/http" |
| 19 | + |
| 20 | + "go.opentelemetry.io/otel/api/metric" |
| 21 | + "go.opentelemetry.io/otel/api/propagation" |
| 22 | + "go.opentelemetry.io/otel/api/trace" |
| 23 | +) |
| 24 | + |
| 25 | +// Config represents the configuration options available for the http.Handler |
| 26 | +// and http.Transport types. |
| 27 | +type Config struct { |
| 28 | + Tracer trace.Tracer |
| 29 | + Meter metric.Meter |
| 30 | + Propagators propagation.Propagators |
| 31 | + SpanStartOptions []trace.StartOption |
| 32 | + ReadEvent bool |
| 33 | + WriteEvent bool |
| 34 | + Filters []Filter |
| 35 | + SpanNameFormatter func(string, *http.Request) string |
| 36 | +} |
| 37 | + |
| 38 | +// Option Interface used for setting *optional* Config properties |
| 39 | +type Option interface { |
| 40 | + Apply(*Config) |
| 41 | +} |
| 42 | + |
| 43 | +// OptionFunc provides a convenience wrapper for simple Options |
| 44 | +// that can be represented as functions. |
| 45 | +type OptionFunc func(*Config) |
| 46 | + |
| 47 | +func (o OptionFunc) Apply(c *Config) { |
| 48 | + o(c) |
| 49 | +} |
| 50 | + |
| 51 | +// NewConfig creates a new Config struct and applies opts to it. |
| 52 | +func NewConfig(opts ...Option) *Config { |
| 53 | + c := &Config{} |
| 54 | + for _, opt := range opts { |
| 55 | + opt.Apply(c) |
| 56 | + } |
| 57 | + return c |
| 58 | +} |
| 59 | + |
| 60 | +// WithTracer configures a specific tracer. If this option |
| 61 | +// isn't specified then the global tracer is used. |
| 62 | +func WithTracer(tracer trace.Tracer) Option { |
| 63 | + return OptionFunc(func(c *Config) { |
| 64 | + c.Tracer = tracer |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +// WithMeter configures a specific meter. If this option |
| 69 | +// isn't specified then the global meter is used. |
| 70 | +func WithMeter(meter metric.Meter) Option { |
| 71 | + return OptionFunc(func(c *Config) { |
| 72 | + c.Meter = meter |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +// WithPublicEndpoint configures the Handler to link the span with an incoming |
| 77 | +// span context. If this option is not provided, then the association is a child |
| 78 | +// association instead of a link. |
| 79 | +func WithPublicEndpoint() Option { |
| 80 | + return OptionFunc(func(c *Config) { |
| 81 | + c.SpanStartOptions = append(c.SpanStartOptions, trace.WithNewRoot()) |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +// WithPropagators configures specific propagators. If this |
| 86 | +// option isn't specified then |
| 87 | +// go.opentelemetry.io/otel/api/global.Propagators are used. |
| 88 | +func WithPropagators(ps propagation.Propagators) Option { |
| 89 | + return OptionFunc(func(c *Config) { |
| 90 | + c.Propagators = ps |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +// WithSpanOptions configures an additional set of |
| 95 | +// trace.StartOptions, which are applied to each new span. |
| 96 | +func WithSpanOptions(opts ...trace.StartOption) Option { |
| 97 | + return OptionFunc(func(c *Config) { |
| 98 | + c.SpanStartOptions = append(c.SpanStartOptions, opts...) |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +// WithFilter adds a filter to the list of filters used by the handler. |
| 103 | +// If any filter indicates to exclude a request then the request will not be |
| 104 | +// traced. All filters must allow a request to be traced for a Span to be created. |
| 105 | +// If no filters are provided then all requests are traced. |
| 106 | +// Filters will be invoked for each processed request, it is advised to make them |
| 107 | +// simple and fast. |
| 108 | +func WithFilter(f Filter) Option { |
| 109 | + return OptionFunc(func(c *Config) { |
| 110 | + c.Filters = append(c.Filters, f) |
| 111 | + }) |
| 112 | +} |
| 113 | + |
| 114 | +type event int |
| 115 | + |
| 116 | +// Different types of events that can be recorded, see WithMessageEvents |
| 117 | +const ( |
| 118 | + ReadEvents event = iota |
| 119 | + WriteEvents |
| 120 | +) |
| 121 | + |
| 122 | +// WithMessageEvents configures the Handler to record the specified events |
| 123 | +// (span.AddEvent) on spans. By default only summary attributes are added at the |
| 124 | +// end of the request. |
| 125 | +// |
| 126 | +// Valid events are: |
| 127 | +// * ReadEvents: Record the number of bytes read after every http.Request.Body.Read |
| 128 | +// using the ReadBytesKey |
| 129 | +// * WriteEvents: Record the number of bytes written after every http.ResponeWriter.Write |
| 130 | +// using the WriteBytesKey |
| 131 | +func WithMessageEvents(events ...event) Option { |
| 132 | + return OptionFunc(func(c *Config) { |
| 133 | + for _, e := range events { |
| 134 | + switch e { |
| 135 | + case ReadEvents: |
| 136 | + c.ReadEvent = true |
| 137 | + case WriteEvents: |
| 138 | + c.WriteEvent = true |
| 139 | + } |
| 140 | + } |
| 141 | + }) |
| 142 | +} |
| 143 | + |
| 144 | +// WithSpanNameFormatter takes a function that will be called on every |
| 145 | +// request and the returned string will become the Span Name |
| 146 | +func WithSpanNameFormatter(f func(operation string, r *http.Request) string) Option { |
| 147 | + return OptionFunc(func(c *Config) { |
| 148 | + c.SpanNameFormatter = f |
| 149 | + }) |
| 150 | +} |
0 commit comments