Skip to content

Commit 99a3aea

Browse files
authored
chore: otel agent logs gated (#815)
1 parent dbb12ac commit 99a3aea

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

apps/twig/src/main/services/agent/service.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { MAIN_TOKENS } from "../../di/tokens.js";
2626
import { logger } from "../../lib/logger.js";
2727
import { TypedEventEmitter } from "../../lib/typed-event-emitter.js";
2828
import type { FsService } from "../fs/service.js";
29+
import { getCurrentUserId, getPostHogClient } from "../posthog-analytics.js";
2930
import type { ProcessTrackingService } from "../process-tracking/service.js";
3031
import type { SleepService } from "../sleep/service.js";
3132
import {
@@ -437,23 +438,30 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
437438
const mockNodeDir = this.setupMockNodeEnvironment(taskRunId);
438439
this.setupEnvironment(credentials, mockNodeDir);
439440

440-
// Route agent logs to dedicated agent_logs Kafka topic via capture-logs-agent service
441-
// In local dev, use Caddy proxy (port 8010) which routes /i/v1/agent-logs to capture-logs-agent
442-
// In prod, use the main API host which proxies /i/v1/agent-logs to capture-logs-agent
443-
const otelHost = credentials.apiHost;
444-
const otelPath = "/i/v1/agent-logs";
441+
// OTEL log pipeline or legacy S3 writer if FF false
442+
const useOtelPipeline = await this.isFeatureFlagEnabled(
443+
"twig-agent-logs-pipeline",
444+
);
445+
446+
log.info("Agent log transport", {
447+
transport: useOtelPipeline ? "otel" : "s3",
448+
taskId,
449+
taskRunId,
450+
});
445451

446452
const agent = new Agent({
447453
posthog: {
448454
apiUrl: credentials.apiHost,
449455
getApiKey: () => this.getToken(credentials.apiKey),
450456
projectId: credentials.projectId,
451457
},
452-
otelTransport: {
453-
host: otelHost,
454-
apiKey: this.getToken(credentials.apiKey),
455-
logsPath: otelPath,
456-
},
458+
otelTransport: useOtelPipeline
459+
? {
460+
host: credentials.apiHost,
461+
apiKey: this.getToken(credentials.apiKey),
462+
logsPath: "/i/v1/agent-logs",
463+
}
464+
: undefined,
457465
debug: !app.isPackaged,
458466
onLog: onAgentLog,
459467
});
@@ -956,6 +964,20 @@ For git operations while detached:
956964
return mockNodeDir;
957965
}
958966

967+
private async isFeatureFlagEnabled(flagKey: string): Promise<boolean> {
968+
try {
969+
const client = getPostHogClient();
970+
const userId = getCurrentUserId();
971+
if (!client || !userId) {
972+
return false;
973+
}
974+
return (await client.isFeatureEnabled(flagKey, userId)) ?? false;
975+
} catch (error) {
976+
log.warn(`Error checking feature flag "${flagKey}":`, error);
977+
return false;
978+
}
979+
}
980+
959981
private cleanupMockNodeEnvironment(mockNodeDir: string): void {
960982
try {
961983
rmSync(mockNodeDir, { recursive: true, force: true });

packages/agent/src/agent.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,20 @@ export class Agent {
3232
this.posthogAPI = new PostHogAPIClient(config.posthog);
3333
}
3434

35-
if (config.posthog || config.otelTransport) {
35+
if (config.otelTransport) {
36+
// OTEL pipeline: use OtelLogWriter only (no S3 writer)
37+
this.sessionLogWriter = new SessionLogWriter({
38+
otelConfig: {
39+
posthogHost: config.otelTransport.host,
40+
apiKey: config.otelTransport.apiKey,
41+
logsPath: config.otelTransport.logsPath,
42+
},
43+
logger: this.logger.child("SessionLogWriter"),
44+
});
45+
} else if (config.posthog) {
46+
// Legacy: use S3 writer via PostHog API
3647
this.sessionLogWriter = new SessionLogWriter({
3748
posthogAPI: this.posthogAPI,
38-
otelConfig: config.otelTransport
39-
? {
40-
posthogHost: config.otelTransport.host,
41-
apiKey: config.otelTransport.apiKey,
42-
logsPath: config.otelTransport.logsPath,
43-
}
44-
: undefined,
4549
logger: this.logger.child("SessionLogWriter"),
4650
});
4751
}

0 commit comments

Comments
 (0)