Files
outline/server/logging/tracer.ts
T
Tom Moor fca10221b9 chore: promote no-explicit-any from warn to error (#12244)
* chore: promote no-explicit-any from warn to error and resolve violations

Upgrades the oxlint rule severity and removes all 40 existing
`no-explicit-any` warnings across the codebase. Most call sites gained
proper types (SharedEditor refs, JSONNode/JSONMark for ProseMirror JSON
walking, DocumentsStore, dd-trace `Span` parameter inference, prosemirror
Fragment public API in place of internal `(fragment as any).content`).
A few load-bearing `any` uses were preserved with scoped disable
comments where changing the type would cascade widely (Sequelize JSONB
columns on `Event`, the `withTracing` higher-order function generic,
`Extension.options` consumed by many subclasses, dd-trace's `req`
patching).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-02 12:14:23 -04:00

115 lines
2.9 KiB
TypeScript

import type { Span } from "dd-trace";
import 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;
};
};
};
// 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
if (env.DD_API_KEY) {
tracer.init({
version: env.VERSION,
service: env.DD_SERVICE,
env: env.ENVIRONMENT,
logInjection: true,
});
}
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;