Skip to content
Closed
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
13 changes: 10 additions & 3 deletions src/cli/commands/dev/browser-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { AgentCoreProjectSpec } from '../../../schema';
import { ANSI } from '../../constants';
import { isDeploySkippable } from '../../operations/deploy/change-detection';
import { getDevConfig, getDevSupportedAgents, loadDevEnv, loadProjectConfig } from '../../operations/dev';
import { type OtelCollector, startOtelCollector } from '../../operations/dev/otel';
import { type OtelCollector, hasExternalOtelEndpoint, startOtelCollector } from '../../operations/dev/otel';
import {
type AgentInfo,
type ListMemoryRecordsHandler,
Expand Down Expand Up @@ -142,8 +142,15 @@ export async function launchBrowserDev(): Promise<void> {
}

const configRoot = findConfigRoot(workingDir);
const persistTracesDir = path.join(configRoot ?? workingDir, '.cli', 'traces');
const { collector, otelEnvVars } = await startOtelCollector(persistTracesDir);
let collector: OtelCollector | undefined;
let otelEnvVars: Record<string, string> = {};

if (!hasExternalOtelEndpoint()) {
const persistTracesDir = path.join(configRoot ?? workingDir, '.cli', 'traces');
const result = await startOtelCollector(persistTracesDir);
collector = result.collector;
otelEnvVars = result.otelEnvVars;
}

await runBrowserMode({
workingDir,
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/dev/command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
loadProjectConfig,
onShutdownSignal,
} from '../../operations/dev';
import { OtelCollector, startOtelCollector } from '../../operations/dev/otel';
import { OtelCollector, hasExternalOtelEndpoint, startOtelCollector } from '../../operations/dev/otel';
import { withCommandRunTelemetry } from '../../telemetry/cli-command-run.js';
import { AgentProtocol, standardize } from '../../telemetry/schemas/common-shapes.js';
import { LayoutProvider } from '../../tui/context';
Expand Down Expand Up @@ -358,7 +358,7 @@ export const registerDev = (program: Command) => {
let otelEnvVars: Record<string, string> = {};
let collector: OtelCollector | undefined;

if (opts.traces !== false) {
if (opts.traces !== false && !hasExternalOtelEndpoint()) {
const persistTracesDir = path.join(configRoot ?? workingDir, '.cli', 'traces');
const otelResult = await startOtelCollector(persistTracesDir);
collector = otelResult.collector;
Expand Down
34 changes: 34 additions & 0 deletions src/cli/operations/dev/__tests__/otel-collector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { hasExternalOtelEndpoint } from '../otel';
import { afterEach, describe, expect, it } from 'vitest';

describe('hasExternalOtelEndpoint', () => {
const savedEnv = { ...process.env };

afterEach(() => {
process.env = { ...savedEnv };
});

it('returns false when no OTEL endpoint env vars are set', () => {
delete process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
delete process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT;
expect(hasExternalOtelEndpoint()).toBe(false);
});

it('returns true when OTEL_EXPORTER_OTLP_ENDPOINT is set', () => {
process.env.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://localhost:4318';
delete process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT;
expect(hasExternalOtelEndpoint()).toBe(true);
});

it('returns true when OTEL_EXPORTER_OTLP_TRACES_ENDPOINT is set', () => {
delete process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = 'http://localhost:4318/v1/traces';
expect(hasExternalOtelEndpoint()).toBe(true);
});

it('returns true when both are set', () => {
process.env.OTEL_EXPORTER_OTLP_ENDPOINT = 'http://localhost:4318';
process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = 'http://localhost:4318/v1/traces';
expect(hasExternalOtelEndpoint()).toBe(true);
});
});
5 changes: 5 additions & 0 deletions src/cli/operations/dev/otel/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ function readBodyAsBuffer(req: IncomingMessage): Promise<Buffer> {
});
}

/** Returns true if the user has already configured an external OTLP endpoint. */
export function hasExternalOtelEndpoint(): boolean {
return !!process.env.OTEL_EXPORTER_OTLP_ENDPOINT || !!process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT;
}

/**
* Start an OTEL collector and return it along with the env vars agents need
* to export traces to it.
Expand Down
2 changes: 1 addition & 1 deletion src/cli/operations/dev/otel/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { OtelCollector, startOtelCollector } from './collector';
export { OtelCollector, hasExternalOtelEndpoint, startOtelCollector } from './collector';
export type { OtlpResourceSpan, OtlpResourceLog } from './types';
Loading