Skip to content

Commit

Permalink
Merge pull request #169 from solarwinds/dependabot/npm_and_yarn/build…
Browse files Browse the repository at this point in the history
…-c48c7ee7df

Bump the build group with 2 updates
  • Loading branch information
raphael-theriault-swi authored Nov 20, 2023
2 parents 58db9a5 + 89f8a3b commit 07421cf
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 130 deletions.
2 changes: 2 additions & 0 deletions .yarn/versions/3e88a345.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
"@solarwinds-apm/compat": minor
4 changes: 1 addition & 3 deletions packages/compat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
"publish": "node ../../scripts/publish.js",
"test": "swtest -p test/tsconfig.json -c src"
},
"dependencies": {
"@opentelemetry/semantic-conventions": "~1.18.1"
},
"peerDependencies": {
"@opentelemetry/api": "^1.3.0"
},
Expand All @@ -50,6 +47,7 @@
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/sdk-trace-base": "~1.18.1",
"@opentelemetry/sdk-trace-node": "~1.18.1",
"@opentelemetry/semantic-conventions": "~1.18.1",
"@solarwinds-apm/eslint-config": "workspace:^",
"@solarwinds-apm/rollup-config": "workspace:^",
"@solarwinds-apm/test": "workspace:^",
Expand Down
19 changes: 8 additions & 11 deletions packages/compat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ import {
SpanStatusCode,
trace,
} from "@opentelemetry/api"
import { SemanticAttributes } from "@opentelemetry/semantic-conventions"

import packageJson from "../package.json"

interface InstrumentOptions {
enabled?: boolean
collectBacktraces?: boolean
}

interface SpanInfo {
Expand All @@ -50,25 +48,24 @@ function startActiveSpan<T>(span: SpanOptions, f: (span: Span) => T): T {
})
}

function setError(span: Span, options: InstrumentOptions, err: unknown) {
function setError(span: Span, err: unknown) {
const status: SpanStatus = { code: SpanStatusCode.ERROR }
if (err instanceof Error) {
span.recordException(err)
status.message = err.message
if (options.collectBacktraces !== false && err.stack) {
span.setAttribute(SemanticAttributes.EXCEPTION_STACKTRACE, err.stack)
}
} else {
span.recordException(String(err))
}
span.setStatus(status)
}

function makeDone<T, P extends unknown[]>(
span: Span,
options: InstrumentOptions,
cb: AsyncCallback<T, P>,
): AsyncCallback<T, P> {
return (...args) => {
if (args[0] instanceof Error) {
setError(span, options, args[0])
setError(span, args[0])
}

const r = cb(...args)
Expand Down Expand Up @@ -114,11 +111,11 @@ export function instrument<T>(
}

return startActiveSpan(span, (span) => {
const done = makeDone(span, o, cb)
const done = makeDone(span, cb)
try {
return run(done)
} catch (err) {
setError(span, o, err)
setError(span, err)
throw err
} finally {
if (run.length === 0) {
Expand All @@ -141,7 +138,7 @@ export function pInstrument<T>(
try {
return await run()
} catch (err) {
setError(span, options ?? {}, err)
setError(span, err)
throw err
} finally {
span.end()
Expand Down
68 changes: 10 additions & 58 deletions packages/compat/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,10 @@ describe("instrument", () => {
code: SpanStatusCode.ERROR,
message: "error",
})
expect(span?.attributes).to.have.property(
SemanticAttributes.EXCEPTION_STACKTRACE,
)
})

it("doesn't collect backtraces if explicitly disabled", async () => {
await inParent(() => {
let error: unknown = undefined
try {
instrument(
"child",
() => {
throw new Error("error")
},
{ collectBacktraces: false },
)
} catch (e) {
error = e
}
expect(error).to.be.an.instanceof(Error)
expect(span?.events[0]!.attributes).to.include({
[SemanticAttributes.EXCEPTION_MESSAGE]: "error",
})

const span = exporter.getFinishedSpans()[0]
expect(span?.name).to.equal("child")
expect(span?.status).to.include({
code: SpanStatusCode.ERROR,
message: "error",
})
expect(span?.attributes).not.to.have.property(
expect(span?.events[0]!.attributes).to.have.property(
SemanticAttributes.EXCEPTION_STACKTRACE,
)
})
Expand Down Expand Up @@ -179,7 +154,10 @@ describe("instrument", () => {
code: SpanStatusCode.ERROR,
message: "error",
})
expect(span?.attributes).to.have.property(
expect(span?.events[0]!.attributes).to.include({
[SemanticAttributes.EXCEPTION_MESSAGE]: "error",
})
expect(span?.events[0]!.attributes).to.have.property(
SemanticAttributes.EXCEPTION_STACKTRACE,
)
})
Expand Down Expand Up @@ -275,36 +253,10 @@ describe("pInstrument", () => {
code: SpanStatusCode.ERROR,
message: "error",
})
expect(span?.attributes).to.have.property(
SemanticAttributes.EXCEPTION_STACKTRACE,
)
})

it("doesn't collect backtraces if explicitly disabled", async () => {
await inParent(async () => {
let error: unknown = undefined
try {
await pInstrument(
"child",
async () => {
await Promise.resolve()
throw new Error("error")
},
{ collectBacktraces: false },
)
} catch (e) {
error = e
}
expect(error).to.be.an.instanceof(Error)
})

const span = exporter.getFinishedSpans()[0]
expect(span?.name).to.equal("child")
expect(span?.status).to.include({
code: SpanStatusCode.ERROR,
message: "error",
expect(span?.events[0]!.attributes).to.include({
[SemanticAttributes.EXCEPTION_MESSAGE]: "error",
})
expect(span?.attributes).not.to.have.property(
expect(span?.events[0]!.attributes).to.have.property(
SemanticAttributes.EXCEPTION_STACKTRACE,
)
})
Expand Down
116 changes: 58 additions & 58 deletions yarn.lock

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

0 comments on commit 07421cf

Please sign in to comment.