Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore_urls to central config #872

Merged
merged 9 commits into from Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions config.go
Expand Up @@ -53,6 +53,8 @@ const (
envAPIBufferSize = "ELASTIC_APM_API_BUFFER_SIZE"
envMetricsBufferSize = "ELASTIC_APM_METRICS_BUFFER_SIZE"
envDisableMetrics = "ELASTIC_APM_DISABLE_METRICS"
envIgnoreURLs = "ELASTIC_APM_TRANSACTION_IGNORE_URLS"
deprecatedEnvIgnoreURLs = "ELASTIC_APM_IGNORE_URLS"
envGlobalLabels = "ELASTIC_APM_GLOBAL_LABELS"
envStackTraceLimit = "ELASTIC_APM_STACK_TRACE_LIMIT"
envCentralConfig = "ELASTIC_APM_CENTRAL_CONFIG"
Expand Down Expand Up @@ -263,6 +265,14 @@ func initialDisabledMetrics() wildcard.Matchers {
return configutil.ParseWildcardPatternsEnv(envDisableMetrics, nil)
}

func initialIgnoreUrls() wildcard.Matchers {
matchers := configutil.ParseWildcardPatternsEnv(envIgnoreURLs, nil)
if len(matchers) == 0 {
matchers = configutil.ParseWildcardPatternsEnv(deprecatedEnvIgnoreURLs, nil)
}
return matchers
}

func initialStackTraceLimit() (int, error) {
value := os.Getenv(envStackTraceLimit)
if value == "" {
Expand Down Expand Up @@ -348,6 +358,11 @@ func (t *Tracer) updateRemoteConfig(logger WarningLogger, old, attrs map[string]
cfg.maxSpans = value
})
}
case envIgnoreURLs:
matchers := configutil.ParseWildcardPatterns(v)
updates = append(updates, func(cfg *instrumentationConfig) {
cfg.ignoreURLs = matchers
})
case envRecording:
recording, err := strconv.ParseBool(v)
if err != nil {
Expand Down Expand Up @@ -480,6 +495,11 @@ func (t *Tracer) updateInstrumentationConfig(f func(cfg *instrumentationConfig))
}
}

// GetIgnoreURLs returns the ignored URLs
func (t *Tracer) GetIgnoreURLs() wildcard.Matchers {
jalvz marked this conversation as resolved.
Show resolved Hide resolved
return t.instrumentationConfig().ignoreURLs
}

// instrumentationConfig holds current configuration values, as well as information
// required to revert from remote to local configuration.
type instrumentationConfig struct {
Expand Down Expand Up @@ -510,4 +530,5 @@ type instrumentationConfigValues struct {
stackTraceLimit int
propagateLegacyHeader bool
sanitizedFieldNames wildcard.Matchers
ignoreURLs wildcard.Matchers
}
2 changes: 1 addition & 1 deletion module/apmecho/middleware.go
Expand Up @@ -41,7 +41,7 @@ import (
func Middleware(o ...Option) echo.MiddlewareFunc {
opts := options{
tracer: apm.DefaultTracer,
requestIgnorer: apmhttp.DefaultServerRequestIgnorer(),
requestIgnorer: apmhttp.DynamicServerRequestIgnorer(apm.DefaultTracer),
}
for _, o := range o {
o(&opts)
Expand Down
2 changes: 1 addition & 1 deletion module/apmgin/middleware.go
Expand Up @@ -47,7 +47,7 @@ func Middleware(engine *gin.Engine, o ...Option) gin.HandlerFunc {
m := &middleware{
engine: engine,
tracer: apm.DefaultTracer,
requestIgnorer: apmhttp.DefaultServerRequestIgnorer(),
requestIgnorer: apmhttp.DynamicServerRequestIgnorer(apm.DefaultTracer),
}
for _, o := range o {
o(m)
Expand Down
2 changes: 1 addition & 1 deletion module/apmgorilla/middleware.go
Expand Up @@ -77,7 +77,7 @@ func WrapMethodNotAllowedHandler(h http.Handler, m mux.MiddlewareFunc) http.Hand
func Middleware(o ...Option) mux.MiddlewareFunc {
opts := options{
tracer: apm.DefaultTracer,
requestIgnorer: apmhttp.DefaultServerRequestIgnorer(),
requestIgnorer: apmhttp.DynamicServerRequestIgnorer(apm.DefaultTracer),
}
for _, o := range o {
o(&opts)
Expand Down
2 changes: 1 addition & 1 deletion module/apmhttp/handler.go
Expand Up @@ -42,7 +42,7 @@ func Wrap(h http.Handler, o ...ServerOption) http.Handler {
handler: h,
tracer: apm.DefaultTracer,
requestName: ServerRequestName,
requestIgnorer: DefaultServerRequestIgnorer(),
requestIgnorer: DynamicServerRequestIgnorer(apm.DefaultTracer),
jalvz marked this conversation as resolved.
Show resolved Hide resolved
}
for _, o := range o {
o(handler)
Expand Down
11 changes: 11 additions & 0 deletions module/apmhttp/ignorer.go
Expand Up @@ -22,6 +22,7 @@ import (
"regexp"
"sync"

"go.elastic.co/apm"
"go.elastic.co/apm/internal/configutil"
"go.elastic.co/apm/internal/wildcard"
)
Expand Down Expand Up @@ -53,6 +54,16 @@ func DefaultServerRequestIgnorer() RequestIgnorerFunc {
return defaultServerRequestIgnorer
}

// DynamicServerRequestIgnorer returns the RequestIgnorer to use in
axw marked this conversation as resolved.
Show resolved Hide resolved
// handlers. The list of wildcard patterns comes from central config
func DynamicServerRequestIgnorer(t *apm.Tracer) RequestIgnorerFunc {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func DynamicServerRequestIgnorer(t *apm.Tracer) RequestIgnorerFunc {
func NewDynamicServerRequestIgnorer(t *apm.Tracer) RequestIgnorerFunc {

Let's align the naming with the other functions below, since DefaultServerRequestIgnorer will be going away eventually.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, if you implement the suggestion above about changing Tracer.IgnoredTransactionURLMatchers to IgnoredTransactionURL, then we can just pass tracer.IgnoredTransactionURL directly in as it will satisfy the function signature -- no need for (New)DynamicServerRequestIgnorer then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realise now that RequestIgnorerFunc takes an *http.Request, whereas IgnoredTransactionURL takes a URL, so my second comment doesn't work. But let's please align the naming, and add New to the front.

matchers := t.GetIgnoreURLs()
if len(matchers) != 0 {
defaultServerRequestIgnorer = NewWildcardPatternsRequestIgnorer(matchers)
}
return defaultServerRequestIgnorer
jalvz marked this conversation as resolved.
Show resolved Hide resolved
}

// NewRegexpRequestIgnorer returns a RequestIgnorerFunc which matches requests'
// URLs against re. Note that for server requests, typically only Path and
// possibly RawQuery will be set, so the regular expression should take this
Expand Down
2 changes: 1 addition & 1 deletion module/apmhttprouter/handler.go
Expand Up @@ -97,7 +97,7 @@ func wrapHandlerUnknownRoute(h http.Handler, o ...Option) http.Handler {
func gatherOptions(o ...Option) options {
opts := options{
tracer: apm.DefaultTracer,
requestIgnorer: apmhttp.DefaultServerRequestIgnorer(),
requestIgnorer: apmhttp.DynamicServerRequestIgnorer(apm.DefaultTracer),
}
for _, o := range o {
o(&opts)
Expand Down
2 changes: 1 addition & 1 deletion module/apmrestful/filter.go
Expand Up @@ -34,7 +34,7 @@ import (
func Filter(o ...Option) restful.FilterFunction {
opts := options{
tracer: apm.DefaultTracer,
requestIgnorer: apmhttp.DefaultServerRequestIgnorer(),
requestIgnorer: apmhttp.DynamicServerRequestIgnorer(apm.DefaultTracer),
}
for _, o := range o {
o(&opts)
Expand Down
2 changes: 2 additions & 0 deletions tracer.go
Expand Up @@ -103,6 +103,7 @@ type TracerOptions struct {
sampler Sampler
sanitizedFieldNames wildcard.Matchers
disabledMetrics wildcard.Matchers
ignoreURLs wildcard.Matchers
captureHeaders bool
captureBody CaptureBodyMode
spanFramesMinDuration time.Duration
Expand Down Expand Up @@ -246,6 +247,7 @@ func (opts *TracerOptions) initDefaults(continueOnError bool) error {
opts.sampler = sampler
opts.sanitizedFieldNames = initialSanitizedFieldNames()
opts.disabledMetrics = initialDisabledMetrics()
opts.ignoreURLs = initialIgnoreUrls()
opts.breakdownMetrics = breakdownMetricsEnabled
opts.captureHeaders = captureHeaders
opts.captureBody = captureBody
Expand Down