-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from axw/config-ignore-urls
module/apmhttp: support ELASTIC_APM_IGNORE_URLS
- Loading branch information
Showing
10 changed files
with
216 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package apmhttp | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"regexp" | ||
"sync" | ||
) | ||
|
||
const ( | ||
envIgnoreURLs = "ELASTIC_APM_IGNORE_URLS" | ||
) | ||
|
||
var ( | ||
defaultServerRequestIgnorerOnce sync.Once | ||
defaultServerRequestIgnorer RequestIgnorerFunc = IgnoreNone | ||
) | ||
|
||
// DefaultServerRequestIgnorer returns the default RequestIgnorer to use in | ||
// handlers. If ELASTIC_APM_IGNORE_URLS is set to a valid regular expression, | ||
// then it will be used to ignore matching requests; otherwise none are ignored. | ||
func DefaultServerRequestIgnorer() RequestIgnorerFunc { | ||
defaultServerRequestIgnorerOnce.Do(func() { | ||
value := os.Getenv(envIgnoreURLs) | ||
if value == "" { | ||
return | ||
} | ||
re, err := regexp.Compile(fmt.Sprintf("(?i:%s)", value)) | ||
if err == nil { | ||
defaultServerRequestIgnorer = NewRegexpRequestIgnorer(re) | ||
} | ||
}) | ||
return defaultServerRequestIgnorer | ||
} | ||
|
||
// 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 | ||
// into account. | ||
func NewRegexpRequestIgnorer(re *regexp.Regexp) RequestIgnorerFunc { | ||
if re == nil { | ||
panic("re == nil") | ||
} | ||
return func(r *http.Request) bool { | ||
fmt.Println(re.String(), r.URL.String(), re.MatchString(r.URL.String())) | ||
return re.MatchString(r.URL.String()) | ||
} | ||
} | ||
|
||
// IgnoreNone is a RequestIgnorerFunc which ignores no requests. | ||
func IgnoreNone(*http.Request) bool { | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package apmhttp_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"os/exec" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/apm-agent-go/module/apmhttp" | ||
) | ||
|
||
func TestDefaultServerRequestIgnorer(t *testing.T) { | ||
r1 := &http.Request{URL: &url.URL{Path: "/foo"}} | ||
r2 := &http.Request{URL: &url.URL{Path: "/foo", RawQuery: "bar=baz"}} | ||
r3 := &http.Request{URL: &url.URL{Scheme: "http", Host: "testing.invalid", Path: "/foo", RawQuery: "bar=baz"}} | ||
|
||
testDefaultServerRequestIgnorer(t, "", r1, false) | ||
testDefaultServerRequestIgnorer(t, "", r2, false) | ||
testDefaultServerRequestIgnorer(t, "", r3, false) | ||
testDefaultServerRequestIgnorer(t, "[", r1, false) // invalid regexp matches nothing | ||
|
||
testDefaultServerRequestIgnorer(t, "/foo", r1, true) | ||
testDefaultServerRequestIgnorer(t, "/foo", r2, true) | ||
testDefaultServerRequestIgnorer(t, "/foo", r3, true) | ||
testDefaultServerRequestIgnorer(t, "/FOO", r3, true) // case insensitive by default | ||
|
||
testDefaultServerRequestIgnorer(t, "/foo\\?bar=baz", r1, false) | ||
testDefaultServerRequestIgnorer(t, "/foo\\?bar=baz", r2, true) | ||
testDefaultServerRequestIgnorer(t, "/foo\\?bar=baz", r3, true) | ||
|
||
testDefaultServerRequestIgnorer(t, "http://.*", r1, false) | ||
testDefaultServerRequestIgnorer(t, "http://.*", r2, false) | ||
testDefaultServerRequestIgnorer(t, "http://.*", r3, true) | ||
} | ||
|
||
func testDefaultServerRequestIgnorer(t *testing.T, ignoreURLs string, r *http.Request, expect bool) { | ||
testName := fmt.Sprintf("%s_%s", ignoreURLs, r.URL.String()) | ||
t.Run(testName, func(t *testing.T) { | ||
if os.Getenv("_INSIDE_TEST") != "1" { | ||
cmd := exec.Command(os.Args[0], "-test.run=^"+regexp.QuoteMeta(t.Name())+"$") | ||
cmd.Env = append(os.Environ(), "_INSIDE_TEST=1") | ||
cmd.Env = append(cmd.Env, "ELASTIC_APM_IGNORE_URLS="+ignoreURLs) | ||
assert.NoError(t, cmd.Run()) | ||
return | ||
} | ||
ignorer := apmhttp.DefaultServerRequestIgnorer() | ||
assert.Equal(t, expect, ignorer(r)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters