Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 26 additions & 16 deletions packages/shared-api/lib/otel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { SeverityNumber } from "@opentelemetry/api-logs";
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-proto";
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-proto";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { PgInstrumentation } from "@opentelemetry/instrumentation-pg";
import { CompressionAlgorithm } from "@opentelemetry/otlp-exporter-base";
import { resourceFromAttributes } from "@opentelemetry/resources";
Expand Down Expand Up @@ -82,24 +81,35 @@ export const createOtelLogger = (serviceName: string, minimumSeverity: SeverityN
return loggerProvider.getLogger(serviceName);
};

registerInstrumentations({
instrumentations: [
new PgInstrumentation({
requireParentSpan: true,
enhancedDatabaseReporting: false,
}),
new BunSqlInstrumentation({
requireParentSpan: true,
ignoreConnectionSpans: true,
// FUTURE: set to true to avoid leaking sensitive information
maskStatement: false,
}),
],
});

export const getOtelConfig = (serviceName: string): ElysiaOpenTelemetryOptions => {
const pgInstrumentation = new PgInstrumentation({
requireParentSpan: true,
enhancedDatabaseReporting: false,
});

// Bun loads all static ESM imports before any module body runs, so RITM hooks
// never fire for pg (https://github.com/oven-sh/bun/issues/3775). Patch it
// directly so the NodeSDK can produce spans and call setMeterProvider() on it.
try {
// oxlint-disable no-unsafe-assignment no-unsafe-call no-unsafe-member-access
const { createRequire } = require("module");
const pg = createRequire(require.resolve("@prisma/adapter-pg"))("pg");
// @ts-expect-error _patchPgClient is a private method on PgInstrumentation
pgInstrumentation._patchPgClient(pg.Client);
// oxlint-enable no-unsafe-assignment no-unsafe-call no-unsafe-member-access
} catch {}

return {
serviceName,
instrumentations: [
pgInstrumentation,
new BunSqlInstrumentation({
requireParentSpan: true,
ignoreConnectionSpans: true,
// FUTURE: set to true to avoid leaking sensitive information
maskStatement: false,
}),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align coding pattern across pg and bun instrumentation.

],
headersToSpanAttributes: {
requestHeaders: ALLOWED_REQUEST_HEADERS,
responseHeaders: ALLOWED_RESPONSE_HEADERS,
Expand Down