mirror of
https://github.com/outline/outline.git
synced 2026-11-25 15:15:15 +03:00
* perf: lazy-load jsdom in DocumentHelper to keep it off startup path
DocumentHelper is imported by the Document and Collection models, so its
eager `import { JSDOM } from "jsdom"` pulled jsdom (~140ms) into every
server process at boot. jsdom is only needed by toEmailDiff for HTML
export, so load it lazily there — matching the existing pattern in the
sibling ProsemirrorHelper.toHTML.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* perf: defer AWS CRT signer load out of the startup path
S3Storage had a top-level `import "@aws-sdk/signature-v4-crt"` side effect
that loaded the native CRT binding (~140ms) whenever the module was
imported — which happens at boot via the Attachment model in every
process, even for local-storage deployments that never construct S3Storage.
Move the import into the S3Storage constructor so the native binding only
loads when S3 storage is actually instantiated, keeping it off the startup
path for local-storage deployments.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* perf: load dd-trace only when APM is enabled
tracer.ts imported dd-trace unconditionally, so the library (which patches
Node internals on require, ~35-80ms) loaded at startup in every process —
including the common self-hosted case where DD_API_KEY is unset and APM
does nothing. tracer.ts is the first import in server/index.ts, so this
cost is fully serial on the boot path.
Require dd-trace only when DD_API_KEY is set; otherwise fall back to a
no-op tracer covering the surface used by callers (scope/startSpan and a
no-op span), so tracing.ts and the named helpers work unchanged. The
dd-trace/ext/tags import used by tracing.ts is a standalone constants
module and does not pull in the full tracer.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
142 lines
3.8 KiB
TypeScript
142 lines
3.8 KiB
TypeScript
import type { Span, Tracer } from "dd-trace";
|
|
import env from "@server/env";
|
|
|
|
interface ReportableError extends Error {
|
|
isReportable?: boolean;
|
|
}
|
|
|
|
/** Whether the error has been explicitly marked as non-reportable. */
|
|
function isExplicitlyNonReportable(error: Error): error is ReportableError {
|
|
return (
|
|
"isReportable" in error && (error as ReportableError).isReportable === false
|
|
);
|
|
}
|
|
|
|
type PrivateDatadogContext = {
|
|
// oxlint-disable-next-line @typescript-eslint/no-explicit-any
|
|
req: Record<string, any> & {
|
|
_datadog?: {
|
|
span?: Span;
|
|
};
|
|
};
|
|
};
|
|
|
|
// dd-trace patches Node internals on require and is comparatively expensive to
|
|
// load, so it is kept off the startup path unless APM is actually enabled. When
|
|
// disabled we fall back to a no-op tracer that satisfies the surface consumed by
|
|
// callers (see tracing.ts) so they don't need to branch.
|
|
const noopSpan = {
|
|
addTags: () => noopSpan,
|
|
setTag: () => noopSpan,
|
|
finish: () => undefined,
|
|
} as unknown as Span;
|
|
|
|
let tracer: Tracer;
|
|
|
|
// If the DataDog agent is installed and the DD_API_KEY environment variable is
|
|
// in the environment then we can safely attempt to start the DD tracer. This
|
|
// must happen before any instrumented module is imported.
|
|
if (env.DD_API_KEY) {
|
|
const ddTrace = require("dd-trace") as { default?: Tracer };
|
|
tracer = ddTrace.default ?? (ddTrace as unknown as Tracer);
|
|
|
|
tracer.init({
|
|
version: env.VERSION,
|
|
service: env.DD_SERVICE,
|
|
env: env.ENVIRONMENT,
|
|
logInjection: true,
|
|
});
|
|
|
|
// Disable per-middleware spans so that non-reportable errors are not captured
|
|
// This is also generally excessive noise
|
|
tracer.use("koa", { middleware: false });
|
|
} else {
|
|
tracer = {
|
|
scope: () => ({
|
|
active: () => null,
|
|
activate: <T>(_span: Span, fn: () => T) => fn(),
|
|
}),
|
|
startSpan: () => noopSpan,
|
|
} as unknown as Tracer;
|
|
}
|
|
|
|
const getCurrentSpan = (): Span | null => tracer.scope().active();
|
|
|
|
/**
|
|
* Add tags to a span to have more context about how and why it was running.
|
|
* If added to the root span, tags are searchable and filterable.
|
|
*
|
|
* @param tags An object with the tags to add to the span
|
|
* @param span An optional span object to add the tags to. If none provided,the current span will be used.
|
|
*/
|
|
export function addTags(
|
|
tags: Parameters<Span["addTags"]>[0],
|
|
span?: Span | null
|
|
): void {
|
|
if (tracer) {
|
|
const currentSpan = span || getCurrentSpan();
|
|
|
|
if (!currentSpan) {
|
|
return;
|
|
}
|
|
|
|
currentSpan.addTags(tags);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The root span is an undocumented internal property that DataDog adds to `context.req`.
|
|
* The root span is required in order to add searchable tags.
|
|
* Unfortunately, there is no API to access the root span directly.
|
|
* See: node_modules/dd-trace/src/plugins/util/web.js
|
|
*
|
|
* @param context A Koa context object
|
|
*/
|
|
export function getRootSpanFromRequestContext(
|
|
context: PrivateDatadogContext
|
|
): Span | null {
|
|
// oxlint-disable-next-line no-undef
|
|
return context?.req?._datadog?.span ?? null;
|
|
}
|
|
|
|
/**
|
|
* Change the resource of the active APM span. This method wraps addTags to allow
|
|
* safe use in environments where APM is disabled.
|
|
*
|
|
* @param name The name of the resource
|
|
*/
|
|
export function setResource(name: string) {
|
|
if (tracer) {
|
|
addTags({
|
|
"resource.name": `${name}`,
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Mark the current active span as an error. This method wraps addTags to allow
|
|
* safe use in environments where APM is disabled. Errors with isReportable set
|
|
* to false are skipped.
|
|
*
|
|
* @param error The error to add to the current span.
|
|
*/
|
|
export function setError(error: Error, span?: Span) {
|
|
if (isExplicitlyNonReportable(error)) {
|
|
return;
|
|
}
|
|
|
|
if (tracer) {
|
|
addTags(
|
|
{
|
|
errorMessage: error.message,
|
|
"error.type": error.name,
|
|
"error.msg": error.message,
|
|
"error.stack": error.stack,
|
|
},
|
|
span
|
|
);
|
|
}
|
|
}
|
|
|
|
export default tracer;
|