@aztec/telemetry-client 0.0.1-commit.24de95ac → 0.0.1-commit.2606882
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/dest/attributes.d.ts +24 -2
- package/dest/attributes.d.ts.map +1 -1
- package/dest/attributes.js +12 -1
- package/dest/bench.d.ts +3 -1
- package/dest/bench.d.ts.map +1 -1
- package/dest/bench.js +24 -14
- package/dest/config.d.ts +4 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +24 -10
- package/dest/index.d.ts +1 -1
- package/dest/l1_metrics.d.ts +2 -2
- package/dest/l1_metrics.d.ts.map +1 -1
- package/dest/l1_metrics.js +4 -20
- package/dest/lmdb_metrics.d.ts +3 -3
- package/dest/lmdb_metrics.d.ts.map +1 -1
- package/dest/lmdb_metrics.js +4 -20
- package/dest/metric-utils.d.ts +24 -0
- package/dest/metric-utils.d.ts.map +1 -0
- package/dest/metric-utils.js +59 -0
- package/dest/metrics.d.ts +311 -196
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +1594 -194
- package/dest/monitored_batch_span_processor.d.ts +29 -0
- package/dest/monitored_batch_span_processor.d.ts.map +1 -0
- package/dest/monitored_batch_span_processor.js +70 -0
- package/dest/nodejs_metrics_monitor.d.ts +3 -5
- package/dest/nodejs_metrics_monitor.d.ts.map +1 -1
- package/dest/nodejs_metrics_monitor.js +19 -40
- package/dest/noop.d.ts +6 -3
- package/dest/noop.d.ts.map +1 -1
- package/dest/noop.js +32 -1
- package/dest/otel.d.ts +5 -3
- package/dest/otel.d.ts.map +1 -1
- package/dest/otel.js +128 -8
- package/dest/otel_filter_metric_exporter.d.ts +1 -1
- package/dest/otel_filter_metric_exporter.d.ts.map +1 -1
- package/dest/otel_logger_provider.d.ts +1 -1
- package/dest/otel_propagation.d.ts +3 -1
- package/dest/otel_propagation.d.ts.map +1 -1
- package/dest/otel_propagation.js +49 -1
- package/dest/otel_resource.d.ts +1 -1
- package/dest/otel_resource.d.ts.map +1 -1
- package/dest/otel_resource.js +14 -2
- package/dest/prom_otel_adapter.d.ts +4 -4
- package/dest/prom_otel_adapter.d.ts.map +1 -1
- package/dest/prom_otel_adapter.js +19 -10
- package/dest/start.d.ts +3 -2
- package/dest/start.d.ts.map +1 -1
- package/dest/start.js +5 -4
- package/dest/telemetry.d.ts +38 -26
- package/dest/telemetry.d.ts.map +1 -1
- package/dest/telemetry.js +46 -23
- package/dest/vendor/attributes.d.ts +1 -1
- package/dest/vendor/otel-pino-stream.d.ts +1 -1
- package/dest/vendor/otel-pino-stream.d.ts.map +1 -1
- package/dest/with_tracer.d.ts +1 -1
- package/dest/with_tracer.d.ts.map +1 -1
- package/dest/wrappers/fetch.d.ts +3 -3
- package/dest/wrappers/fetch.d.ts.map +1 -1
- package/dest/wrappers/fetch.js +3 -2
- package/dest/wrappers/index.d.ts +1 -1
- package/dest/wrappers/json_rpc_server.d.ts +1 -1
- package/dest/wrappers/l2_block_stream.d.ts +4 -3
- package/dest/wrappers/l2_block_stream.d.ts.map +1 -1
- package/dest/wrappers/l2_block_stream.js +385 -11
- package/package.json +9 -6
- package/src/attributes.ts +32 -1
- package/src/bench.ts +27 -15
- package/src/config.ts +38 -10
- package/src/l1_metrics.ts +5 -20
- package/src/lmdb_metrics.ts +5 -26
- package/src/metric-utils.ts +75 -0
- package/src/metrics.ts +1677 -237
- package/src/monitored_batch_span_processor.ts +93 -0
- package/src/nodejs_metrics_monitor.ts +18 -51
- package/src/noop.ts +61 -3
- package/src/otel.ts +130 -10
- package/src/otel_propagation.ts +42 -1
- package/src/otel_resource.ts +19 -2
- package/src/prom_otel_adapter.ts +16 -23
- package/src/start.ts +14 -5
- package/src/telemetry.ts +105 -67
- package/src/wrappers/fetch.ts +9 -3
- package/src/wrappers/l2_block_stream.ts +3 -2
package/src/start.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { createLogger } from '@aztec/foundation/log';
|
|
1
|
+
import { type LoggerBindings, createLogger } from '@aztec/foundation/log';
|
|
2
2
|
|
|
3
3
|
import type { TelemetryClientConfig } from './config.js';
|
|
4
4
|
import { NoopTelemetryClient } from './noop.js';
|
|
5
|
-
import { OpenTelemetryClient } from './otel.js';
|
|
6
5
|
import type { TelemetryClient } from './telemetry.js';
|
|
7
6
|
|
|
8
7
|
export * from './config.js';
|
|
@@ -10,15 +9,25 @@ export * from './config.js';
|
|
|
10
9
|
let initialized = false;
|
|
11
10
|
let telemetry: TelemetryClient = new NoopTelemetryClient();
|
|
12
11
|
|
|
13
|
-
export function initTelemetryClient(
|
|
14
|
-
|
|
12
|
+
export async function initTelemetryClient(
|
|
13
|
+
config: TelemetryClientConfig,
|
|
14
|
+
bindings?: LoggerBindings,
|
|
15
|
+
): Promise<TelemetryClient> {
|
|
16
|
+
const log = createLogger('telemetry:client', bindings);
|
|
15
17
|
if (initialized) {
|
|
16
18
|
log.warn('Telemetry client has already been initialized once');
|
|
17
19
|
return telemetry;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
if (
|
|
22
|
+
if (
|
|
23
|
+
config.metricsCollectorUrl ||
|
|
24
|
+
config.publicMetricsCollectorUrl ||
|
|
25
|
+
config.tracesCollectorUrl ||
|
|
26
|
+
config.logsCollectorUrl
|
|
27
|
+
) {
|
|
21
28
|
log.info(`Using OpenTelemetry client with custom collector`);
|
|
29
|
+
// Lazy load OpenTelemetry to avoid loading heavy deps at startup
|
|
30
|
+
const { OpenTelemetryClient } = await import('./otel.js');
|
|
22
31
|
telemetry = OpenTelemetryClient.createAndStart(config, log);
|
|
23
32
|
} else {
|
|
24
33
|
log.info('Using NoopTelemetryClient');
|
package/src/telemetry.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type AttributeValue,
|
|
3
3
|
type BatchObservableCallback,
|
|
4
|
+
type Context,
|
|
4
5
|
type MetricOptions,
|
|
5
6
|
type Observable,
|
|
6
7
|
type BatchObservableResult as OtelBatchObservableResult,
|
|
@@ -14,13 +15,14 @@ import {
|
|
|
14
15
|
SpanStatusCode,
|
|
15
16
|
type Tracer,
|
|
16
17
|
} from '@opentelemetry/api';
|
|
17
|
-
import { isPromise } from 'node:util/types';
|
|
18
18
|
|
|
19
19
|
import type * as Attributes from './attributes.js';
|
|
20
|
-
import type
|
|
20
|
+
import type { MetricDefinition } from './metrics.js';
|
|
21
21
|
import { getTelemetryClient } from './start.js';
|
|
22
22
|
|
|
23
|
-
export {
|
|
23
|
+
export { toMetricOptions, createUpDownCounterWithDefault } from './metric-utils.js';
|
|
24
|
+
|
|
25
|
+
export { type Span, SpanStatusCode, ValueType, type Context } from '@opentelemetry/api';
|
|
24
26
|
|
|
25
27
|
type ValuesOf<T> = T extends Record<string, infer U> ? U : never;
|
|
26
28
|
|
|
@@ -46,7 +48,6 @@ type BannedMetricAttributeNames = (typeof Attributes)[
|
|
|
46
48
|
| 'TX_HASH'
|
|
47
49
|
| 'PROVING_JOB_ID'
|
|
48
50
|
| 'P2P_ID'
|
|
49
|
-
| 'P2P_REQ_RESP_BATCH_REQUESTS_COUNT'
|
|
50
51
|
| 'TARGET_ADDRESS'
|
|
51
52
|
| 'MANA_USED'
|
|
52
53
|
| 'TOTAL_INSTRUCTIONS'];
|
|
@@ -54,11 +55,13 @@ type BannedMetricAttributeNames = (typeof Attributes)[
|
|
|
54
55
|
/** Global registry of attributes */
|
|
55
56
|
export type AttributesType = Partial<Record<AttributeNames, AttributeValue>>;
|
|
56
57
|
|
|
58
|
+
export type AllowedAttributeNames = Exclude<AttributeNames, BannedMetricAttributeNames>;
|
|
59
|
+
|
|
57
60
|
/** Subset of attributes allowed to be added to metrics */
|
|
58
|
-
export type MetricAttributesType = Partial<Record<
|
|
61
|
+
export type MetricAttributesType = Partial<Record<AllowedAttributeNames, AttributeValue>>;
|
|
59
62
|
|
|
60
|
-
/**
|
|
61
|
-
export type
|
|
63
|
+
/** Re-export MetricDefinition for convenience */
|
|
64
|
+
export type { MetricDefinition } from './metrics.js';
|
|
62
65
|
|
|
63
66
|
export type Gauge = OtelGauge<MetricAttributesType>;
|
|
64
67
|
export type Histogram = OtelHistogram<MetricAttributesType>;
|
|
@@ -77,17 +80,15 @@ export type { Tracer };
|
|
|
77
80
|
export interface Meter {
|
|
78
81
|
/**
|
|
79
82
|
* Creates a new gauge instrument. A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
|
|
80
|
-
* @param
|
|
81
|
-
* @param options - The options for the gauge
|
|
83
|
+
* @param metric - The metric definition
|
|
82
84
|
*/
|
|
83
|
-
createGauge(
|
|
85
|
+
createGauge(metric: MetricDefinition): Gauge;
|
|
84
86
|
|
|
85
87
|
/**
|
|
86
|
-
* Creates a new gauge instrument. A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
|
|
87
|
-
* @param
|
|
88
|
-
* @param options - The options for the gauge
|
|
88
|
+
* Creates a new observable gauge instrument. A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
|
|
89
|
+
* @param metric - The metric definition
|
|
89
90
|
*/
|
|
90
|
-
createObservableGauge(
|
|
91
|
+
createObservableGauge(metric: MetricDefinition): ObservableGauge;
|
|
91
92
|
|
|
92
93
|
addBatchObservableCallback(
|
|
93
94
|
callback: BatchObservableCallback<AttributesType>,
|
|
@@ -101,24 +102,22 @@ export interface Meter {
|
|
|
101
102
|
|
|
102
103
|
/**
|
|
103
104
|
* Creates a new histogram instrument. A histogram is a metric that samples observations (usually things like request durations or response sizes) and counts them in configurable buckets.
|
|
104
|
-
* @param
|
|
105
|
-
* @param
|
|
105
|
+
* @param metric - The metric definition
|
|
106
|
+
* @param extraOptions - Optional extra options (e.g., advice for bucket boundaries)
|
|
106
107
|
*/
|
|
107
|
-
createHistogram(
|
|
108
|
+
createHistogram(metric: MetricDefinition, extraOptions?: Partial<MetricOptions>): Histogram;
|
|
108
109
|
|
|
109
110
|
/**
|
|
110
111
|
* Creates a new counter instrument. A counter can go up or down with a delta from the previous value.
|
|
111
|
-
* @param
|
|
112
|
-
* @param options - The options for the counter
|
|
112
|
+
* @param metric - The metric definition
|
|
113
113
|
*/
|
|
114
|
-
createUpDownCounter(
|
|
114
|
+
createUpDownCounter(metric: MetricDefinition): UpDownCounter;
|
|
115
115
|
|
|
116
116
|
/**
|
|
117
|
-
* Creates a new
|
|
118
|
-
* @param
|
|
119
|
-
* @param options - The options for the gauge
|
|
117
|
+
* Creates a new observable up/down counter instrument.
|
|
118
|
+
* @param metric - The metric definition
|
|
120
119
|
*/
|
|
121
|
-
createObservableUpDownCounter(
|
|
120
|
+
createObservableUpDownCounter(metric: MetricDefinition): ObservableUpDownCounter;
|
|
122
121
|
}
|
|
123
122
|
|
|
124
123
|
/**
|
|
@@ -160,6 +159,17 @@ export interface TelemetryClient {
|
|
|
160
159
|
* Updates the roles that would share telemetry (if enabled)
|
|
161
160
|
*/
|
|
162
161
|
setPublicTelemetryCollectFrom(roles: string[]): void;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Get current trace context as an opaque string for propagation.
|
|
165
|
+
* Returns the W3C traceparent header value if a trace is active, undefined otherwise.
|
|
166
|
+
*/
|
|
167
|
+
getTraceContext(): string | undefined;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Recreates a context propagated by a remote system
|
|
171
|
+
*/
|
|
172
|
+
extractPropagatedContext(traceContext: string): Context | undefined;
|
|
163
173
|
}
|
|
164
174
|
|
|
165
175
|
/** Objects that adhere to this interface can use @trackSpan */
|
|
@@ -204,24 +214,36 @@ export function trackSpan<T extends Traceable, F extends (...args: any[]) => any
|
|
|
204
214
|
// "active" means the span will be alive for the duration of the function execution
|
|
205
215
|
// and if any other spans are started during the execution of originalMethod, they will be children of this span
|
|
206
216
|
// behind the scenes this uses AsyncLocalStorage https://nodejs.org/dist/latest-v22.x/docs/api/async_context.html
|
|
207
|
-
return this.tracer.startActiveSpan(
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
217
|
+
return this.tracer.startActiveSpan(
|
|
218
|
+
name,
|
|
219
|
+
{
|
|
220
|
+
attributes: currentAttrs,
|
|
221
|
+
},
|
|
222
|
+
async (span: Span) => {
|
|
223
|
+
try {
|
|
224
|
+
const res = await originalMethod.call(this, ...args);
|
|
225
|
+
const extraAttrs = extraAttributes?.call(this, res);
|
|
226
|
+
span.setAttributes(extraAttrs ?? {});
|
|
227
|
+
span.setStatus({
|
|
228
|
+
code: SpanStatusCode.OK,
|
|
229
|
+
});
|
|
230
|
+
return res;
|
|
231
|
+
} catch (err) {
|
|
232
|
+
span.setStatus({
|
|
233
|
+
code: SpanStatusCode.ERROR,
|
|
234
|
+
message: String(err),
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
if (typeof err === 'string' || (err && err instanceof Error)) {
|
|
238
|
+
span.recordException(err);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
throw err;
|
|
242
|
+
} finally {
|
|
243
|
+
span.end();
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
);
|
|
225
247
|
} as F;
|
|
226
248
|
};
|
|
227
249
|
}
|
|
@@ -265,41 +287,57 @@ export function wrapCallbackInSpan<F extends (...args: any[]) => any>(
|
|
|
265
287
|
export function runInSpan<A extends any[], R>(
|
|
266
288
|
tracer: Tracer | string,
|
|
267
289
|
spanName: string,
|
|
268
|
-
callback: (span: Span, ...args: A) => R
|
|
269
|
-
): (...args: A) => R {
|
|
270
|
-
return (...args: A): R => {
|
|
290
|
+
callback: (span: Span, ...args: A) => Promise<R>,
|
|
291
|
+
): (...args: A) => Promise<R> {
|
|
292
|
+
return (...args: A): Promise<R> => {
|
|
271
293
|
const actualTracer = typeof tracer === 'string' ? getTelemetryClient().getTracer(tracer) : tracer;
|
|
272
|
-
return actualTracer.startActiveSpan(spanName, (span: Span): R => {
|
|
273
|
-
let deferSpanEnd = false;
|
|
294
|
+
return actualTracer.startActiveSpan(spanName, async (span: Span): Promise<R> => {
|
|
274
295
|
try {
|
|
275
|
-
const res = callback(span, ...args);
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
code: SpanStatusCode.ERROR,
|
|
282
|
-
message: String(err),
|
|
283
|
-
});
|
|
284
|
-
throw err;
|
|
285
|
-
})
|
|
286
|
-
.finally(() => {
|
|
287
|
-
span.end();
|
|
288
|
-
}) as R;
|
|
289
|
-
} else {
|
|
290
|
-
return res;
|
|
291
|
-
}
|
|
296
|
+
const res = await callback(span, ...args);
|
|
297
|
+
span.setStatus({
|
|
298
|
+
code: SpanStatusCode.OK,
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
return res;
|
|
292
302
|
} catch (err) {
|
|
293
303
|
span.setStatus({
|
|
294
304
|
code: SpanStatusCode.ERROR,
|
|
295
305
|
message: String(err),
|
|
296
306
|
});
|
|
307
|
+
if (typeof err === 'string' || (err && err instanceof Error)) {
|
|
308
|
+
span.recordException(err);
|
|
309
|
+
}
|
|
297
310
|
throw err;
|
|
298
311
|
} finally {
|
|
299
|
-
|
|
300
|
-
span.end();
|
|
301
|
-
}
|
|
312
|
+
span.end();
|
|
302
313
|
}
|
|
303
314
|
});
|
|
304
315
|
};
|
|
305
316
|
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Execute a callback within a span immediately (for one-off traced calls).
|
|
320
|
+
* Unlike runInSpan which returns a reusable function, this executes right away.
|
|
321
|
+
*/
|
|
322
|
+
export function execInSpan<R>(
|
|
323
|
+
tracer: Tracer | string,
|
|
324
|
+
spanName: string,
|
|
325
|
+
callback: (span: Span) => Promise<R>,
|
|
326
|
+
): Promise<R> {
|
|
327
|
+
const actualTracer = typeof tracer === 'string' ? getTelemetryClient().getTracer(tracer) : tracer;
|
|
328
|
+
return actualTracer.startActiveSpan(spanName, async (span: Span): Promise<R> => {
|
|
329
|
+
try {
|
|
330
|
+
const res = await callback(span);
|
|
331
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
332
|
+
return res;
|
|
333
|
+
} catch (err) {
|
|
334
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: String(err) });
|
|
335
|
+
if (typeof err === 'string' || (err && err instanceof Error)) {
|
|
336
|
+
span.recordException(err);
|
|
337
|
+
}
|
|
338
|
+
throw err;
|
|
339
|
+
} finally {
|
|
340
|
+
span.end();
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
}
|
package/src/wrappers/fetch.ts
CHANGED
|
@@ -9,12 +9,17 @@ import { ATTR_JSONRPC_METHOD, ATTR_JSONRPC_REQUEST_ID } from '../vendor/attribut
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Makes a fetch function that retries based on the given attempts and propagates trace information.
|
|
12
|
-
* @param retries - Sequence of intervals (in seconds) to retry.
|
|
12
|
+
* @param retries - Sequence of intervals (in seconds) to retry, or a factory function returning an iterator for custom/indefinite backoff.
|
|
13
13
|
* @param noRetry - Whether to stop retries on server errors.
|
|
14
14
|
* @param log - Optional logger for logging attempts.
|
|
15
15
|
* @returns A fetch function.
|
|
16
16
|
*/
|
|
17
|
-
export function makeTracedFetch(
|
|
17
|
+
export function makeTracedFetch(
|
|
18
|
+
retries: number[] | (() => Generator<number>),
|
|
19
|
+
defaultNoRetry: boolean,
|
|
20
|
+
fetch = defaultFetch,
|
|
21
|
+
log?: Logger,
|
|
22
|
+
) {
|
|
18
23
|
return (host: string, body: unknown, extraHeaders: Record<string, string> = {}, noRetry?: boolean) => {
|
|
19
24
|
const telemetry = getTelemetryClient();
|
|
20
25
|
return telemetry.getTracer('fetch').startActiveSpan(`JsonRpcClient`, { kind: SpanKind.CLIENT }, async span => {
|
|
@@ -27,10 +32,11 @@ export function makeTracedFetch(retries: number[], defaultNoRetry: boolean, fetc
|
|
|
27
32
|
}
|
|
28
33
|
const headers = { ...extraHeaders };
|
|
29
34
|
propagation.inject(context.active(), headers);
|
|
35
|
+
const backoff = typeof retries === 'function' ? retries() : makeBackoff(retries);
|
|
30
36
|
return await retry(
|
|
31
37
|
() => fetch(host, body, headers, noRetry ?? defaultNoRetry),
|
|
32
38
|
`JsonRpcClient request to ${host}`,
|
|
33
|
-
|
|
39
|
+
backoff,
|
|
34
40
|
log,
|
|
35
41
|
false,
|
|
36
42
|
);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
1
2
|
import { createLogger } from '@aztec/foundation/log';
|
|
2
3
|
import {
|
|
3
4
|
type L2BlockSource,
|
|
@@ -10,7 +11,7 @@ import { type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client'
|
|
|
10
11
|
/** Extends an L2BlockStream with a tracer to create a new trace per iteration. */
|
|
11
12
|
export class TraceableL2BlockStream extends L2BlockStream implements Traceable {
|
|
12
13
|
constructor(
|
|
13
|
-
l2BlockSource: Pick<L2BlockSource, '
|
|
14
|
+
l2BlockSource: Pick<L2BlockSource, 'getBlocks' | 'getBlockData' | 'getL2Tips' | 'getCheckpoints'>,
|
|
14
15
|
localData: L2BlockStreamLocalDataProvider,
|
|
15
16
|
handler: L2BlockStreamEventHandler,
|
|
16
17
|
public readonly tracer: Tracer,
|
|
@@ -20,7 +21,7 @@ export class TraceableL2BlockStream extends L2BlockStream implements Traceable {
|
|
|
20
21
|
proven?: boolean;
|
|
21
22
|
pollIntervalMS?: number;
|
|
22
23
|
batchSize?: number;
|
|
23
|
-
startingBlock?:
|
|
24
|
+
startingBlock?: BlockNumber;
|
|
24
25
|
} = {},
|
|
25
26
|
) {
|
|
26
27
|
super(l2BlockSource, localData, handler, log, opts);
|