import { console, logs, metrics, traces } from 'openspan' import { environment } from '@toa.io/generic' import { LOGS_PREFIX, METRICS_ENV, TRACES_ENV } from '@toa.io/definitions/extensions.telemetry' import { Logs } from './Logs.ts' import { Span } from './Span.ts' import { Metrics } from './Metrics.ts' import { Ready } from './Ready.ts' import type { LogsOptions } from './Logs.ts' import type { Declaration } from '@toa.io/definitions/extensions.telemetry' import type { Locator } from '@toa.io/core' import type { extensions } from '@toa.io/core/types' import type { LogExportersConfig, MetricsOptions, TracesOptions } from 'openspan' type Resident = extensions.Resident export class Factory implements extensions.Factory { private readonly logsConfig: LogsConfig private readonly ready: Ready | null public constructor() { const globEnv = environment.get(LOGS_PREFIX) const level = environment.get('TOA_DEV') === '1' ? 'trace' : 'info' this.logsConfig = globEnv === undefined ? { level } : JSON.parse(globEnv) this.logsConfig.level ??= level console.configure({ level: this.logsConfig.level }) // after the level, which is what decides whether an entry is written at all logs({ exporters: records(this.logsConfig.exporters) }) const tracesEnv = environment.get(TRACES_ENV) const options = tracesEnv === undefined ? development() : (JSON.parse(tracesEnv) as TracesOptions) // `openspan` is not Toa's and reads the environment, which no longer holds the context by // the time the exporter is made — so the service is named here if (options.exporters?.otlp !== undefined) options.exporters.otlp.service ??= environment.get('TOA_CONTEXT') traces(options) metrics(measurements()) this.ready = Ready.create() } public aspect(locator: Locator, declaration: Declaration): extensions.Aspect[] { const logs = this.createLogs(locator) const span = new Span(locator) const metrics = new Metrics(locator, declaration ?? {}) return [logs, span, metrics] } /** * The probe answers for the process, so it is the process it belongs to — and not whichever * composition happens to be nearest, which in a service is the one nested inside it. That is * why the explorer reported ready when the components it hosts connected rather than when * the explorer did, and why the gateway carries a probe of its own to work around it. * * It binds before the process is built and answers `503` until `complete()`, which is the * phase the `connect` this used to patch was standing in for. */ public resident(): Resident | null { return this.ready } private createLogs(locator: Locator): extensions.Aspect { const overEnv = environment.get(`${LOGS_PREFIX}_${locator.uppercase}`) const override = overEnv !== undefined ? JSON.parse(overEnv) : undefined const { level } = Object.assign({}, this.logsConfig, override) return new Logs(locator, { level }) } } /** * What the records say they came from. The same resource the series carry, so that one backend * holding several products and several environments separates them by the same attributes * whichever signal is being read. */ function records(exporters?: LogExportersConfig): LogExportersConfig | undefined { if (exporters?.otlp !== undefined) { const resource = (exporters.otlp.resource ??= {}) resource['service.name'] ??= environment.get('TOA_CONTEXT') resource['service.namespace'] ??= environment.get('TOA_CONTEXT') resource['deployment.environment.name'] ??= environment.get('TOA_ENV') } return exporters } /** * What the series say they came from. The metric names are the same in every deployment, so what * tells two products apart in one backend is the resource: the context is the namespace, and the * environment is beside it. * * Nothing when the annotation is absent, which is what turns measuring off: there is no console * exporter for this signal, so a local run measures only where it was asked to. */ function measurements(): MetricsOptions | undefined { const env = environment.get(METRICS_ENV) if (env === undefined) return undefined const options = JSON.parse(env) as MetricsOptions options.prefix ??= 'toa' if (options.exporters?.otlp !== undefined) { const resource = (options.exporters.otlp.resource ??= {}) resource['service.namespace'] ??= environment.get('TOA_CONTEXT') resource['deployment.environment.name'] ??= environment.get('TOA_ENV') } return options } interface LogsConfig extends LogsOptions { exporters?: LogExportersConfig } /** * Tracing is off unless it is configured. The console exporter is a local development * mechanism, so it is turned on for `toa dev` and for a boot trace the CLI has already * asked for (`runtime/boot/src/span.js`), and nowhere else — a deployment that wants * traces annotates `telemetry.traces.exporters`. * * `extensions/exposition/source/Factory.ts` says the same thing for the gateway process, * which boots without this extension. */ function development(): TracesOptions { const local = environment.get('TOA_DEV') === '1' || environment.get('TOA_BOOT_TRACE') === '1' return local ? { exporters: { console: {} } } : {} }