Files
Genarrative/scripts/run-otelcol.mjs
T
kdletters 071faa482c 统一 Rust 与 TypeScript 格式化门禁
纳入 AGC Cargo workspace 的统一 rustfmt 检查与格式化入口

完成项目 TypeScript/Prettier 与 Rust 全量格式化

修复 Pingora expected executable 门禁的空白敏感误报

同步开发运维文档与 AGC skill pack 格式化忽略规则
2026-09-01 16:28:34 +08:00

131 lines
3.2 KiB
JavaScript

import { spawn } from 'node:child_process';
import { mkdirSync, writeFileSync } from 'node:fs';
import path from 'node:path';
const [, , rawMode = 'debug', ...args] = process.argv;
const mode = rawMode.trim();
const printConfigOnly = args.includes('--print-config');
const supportedModes = new Set(['debug', 'rider']);
if (!supportedModes.has(mode)) {
console.error('[otelcol] mode must be one of: debug, rider');
process.exit(1);
}
const otlpHttpEndpoint = readEnv(
'OTELCOL_OTLP_HTTP_ENDPOINT',
'127.0.0.1:4318',
);
const otlpGrpcEndpoint = readEnv(
'OTELCOL_OTLP_GRPC_ENDPOINT',
'127.0.0.1:4317',
);
const riderEndpoint = readEnv('RIDER_OTLP_GRPC_ENDPOINT', '127.0.0.1:17011');
const debugVerbosity = readEnv('OTELCOL_DEBUG_VERBOSITY', 'detailed');
const otelcolBin = readEnv('OTELCOL_BIN', 'otelcol-contrib');
const configText = buildConfig(mode);
const configDir = path.resolve('.codex-temp', 'otelcol');
const configPath = path.join(configDir, `genarrative-${mode}.yaml`);
mkdirSync(configDir, { recursive: true });
writeFileSync(configPath, configText, 'utf8');
console.log(`[otelcol] wrote ${configPath}`);
console.log(`[otelcol] receiving OTLP HTTP at http://${otlpHttpEndpoint}`);
console.log(`[otelcol] receiving OTLP gRPC at ${otlpGrpcEndpoint}`);
if (mode === 'rider') {
console.log(
`[otelcol] forwarding traces/metrics/logs to Rider OTLP gRPC at ${riderEndpoint}`,
);
}
console.log(
'[otelcol] api-server env: GENARRATIVE_OTEL_ENABLED=true OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318',
);
if (printConfigOnly) {
console.log(configText);
process.exit(0);
}
const child = spawn(otelcolBin, ['--config', configPath], {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit',
});
const stopChild = () => {
if (!child.killed) {
child.kill();
}
};
for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP']) {
process.on(signal, () => {
stopChild();
process.exit(130);
});
}
process.on('exit', stopChild);
child.on('error', (error) => {
console.error(`[otelcol] failed to start ${otelcolBin}: ${error.message}`);
console.error(
'[otelcol] install otelcol-contrib and make sure it is on PATH, or set OTELCOL_BIN.',
);
process.exit(1);
});
child.on('exit', (code, signal) => {
if (signal) {
console.error(`[otelcol] exited by signal: ${signal}`);
process.exit(1);
}
process.exit(code ?? 0);
});
function readEnv(key, fallback) {
const value = process.env[key]?.trim();
return value ? value : fallback;
}
function buildConfig(selectedMode) {
const exporters =
selectedMode === 'rider'
? ` otlp/rider:
endpoint: ${riderEndpoint}
tls:
insecure: true
debug:
verbosity: ${debugVerbosity}`
: ` debug:
verbosity: ${debugVerbosity}`;
const pipelineExporters =
selectedMode === 'rider' ? '[otlp/rider, debug]' : '[debug]';
return `receivers:
otlp:
protocols:
grpc:
endpoint: ${otlpGrpcEndpoint}
http:
endpoint: ${otlpHttpEndpoint}
exporters:
${exporters}
service:
pipelines:
traces:
receivers: [otlp]
exporters: ${pipelineExporters}
metrics:
receivers: [otlp]
exporters: ${pipelineExporters}
logs:
receivers: [otlp]
exporters: ${pipelineExporters}
`;
}