import { LOGS_PREFIX, METRICS_ENV, READY_ENV, TRACES_ENV } from './const.ts' import { DEFAULT_ANNOTATION, normalizeAnnotation, type ReadyAnnotation } from './ready.ts' import type { Dependency, Probe, Variables } from '@toa.io/operations' import type { ExportersConfig, LevelName, LogExportersConfig, MetersConfig } from 'openspan' export function deployment(_: unknown, annotation?: Annotation): Dependency { const variables: Variables = { global: [] } if (annotation?.logs !== undefined) addLogsVariables(annotation.logs, variables) if (annotation?.traces !== undefined) addTracesVariables(annotation.traces, variables) if (annotation?.metrics !== undefined) addMetricsVariables(annotation.metrics, variables) const ready = normalizeAnnotation(annotation?.ready) if (ready === false) { variables.global.push({ name: READY_ENV, value: JSON.stringify(false) }) return { variables, probe: false } } variables.global.push({ name: READY_ENV, value: JSON.stringify(ready) }) const probe: Probe = { path: ready.path ?? DEFAULT_ANNOTATION.path, port: ready.port ?? DEFAULT_ANNOTATION.port } return { variables, probe } } function addLogsVariables(annotation: LogsAnnotation, variables: Variables): void { const { level, exporters, ...components } = annotation // the level is per component, and an exporter is the process's: a component override carries // the one it can change const global = { level } if (exporters?.otlp !== undefined) if (typeof exporters.otlp.endpoint !== 'string') throw new Error('telemetry.logs.exporters.otlp.endpoint is required') if (level !== undefined || exporters !== undefined) variables.global.push({ name: LOGS_PREFIX, value: JSON.stringify({ level, exporters }) }) for (const [id, override] of Object.entries(components)) { const [namespace, name] = id.split('.') const value = Object.assign({}, global, override) variables.global.push({ name: `${LOGS_PREFIX}_${namespace.toUpperCase()}_${name.toUpperCase()}`, value: JSON.stringify(value) }) } } function addTracesVariables(annotation: TracesAnnotation, variables: Variables): void { const { sample, rate, exporters } = annotation if (sample !== undefined) if (!(typeof sample === 'number' && sample >= 0 && sample <= 1)) throw new Error('telemetry.traces.sample must be a number within [0, 1]') if (rate !== undefined) if (!(typeof rate === 'number' && rate > 0)) throw new Error('telemetry.traces.rate must be a positive number') if (exporters?.otlp !== undefined) if (typeof exporters.otlp.endpoint !== 'string') throw new Error('telemetry.traces.exporters.otlp.endpoint is required') variables.global.push({ name: TRACES_ENV, value: JSON.stringify({ sample, rate, exporters }) }) } function addMetricsVariables(annotation: MetricsAnnotation, variables: Variables): void { const { interval, exporters } = annotation if (interval !== undefined) if (!(typeof interval === 'number' && interval > 0)) throw new Error('telemetry.metrics.interval must be a positive number') if (exporters?.otlp !== undefined) if (typeof exporters.otlp.endpoint !== 'string') throw new Error('telemetry.metrics.exporters.otlp.endpoint is required') variables.global.push({ name: METRICS_ENV, value: JSON.stringify({ interval, exporters }) }) } interface Annotation { logs?: LogsAnnotation & Record traces?: TracesAnnotation metrics?: MetricsAnnotation ready?: ReadyAnnotation } interface LogsAnnotation { level?: LevelName exporters?: LogExportersConfig } interface TracesAnnotation { sample?: number rate?: number exporters?: ExportersConfig } interface MetricsAnnotation { interval?: number exporters?: MetersConfig }