From a70d91950f8c66201711ffc5d8505979f0403332 Mon Sep 17 00:00:00 2001 From: Jocelyn Boullier Date: Sun, 8 Jun 2025 04:31:28 +0200 Subject: [PATCH] feat(otel): record uncaught expections (#1191) --- .changeset/rude-squids-shout.md | 5 +++++ packages/otel/src/index.test.ts | 12 ++++++++++++ packages/otel/src/index.ts | 4 ++++ 3 files changed, 21 insertions(+) create mode 100644 .changeset/rude-squids-shout.md diff --git a/.changeset/rude-squids-shout.md b/.changeset/rude-squids-shout.md new file mode 100644 index 00000000..3db0124d --- /dev/null +++ b/.changeset/rude-squids-shout.md @@ -0,0 +1,5 @@ +--- +'@hono/otel': patch +--- + +Record uncaught exceptions as events attached to the request span diff --git a/packages/otel/src/index.test.ts b/packages/otel/src/index.test.ts index 1908840c..24eb5bab 100644 --- a/packages/otel/src/index.test.ts +++ b/packages/otel/src/index.test.ts @@ -7,6 +7,9 @@ import { ATTR_HTTP_RESPONSE_STATUS_CODE, ATTR_URL_FULL, ATTR_HTTP_ROUTE, + ATTR_EXCEPTION_MESSAGE, + ATTR_EXCEPTION_TYPE, + ATTR_EXCEPTION_STACKTRACE, } from '@opentelemetry/semantic-conventions' import { Hono } from 'hono' import { otel } from '.' @@ -65,6 +68,15 @@ describe('OpenTelemetry middleware', () => { expect(span.attributes[ATTR_HTTP_REQUEST_METHOD]).toBe('POST') expect(span.attributes[ATTR_URL_FULL]).toBe('http://localhost/error') expect(span.attributes[ATTR_HTTP_ROUTE]).toBe('/error') + expect(span.events.length).toBe(1) + const [event] = span.events + expect(event.attributes).toBeDefined() + const attributes = event.attributes! + expect(attributes[ATTR_EXCEPTION_TYPE]).toBe('Error') + expect(attributes[ATTR_EXCEPTION_MESSAGE]).toBe('error message') + expect(attributes[ATTR_EXCEPTION_STACKTRACE]).toEqual( + expect.stringMatching(/Error: error message\n.*at \S+\/src\/index.test.ts:\d+:\d+\n/) + ) }) it('Should update the active span', async () => { diff --git a/packages/otel/src/index.ts b/packages/otel/src/index.ts index 064e64be..e359199d 100644 --- a/packages/otel/src/index.ts +++ b/packages/otel/src/index.ts @@ -75,9 +75,13 @@ export const otel =