@smithers-orchestrator/observability 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/package.json +44 -0
- package/src/MetricName.ts +1 -0
- package/src/MetricsServiceLive.js +9 -0
- package/src/ResolvedSmithersObservabilityOptions.ts +10 -0
- package/src/SmithersEvent.ts +603 -0
- package/src/SmithersLogFormat.ts +1 -0
- package/src/SmithersMetricDefinition.ts +15 -0
- package/src/SmithersMetricType.ts +1 -0
- package/src/SmithersMetricUnit.ts +8 -0
- package/src/SmithersObservability.js +6 -0
- package/src/SmithersObservabilityOptions.ts +10 -0
- package/src/SmithersObservabilityService.ts +15 -0
- package/src/_coreCorrelation/CorrelationContext.ts +10 -0
- package/src/_coreCorrelation/CorrelationContextLive.js +11 -0
- package/src/_coreCorrelation/CorrelationContextService.js +6 -0
- package/src/_coreCorrelation/CorrelationContextServiceShape.ts +14 -0
- package/src/_coreCorrelation/CorrelationPatch.ts +3 -0
- package/src/_coreCorrelation/_correlationStorage.js +4 -0
- package/src/_coreCorrelation/correlationContextFiberRef.js +2 -0
- package/src/_coreCorrelation/correlationContextToLogAnnotations.js +28 -0
- package/src/_coreCorrelation/getCurrentCorrelationContext.js +9 -0
- package/src/_coreCorrelation/getCurrentCorrelationContextEffect.js +11 -0
- package/src/_coreCorrelation/index.js +14 -0
- package/src/_coreCorrelation/mergeCorrelationContext.js +61 -0
- package/src/_coreCorrelation/runWithCorrelationContext.js +14 -0
- package/src/_coreCorrelation/updateCurrentCorrelationContext.js +19 -0
- package/src/_coreCorrelation/withCorrelationContext.js +15 -0
- package/src/_coreCorrelation/withCurrentCorrelationContext.js +13 -0
- package/src/_coreMetrics.js +510 -0
- package/src/_coreMetricsShape.ts +55 -0
- package/src/_corePrometheus.js +93 -0
- package/src/_corePrometheusShape.ts +11 -0
- package/src/_coreTracing.js +142 -0
- package/src/_coreTracingShape.ts +17 -0
- package/src/_smithersSpanAttributeAliases.js +19 -0
- package/src/_smithersTraceSpanStorage.js +3 -0
- package/src/annotateSmithersTrace.js +11 -0
- package/src/correlation.js +20 -0
- package/src/createSmithersObservabilityLayer.js +49 -0
- package/src/createSmithersOtelLayer.js +21 -0
- package/src/createSmithersRuntimeLayer.js +2 -0
- package/src/getCurrentSmithersTraceAnnotations.js +14 -0
- package/src/getCurrentSmithersTraceSpan.js +7 -0
- package/src/index.d.ts +1032 -0
- package/src/index.js +35 -0
- package/src/logging.js +91 -0
- package/src/makeSmithersSpanAttributes.js +20 -0
- package/src/metrics/SmithersMetricDefinition.ts +17 -0
- package/src/metrics/SmithersMetricType.ts +1 -0
- package/src/metrics/SmithersMetricUnit.ts +8 -0
- package/src/metrics/_asyncExternalWaitCounts.js +4 -0
- package/src/metrics/_buckets.js +43 -0
- package/src/metrics/_processStartMs.js +1 -0
- package/src/metrics/activeNodes.js +2 -0
- package/src/metrics/activeRuns.js +2 -0
- package/src/metrics/agentActionsTotal.js +2 -0
- package/src/metrics/agentDurationMs.js +3 -0
- package/src/metrics/agentErrorsTotal.js +2 -0
- package/src/metrics/agentEventsTotal.js +2 -0
- package/src/metrics/agentInvocationsTotal.js +2 -0
- package/src/metrics/agentRetriesTotal.js +2 -0
- package/src/metrics/agentSessionsTotal.js +2 -0
- package/src/metrics/agentTokensTotal.js +2 -0
- package/src/metrics/alertDeliveriesAttempted.js +2 -0
- package/src/metrics/alertDeliveriesSuppressed.js +2 -0
- package/src/metrics/alertsAcknowledgedTotal.js +2 -0
- package/src/metrics/alertsActive.js +2 -0
- package/src/metrics/alertsEscalatedTotal.js +2 -0
- package/src/metrics/alertsFiredTotal.js +2 -0
- package/src/metrics/alertsReopenedTotal.js +2 -0
- package/src/metrics/alertsResolvedTotal.js +2 -0
- package/src/metrics/alertsSilencedTotal.js +2 -0
- package/src/metrics/approvalPending.js +2 -0
- package/src/metrics/approvalWaitDuration.js +3 -0
- package/src/metrics/approvalsDenied.js +2 -0
- package/src/metrics/approvalsGranted.js +2 -0
- package/src/metrics/approvalsRequested.js +2 -0
- package/src/metrics/attemptDuration.js +3 -0
- package/src/metrics/attentionBacklog.js +2 -0
- package/src/metrics/cacheHits.js +2 -0
- package/src/metrics/cacheMisses.js +2 -0
- package/src/metrics/dbQueryDuration.js +3 -0
- package/src/metrics/dbRetries.js +2 -0
- package/src/metrics/dbTransactionDuration.js +3 -0
- package/src/metrics/dbTransactionRetries.js +2 -0
- package/src/metrics/dbTransactionRollbacks.js +2 -0
- package/src/metrics/devtoolsActiveSubscribers.js +2 -0
- package/src/metrics/devtoolsBackpressureDisconnectTotal.js +2 -0
- package/src/metrics/devtoolsDeltaBuildMs.js +3 -0
- package/src/metrics/devtoolsEventBytes.js +3 -0
- package/src/metrics/devtoolsEventTotal.js +2 -0
- package/src/metrics/devtoolsSnapshotBuildMs.js +3 -0
- package/src/metrics/devtoolsSubscribeTotal.js +2 -0
- package/src/metrics/errorsTotal.js +2 -0
- package/src/metrics/eventsEmittedTotal.js +2 -0
- package/src/metrics/externalWaitAsyncPending.js +2 -0
- package/src/metrics/gatewayApprovalDecisionsTotal.js +2 -0
- package/src/metrics/gatewayAuthEventsTotal.js +2 -0
- package/src/metrics/gatewayConnectionsActive.js +2 -0
- package/src/metrics/gatewayConnectionsClosedTotal.js +2 -0
- package/src/metrics/gatewayConnectionsTotal.js +2 -0
- package/src/metrics/gatewayCronTriggersTotal.js +2 -0
- package/src/metrics/gatewayErrorsTotal.js +2 -0
- package/src/metrics/gatewayHeartbeatTicksTotal.js +2 -0
- package/src/metrics/gatewayMessagesReceivedTotal.js +2 -0
- package/src/metrics/gatewayMessagesSentTotal.js +2 -0
- package/src/metrics/gatewayRpcCallsTotal.js +2 -0
- package/src/metrics/gatewayRpcDuration.js +3 -0
- package/src/metrics/gatewayRunsCompletedTotal.js +2 -0
- package/src/metrics/gatewayRunsStartedTotal.js +2 -0
- package/src/metrics/gatewaySignalsTotal.js +2 -0
- package/src/metrics/gatewayWebhooksReceivedTotal.js +2 -0
- package/src/metrics/gatewayWebhooksRejectedTotal.js +2 -0
- package/src/metrics/gatewayWebhooksVerifiedTotal.js +2 -0
- package/src/metrics/heartbeatDataSizeBytes.js +3 -0
- package/src/metrics/heartbeatIntervalMs.js +3 -0
- package/src/metrics/hotReloadDuration.js +3 -0
- package/src/metrics/hotReloadFailures.js +2 -0
- package/src/metrics/hotReloads.js +2 -0
- package/src/metrics/httpRequestDuration.js +3 -0
- package/src/metrics/httpRequests.js +2 -0
- package/src/metrics/index.js +151 -0
- package/src/metrics/metricsServiceAdapter.js +188 -0
- package/src/metrics/nodeDuration.js +3 -0
- package/src/metrics/nodeRetriesTotal.js +2 -0
- package/src/metrics/nodesFailed.js +2 -0
- package/src/metrics/nodesFinished.js +2 -0
- package/src/metrics/nodesStarted.js +2 -0
- package/src/metrics/processHeapUsedBytes.js +2 -0
- package/src/metrics/processMemoryRssBytes.js +2 -0
- package/src/metrics/processUptimeSeconds.js +2 -0
- package/src/metrics/promptSizeBytes.js +3 -0
- package/src/metrics/responseSizeBytes.js +3 -0
- package/src/metrics/rewindDurationMs.js +3 -0
- package/src/metrics/rewindFramesDeleted.js +3 -0
- package/src/metrics/rewindRollbackTotal.js +2 -0
- package/src/metrics/rewindSandboxesReverted.js +3 -0
- package/src/metrics/rewindTotal.js +2 -0
- package/src/metrics/runDuration.js +3 -0
- package/src/metrics/runsAncestryDepth.js +3 -0
- package/src/metrics/runsCancelledTotal.js +2 -0
- package/src/metrics/runsCarriedStateBytes.js +3 -0
- package/src/metrics/runsContinuedTotal.js +2 -0
- package/src/metrics/runsFailedTotal.js +2 -0
- package/src/metrics/runsFinishedTotal.js +2 -0
- package/src/metrics/runsResumedTotal.js +2 -0
- package/src/metrics/runsTotal.js +2 -0
- package/src/metrics/sandboxActive.js +2 -0
- package/src/metrics/sandboxBundleSizeBytes.js +3 -0
- package/src/metrics/sandboxCompletedTotal.js +2 -0
- package/src/metrics/sandboxCreatedTotal.js +2 -0
- package/src/metrics/sandboxDurationMs.js +3 -0
- package/src/metrics/sandboxPatchCount.js +3 -0
- package/src/metrics/sandboxTransportDurationMs.js +3 -0
- package/src/metrics/schedulerConcurrencyUtilization.js +2 -0
- package/src/metrics/schedulerQueueDepth.js +2 -0
- package/src/metrics/schedulerWaitDuration.js +3 -0
- package/src/metrics/scorerEventsFailed.js +2 -0
- package/src/metrics/scorerEventsFinished.js +2 -0
- package/src/metrics/scorerEventsStarted.js +2 -0
- package/src/metrics/smithersMetricCatalog.js +484 -0
- package/src/metrics/smithersMetricCatalogByKey.js +2 -0
- package/src/metrics/smithersMetricCatalogByName.js +2 -0
- package/src/metrics/smithersMetricCatalogByPrometheusName.js +2 -0
- package/src/metrics/supervisorPollDuration.js +3 -0
- package/src/metrics/supervisorPollsTotal.js +2 -0
- package/src/metrics/supervisorResumeLag.js +3 -0
- package/src/metrics/supervisorResumedTotal.js +2 -0
- package/src/metrics/supervisorSkippedTotal.js +2 -0
- package/src/metrics/supervisorStaleDetected.js +2 -0
- package/src/metrics/taskHeartbeatTimeoutTotal.js +2 -0
- package/src/metrics/taskHeartbeatsTotal.js +2 -0
- package/src/metrics/timerDelayDuration.js +3 -0
- package/src/metrics/timersCancelled.js +2 -0
- package/src/metrics/timersCreated.js +2 -0
- package/src/metrics/timersFired.js +2 -0
- package/src/metrics/timersPending.js +2 -0
- package/src/metrics/toPrometheusMetricName.js +8 -0
- package/src/metrics/tokensCacheReadTotal.js +2 -0
- package/src/metrics/tokensCacheWriteTotal.js +2 -0
- package/src/metrics/tokensContextWindowBucketTotal.js +2 -0
- package/src/metrics/tokensContextWindowPerCall.js +3 -0
- package/src/metrics/tokensInputPerCall.js +3 -0
- package/src/metrics/tokensInputTotal.js +2 -0
- package/src/metrics/tokensOutputPerCall.js +3 -0
- package/src/metrics/tokensOutputTotal.js +2 -0
- package/src/metrics/tokensReasoningTotal.js +2 -0
- package/src/metrics/toolCallErrorsTotal.js +2 -0
- package/src/metrics/toolCallsTotal.js +2 -0
- package/src/metrics/toolDuration.js +3 -0
- package/src/metrics/toolOutputTruncatedTotal.js +2 -0
- package/src/metrics/trackEvent.js +604 -0
- package/src/metrics/updateAsyncExternalWaitPending.js +14 -0
- package/src/metrics/updateProcessMetrics.js +17 -0
- package/src/metrics/vcsDuration.js +3 -0
- package/src/prometheusContentType.js +1 -0
- package/src/renderPrometheusMetrics.js +205 -0
- package/src/resolveSmithersObservabilityOptions.js +79 -0
- package/src/smithersMetrics.js +2 -0
- package/src/smithersSpanNames.js +6 -0
- package/src/withSmithersSpan.js +15 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { Effect, Metric, MetricState, Option } from "effect";
|
|
2
|
+
import { smithersMetricCatalog, toPrometheusMetricName, updateProcessMetrics, } from "./metrics/index.js";
|
|
3
|
+
/** @typedef {import("./SmithersMetricDefinition.ts").SmithersMetricDefinition} SmithersMetricDefinition */
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {string} text
|
|
7
|
+
* @returns {string}
|
|
8
|
+
*/
|
|
9
|
+
function escapePrometheusText(text) {
|
|
10
|
+
return text.replace(/\\/g, "\\\\").replace(/\n/g, "\\n");
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} value
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
function escapePrometheusLabelValue(value) {
|
|
17
|
+
return escapePrometheusText(value).replace(/"/g, '\\"');
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* @param {number | bigint} value
|
|
21
|
+
* @returns {string}
|
|
22
|
+
*/
|
|
23
|
+
function formatPrometheusNumber(value) {
|
|
24
|
+
if (typeof value === "bigint")
|
|
25
|
+
return value.toString();
|
|
26
|
+
if (Number.isNaN(value))
|
|
27
|
+
return "NaN";
|
|
28
|
+
if (value === Number.POSITIVE_INFINITY)
|
|
29
|
+
return "+Inf";
|
|
30
|
+
if (value === Number.NEGATIVE_INFINITY)
|
|
31
|
+
return "-Inf";
|
|
32
|
+
return String(value);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* @param {ReadonlyArray<[string, string]>} labels
|
|
36
|
+
* @returns {string}
|
|
37
|
+
*/
|
|
38
|
+
function formatPrometheusLabels(labels) {
|
|
39
|
+
if (labels.length === 0)
|
|
40
|
+
return "";
|
|
41
|
+
return `{${labels
|
|
42
|
+
.map(([key, value]) => `${toPrometheusMetricName(key)}="${escapePrometheusLabelValue(value)}"`)
|
|
43
|
+
.join(",")}}`;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* @param {ReadonlyArray<[string, string]>} base
|
|
47
|
+
* @param {ReadonlyArray<[string, string]>} extra
|
|
48
|
+
* @returns {string}
|
|
49
|
+
*/
|
|
50
|
+
function mergePrometheusLabels(base, extra) {
|
|
51
|
+
const merged = [...base, ...extra].sort(([left], [right]) => left.localeCompare(right));
|
|
52
|
+
return formatPrometheusLabels(merged);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @param {any} metricKey
|
|
56
|
+
* @returns {ReadonlyArray<[string, string]>}
|
|
57
|
+
*/
|
|
58
|
+
function metricLabels(metricKey) {
|
|
59
|
+
const tags = Array.isArray(metricKey?.tags) ? metricKey.tags : [];
|
|
60
|
+
return tags
|
|
61
|
+
.map((tag) => [String(tag.key), String(tag.value)])
|
|
62
|
+
.sort(([left], [right]) => left.localeCompare(right));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* @param {any} metricKey
|
|
66
|
+
* @returns {string | undefined}
|
|
67
|
+
*/
|
|
68
|
+
function metricHelp(metricKey) {
|
|
69
|
+
const description = Option.getOrElse(metricKey?.description, () => "");
|
|
70
|
+
return description.trim() ? description : undefined;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @param {any} metricState
|
|
74
|
+
* @returns {PrometheusBucket[]}
|
|
75
|
+
*/
|
|
76
|
+
function histogramBuckets(metricState) {
|
|
77
|
+
const buckets = [];
|
|
78
|
+
if (!metricState?.buckets ||
|
|
79
|
+
typeof metricState.buckets[Symbol.iterator] !== "function") {
|
|
80
|
+
return buckets;
|
|
81
|
+
}
|
|
82
|
+
for (const [boundary, count] of metricState.buckets) {
|
|
83
|
+
buckets.push({ boundary, count });
|
|
84
|
+
}
|
|
85
|
+
return buckets.sort((left, right) => left.boundary - right.boundary);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @param {SmithersMetricDefinition} definition
|
|
89
|
+
* @returns {string | undefined}
|
|
90
|
+
*/
|
|
91
|
+
function defaultMetricHelp(definition) {
|
|
92
|
+
return definition.description ?? definition.label;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* @param {SmithersMetricDefinition} definition
|
|
96
|
+
* @returns {string[]}
|
|
97
|
+
*/
|
|
98
|
+
function defaultPrometheusMetricLines(definition) {
|
|
99
|
+
const labelSets = definition.defaultLabels && definition.defaultLabels.length > 0
|
|
100
|
+
? definition.defaultLabels.map((labels) => Object.entries(labels))
|
|
101
|
+
: [[]];
|
|
102
|
+
if (definition.type === "histogram") {
|
|
103
|
+
const boundaries = definition.boundaries ?? [];
|
|
104
|
+
return labelSets.flatMap((labelSet) => {
|
|
105
|
+
const baseLabels = labelSet;
|
|
106
|
+
return [
|
|
107
|
+
...boundaries.map((boundary) => `${definition.prometheusName}_bucket${mergePrometheusLabels(baseLabels, [["le", String(boundary)]])} 0`),
|
|
108
|
+
`${definition.prometheusName}_bucket${mergePrometheusLabels(baseLabels, [["le", "+Inf"]])} 0`,
|
|
109
|
+
`${definition.prometheusName}_sum${formatPrometheusLabels(baseLabels)} 0`,
|
|
110
|
+
`${definition.prometheusName}_count${formatPrometheusLabels(baseLabels)} 0`,
|
|
111
|
+
];
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return labelSets.map((labelSet) => `${definition.prometheusName}${formatPrometheusLabels(labelSet)} 0`);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* @param {Map<string, PrometheusMetricRecord>} registry
|
|
118
|
+
* @param {string} name
|
|
119
|
+
* @param {PrometheusMetricType} type
|
|
120
|
+
* @param {string | undefined} help
|
|
121
|
+
*/
|
|
122
|
+
function registerPrometheusMetric(registry, name, type, help) {
|
|
123
|
+
const existing = registry.get(name);
|
|
124
|
+
if (existing) {
|
|
125
|
+
if (!existing.help && help) {
|
|
126
|
+
existing.help = help;
|
|
127
|
+
}
|
|
128
|
+
return existing;
|
|
129
|
+
}
|
|
130
|
+
const created = { type, help, lines: [] };
|
|
131
|
+
registry.set(name, created);
|
|
132
|
+
return created;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* @returns {string}
|
|
136
|
+
*/
|
|
137
|
+
export function renderPrometheusMetrics() {
|
|
138
|
+
// Snapshot process-level gauges before rendering
|
|
139
|
+
try {
|
|
140
|
+
Effect.runSync(updateProcessMetrics());
|
|
141
|
+
}
|
|
142
|
+
catch { /* non-critical */ }
|
|
143
|
+
const registry = new Map();
|
|
144
|
+
for (const snapshot of Metric.unsafeSnapshot()) {
|
|
145
|
+
const metricKey = snapshot.metricKey;
|
|
146
|
+
const metricState = snapshot.metricState;
|
|
147
|
+
const name = toPrometheusMetricName(String(metricKey.name ?? ""));
|
|
148
|
+
if (!name)
|
|
149
|
+
continue;
|
|
150
|
+
const labels = metricLabels(metricKey);
|
|
151
|
+
const help = metricHelp(metricKey);
|
|
152
|
+
if (MetricState.isCounterState(metricState)) {
|
|
153
|
+
const metric = registerPrometheusMetric(registry, name, "counter", help);
|
|
154
|
+
metric.lines.push(`${name}${formatPrometheusLabels(labels)} ${formatPrometheusNumber(metricState.count)}`);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (MetricState.isGaugeState(metricState)) {
|
|
158
|
+
const metric = registerPrometheusMetric(registry, name, "gauge", help);
|
|
159
|
+
metric.lines.push(`${name}${formatPrometheusLabels(labels)} ${formatPrometheusNumber(metricState.value)}`);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (MetricState.isHistogramState(metricState)) {
|
|
163
|
+
const metric = registerPrometheusMetric(registry, name, "histogram", help);
|
|
164
|
+
for (const bucket of histogramBuckets(metricState)) {
|
|
165
|
+
metric.lines.push(`${name}_bucket${mergePrometheusLabels(labels, [["le", String(bucket.boundary)]])} ${formatPrometheusNumber(bucket.count)}`);
|
|
166
|
+
}
|
|
167
|
+
metric.lines.push(`${name}_bucket${mergePrometheusLabels(labels, [["le", "+Inf"]])} ${formatPrometheusNumber(metricState.count)}`);
|
|
168
|
+
metric.lines.push(`${name}_sum${formatPrometheusLabels(labels)} ${formatPrometheusNumber(metricState.sum)}`);
|
|
169
|
+
metric.lines.push(`${name}_count${formatPrometheusLabels(labels)} ${formatPrometheusNumber(metricState.count)}`);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (MetricState.isFrequencyState(metricState)) {
|
|
173
|
+
const metric = registerPrometheusMetric(registry, name, "counter", help);
|
|
174
|
+
for (const [key, count] of metricState.occurrences) {
|
|
175
|
+
metric.lines.push(`${name}${mergePrometheusLabels(labels, [["key", key]])} ${formatPrometheusNumber(count)}`);
|
|
176
|
+
}
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (MetricState.isSummaryState(metricState)) {
|
|
180
|
+
const metric = registerPrometheusMetric(registry, name, "summary", help);
|
|
181
|
+
metric.lines.push(`${name}${mergePrometheusLabels(labels, [["quantile", "min"]])} ${formatPrometheusNumber(metricState.min)}`);
|
|
182
|
+
for (const [quantile, value] of metricState.quantiles) {
|
|
183
|
+
metric.lines.push(`${name}${mergePrometheusLabels(labels, [["quantile", String(quantile)]])} ${formatPrometheusNumber(Option.getOrElse(value, () => 0))}`);
|
|
184
|
+
}
|
|
185
|
+
metric.lines.push(`${name}${mergePrometheusLabels(labels, [["quantile", "max"]])} ${formatPrometheusNumber(metricState.max)}`);
|
|
186
|
+
metric.lines.push(`${name}_sum${formatPrometheusLabels(labels)} ${formatPrometheusNumber(metricState.sum)}`);
|
|
187
|
+
metric.lines.push(`${name}_count${formatPrometheusLabels(labels)} ${formatPrometheusNumber(metricState.count)}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
for (const definition of smithersMetricCatalog) {
|
|
191
|
+
const metric = registerPrometheusMetric(registry, definition.prometheusName, definition.type, defaultMetricHelp(definition));
|
|
192
|
+
if (metric.lines.length === 0) {
|
|
193
|
+
metric.lines.push(...defaultPrometheusMetricLines(definition));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
const lines = [];
|
|
197
|
+
for (const [name, metric] of [...registry.entries()].sort(([left], [right]) => left.localeCompare(right))) {
|
|
198
|
+
if (metric.help) {
|
|
199
|
+
lines.push(`# HELP ${name} ${escapePrometheusText(metric.help)}`);
|
|
200
|
+
}
|
|
201
|
+
lines.push(`# TYPE ${name} ${metric.type}`);
|
|
202
|
+
lines.push(...metric.lines);
|
|
203
|
+
}
|
|
204
|
+
return lines.join("\n") + (lines.length > 0 ? "\n" : "");
|
|
205
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { LogLevel } from "effect";
|
|
2
|
+
/** @typedef {import("./SmithersLogFormat.ts").SmithersLogFormat} SmithersLogFormat */
|
|
3
|
+
|
|
4
|
+
/** @typedef {import("./ResolvedSmithersObservabilityOptions.ts").ResolvedSmithersObservabilityOptions} ResolvedSmithersObservabilityOptions */
|
|
5
|
+
/** @typedef {import("./SmithersObservabilityOptions.ts").SmithersObservabilityOptions} SmithersObservabilityOptions */
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {LogLevel.LogLevel | string | undefined} value
|
|
9
|
+
* @returns {LogLevel.LogLevel}
|
|
10
|
+
*/
|
|
11
|
+
function resolveLogLevel(value) {
|
|
12
|
+
if (typeof value !== "string") {
|
|
13
|
+
return value ?? LogLevel.Info;
|
|
14
|
+
}
|
|
15
|
+
switch (value.toLowerCase()) {
|
|
16
|
+
case "none":
|
|
17
|
+
return LogLevel.None;
|
|
18
|
+
case "trace":
|
|
19
|
+
return LogLevel.Trace;
|
|
20
|
+
case "debug":
|
|
21
|
+
return LogLevel.Debug;
|
|
22
|
+
case "warning":
|
|
23
|
+
case "warn":
|
|
24
|
+
return LogLevel.Warning;
|
|
25
|
+
case "error":
|
|
26
|
+
return LogLevel.Error;
|
|
27
|
+
case "fatal":
|
|
28
|
+
return LogLevel.Fatal;
|
|
29
|
+
case "all":
|
|
30
|
+
return LogLevel.All;
|
|
31
|
+
case "info":
|
|
32
|
+
default:
|
|
33
|
+
return LogLevel.Info;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* @param {string | undefined} value
|
|
38
|
+
* @returns {SmithersLogFormat}
|
|
39
|
+
*/
|
|
40
|
+
function resolveLogFormat(value) {
|
|
41
|
+
switch ((value ?? "").toLowerCase()) {
|
|
42
|
+
case "json":
|
|
43
|
+
return "json";
|
|
44
|
+
case "pretty":
|
|
45
|
+
return "pretty";
|
|
46
|
+
case "string":
|
|
47
|
+
return "string";
|
|
48
|
+
case "logfmt":
|
|
49
|
+
default:
|
|
50
|
+
return "logfmt";
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* @param {boolean | undefined} value
|
|
55
|
+
* @returns {boolean}
|
|
56
|
+
*/
|
|
57
|
+
function resolveEnabled(value) {
|
|
58
|
+
if (typeof value === "boolean")
|
|
59
|
+
return value;
|
|
60
|
+
const env = (process.env.SMITHERS_OTEL_ENABLED ?? "").toLowerCase();
|
|
61
|
+
return env === "1" || env === "true";
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* @param {SmithersObservabilityOptions} [options]
|
|
65
|
+
* @returns {ResolvedSmithersObservabilityOptions}
|
|
66
|
+
*/
|
|
67
|
+
export function resolveSmithersObservabilityOptions(options = {}) {
|
|
68
|
+
return {
|
|
69
|
+
enabled: resolveEnabled(options.enabled),
|
|
70
|
+
endpoint: options.endpoint ??
|
|
71
|
+
process.env.OTEL_EXPORTER_OTLP_ENDPOINT ??
|
|
72
|
+
"http://localhost:4318",
|
|
73
|
+
serviceName: options.serviceName ?? process.env.OTEL_SERVICE_NAME ?? "smithers",
|
|
74
|
+
logFormat: options.logFormat
|
|
75
|
+
? resolveLogFormat(options.logFormat)
|
|
76
|
+
: resolveLogFormat(process.env.SMITHERS_LOG_FORMAT),
|
|
77
|
+
logLevel: resolveLogLevel(options.logLevel ?? process.env.SMITHERS_LOG_LEVEL),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { TracingService, withSmithersSpan as withCoreSmithersSpan, } from "./_coreTracing.js";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
/**
|
|
4
|
+
* @template A, E, R
|
|
5
|
+
* @param {string} name
|
|
6
|
+
* @param {Effect.Effect<A, E, R>} effect
|
|
7
|
+
* @param {Readonly<Record<string, unknown>>} [attributes]
|
|
8
|
+
* @param {Omit<Tracer.SpanOptions, "attributes" | "kind"> & { readonly kind?: Tracer.SpanKind; }} [_options]
|
|
9
|
+
* @returns {Effect.Effect<A, E, Exclude<R, Tracer.ParentSpan>>}
|
|
10
|
+
*/
|
|
11
|
+
export function withSmithersSpan(name, effect, attributes, _options) {
|
|
12
|
+
return Effect.flatMap(Effect.serviceOption(TracingService), (service) => service._tag === "Some"
|
|
13
|
+
? service.value.withSpan(name, effect, attributes ? { ...attributes } : undefined)
|
|
14
|
+
: withCoreSmithersSpan(name, effect, attributes));
|
|
15
|
+
}
|