|
| 1 | +/** |
| 2 | + * Diagnostic: verify OTel spans reach W&B Weave end-to-end |
| 3 | + * |
| 4 | + * Usage: WANDB_API_KEY=<key> WANDB_PROJECT_ID=<entity/project> bun run scripts/trace-test.ts |
| 5 | + */ |
| 6 | +import { maybeSetOtelProviders } from '@google/adk'; |
| 7 | +import { trace } from '@opentelemetry/api'; |
| 8 | +import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'; |
| 9 | +import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'; |
| 10 | + |
| 11 | +const apiKey = process.env.WANDB_API_KEY; |
| 12 | +const projectId = process.env.WANDB_PROJECT_ID; |
| 13 | + |
| 14 | +if (!apiKey || !projectId) { |
| 15 | + console.error('Set WANDB_API_KEY and WANDB_PROJECT_ID'); |
| 16 | + process.exit(1); |
| 17 | +} |
| 18 | + |
| 19 | +const url = 'https://trace.wandb.ai/otel/v1/traces'; |
| 20 | +const auth = Buffer.from(`api:${apiKey}`).toString('base64'); |
| 21 | + |
| 22 | +console.log('Config:'); |
| 23 | +console.log(' url:', url); |
| 24 | +console.log(' project_id:', projectId); |
| 25 | + |
| 26 | +const otlpExporter = new OTLPTraceExporter({ |
| 27 | + url, |
| 28 | + headers: { |
| 29 | + Authorization: `Basic ${auth}`, |
| 30 | + project_id: projectId, |
| 31 | + }, |
| 32 | +}); |
| 33 | + |
| 34 | +// Use BatchSpanProcessor directly (no WeaveSpanProcessor) to test raw export |
| 35 | +const batchProcessor = new BatchSpanProcessor(otlpExporter); |
| 36 | +maybeSetOtelProviders([{ spanProcessors: [batchProcessor] }]); |
| 37 | + |
| 38 | +const tracer = trace.getTracer('solenoid'); |
| 39 | + |
| 40 | +// Test span with sanitized name (no special chars) + Weave thread attributes |
| 41 | +const span = tracer.startSpan('diagnostic_agent_run'); |
| 42 | +span.setAttribute('gen_ai.agent.name', 'diagnostic_agent'); |
| 43 | +span.setAttribute('gen_ai.operation.name', 'invoke_agent'); |
| 44 | +span.setAttribute('wandb.thread_id', 'test-thread-123'); |
| 45 | +span.setAttribute('wandb.is_turn', true); |
| 46 | +span.end(); |
| 47 | + |
| 48 | +console.log('Span created and ended. Flushing to Weave...'); |
| 49 | + |
| 50 | +// Call forceFlush directly to surface any export errors |
| 51 | +try { |
| 52 | + await batchProcessor.forceFlush(); |
| 53 | + console.log('Export OK! Check: https://wandb.ai/' + projectId + '/weave/traces'); |
| 54 | +} catch (e) { |
| 55 | + console.error('Export FAILED:', e); |
| 56 | +} |
| 57 | + |
| 58 | +await batchProcessor.shutdown(); |
0 commit comments