Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions apps/api/src/db/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { ProviderConfigSchema, type ProviderConfig } from "~api/modules/provider

const DB_NULL = null;

const prisma = new PrismaClient({
adapter: createPrismaAdapter("api"),
});
/** Deferred to first request so OTel instrumentation is registered before the driver loads. */
let _prisma: PrismaClient;
const getPrisma = () => (_prisma ??= new PrismaClient({ adapter: createPrismaAdapter("api") }));

export const createPrismaClient = (organizationId: string, userId: string) => {
if (!organizationId || !userId) {
Expand All @@ -17,7 +17,7 @@ export const createPrismaClient = (organizationId: string, userId: string) => {

const tenantFilters = { deleted_at: DB_NULL, organization_id: organizationId };

return prisma.$extends({
return getPrisma().$extends({
query: {
$allModels: {
$allOperations({ args, query, operation }) {
Expand Down Expand Up @@ -94,7 +94,7 @@ export const createPrismaClient = (organizationId: string, userId: string) => {
},
provider_configs: {
getUnredacted(slug: string) {
return prisma.provider_configs.findFirst({
return getPrisma().provider_configs.findFirst({
where: {
provider_slug: slug,
created_by: userId,
Expand Down
3 changes: 2 additions & 1 deletion 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,6 +22,7 @@ export const createPrismaAdapter = (
schema: string,
max: number = DEFAULT_DB_POOL_MAX,
): PrismaPg => {
const { PrismaPg } = require("@prisma/adapter-pg") as typeof import("@prisma/adapter-pg");
return new PrismaPg(
{
connectionString: getConnectionString(schema),
Expand Down
28 changes: 12 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,28 +81,25 @@ 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,
additionalRequestHeaders?: readonly string[],
additionalResponseHeaders?: readonly 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: {
request: [...ALLOWED_REQUEST_HEADERS, ...(additionalRequestHeaders ?? [])],
response: [...ALLOWED_RESPONSE_HEADERS, ...(additionalResponseHeaders ?? [])],
Expand Down