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

Recording Exceptions Example for Typescript Doesn't Work Out of the Box #5805

Open
Grunet opened this issue Dec 17, 2024 · 5 comments
Open

Comments

@Grunet
Copy link

Grunet commented Dec 17, 2024

The example for "Recording exceptions" for Typescript doesn't work as-is. You have to do something to convince the compiler that you're allowed to pass the ex (typed as unknown by default since I think JS lets you throw anything) into the recordException call.

https://opentelemetry.io/docs/languages/js/instrumentation/#recording-exceptions

Typing it as any in the catch block or explicitly casting it as Error in the recordException call both seem to work.

Not sure if this is the right code but it seems like recordException is not making any assumptions about the input type anyways, so it seems safe to do these sorts of things.

https://github.com/open-telemetry/opentelemetry-js/blob/84cce753617a1195138cab3254e1b22f80982150/packages/opentelemetry-sdk-trace-base/src/Span.ts#L317

@Grunet
Copy link
Author

Grunet commented Dec 17, 2024

Definitely the tiniest of papercuts, but I figured I'd mention it while I was here

@svrnm
Copy link
Member

svrnm commented Dec 18, 2024

@Grunet thanks for reporting! All feedback is helpful

@open-telemetry/javascript-approvers PTAL!

@cartermp
Copy link
Contributor

Huh, I get (local var) ex: any when I do a snippet like this:

const doWork = (x: number) => {
  if (x > 0) {
    throw new Error("uh oh! failed to do the thing");
  }

  return x;
}

//...

try {
  doWork(1);
} catch (ex) {
  span.recordException(ex);
  span.setStatus({ code: SpanStatusCode.ERROR });
}

@cartermp
Copy link
Contributor

@Grunet do you have a copy-pastable repro per chance?

@Grunet
Copy link
Author

Grunet commented Dec 27, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

3 participants