Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions artifacts/api-server/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# OpenTelemetry Configuration
OTEL_SERVICE_NAME=api-server
OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.kubiks.app
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_EXPORTER_OTLP_HEADERS=x-kubiks-key=YOUR_KUBIKS_API_KEY

# Or alternatively, use KUBIKS_API_KEY
# KUBIKS_API_KEY=YOUR_KUBIKS_API_KEY
7 changes: 7 additions & 0 deletions artifacts/api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
},
"dependencies": {
"@clerk/express": "^2.1.10",
"@kubiks/otel-drizzle": "^2.1.0",
"@opentelemetry/api": "^1.9.1",
"@opentelemetry/auto-instrumentations-node": "^0.76.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.218.0",
"@opentelemetry/resources": "^2.7.1",
"@opentelemetry/sdk-node": "^0.218.0",
"@opentelemetry/semantic-conventions": "^1.41.1",
"@workspace/api-zod": "workspace:*",
"@workspace/db": "workspace:*",
"@workspace/integrations-openai-ai-server": "workspace:*",
Expand Down
3 changes: 3 additions & 0 deletions artifacts/api-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Initialize OpenTelemetry instrumentation first
import './instrumentation';

import app from "./app";
import { logger } from "./lib/logger";

Expand Down
57 changes: 57 additions & 0 deletions artifacts/api-server/src/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { DrizzleInstrumentation } from '@kubiks/otel-drizzle';

const resource = Resource.default().merge(
new Resource({
[SemanticResourceAttributes.SERVICE_NAME]:
process.env.OTEL_SERVICE_NAME || 'api-server',
[SemanticResourceAttributes.SERVICE_VERSION]: '1.0.0',
}),
);

const traceExporter = new OTLPTraceExporter({
url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT || 'https://ingest.kubiks.app',
headers: {
'x-kubiks-key':
process.env.OTEL_EXPORTER_OTLP_HEADERS?.split('=')[1] ||
process.env.KUBIKS_API_KEY ||
'',
},
});

const sdk = new NodeSDK({
resource,
traceExporter,
instrumentations: [
getNodeAutoInstrumentations({
'@opentelemetry/instrumentation-express': {
enabled: true,
},
'@opentelemetry/instrumentation-http': {
enabled: true,
},
}),
new DrizzleInstrumentation({
responseHook: (span, response) => {
span.setAttributes({
'db.statement': response.statement,
});
},
}),
],
});

sdk.start();

console.log('OpenTelemetry instrumentation initialized');

process.on('SIGTERM', () => {
sdk
.shutdown()
.then(() => console.log('OpenTelemetry SDK shut down successfully'))
.catch((err) => console.error('OpenTelemetry SDK shutdown failed', err));
});
Loading