From 2a1f8d745e42e8d48c39c190ef0e5387d7d099e6 Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Thu, 21 Jul 2022 12:26:47 +0100 Subject: [PATCH 1/2] feat(remix): Set `sentry-trace` `` on server-side. --- packages/remix/src/utils/instrumentServer.ts | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/packages/remix/src/utils/instrumentServer.ts b/packages/remix/src/utils/instrumentServer.ts index 8f2179633e24..0bcf4633e82c 100644 --- a/packages/remix/src/utils/instrumentServer.ts +++ b/packages/remix/src/utils/instrumentServer.ts @@ -1,3 +1,4 @@ +/* eslint-disable max-lines */ import { captureException, getCurrentHub } from '@sentry/node'; import { getActiveTransaction } from '@sentry/tracing'; import { addExceptionMechanism, fill, loadModule, logger, stripUrlQueryAndFragment } from '@sentry/utils'; @@ -20,11 +21,26 @@ interface Route { parentId?: string; path?: string; } +export interface RouteData { + [routeId: string]: AppData; +} + +export interface MetaFunction { + (args: { data: AppData; parentsData: RouteData; params: Params; location: Location }): HtmlMetaDescriptor; +} + +export interface HtmlMetaDescriptor { + [name: string]: null | string | undefined | Record | Array | string>; + charset?: 'utf-8'; + charSet?: 'utf-8'; + title?: string; +} interface ServerRouteModule { action?: DataFunction; headers?: unknown; loader?: DataFunction; + meta?: MetaFunction | HtmlMetaDescriptor; } interface ServerRoute extends Route { @@ -206,6 +222,34 @@ function makeWrappedLoader(origAction: DataFunction): DataFunction { return makeWrappedDataFunction(origAction, 'loader'); } +function makeWrappedMeta(origMeta: MetaFunction | HtmlMetaDescriptor = {}): MetaFunction { + return function ( + this: unknown, + args: { data: AppData; parentsData: RouteData; params: Params; location: Location }, + ): HtmlMetaDescriptor { + let origMetaResult; + if (origMeta instanceof Function) { + origMetaResult = origMeta.call(this, args); + } else { + origMetaResult = origMeta; + } + + const scope = getCurrentHub().getScope(); + if (scope) { + const span = scope.getSpan(); + + if (span) { + return { + ...origMetaResult, + 'sentry-trace': span.toTraceparent(), + }; + } + } + + return origMetaResult; + }; +} + function wrapRequestHandler(origRequestHandler: RequestHandler): RequestHandler { return async function (this: unknown, request: Request, loadContext?: unknown): Promise { const hub = getCurrentHub(); @@ -247,6 +291,8 @@ function makeWrappedCreateRequestHandler( for (const [id, route] of Object.entries(build.routes)) { const wrappedRoute = { ...route, module: { ...route.module } }; + fill(wrappedRoute.module, 'meta', makeWrappedMeta); + if (wrappedRoute.module.action) { fill(wrappedRoute.module, 'action', makeWrappedAction); } From 4302c7bf0065125e64261b8fa90b7419c81bff67 Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Fri, 22 Jul 2022 11:09:37 +0100 Subject: [PATCH 2/2] Add transaction `baggage` as a ``. --- packages/remix/src/utils/instrumentServer.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/remix/src/utils/instrumentServer.ts b/packages/remix/src/utils/instrumentServer.ts index 0bcf4633e82c..05b2157a3cd3 100644 --- a/packages/remix/src/utils/instrumentServer.ts +++ b/packages/remix/src/utils/instrumentServer.ts @@ -1,7 +1,14 @@ /* eslint-disable max-lines */ import { captureException, getCurrentHub } from '@sentry/node'; import { getActiveTransaction } from '@sentry/tracing'; -import { addExceptionMechanism, fill, loadModule, logger, stripUrlQueryAndFragment } from '@sentry/utils'; +import { + addExceptionMechanism, + fill, + loadModule, + logger, + serializeBaggage, + stripUrlQueryAndFragment, +} from '@sentry/utils'; // Types vendored from @remix-run/server-runtime@1.6.0: // https://github.com/remix-run/remix/blob/f3691d51027b93caa3fd2cdfe146d7b62a6eb8f2/packages/remix-server-runtime/server.ts @@ -21,15 +28,15 @@ interface Route { parentId?: string; path?: string; } -export interface RouteData { +interface RouteData { [routeId: string]: AppData; } -export interface MetaFunction { +interface MetaFunction { (args: { data: AppData; parentsData: RouteData; params: Params; location: Location }): HtmlMetaDescriptor; } -export interface HtmlMetaDescriptor { +interface HtmlMetaDescriptor { [name: string]: null | string | undefined | Record | Array | string>; charset?: 'utf-8'; charSet?: 'utf-8'; @@ -237,11 +244,13 @@ function makeWrappedMeta(origMeta: MetaFunction | HtmlMetaDescriptor = {}): Meta const scope = getCurrentHub().getScope(); if (scope) { const span = scope.getSpan(); + const transaction = getActiveTransaction(); - if (span) { + if (span && transaction) { return { ...origMetaResult, 'sentry-trace': span.toTraceparent(), + baggage: serializeBaggage(transaction.getBaggage()), }; } }