@aztec/telemetry-client 0.0.1-commit.b655e406 → 0.0.1-commit.d1f2d6c

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.
Files changed (72) hide show
  1. package/dest/attributes.d.ts +12 -1
  2. package/dest/attributes.d.ts.map +1 -1
  3. package/dest/attributes.js +6 -0
  4. package/dest/bench.d.ts +3 -1
  5. package/dest/bench.d.ts.map +1 -1
  6. package/dest/bench.js +24 -14
  7. package/dest/config.d.ts +2 -1
  8. package/dest/config.d.ts.map +1 -1
  9. package/dest/config.js +7 -1
  10. package/dest/index.d.ts +1 -1
  11. package/dest/l1_metrics.d.ts +2 -2
  12. package/dest/l1_metrics.d.ts.map +1 -1
  13. package/dest/l1_metrics.js +4 -20
  14. package/dest/lmdb_metrics.d.ts +3 -3
  15. package/dest/lmdb_metrics.d.ts.map +1 -1
  16. package/dest/lmdb_metrics.js +4 -20
  17. package/dest/metric-utils.d.ts +5 -0
  18. package/dest/metric-utils.d.ts.map +1 -0
  19. package/dest/metric-utils.js +7 -0
  20. package/dest/metrics.d.ts +246 -196
  21. package/dest/metrics.d.ts.map +1 -1
  22. package/dest/metrics.js +1238 -194
  23. package/dest/nodejs_metrics_monitor.d.ts +3 -5
  24. package/dest/nodejs_metrics_monitor.d.ts.map +1 -1
  25. package/dest/nodejs_metrics_monitor.js +14 -41
  26. package/dest/noop.d.ts +6 -3
  27. package/dest/noop.d.ts.map +1 -1
  28. package/dest/noop.js +32 -1
  29. package/dest/otel.d.ts +5 -3
  30. package/dest/otel.d.ts.map +1 -1
  31. package/dest/otel.js +66 -5
  32. package/dest/otel_filter_metric_exporter.d.ts +1 -1
  33. package/dest/otel_filter_metric_exporter.d.ts.map +1 -1
  34. package/dest/otel_logger_provider.d.ts +1 -1
  35. package/dest/otel_propagation.d.ts +1 -1
  36. package/dest/otel_resource.d.ts +1 -1
  37. package/dest/prom_otel_adapter.d.ts +1 -1
  38. package/dest/prom_otel_adapter.d.ts.map +1 -1
  39. package/dest/prom_otel_adapter.js +15 -7
  40. package/dest/start.d.ts +2 -2
  41. package/dest/start.d.ts.map +1 -1
  42. package/dest/start.js +3 -2
  43. package/dest/telemetry.d.ts +35 -24
  44. package/dest/telemetry.d.ts.map +1 -1
  45. package/dest/telemetry.js +46 -23
  46. package/dest/vendor/attributes.d.ts +1 -1
  47. package/dest/vendor/otel-pino-stream.d.ts +1 -1
  48. package/dest/vendor/otel-pino-stream.d.ts.map +1 -1
  49. package/dest/with_tracer.d.ts +1 -1
  50. package/dest/with_tracer.d.ts.map +1 -1
  51. package/dest/wrappers/fetch.d.ts +2 -2
  52. package/dest/wrappers/fetch.d.ts.map +1 -1
  53. package/dest/wrappers/index.d.ts +1 -1
  54. package/dest/wrappers/json_rpc_server.d.ts +1 -1
  55. package/dest/wrappers/l2_block_stream.d.ts +4 -3
  56. package/dest/wrappers/l2_block_stream.d.ts.map +1 -1
  57. package/dest/wrappers/l2_block_stream.js +385 -11
  58. package/package.json +9 -6
  59. package/src/attributes.ts +15 -0
  60. package/src/bench.ts +27 -15
  61. package/src/config.ts +14 -1
  62. package/src/l1_metrics.ts +5 -20
  63. package/src/lmdb_metrics.ts +5 -26
  64. package/src/metric-utils.ts +12 -0
  65. package/src/metrics.ts +1311 -237
  66. package/src/nodejs_metrics_monitor.ts +16 -52
  67. package/src/noop.ts +61 -3
  68. package/src/otel.ts +92 -8
  69. package/src/prom_otel_adapter.ts +12 -18
  70. package/src/start.ts +3 -2
  71. package/src/telemetry.ts +102 -65
  72. package/src/wrappers/l2_block_stream.ts +6 -2
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 * as Metrics from './metrics.js';
20
+ import type { MetricDefinition } from './metrics.js';
21
21
  import { getTelemetryClient } from './start.js';
22
22
 
23
- export { type Span, SpanStatusCode, ValueType } from '@opentelemetry/api';
23
+ export { toMetricOptions } 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
 
@@ -57,8 +59,8 @@ export type AttributesType = Partial<Record<AttributeNames, AttributeValue>>;
57
59
  /** Subset of attributes allowed to be added to metrics */
58
60
  export type MetricAttributesType = Partial<Record<Exclude<AttributeNames, BannedMetricAttributeNames>, AttributeValue>>;
59
61
 
60
- /** Global registry of metrics */
61
- export type MetricsType = (typeof Metrics)[keyof typeof Metrics];
62
+ /** Re-export MetricDefinition for convenience */
63
+ export type { MetricDefinition } from './metrics.js';
62
64
 
63
65
  export type Gauge = OtelGauge<MetricAttributesType>;
64
66
  export type Histogram = OtelHistogram<MetricAttributesType>;
@@ -77,17 +79,15 @@ export type { Tracer };
77
79
  export interface Meter {
78
80
  /**
79
81
  * 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 name - The name of the gauge
81
- * @param options - The options for the gauge
82
+ * @param metric - The metric definition
82
83
  */
83
- createGauge(name: MetricsType, options?: MetricOptions): Gauge;
84
+ createGauge(metric: MetricDefinition): Gauge;
84
85
 
85
86
  /**
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 name - The name of the gauge
88
- * @param options - The options for the gauge
87
+ * Creates a new observable gauge instrument. A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
88
+ * @param metric - The metric definition
89
89
  */
90
- createObservableGauge(name: MetricsType, options?: MetricOptions): ObservableGauge;
90
+ createObservableGauge(metric: MetricDefinition): ObservableGauge;
91
91
 
92
92
  addBatchObservableCallback(
93
93
  callback: BatchObservableCallback<AttributesType>,
@@ -101,24 +101,22 @@ export interface Meter {
101
101
 
102
102
  /**
103
103
  * 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 name - The name of the histogram
105
- * @param options - The options for the histogram
104
+ * @param metric - The metric definition
105
+ * @param extraOptions - Optional extra options (e.g., advice for bucket boundaries)
106
106
  */
107
- createHistogram(name: MetricsType, options?: MetricOptions): Histogram;
107
+ createHistogram(metric: MetricDefinition, extraOptions?: Partial<MetricOptions>): Histogram;
108
108
 
109
109
  /**
110
110
  * Creates a new counter instrument. A counter can go up or down with a delta from the previous value.
111
- * @param name - The name of the counter
112
- * @param options - The options for the counter
111
+ * @param metric - The metric definition
113
112
  */
114
- createUpDownCounter(name: MetricsType, options?: MetricOptions): UpDownCounter;
113
+ createUpDownCounter(metric: MetricDefinition): UpDownCounter;
115
114
 
116
115
  /**
117
- * Creates a new gauge instrument. A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
118
- * @param name - The name of the gauge
119
- * @param options - The options for the gauge
116
+ * Creates a new observable up/down counter instrument.
117
+ * @param metric - The metric definition
120
118
  */
121
- createObservableUpDownCounter(name: MetricsType, options?: MetricOptions): ObservableUpDownCounter;
119
+ createObservableUpDownCounter(metric: MetricDefinition): ObservableUpDownCounter;
122
120
  }
123
121
 
124
122
  /**
@@ -160,6 +158,17 @@ export interface TelemetryClient {
160
158
  * Updates the roles that would share telemetry (if enabled)
161
159
  */
162
160
  setPublicTelemetryCollectFrom(roles: string[]): void;
161
+
162
+ /**
163
+ * Get current trace context as an opaque string for propagation.
164
+ * Returns the W3C traceparent header value if a trace is active, undefined otherwise.
165
+ */
166
+ getTraceContext(): string | undefined;
167
+
168
+ /**
169
+ * Recreates a context propagated by a remote system
170
+ */
171
+ extractPropagatedContext(traceContext: string): Context | undefined;
163
172
  }
164
173
 
165
174
  /** Objects that adhere to this interface can use @trackSpan */
@@ -204,24 +213,36 @@ export function trackSpan<T extends Traceable, F extends (...args: any[]) => any
204
213
  // "active" means the span will be alive for the duration of the function execution
205
214
  // and if any other spans are started during the execution of originalMethod, they will be children of this span
206
215
  // behind the scenes this uses AsyncLocalStorage https://nodejs.org/dist/latest-v22.x/docs/api/async_context.html
207
- return this.tracer.startActiveSpan(name, async (span: Span) => {
208
- span.setAttributes(currentAttrs ?? {});
209
-
210
- try {
211
- const res = await originalMethod.call(this, ...args);
212
- const extraAttrs = extraAttributes?.call(this, res);
213
- span.setAttributes(extraAttrs ?? {});
214
- return res;
215
- } catch (err) {
216
- span.setStatus({
217
- code: SpanStatusCode.ERROR,
218
- message: String(err),
219
- });
220
- throw err;
221
- } finally {
222
- span.end();
223
- }
224
- });
216
+ return this.tracer.startActiveSpan(
217
+ name,
218
+ {
219
+ attributes: currentAttrs,
220
+ },
221
+ async (span: Span) => {
222
+ try {
223
+ const res = await originalMethod.call(this, ...args);
224
+ const extraAttrs = extraAttributes?.call(this, res);
225
+ span.setAttributes(extraAttrs ?? {});
226
+ span.setStatus({
227
+ code: SpanStatusCode.OK,
228
+ });
229
+ return res;
230
+ } catch (err) {
231
+ span.setStatus({
232
+ code: SpanStatusCode.ERROR,
233
+ message: String(err),
234
+ });
235
+
236
+ if (typeof err === 'string' || (err && err instanceof Error)) {
237
+ span.recordException(err);
238
+ }
239
+
240
+ throw err;
241
+ } finally {
242
+ span.end();
243
+ }
244
+ },
245
+ );
225
246
  } as F;
226
247
  };
227
248
  }
@@ -265,41 +286,57 @@ export function wrapCallbackInSpan<F extends (...args: any[]) => any>(
265
286
  export function runInSpan<A extends any[], R>(
266
287
  tracer: Tracer | string,
267
288
  spanName: string,
268
- callback: (span: Span, ...args: A) => R,
269
- ): (...args: A) => R {
270
- return (...args: A): R => {
289
+ callback: (span: Span, ...args: A) => Promise<R>,
290
+ ): (...args: A) => Promise<R> {
291
+ return (...args: A): Promise<R> => {
271
292
  const actualTracer = typeof tracer === 'string' ? getTelemetryClient().getTracer(tracer) : tracer;
272
- return actualTracer.startActiveSpan(spanName, (span: Span): R => {
273
- let deferSpanEnd = false;
293
+ return actualTracer.startActiveSpan(spanName, async (span: Span): Promise<R> => {
274
294
  try {
275
- const res = callback(span, ...args);
276
- if (isPromise(res)) {
277
- deferSpanEnd = true;
278
- return res
279
- .catch(err => {
280
- span.setStatus({
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
- }
295
+ const res = await callback(span, ...args);
296
+ span.setStatus({
297
+ code: SpanStatusCode.OK,
298
+ });
299
+
300
+ return res;
292
301
  } catch (err) {
293
302
  span.setStatus({
294
303
  code: SpanStatusCode.ERROR,
295
304
  message: String(err),
296
305
  });
306
+ if (typeof err === 'string' || (err && err instanceof Error)) {
307
+ span.recordException(err);
308
+ }
297
309
  throw err;
298
310
  } finally {
299
- if (!deferSpanEnd) {
300
- span.end();
301
- }
311
+ span.end();
302
312
  }
303
313
  });
304
314
  };
305
315
  }
316
+
317
+ /**
318
+ * Execute a callback within a span immediately (for one-off traced calls).
319
+ * Unlike runInSpan which returns a reusable function, this executes right away.
320
+ */
321
+ export function execInSpan<R>(
322
+ tracer: Tracer | string,
323
+ spanName: string,
324
+ callback: (span: Span) => Promise<R>,
325
+ ): Promise<R> {
326
+ const actualTracer = typeof tracer === 'string' ? getTelemetryClient().getTracer(tracer) : tracer;
327
+ return actualTracer.startActiveSpan(spanName, async (span: Span): Promise<R> => {
328
+ try {
329
+ const res = await callback(span);
330
+ span.setStatus({ code: SpanStatusCode.OK });
331
+ return res;
332
+ } catch (err) {
333
+ span.setStatus({ code: SpanStatusCode.ERROR, message: String(err) });
334
+ if (typeof err === 'string' || (err && err instanceof Error)) {
335
+ span.recordException(err);
336
+ }
337
+ throw err;
338
+ } finally {
339
+ span.end();
340
+ }
341
+ });
342
+ }
@@ -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,10 @@ 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, 'getPublishedBlocks' | 'getBlockHeader' | 'getL2Tips'>,
14
+ l2BlockSource: Pick<
15
+ L2BlockSource,
16
+ 'getBlocks' | 'getBlockHeader' | 'getL2Tips' | 'getCheckpoints' | 'getCheckpointedBlocks'
17
+ >,
14
18
  localData: L2BlockStreamLocalDataProvider,
15
19
  handler: L2BlockStreamEventHandler,
16
20
  public readonly tracer: Tracer,
@@ -20,7 +24,7 @@ export class TraceableL2BlockStream extends L2BlockStream implements Traceable {
20
24
  proven?: boolean;
21
25
  pollIntervalMS?: number;
22
26
  batchSize?: number;
23
- startingBlock?: number;
27
+ startingBlock?: BlockNumber;
24
28
  } = {},
25
29
  ) {
26
30
  super(l2BlockSource, localData, handler, log, opts);