Skip to content
Draft

debug #1139

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
11 changes: 8 additions & 3 deletions agents-run-api/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import './src/env';
import { defaultSDK } from './src/instrumentation';

defaultSDK.start();
const isVercel = process.env.VERCEL === '1';

import app from './src/index';
if (!isVercel) {
const { defaultSDK } = await import('./src/instrumentation.js');
defaultSDK.start();
}

const appModule = await import('./src/index.js');
const app = appModule.default;

export const runtime = 'nodejs';
export default app;
9 changes: 9 additions & 0 deletions agents-run-api/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { registerOTel } from '@vercel/otel';

export async function register() {
await registerOTel({
serviceName: 'inkeep-agents-run-api',
traceExporter: 'otlp-http',
});
}

1 change: 1 addition & 0 deletions agents-run-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@opentelemetry/sdk-trace-base": "^2.1.0",
"@opentelemetry/sdk-trace-node": "^2.1.0",
"@opentelemetry/semantic-conventions": "^1.37.0",
"@vercel/otel": "^1.9.2",
"@vercel/sandbox": "^0.0.24",
"ai": "5.0.11",
"ajv": "^8.17.1",
Expand Down
20 changes: 19 additions & 1 deletion agents-run-api/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,26 @@ import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { env } from './env';
import { getLogger } from './logger';

const otlpExporter = new OTLPTraceExporter();
const logger = getLogger('instrumentation');

const isVercel = process.env.VERCEL === '1' || process.env.NEXT_RUNTIME === 'nodejs';

const otlpExporter = isVercel ? undefined : new OTLPTraceExporter();
/**
* Creates a safe batch processor that falls back to no-op when SignOz is not configured
* or when running on Vercel (which uses the root instrumentation.ts instead)
*/
function createSafeBatchProcessor(): SpanProcessor {
if (isVercel) {
logger.info({}, 'Running on Vercel, using root instrumentation.ts for tracing');
return new NoopSpanProcessor();
}

if (!otlpExporter) {
logger.warn({}, 'No OTLP exporter available');
return new NoopSpanProcessor();
}

try {
return new BatchSpanProcessor(otlpExporter, {
scheduledDelayMillis: env.OTEL_BSP_SCHEDULE_DELAY,
Expand Down Expand Up @@ -88,6 +102,10 @@ export const defaultSDK = new NodeSDK({
});

export async function flushBatchProcessor(): Promise<void> {
if (isVercel) {
return;
}

try {
await defaultBatchProcessor.forceFlush();
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion agents-run-api/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rootConfig from '../tsup.config';

export default defineConfig({
...rootConfig,
entry: ['src/index.ts', 'src/instrumentation.ts'],
entry: ['index.ts', 'src/index.ts', 'src/instrumentation.ts'],
external: ['keytar'],
esbuildOptions(options) {
options.loader = {
Expand Down
6 changes: 6 additions & 0 deletions agents-run-api/vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://openapi.vercel.sh/vercel.json",
"framework": "hono",
"buildCommand": "cd ../.. && pnpm build --filter=@inkeep/agents-run-api"
}

25 changes: 25 additions & 0 deletions pnpm-lock.yaml

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

Loading