@aztec/telemetry-client 0.0.1-commit.d3ec352c → 0.0.1-commit.e310a4c8

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