mirror of https://github.com/goharbor/harbor.git
111 lines
3.8 KiB
Go
111 lines
3.8 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package otelmux // import "go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"go.opentelemetry.io/otel/propagation"
|
|
oteltrace "go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// config is used to configure the mux middleware.
|
|
type config struct {
|
|
TracerProvider oteltrace.TracerProvider
|
|
Propagators propagation.TextMapPropagator
|
|
spanNameFormatter func(string, *http.Request) string
|
|
PublicEndpoint bool
|
|
PublicEndpointFn func(*http.Request) bool
|
|
Filters []Filter
|
|
}
|
|
|
|
// Option specifies instrumentation configuration options.
|
|
type Option interface {
|
|
apply(*config)
|
|
}
|
|
|
|
type optionFunc func(*config)
|
|
|
|
func (o optionFunc) apply(c *config) {
|
|
o(c)
|
|
}
|
|
|
|
// Filter is a predicate used to determine whether a given http.request should
|
|
// be traced. A Filter must return true if the request should be traced.
|
|
type Filter func(*http.Request) bool
|
|
|
|
// WithPublicEndpoint configures the Handler to link the span with an incoming
|
|
// span context. If this option is not provided, then the association is a child
|
|
// association instead of a link.
|
|
func WithPublicEndpoint() Option {
|
|
return optionFunc(func(c *config) {
|
|
c.PublicEndpoint = true
|
|
})
|
|
}
|
|
|
|
// WithPublicEndpointFn runs with every request, and allows conditionnally
|
|
// configuring the Handler to link the span with an incoming span context. If
|
|
// this option is not provided or returns false, then the association is a
|
|
// child association instead of a link.
|
|
// Note: WithPublicEndpoint takes precedence over WithPublicEndpointFn.
|
|
func WithPublicEndpointFn(fn func(*http.Request) bool) Option {
|
|
return optionFunc(func(c *config) {
|
|
c.PublicEndpointFn = fn
|
|
})
|
|
}
|
|
|
|
// WithPropagators specifies propagators to use for extracting
|
|
// information from the HTTP requests. If none are specified, global
|
|
// ones will be used.
|
|
func WithPropagators(propagators propagation.TextMapPropagator) Option {
|
|
return optionFunc(func(cfg *config) {
|
|
if propagators != nil {
|
|
cfg.Propagators = propagators
|
|
}
|
|
})
|
|
}
|
|
|
|
// WithTracerProvider specifies a tracer provider to use for creating a tracer.
|
|
// If none is specified, the global provider is used.
|
|
func WithTracerProvider(provider oteltrace.TracerProvider) Option {
|
|
return optionFunc(func(cfg *config) {
|
|
if provider != nil {
|
|
cfg.TracerProvider = provider
|
|
}
|
|
})
|
|
}
|
|
|
|
// WithSpanNameFormatter specifies a function to use for generating a custom span
|
|
// name. By default, the route name (path template or regexp) is used. The route
|
|
// name is provided so you can use it in the span name without needing to
|
|
// duplicate the logic for extracting it from the request.
|
|
func WithSpanNameFormatter(fn func(routeName string, r *http.Request) string) Option {
|
|
return optionFunc(func(cfg *config) {
|
|
cfg.spanNameFormatter = fn
|
|
})
|
|
}
|
|
|
|
// WithFilter adds a filter to the list of filters used by the handler.
|
|
// If any filter indicates to exclude a request then the request will not be
|
|
// traced. All filters must allow a request to be traced for a Span to be created.
|
|
// If no filters are provided then all requests are traced.
|
|
// Filters will be invoked for each processed request, it is advised to make them
|
|
// simple and fast.
|
|
func WithFilter(f Filter) Option {
|
|
return optionFunc(func(c *config) {
|
|
c.Filters = append(c.Filters, f)
|
|
})
|
|
}
|