From 748a7ebd40f4424f65db3294208bf7f4ed5329a5 Mon Sep 17 00:00:00 2001 From: Alex Holovach Date: Fri, 3 Oct 2025 07:34:46 -0500 Subject: [PATCH] remove default export --- packages/otel-better-auth/src/index.ts | 60 ++++++++++++++------------ 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/packages/otel-better-auth/src/index.ts b/packages/otel-better-auth/src/index.ts index 67782f1..1a7b8cb 100644 --- a/packages/otel-better-auth/src/index.ts +++ b/packages/otel-better-auth/src/index.ts @@ -47,12 +47,13 @@ interface InstrumentedAuth { type BetterAuthInstance = ReturnType; type BetterAuthInstanceOptions = BetterAuthInstance["options"]; -type AuthWithOptions = - Omit, "options"> & { - options: Options & { - plugins?: BetterAuthPlugin[] | Iterable; - }; +type AuthWithOptions< + Options extends BetterAuthInstanceOptions = BetterAuthInstanceOptions, +> = Omit, "options"> & { + options: Options & { + plugins?: BetterAuthPlugin[] | Iterable; }; +}; /** * Finalizes a span with status, timing, and optional error. @@ -80,7 +81,7 @@ function wrapAuthMethod Promise>( attributes: Record, tracer: Tracer, ): T { - return (async function instrumented( + return async function instrumented( this: any, ...args: Parameters ): Promise { @@ -141,10 +142,9 @@ function wrapAuthMethod Promise>( throw error; } }); - }) as T; + } as T; } - /** * Maps API method names to their operation metadata. */ @@ -236,7 +236,9 @@ function instrumentServer = any>( return server; } -function ensureOtelPlugin( +function ensureOtelPlugin< + Options extends BetterAuthInstanceOptions = BetterAuthInstanceOptions, +>( auth: Auth, config: InstrumentBetterAuthConfig & { tracer: Tracer }, ): void { @@ -245,7 +247,8 @@ function ensureOtelPlugin) : []; @@ -296,10 +299,7 @@ function ensureOtelPlugin( - auth: Auth, - config?: InstrumentBetterAuthConfig, -): Auth { +>(auth: Auth, config?: InstrumentBetterAuthConfig): Auth { if (!auth || typeof auth !== "object") { return auth; } @@ -345,11 +345,11 @@ export function instrumentBetterAuth< * }); * ``` */ -export function otelPlugin(config?: InstrumentBetterAuthConfig): BetterAuthPlugin { - const { - tracerName = DEFAULT_TRACER_NAME, - tracer: customTracer, - } = config ?? {}; +export function otelPlugin( + config?: InstrumentBetterAuthConfig, +): BetterAuthPlugin { + const { tracerName = DEFAULT_TRACER_NAME, tracer: customTracer } = + config ?? {}; const tracer = customTracer ?? trace.getTracer(tracerName); @@ -363,15 +363,16 @@ export function otelPlugin(config?: InstrumentBetterAuthConfig): BetterAuthPlugi handler: createAuthMiddleware(async (ctx) => { try { const path = ctx.path; - + // Get the span we created in onRequest - const spanKey = `${ctx.request?.method || "GET"}:${ctx.request?.url}`; + const spanKey = `${ctx.request?.method || "GET"}:${ctx.request + ?.url}`; const span = requestSpans.get(spanKey); - + if (span && ctx.context.newSession) { // Extract user and session data from newSession const { user, session } = ctx.context.newSession; - + if (user?.id) { span.setAttribute(SEMATTRS_USER_ID, user.id); } @@ -408,7 +409,10 @@ export function otelPlugin(config?: InstrumentBetterAuthConfig): BetterAuthPlugi attributes[SEMATTRS_AUTH_OPERATION] = "signin"; attributes[SEMATTRS_AUTH_METHOD] = "email"; } else if (path.includes("/callback/")) { - const provider = path.split("/callback/")[1]?.split("/")[0]?.split("?")[0]; + const provider = path + .split("/callback/")[1] + ?.split("/")[0] + ?.split("?")[0]; if (provider) { spanName = `auth.http.oauth.callback.${provider}`; attributes[SEMATTRS_AUTH_OPERATION] = "oauth_callback"; @@ -430,7 +434,10 @@ export function otelPlugin(config?: InstrumentBetterAuthConfig): BetterAuthPlugi } else if (path.endsWith("/get-session")) { spanName = "auth.http.get_session"; attributes[SEMATTRS_AUTH_OPERATION] = "get_session"; - } else if (path.includes("/sign-in/") && !path.endsWith("/sign-in/email")) { + } else if ( + path.includes("/sign-in/") && + !path.endsWith("/sign-in/email") + ) { // OAuth initiation const pathParts = path.split("/sign-in/"); const provider = pathParts[1]?.split("/")[0]?.split("?")[0]; @@ -494,6 +501,3 @@ export function otelPlugin(config?: InstrumentBetterAuthConfig): BetterAuthPlugi }, }; } - -// Re-export for convenience -export { instrumentBetterAuth as default, otelPlugin as plugin };