|
| 1 | +import packageJson from "../../package.json"; |
| 2 | + |
| 3 | +export type TelemetryRole = "api" | "moderation-worker"; |
| 4 | + |
| 5 | +export interface TelemetryConfig { |
| 6 | + enabled: boolean; |
| 7 | + endpoint?: string; |
| 8 | + sampleRatio: number; |
| 9 | + serviceName: "cnode-api" | "cnode-moderation-worker"; |
| 10 | + serviceVersion: string; |
| 11 | + commitRevision: string; |
| 12 | + deploymentEnvironment: string; |
| 13 | +} |
| 14 | + |
| 15 | +export class TelemetryConfigError extends Error { |
| 16 | + constructor(variable: string, expectation: string) { |
| 17 | + super(`${variable} ${expectation}`); |
| 18 | + this.name = "TelemetryConfigError"; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +function parseEnabled(value: string | undefined) { |
| 23 | + if (value === undefined || value === "" || value === "0" || value === "false") return false; |
| 24 | + if (value === "1" || value === "true") return true; |
| 25 | + throw new TelemetryConfigError("CNODE_OTEL_ENABLED", "must be a boolean"); |
| 26 | +} |
| 27 | + |
| 28 | +function parseSampleRatio(value: string | undefined) { |
| 29 | + if (value === undefined || value === "") return 0.1; |
| 30 | + const ratio = Number(value); |
| 31 | + if (!Number.isFinite(ratio) || ratio < 0 || ratio > 1) { |
| 32 | + throw new TelemetryConfigError( |
| 33 | + "CNODE_OTEL_TRACE_SAMPLE_RATIO", |
| 34 | + "must be a finite number between 0 and 1", |
| 35 | + ); |
| 36 | + } |
| 37 | + return ratio; |
| 38 | +} |
| 39 | + |
| 40 | +function parseEndpoint(value: string | undefined) { |
| 41 | + if (!value) { |
| 42 | + throw new TelemetryConfigError("CNODE_OTEL_EXPORTER_OTLP_ENDPOINT", "is required"); |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + const endpoint = new URL(value); |
| 47 | + if (endpoint.protocol !== "http:" && endpoint.protocol !== "https:") throw new Error(); |
| 48 | + if (endpoint.username || endpoint.password) throw new Error(); |
| 49 | + return endpoint.toString(); |
| 50 | + } catch { |
| 51 | + throw new TelemetryConfigError( |
| 52 | + "CNODE_OTEL_EXPORTER_OTLP_ENDPOINT", |
| 53 | + "must be an HTTP URL without credentials", |
| 54 | + ); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +export function parseTelemetryConfig( |
| 59 | + role: TelemetryRole, |
| 60 | + env: NodeJS.ProcessEnv = process.env, |
| 61 | +): TelemetryConfig { |
| 62 | + const enabled = parseEnabled(env.CNODE_OTEL_ENABLED); |
| 63 | + return { |
| 64 | + enabled, |
| 65 | + endpoint: enabled ? parseEndpoint(env.CNODE_OTEL_EXPORTER_OTLP_ENDPOINT) : undefined, |
| 66 | + sampleRatio: enabled ? parseSampleRatio(env.CNODE_OTEL_TRACE_SAMPLE_RATIO) : 0, |
| 67 | + serviceName: role === "api" ? "cnode-api" : "cnode-moderation-worker", |
| 68 | + serviceVersion: packageJson.version || "unknown", |
| 69 | + commitRevision: env.CNODE_GIT_SHA || env.GIT_SHA || env.COMMIT_SHA || "unknown", |
| 70 | + deploymentEnvironment: env.CNODE_ENV || env.NODE_ENV || "unknown", |
| 71 | + }; |
| 72 | +} |
0 commit comments