Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
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
4 changes: 2 additions & 2 deletions apps/api/src/middlewares/greptime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Elysia } from "elysia";

import { greptimeSqlClient, type BunSqlClient } from "@hebo/shared-api/db/greptime";
import { getGreptimeSqlClient, type BunSqlClient } from "@hebo/shared-api/db/greptime";
import { auth } from "@hebo/shared-api/middlewares/auth";

export const greptime = new Elysia({
Expand All @@ -9,7 +9,7 @@ export const greptime = new Elysia({
.use(auth)
.resolve(function resolveGreptimeDb() {
return {
greptimeDb: greptimeSqlClient,
greptimeDb: getGreptimeSqlClient(),
};
})
.as("scoped");
Expand Down
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.

21 changes: 9 additions & 12 deletions packages/shared-api/db/greptime.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
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. */
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.

The previous createBunSqlClient was lazy as well, what exactly is the change here?

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>;
export type BunSqlClient = typeof _client;
10 changes: 8 additions & 2 deletions packages/shared-api/db/postgres.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaPg } from "@prisma/adapter-pg";
import type { PrismaPg } from "@prisma/adapter-pg";
import { Resource } from "sst";

import { DEFAULT_DB_IDLE_TIMEOUT_MS, DEFAULT_DB_POOL_MAX } from "./config";
Expand All @@ -22,12 +22,18 @@ export const createPrismaAdapter = (
schema: string,
max: number = DEFAULT_DB_POOL_MAX,
): PrismaPg => {
return new PrismaPg(
// Opaque require so the Bun bundler leaves it as a runtime call, letting
// OTel's RITM hooks intercept pg when the module loads.
// oxlint-disable no-unsafe-assignment no-unsafe-call no-unsafe-return
const pkg = ["@prisma", "adapter-pg"].join("/");
const { PrismaPg: Adapter } = require(pkg);
return new Adapter(
{
connectionString: getConnectionString(schema),
max,
idleTimeoutMillis: DEFAULT_DB_IDLE_TIMEOUT_MS,
},
{ schema: schema.toLowerCase() },
);
// oxlint-enable no-unsafe-assignment no-unsafe-call no-unsafe-return
};
32 changes: 14 additions & 18 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 All @@ -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";

/**
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -82,24 +81,21 @@ 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 => {
return {
serviceName,
instrumentations: [
new PgInstrumentation({
requireParentSpan: true,
enhancedDatabaseReporting: false,
}),
new BunSqlInstrumentation({
requireParentSpan: true,
ignoreConnectionSpans: true,
// FUTURE: set to true to avoid leaking sensitive information
maskStatement: false,
}),
],
headersToSpanAttributes: {
requestHeaders: ALLOWED_REQUEST_HEADERS,
responseHeaders: ALLOWED_RESPONSE_HEADERS,
Expand Down