Skip to content

Commit 3ab7ec9

Browse files
committed
deduplicate integrations
1 parent 4b014ce commit 3ab7ec9

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

packages/hono/src/cloudflare/middleware.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { withSentry } from '@sentry/cloudflare';
2-
import { applySdkMetadata, type BaseTransportOptions, debug, type Integration, type Options } from '@sentry/core';
2+
import {
3+
applySdkMetadata,
4+
type BaseTransportOptions,
5+
debug,
6+
getIntegrationsToSetup,
7+
type Integration,
8+
type Options,
9+
} from '@sentry/core';
310
import type { Context, Hono, MiddlewareHandler } from 'hono';
411
import { requestHandler, responseHandler } from '../shared/middlewareHandlers';
512

@@ -20,10 +27,14 @@ export const sentry = (app: Hono, options: HonoOptions | undefined = {}): Middle
2027
withSentry(
2128
() => ({
2229
...options,
23-
// Always filter out the Hono integration from user-provided integrations (or when nothing is specified).
24-
// The Hono integration is already set up by withSentry, so adding it again would cause double-capturing (and non-parametrized URLs).
30+
// Always filter out the Hono integration from defaults and user integrations.
31+
// The Hono integration is already set up by withSentry, so adding it again would cause capturing too early (in Cloudflare SDK) and non-parametrized URLs.
2532
integrations: Array.isArray(userIntegrations)
26-
? defaults => [...defaults.filter(filterHonoIntegration), ...userIntegrations.filter(filterHonoIntegration)]
33+
? defaults =>
34+
getIntegrationsToSetup({
35+
defaultIntegrations: defaults.filter(filterHonoIntegration),
36+
integrations: userIntegrations.filter(filterHonoIntegration),
37+
})
2738
: typeof userIntegrations === 'function'
2839
? defaults => userIntegrations(defaults).filter(filterHonoIntegration)
2940
: defaults => defaults.filter(filterHonoIntegration),

packages/hono/test/cloudflare/middleware.test.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,39 @@ describe('Hono Cloudflare Middleware', () => {
143143
const app = new Hono();
144144
sentry(app, { integrations: input });
145145

146-
const integrationsFn = getIntegrationsResult() as (defaults: SentryCore.Integration[]) => SentryCore.Integration[];
146+
const integrationsFn = getIntegrationsResult() as (
147+
defaults: SentryCore.Integration[],
148+
) => SentryCore.Integration[];
147149
expect(integrationsFn([])).toEqual(expected);
148150
});
149151

150152
it('filters Hono from defaults when user provides an array', () => {
151153
const app = new Hono();
152154
sentry(app, { integrations: [otherIntegration] });
153155

154-
const integrationsFn = getIntegrationsResult() as (defaults: SentryCore.Integration[]) => SentryCore.Integration[];
155-
// Simulates getIntegrationsToSetup: defaults (from Cloudflare) include Hono; result must exclude it
156+
const integrationsFn = getIntegrationsResult() as (
157+
defaults: SentryCore.Integration[],
158+
) => SentryCore.Integration[];
159+
// Defaults (from Cloudflare) include Hono; result must exclude it and deduplicate (user + defaults overlap)
156160
const defaultsWithHono = [honoIntegration, otherIntegration];
157-
expect(integrationsFn(defaultsWithHono)).toEqual([otherIntegration, otherIntegration]);
161+
expect(integrationsFn(defaultsWithHono)).toEqual([otherIntegration]);
162+
});
163+
164+
it('deduplicates when user integrations overlap with defaults (by name)', () => {
165+
const app = new Hono();
166+
const duplicateIntegration = { name: 'Other' } as SentryCore.Integration;
167+
sentry(app, { integrations: [duplicateIntegration] });
168+
169+
const integrationsFn = getIntegrationsResult() as (
170+
defaults: SentryCore.Integration[],
171+
) => SentryCore.Integration[];
172+
const defaultsWithOverlap = [
173+
honoIntegration,
174+
otherIntegration, // same name as duplicateIntegration
175+
];
176+
const result = integrationsFn(defaultsWithOverlap);
177+
expect(result).toHaveLength(1);
178+
expect(result[0]?.name).toBe('Other');
158179
});
159180

160181
it('filters Hono integration out of a function result', () => {

0 commit comments

Comments
 (0)