-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinstrumentation.node.ts
More file actions
106 lines (92 loc) · 3.14 KB
/
Copy pathinstrumentation.node.ts
File metadata and controls
106 lines (92 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { migrate } from "drizzle-orm/libsql/migrator";
import { getDatabase } from "@/lib/db/drizzle";
await migrate(getDatabase(), { migrationsFolder: "./migrations" });
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
import { resourceFromAttributes } from "@opentelemetry/resources";
import { NodeSDK, tracing } from "@opentelemetry/sdk-node";
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
const HONEYCOMB_API_KEY = process.env.HONEYCOMB_API_KEY ?? "";
const useHoneycomb = !!HONEYCOMB_API_KEY;
const otelHost = useHoneycomb
? "https://api.honeycomb.io"
: "http://localhost:4318";
const traceEndpoint = `${otelHost}/v1/traces`;
console.log(
`OpenTelemetry exporting to: ${useHoneycomb ? "Honeycomb.io" : "localhost:4318"}`
);
const traceExporter = new OTLPTraceExporter({
url: traceEndpoint,
headers: { "x-honeycomb-team": HONEYCOMB_API_KEY },
});
const CAPTURED_REQUEST_HEADERS = [
"host",
"origin",
"referer",
"content-type",
"content-length",
"next-action",
"accept",
"accept-language",
] as const;
const httpInstrumentation = new HttpInstrumentation({
requestHook: (span, request) => {
for (const name of CAPTURED_REQUEST_HEADERS) {
let value: string | undefined;
if ("headers" in request && request.headers) {
const raw = request.headers[name];
if (raw) value = Array.isArray(raw) ? raw.join(", ") : raw;
} else if ("getHeader" in request) {
const raw = request.getHeader(name);
if (raw) value = Array.isArray(raw) ? raw.join(", ") : String(raw);
}
if (value) {
span.setAttribute(`http.request.header.${name}`, value);
}
}
},
});
// Drop traces for the /api/build-id poll; ParentBasedSampler drops all child
// spans (middleware, route execution, etc.) along with the root.
const dropBuildIdSampler: tracing.Sampler = {
shouldSample(_context, _traceId, _spanName, _spanKind, attributes) {
const target = attributes["http.target"];
return {
decision:
typeof target === "string" && target.startsWith("/api/build-id")
? tracing.SamplingDecision.NOT_RECORD
: tracing.SamplingDecision.RECORD_AND_SAMPLED,
};
},
toString: () => "DropBuildIdSampler",
};
const sdk = new NodeSDK({
resource: resourceFromAttributes({
[ATTR_SERVICE_NAME]: "nimble.monster",
}),
sampler: new tracing.ParentBasedSampler({ root: dropBuildIdSampler }),
traceExporter,
instrumentations: [httpInstrumentation],
});
const shutdownHandler = async () => {
try {
const { closeBrowser } = await import("@/lib/browser");
await closeBrowser();
} catch (error) {
console.error("Error closing browser:", error);
}
sdk
.shutdown()
.catch((error) =>
console.error("Error shutting down OpenTelemetry SDK", error)
)
.finally(() => process.exit(0));
};
process.on("SIGTERM", shutdownHandler);
process.on("SIGINT", shutdownHandler);
sdk.start();
import("@/lib/browser").then(({ getBrowser }) => {
getBrowser().catch((error) =>
console.error("Failed to pre-warm browser:", error)
);
});