-
Notifications
You must be signed in to change notification settings - Fork 2
fix: enable DB instrumentation metrics via Elysia OTel plugin #452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
c667f24
ad05bd3
ca30fc1
1501084
a61932d
d853188
af7d508
42bd9dd
566fac4
8696ed3
bae1f2c
0d73ccb
6f4e498
da9042f
2cb8384
ce8656b
9b884f9
0445637
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,14 @@ | ||
| import { getSecret } from "../utils/secret"; | ||
| import { DEFAULT_DB_IDLE_TIMEOUT_MS, DEFAULT_DB_POOL_MAX } from "./config"; | ||
|
|
||
| export const getGreptimeConnectionString = async () => { | ||
| return `postgres://${(await getSecret("GREPTIME_HOST")) ?? "localhost"}:4003/public`; | ||
| }; | ||
| export const GREPTIME_HOST = (await getSecret("GREPTIME_HOST")) ?? "localhost"; | ||
|
|
||
| export const createBunSqlClient = (url: string) => { | ||
| const { SQL } = require("bun") as typeof Bun; | ||
| return new SQL({ | ||
| url, | ||
| let _client: Bun.SQL; | ||
|
|
||
| /** Lazily created so BunSqlInstrumentation wraps `bun:sql` before the first `new SQL()` call. */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous |
||
| export const getGreptimeSqlClient = () => | ||
| (_client ??= new (require("bun") as typeof Bun).SQL({ | ||
| url: `postgres://${GREPTIME_HOST}:4003/public`, | ||
| max: DEFAULT_DB_POOL_MAX, | ||
| idleTimeout: DEFAULT_DB_IDLE_TIMEOUT_MS / 1_000, | ||
| }); | ||
| }; | ||
|
|
||
| export const greptimeSqlClient = createBunSqlClient(await getGreptimeConnectionString()); | ||
|
|
||
| export type BunSqlClient = ReturnType<typeof createBunSqlClient>; | ||
| })); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"; | ||
|
|
@@ -18,8 +17,8 @@ import { | |
| import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics"; | ||
| import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base"; | ||
|
|
||
| import { GREPTIME_HOST } from "../db/greptime"; | ||
| import { IS_PRODUCTION } from "../env"; | ||
| import { getSecret } from "../utils/secret"; | ||
| import { isRootPathUrl } from "../utils/url"; | ||
|
|
||
| /** | ||
|
|
@@ -48,7 +47,7 @@ const ALLOWED_RESPONSE_HEADERS = [ | |
| "x-request-id", | ||
| ]; | ||
|
|
||
| export const GREPTIME_OTLP_ENDPOINT = `http://${(await getSecret("GREPTIME_HOST")) ?? "localhost"}:4000/v1/otlp`; | ||
| export const GREPTIME_OTLP_ENDPOINT = `http://${GREPTIME_HOST}:4000/v1/otlp`; | ||
|
|
||
| export const createOtelLogger = (serviceName: string, minimumSeverity: SeverityNumber) => { | ||
| const loggerProvider = new LoggerProvider({ | ||
|
|
@@ -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, | ||
| }); | ||
|
|
||
| // In Bun, pg is already loaded via @prisma/adapter-pg before OTel's module-load | ||
| // hooks can patch it. Patch pg.Client directly as a workaround, while still | ||
| // passing the instrumentation to NodeSDK so providers are configured correctly. | ||
| 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, | ||
| }), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't know this, its completely encapsulated by the shared-api layer. Please revert.