Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
bump: patch
type: add
---

Allow functions as backtrace matchers. Alongside regular expressions, you can also provide custom functions to match and replace paths in the backtrace:

```javascript
const appsignal = new Appsignal({
// ...
matchBacktracePaths: [(path) => {
if (path.indexOf("/bundle/") !== -1) {
return "bundle.js"
}
}]
})
```

The function must take a backtrace line path as an argument. When the function returns a non-empty string, the string will be used as the path for that backtrace line. Otherwise, the path will be left unchanged.
15 changes: 10 additions & 5 deletions packages/javascript/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,17 @@ describe("Appsignal", () => {
appsignal = new Appsignal({
key: "TESTKEY",
namespace: "test",
matchBacktracePaths: [/here(.*)/g]
matchBacktracePaths: [
/here(.*)/g,
path => (path.indexOf("some") !== -1 ? "some.js" : undefined)
]
})

const error = new Error("test error")
error.stack = [
"Error: test error",
" at Foo (http://localhost:8080/here/istheapp.js:13:10)"
" at Foo (http://localhost:8080/here/istheapp.js:13:10)",
" at Bar (http://localhost:8080/some/thingelse.js:13:10)"
].join("\n")

appsignal.send(error)
Expand All @@ -112,10 +116,11 @@ describe("Appsignal", () => {

expect(firstPayload.error.backtrace).toEqual([
"Error: test error",
" at Foo (/istheapp.js:13:10)"
" at Foo (/istheapp.js:13:10)",
" at Bar (some.js:13:10)"
])

expect(firstPayload.environment.backtrace_paths_matched).toEqual("1")
expect(firstPayload.environment.backtrace_paths_matched).toEqual("2")

// As the regex used has the `g` flag, it would
// remember the last matched position and fail to match again:
Expand All @@ -124,7 +129,7 @@ describe("Appsignal", () => {
appsignal.send(error)

const secondPayload = pushMockCall(1)
expect(secondPayload.environment.backtrace_paths_matched).toEqual("1")
expect(secondPayload.environment.backtrace_paths_matched).toEqual("2")
})
})

Expand Down
70 changes: 53 additions & 17 deletions packages/javascript/src/__tests__/span.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Span } from "../span"
import { Span, toBacktraceMatcher } from "../span"

describe("Span", () => {
let span: Span
Expand Down Expand Up @@ -71,7 +71,9 @@ describe("Span", () => {
].join("\n")

span.setError(error)
span.cleanBacktracePath([new RegExp("/assets/(app/.*)$")])
span.cleanBacktracePath(
[new RegExp("/assets/(app/.*)$")].map(toBacktraceMatcher)
)

const backtrace = span.serialize().error.backtrace
expect(backtrace).toEqual([
Expand All @@ -97,7 +99,9 @@ describe("Span", () => {
].join("\n")

span.setError(error)
span.cleanBacktracePath([new RegExp("/assets/(app/.*)$")])
span.cleanBacktracePath(
[new RegExp("/assets/(app/.*)$")].map(toBacktraceMatcher)
)

const backtrace = span.serialize().error.backtrace
expect(backtrace).toEqual([
Expand All @@ -123,9 +127,11 @@ describe("Span", () => {
].join("\n")

span.setError(error)
span.cleanBacktracePath([
new RegExp(".*/(assets/)(?:[0-9a-f]{16}/)?(app/.*)$")
])
span.cleanBacktracePath(
[new RegExp(".*/(assets/)(?:[0-9a-f]{16}/)?(app/.*)$")].map(
toBacktraceMatcher
)
)

const backtrace = span.serialize().error.backtrace
expect(backtrace).toEqual([
Expand All @@ -149,14 +155,16 @@ describe("Span", () => {
].join("\n")

span.setError(error)
span.cleanBacktracePath([
// This can only match `Bar`.
new RegExp("/assets/[0-9a-f]{16}/(.*)$"),

// This can match both `Foo` and `Bar`, but should only
// match `Foo` because the previous matcher takes precedence.
new RegExp("/assets/(.*)$")
])
span.cleanBacktracePath(
[
// This can only match `Bar`.
new RegExp("/assets/[0-9a-f]{16}/(.*)$"),

// This can match both `Foo` and `Bar`, but should only
// match `Foo` because the previous matcher takes precedence.
new RegExp("/assets/(.*)$")
].map(toBacktraceMatcher)
)

const backtrace = span.serialize().error.backtrace
expect(backtrace).toEqual([
Expand All @@ -181,7 +189,7 @@ describe("Span", () => {
// empty string.
//
// This should result in the line not being modified.
span.cleanBacktracePath([new RegExp(".*")])
span.cleanBacktracePath([new RegExp(".*")].map(toBacktraceMatcher))

const backtrace = span.serialize().error.backtrace
expect(backtrace).toEqual([
Expand All @@ -203,7 +211,7 @@ describe("Span", () => {
// empty string.
//
// This should result in the line not being modified.
span.cleanBacktracePath([new RegExp(".*(z*)$")])
span.cleanBacktracePath([new RegExp(".*(z*)$")].map(toBacktraceMatcher))

const backtrace = span.serialize().error.backtrace
expect(backtrace).toEqual([
Expand All @@ -220,7 +228,9 @@ describe("Span", () => {
].join("\n")

span.setError(error)
span.cleanBacktracePath([new RegExp("^pancakes/(.*)$")])
span.cleanBacktracePath(
[new RegExp("^pancakes/(.*)$")].map(toBacktraceMatcher)
)

const backtrace = span.serialize().error.backtrace
expect(backtrace).toEqual([
Expand All @@ -229,6 +239,32 @@ describe("Span", () => {

expect(span.serialize().environment).toBeUndefined()
})

it("can be used with custom backtrace matcher functions", () => {
const error = new Error("test error")
error.stack = [
"Foo@http://localhost:8080/assets/app/first.js:13:10",
"Bar@http://localhost:8080/assets/app/second.js:13:10",
"Baz@http://localhost:8080/assets/app/third.js:13:10"
].join("\n")

span.setError(error)
span.cleanBacktracePath([
path => (path.indexOf("first") !== -1 ? "foo.js" : undefined),
path => (path.indexOf("second") !== -1 ? "bar.js" : undefined)
])

const backtrace = span.serialize().error.backtrace
expect(backtrace).toEqual([
"[email protected]:13:10",
"[email protected]:13:10",
"Baz@http://localhost:8080/assets/app/third.js:13:10"
])

expect(span.serialize().environment).toMatchObject({
backtrace_paths_matched: "2"
})
})
})

describe("getAction", () => {
Expand Down
24 changes: 15 additions & 9 deletions packages/javascript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import { toHashMap } from "./hashmap"
import { VERSION } from "./version"
import { PushApi } from "./api"
import { Environment } from "./environment"
import { Span } from "./span"
import { Span, toBacktraceMatcher } from "./span"
export { Span }
import { Queue } from "./queue"
import { Dispatcher } from "./dispatcher"

import { AppsignalOptions } from "./options"
import { AppsignalOptions, BacktraceMatcher } from "./options"

export default class Appsignal {
public VERSION = VERSION
public ignored: RegExp[] = []
private matchBacktracePaths: RegExp[] = []
private backtraceMatchers: BacktraceMatcher[] = []

private _dispatcher: Dispatcher
private _options: AppsignalOptions
Expand Down Expand Up @@ -83,15 +83,21 @@ export default class Appsignal {
}

if (matchBacktracePaths) {
let paths: (RegExp | BacktraceMatcher)[]

if (Array.isArray(matchBacktracePaths)) {
this.matchBacktracePaths = matchBacktracePaths
paths = matchBacktracePaths
} else {
this.matchBacktracePaths = [matchBacktracePaths]
paths = [matchBacktracePaths]
}

this.matchBacktracePaths = this.matchBacktracePaths
.filter(value => value instanceof RegExp)
.map(unglobalize)
for (const matcher of paths) {
if (matcher instanceof RegExp) {
this.backtraceMatchers.push(toBacktraceMatcher(unglobalize(matcher)))
} else if (typeof matcher === "function") {
this.backtraceMatchers.push(matcher)
}
}
}

this._dispatcher = new Dispatcher(this._queue, this._api)
Expand Down Expand Up @@ -242,7 +248,7 @@ export default class Appsignal {
return
}

span.cleanBacktracePath(this.matchBacktracePaths)
span.cleanBacktracePath(this.backtraceMatchers)

if (Environment.supportsPromises()) {
// clear breadcrumbs as they are now loaded into the span,
Expand Down
Loading
Loading