|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import type { Plugin, PluginContext } from '@objectstack/core'; |
| 4 | +import { |
| 5 | + OBSERVABILITY_METRICS_SERVICE, |
| 6 | + OBSERVABILITY_ERRORS_SERVICE, |
| 7 | +} from '@objectstack/observability'; |
| 8 | +import { |
| 9 | + NoopMetricsRegistry, |
| 10 | + NoopErrorReporter, |
| 11 | + type MetricsRegistry, |
| 12 | + type ErrorReporter, |
| 13 | +} from './index.js'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Canonical service names other plugins look up to find the host's |
| 17 | + * configured observability backends. Re-exported from |
| 18 | + * `@objectstack/observability` so callers can grab them alongside the |
| 19 | + * plugin without straddling two packages. |
| 20 | + */ |
| 21 | +export { OBSERVABILITY_METRICS_SERVICE, OBSERVABILITY_ERRORS_SERVICE }; |
| 22 | + |
| 23 | +/** |
| 24 | + * Options for {@link ObservabilityServicePlugin}. |
| 25 | + * |
| 26 | + * Either or both backends can be omitted; the omitted one resolves to |
| 27 | + * the corresponding no-op exporter so consumers can always rely on the |
| 28 | + * service being present. |
| 29 | + */ |
| 30 | +export interface ObservabilityServicePluginOptions { |
| 31 | + /** Metrics backend (e.g. `ConsoleMetricsRegistry`, `OtlpHttpMetricsRegistry`). */ |
| 32 | + metrics?: MetricsRegistry; |
| 33 | + /** Error reporter backend (e.g. Sentry adapter). */ |
| 34 | + errors?: ErrorReporter; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * `ObservabilityServicePlugin` — registers the host's |
| 39 | + * {@link MetricsRegistry} and {@link ErrorReporter} in the kernel |
| 40 | + * service registry under the canonical names so any other plugin can |
| 41 | + * look them up without each host having to thread observability config |
| 42 | + * through every plugin constructor. |
| 43 | + * |
| 44 | + * Resolution chain other plugins should follow: |
| 45 | + * |
| 46 | + * 1. Explicit option on the plugin's own options object (escape |
| 47 | + * hatch for tests / explicit wiring). |
| 48 | + * 2. `ctx.getService(OBSERVABILITY_METRICS_SERVICE)`. |
| 49 | + * 3. `NoopMetricsRegistry()` — never null. |
| 50 | + * |
| 51 | + * Register this plugin **before** any plugin that wants to consume the |
| 52 | + * services (cache, storage, dispatcher), otherwise the consumer's |
| 53 | + * `init` will fall through to the no-op default. |
| 54 | + * |
| 55 | + * @example |
| 56 | + * ```ts |
| 57 | + * import { ObjectKernel } from '@objectstack/core'; |
| 58 | + * import { |
| 59 | + * ObservabilityServicePlugin, |
| 60 | + * CacheServicePlugin, |
| 61 | + * } from '@objectstack/runtime'; |
| 62 | + * import { ConsoleMetricsRegistry } from '@objectstack/observability'; |
| 63 | + * |
| 64 | + * const kernel = new ObjectKernel(); |
| 65 | + * kernel.use(new ObservabilityServicePlugin({ |
| 66 | + * metrics: new ConsoleMetricsRegistry(), |
| 67 | + * })); |
| 68 | + * kernel.use(new CacheServicePlugin()); // picks metrics up automatically |
| 69 | + * ``` |
| 70 | + */ |
| 71 | +export class ObservabilityServicePlugin implements Plugin { |
| 72 | + name = 'com.objectstack.observability.service'; |
| 73 | + version = '1.0.0'; |
| 74 | + type = 'standard'; |
| 75 | + |
| 76 | + private readonly options: ObservabilityServicePluginOptions; |
| 77 | + |
| 78 | + constructor(options: ObservabilityServicePluginOptions = {}) { |
| 79 | + this.options = options; |
| 80 | + } |
| 81 | + |
| 82 | + async init(ctx: PluginContext): Promise<void> { |
| 83 | + const metrics = this.options.metrics ?? new NoopMetricsRegistry(); |
| 84 | + const errors = this.options.errors ?? new NoopErrorReporter(); |
| 85 | + ctx.registerService(OBSERVABILITY_METRICS_SERVICE, metrics); |
| 86 | + ctx.registerService(OBSERVABILITY_ERRORS_SERVICE, errors); |
| 87 | + ctx.logger.info( |
| 88 | + `ObservabilityServicePlugin: registered metrics=${(metrics as any).constructor?.name ?? 'unknown'} errors=${(errors as any).constructor?.name ?? 'unknown'}`, |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Helper used by consumer plugins to resolve a metrics backend with |
| 95 | + * the canonical fallback chain. Never throws; always returns a usable |
| 96 | + * `MetricsRegistry`. |
| 97 | + * |
| 98 | + * @param ctx the consuming plugin's `PluginContext` |
| 99 | + * @param override explicit option set on the consuming plugin (wins) |
| 100 | + */ |
| 101 | +export function resolveMetrics( |
| 102 | + ctx: PluginContext, |
| 103 | + override?: MetricsRegistry, |
| 104 | +): MetricsRegistry { |
| 105 | + if (override) return override; |
| 106 | + try { |
| 107 | + const m = ctx.getService<MetricsRegistry | undefined>(OBSERVABILITY_METRICS_SERVICE); |
| 108 | + if (m) return m; |
| 109 | + } catch { |
| 110 | + // Service not registered — fall through to the noop default. |
| 111 | + } |
| 112 | + return new NoopMetricsRegistry(); |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Sibling of {@link resolveMetrics} for {@link ErrorReporter}. |
| 117 | + */ |
| 118 | +export function resolveErrorReporter( |
| 119 | + ctx: PluginContext, |
| 120 | + override?: ErrorReporter, |
| 121 | +): ErrorReporter { |
| 122 | + if (override) return override; |
| 123 | + try { |
| 124 | + const e = ctx.getService<ErrorReporter | undefined>(OBSERVABILITY_ERRORS_SERVICE); |
| 125 | + if (e) return e; |
| 126 | + } catch { |
| 127 | + // Service not registered — fall through to the noop default. |
| 128 | + } |
| 129 | + return new NoopErrorReporter(); |
| 130 | +} |
0 commit comments