@aztec/telemetry-client 0.0.0-test.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/dest/attributes.d.ts +99 -0
- package/dest/attributes.d.ts.map +1 -0
- package/dest/attributes.js +67 -0
- package/dest/bench.d.ts +29 -0
- package/dest/bench.d.ts.map +1 -0
- package/dest/bench.js +98 -0
- package/dest/config.d.ts +12 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +39 -0
- package/dest/event_loop_monitor.d.ts +18 -0
- package/dest/event_loop_monitor.d.ts.map +1 -0
- package/dest/event_loop_monitor.js +93 -0
- package/dest/index.d.ts +10 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +9 -0
- package/dest/lmdb_metrics.d.ts +16 -0
- package/dest/lmdb_metrics.d.ts.map +1 -0
- package/dest/lmdb_metrics.js +42 -0
- package/dest/metrics.d.ts +129 -0
- package/dest/metrics.d.ts.map +1 -0
- package/dest/metrics.js +126 -0
- package/dest/noop.d.ts +14 -0
- package/dest/noop.d.ts.map +1 -0
- package/dest/noop.js +71 -0
- package/dest/otel.d.ts +32 -0
- package/dest/otel.d.ts.map +1 -0
- package/dest/otel.js +319 -0
- package/dest/otel_filter_metric_exporter.d.ts +12 -0
- package/dest/otel_filter_metric_exporter.d.ts.map +1 -0
- package/dest/otel_filter_metric_exporter.js +33 -0
- package/dest/otel_logger_provider.d.ts +4 -0
- package/dest/otel_logger_provider.d.ts.map +1 -0
- package/dest/otel_logger_provider.js +25 -0
- package/dest/otel_propagation.d.ts +3 -0
- package/dest/otel_propagation.d.ts.map +1 -0
- package/dest/otel_propagation.js +44 -0
- package/dest/otel_resource.d.ts +3 -0
- package/dest/otel_resource.d.ts.map +1 -0
- package/dest/otel_resource.js +12 -0
- package/dest/prom_otel_adapter.d.ts +127 -0
- package/dest/prom_otel_adapter.d.ts.map +1 -0
- package/dest/prom_otel_adapter.js +183 -0
- package/dest/start.d.ts +6 -0
- package/dest/start.d.ts.map +1 -0
- package/dest/start.js +24 -0
- package/dest/telemetry.d.ts +118 -0
- package/dest/telemetry.d.ts.map +1 -0
- package/dest/telemetry.js +116 -0
- package/dest/vendor/attributes.d.ts +5 -0
- package/dest/vendor/attributes.d.ts.map +1 -0
- package/dest/vendor/attributes.js +5 -0
- package/dest/vendor/otel-pino-stream.d.ts +41 -0
- package/dest/vendor/otel-pino-stream.d.ts.map +1 -0
- package/dest/vendor/otel-pino-stream.js +229 -0
- package/dest/with_tracer.d.ts +33 -0
- package/dest/with_tracer.d.ts.map +1 -0
- package/dest/with_tracer.js +32 -0
- package/dest/wrappers/fetch.d.ts +16 -0
- package/dest/wrappers/fetch.d.ts.map +1 -0
- package/dest/wrappers/fetch.js +39 -0
- package/dest/wrappers/index.d.ts +4 -0
- package/dest/wrappers/index.d.ts.map +1 -0
- package/dest/wrappers/index.js +3 -0
- package/dest/wrappers/json_rpc_server.d.ts +4 -0
- package/dest/wrappers/json_rpc_server.d.ts.map +1 -0
- package/dest/wrappers/json_rpc_server.js +11 -0
- package/dest/wrappers/l2_block_stream.d.ts +15 -0
- package/dest/wrappers/l2_block_stream.d.ts.map +1 -0
- package/dest/wrappers/l2_block_stream.js +26 -0
- package/package.json +89 -0
- package/src/attributes.ts +115 -0
- package/src/bench.ts +147 -0
- package/src/config.ts +56 -0
- package/src/event_loop_monitor.ts +119 -0
- package/src/index.ts +9 -0
- package/src/lmdb_metrics.ts +45 -0
- package/src/metrics.ts +153 -0
- package/src/noop.ts +91 -0
- package/src/otel.ts +286 -0
- package/src/otel_filter_metric_exporter.ts +38 -0
- package/src/otel_logger_provider.ts +31 -0
- package/src/otel_propagation.ts +50 -0
- package/src/otel_resource.ts +16 -0
- package/src/prom_otel_adapter.ts +326 -0
- package/src/start.ts +33 -0
- package/src/telemetry.ts +267 -0
- package/src/vendor/attributes.ts +5 -0
- package/src/vendor/otel-pino-stream.ts +282 -0
- package/src/with_tracer.ts +35 -0
- package/src/wrappers/fetch.ts +52 -0
- package/src/wrappers/index.ts +3 -0
- package/src/wrappers/json_rpc_server.ts +15 -0
- package/src/wrappers/l2_block_stream.ts +37 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
2
|
+
import { Registry } from 'prom-client';
|
|
3
|
+
export var MessageSource = /*#__PURE__*/ function(MessageSource) {
|
|
4
|
+
MessageSource["forward"] = "forward";
|
|
5
|
+
MessageSource["publish"] = "publish";
|
|
6
|
+
return MessageSource;
|
|
7
|
+
}({});
|
|
8
|
+
/**Otel MetricsType Adapters
|
|
9
|
+
*
|
|
10
|
+
* Some dependencies we use export metrics directly in a Prometheus format
|
|
11
|
+
* This adapter is used to convert those metrics to a format that we can use with OpenTelemetry
|
|
12
|
+
*
|
|
13
|
+
* Affected services include:
|
|
14
|
+
* - chainsafe/gossipsub
|
|
15
|
+
* - libp2p
|
|
16
|
+
*/ export class OtelGauge {
|
|
17
|
+
logger;
|
|
18
|
+
labelNames;
|
|
19
|
+
gauge;
|
|
20
|
+
currentValue;
|
|
21
|
+
labeledValues;
|
|
22
|
+
collectFns;
|
|
23
|
+
_collect;
|
|
24
|
+
get collect() {
|
|
25
|
+
return this._collect;
|
|
26
|
+
}
|
|
27
|
+
set collect(fn) {
|
|
28
|
+
this._collect = fn;
|
|
29
|
+
}
|
|
30
|
+
constructor(logger, meter, name, help, labelNames = []){
|
|
31
|
+
this.logger = logger;
|
|
32
|
+
this.labelNames = labelNames;
|
|
33
|
+
this.currentValue = 0;
|
|
34
|
+
this.labeledValues = new Map();
|
|
35
|
+
this.collectFns = [];
|
|
36
|
+
this._collect = ()=>{};
|
|
37
|
+
this.gauge = meter.createObservableGauge(name, {
|
|
38
|
+
description: help
|
|
39
|
+
});
|
|
40
|
+
// Only observe in the callback when collect() is called
|
|
41
|
+
this.gauge.addCallback(this.handleObservation.bind(this));
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Add a collect callback
|
|
45
|
+
* @param collectFn - Callback function
|
|
46
|
+
*/ addCollect(collectFn) {
|
|
47
|
+
this.collectFns.push(collectFn);
|
|
48
|
+
}
|
|
49
|
+
handleObservation(result) {
|
|
50
|
+
// Execute the main collect function if assigned
|
|
51
|
+
this._collect();
|
|
52
|
+
// Execute all the collect functions
|
|
53
|
+
for (const fn of this.collectFns){
|
|
54
|
+
fn(this);
|
|
55
|
+
}
|
|
56
|
+
// Report the current values
|
|
57
|
+
if (this.labelNames.length === 0) {
|
|
58
|
+
result.observe(this.currentValue);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
for (const [labelStr, value] of this.labeledValues.entries()){
|
|
62
|
+
const labels = this.parseLabelsSafely(labelStr);
|
|
63
|
+
if (labels) {
|
|
64
|
+
result.observe(value, labels);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
inc(labelsOrValue, value) {
|
|
69
|
+
if (typeof labelsOrValue === 'number') {
|
|
70
|
+
this.currentValue += labelsOrValue;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (labelsOrValue) {
|
|
74
|
+
this.validateLabels(labelsOrValue);
|
|
75
|
+
const labelKey = JSON.stringify(labelsOrValue);
|
|
76
|
+
const currentValue = this.labeledValues.get(labelKey) ?? 0;
|
|
77
|
+
this.labeledValues.set(labelKey, currentValue + (value ?? 1));
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.currentValue += value ?? 1;
|
|
81
|
+
}
|
|
82
|
+
set(labelsOrValue, value) {
|
|
83
|
+
if (typeof labelsOrValue === 'number') {
|
|
84
|
+
this.currentValue = labelsOrValue;
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
this.validateLabels(labelsOrValue);
|
|
88
|
+
const labelKey = JSON.stringify(labelsOrValue);
|
|
89
|
+
this.labeledValues.set(labelKey, value);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Decrements the gauge value
|
|
93
|
+
* @param labels - Optional labels object
|
|
94
|
+
*/ dec(labels) {
|
|
95
|
+
if (labels) {
|
|
96
|
+
this.validateLabels(labels);
|
|
97
|
+
const labelKey = JSON.stringify(labels);
|
|
98
|
+
const currentValue = this.labeledValues.get(labelKey) ?? 0;
|
|
99
|
+
this.labeledValues.set(labelKey, currentValue - 1);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
this.currentValue -= 1;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Resets the gauge to initial state
|
|
106
|
+
*/ reset() {
|
|
107
|
+
this.currentValue = 0;
|
|
108
|
+
this.labeledValues.clear();
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Validates that provided labels match the expected schema
|
|
112
|
+
* @param labels - Labels object to validate
|
|
113
|
+
* @throws Error if invalid labels are provided
|
|
114
|
+
*/ validateLabels(labels) {
|
|
115
|
+
if (this.labelNames.length === 0) {
|
|
116
|
+
throw new Error('Gauge was initialized without labels support');
|
|
117
|
+
}
|
|
118
|
+
for (const key of Object.keys(labels)){
|
|
119
|
+
if (!this.labelNames.includes(key)) {
|
|
120
|
+
throw new Error(`Invalid label key: ${key}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Safely parses label string back to object
|
|
126
|
+
* @param labelStr - Stringified labels object
|
|
127
|
+
* @returns Labels object or null if parsing fails
|
|
128
|
+
*/ parseLabelsSafely(labelStr) {
|
|
129
|
+
try {
|
|
130
|
+
return JSON.parse(labelStr);
|
|
131
|
+
} catch {
|
|
132
|
+
this.logger.error(`Failed to parse label string: ${labelStr}`);
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Noop implementation of a Historgram collec
|
|
139
|
+
*/ class NoopOtelHistogram {
|
|
140
|
+
logger;
|
|
141
|
+
constructor(logger, _meter, _name, _help, _buckets = [], _labelNames = []){
|
|
142
|
+
this.logger = logger;
|
|
143
|
+
}
|
|
144
|
+
observe(_valueOrLabels, _value) {}
|
|
145
|
+
startTimer(_labels) {
|
|
146
|
+
return ()=>0;
|
|
147
|
+
}
|
|
148
|
+
reset() {
|
|
149
|
+
// OpenTelemetry histograms cannot be reset, but we implement the interface
|
|
150
|
+
this.logger.warn('OpenTelemetry histograms cannot be reset');
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Noop implementation of an AvgMinMax collector
|
|
155
|
+
*/ class NoopOtelAvgMinMax {
|
|
156
|
+
_logger;
|
|
157
|
+
constructor(_logger, _meter, _name, _help, _labelNames = []){
|
|
158
|
+
this._logger = _logger;
|
|
159
|
+
}
|
|
160
|
+
set(_valueOrLabels, _values) {}
|
|
161
|
+
reset() {}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Otel metrics Adapter
|
|
165
|
+
*
|
|
166
|
+
* Maps the PromClient based MetricsRegister from gossipsub and discv5 services to the Otel MetricsRegister
|
|
167
|
+
*/ export class OtelMetricsAdapter extends Registry {
|
|
168
|
+
logger;
|
|
169
|
+
meter;
|
|
170
|
+
constructor(telemetryClient, logger = createLogger('telemetry:otel-metrics-adapter')){
|
|
171
|
+
super(), this.logger = logger;
|
|
172
|
+
this.meter = telemetryClient.getMeter('metrics-adapter');
|
|
173
|
+
}
|
|
174
|
+
gauge(configuration) {
|
|
175
|
+
return new OtelGauge(this.logger, this.meter, configuration.name, configuration.help, configuration.labelNames);
|
|
176
|
+
}
|
|
177
|
+
histogram(configuration) {
|
|
178
|
+
return new NoopOtelHistogram(this.logger, this.meter, configuration.name, configuration.help, configuration.buckets, configuration.labelNames);
|
|
179
|
+
}
|
|
180
|
+
avgMinMax(configuration) {
|
|
181
|
+
return new NoopOtelAvgMinMax(this.logger, this.meter, configuration.name, configuration.help, configuration.labelNames);
|
|
182
|
+
}
|
|
183
|
+
}
|
package/dest/start.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { TelemetryClientConfig } from './config.js';
|
|
2
|
+
import type { TelemetryClient } from './telemetry.js';
|
|
3
|
+
export * from './config.js';
|
|
4
|
+
export declare function initTelemetryClient(config: TelemetryClientConfig): TelemetryClient;
|
|
5
|
+
export declare function getTelemetryClient(): TelemetryClient;
|
|
6
|
+
//# sourceMappingURL=start.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"start.d.ts","sourceRoot":"","sources":["../src/start.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAGzD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEtD,cAAc,aAAa,CAAC;AAK5B,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,qBAAqB,GAAG,eAAe,CAgBlF;AAED,wBAAgB,kBAAkB,IAAI,eAAe,CAEpD"}
|
package/dest/start.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
2
|
+
import { NoopTelemetryClient } from './noop.js';
|
|
3
|
+
import { OpenTelemetryClient } from './otel.js';
|
|
4
|
+
export * from './config.js';
|
|
5
|
+
let initialised = false;
|
|
6
|
+
let telemetry = new NoopTelemetryClient();
|
|
7
|
+
export function initTelemetryClient(config) {
|
|
8
|
+
const log = createLogger('telemetry:client');
|
|
9
|
+
if (initialised) {
|
|
10
|
+
log.warn('Telemetry client has already been initialized once');
|
|
11
|
+
return telemetry;
|
|
12
|
+
}
|
|
13
|
+
if (config.metricsCollectorUrl) {
|
|
14
|
+
log.info(`Using OpenTelemetry client with custom collector`);
|
|
15
|
+
telemetry = OpenTelemetryClient.createAndStart(config, log);
|
|
16
|
+
} else {
|
|
17
|
+
log.info('Using NoopTelemetryClient');
|
|
18
|
+
}
|
|
19
|
+
initialised = true;
|
|
20
|
+
return telemetry;
|
|
21
|
+
}
|
|
22
|
+
export function getTelemetryClient() {
|
|
23
|
+
return telemetry;
|
|
24
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { type AttributeValue, type BatchObservableCallback, type MetricOptions, type Observable, type BatchObservableResult as OtelBatchObservableResult, type Gauge as OtelGauge, type Histogram as OtelHistogram, type ObservableGauge as OtelObservableGauge, type ObservableResult as OtelObservableResult, type ObservableUpDownCounter as OtelObservableUpDownCounter, type UpDownCounter as OtelUpDownCounter, type Span, type Tracer } from '@opentelemetry/api';
|
|
2
|
+
import type * as Attributes from './attributes.js';
|
|
3
|
+
import type * as Metrics from './metrics.js';
|
|
4
|
+
export { type Span, SpanStatusCode, ValueType } from '@opentelemetry/api';
|
|
5
|
+
type ValuesOf<T> = T extends Record<string, infer U> ? U : never;
|
|
6
|
+
/** Global registry of attributes */
|
|
7
|
+
type AttributesType = Partial<Record<ValuesOf<typeof Attributes>, AttributeValue>>;
|
|
8
|
+
export type { AttributesType };
|
|
9
|
+
/** Global registry of metrics */
|
|
10
|
+
type MetricsType = (typeof Metrics)[keyof typeof Metrics];
|
|
11
|
+
export type { MetricsType };
|
|
12
|
+
export type Gauge = OtelGauge<AttributesType>;
|
|
13
|
+
export type Histogram = OtelHistogram<AttributesType>;
|
|
14
|
+
export type UpDownCounter = OtelUpDownCounter<AttributesType>;
|
|
15
|
+
export type ObservableGauge = OtelObservableGauge<AttributesType>;
|
|
16
|
+
export type ObservableUpDownCounter = OtelObservableUpDownCounter<AttributesType>;
|
|
17
|
+
export type ObservableResult = OtelObservableResult<AttributesType>;
|
|
18
|
+
export type BatchObservableResult = OtelBatchObservableResult<AttributesType>;
|
|
19
|
+
export type { Tracer };
|
|
20
|
+
/**
|
|
21
|
+
* A meter that provides instruments for recording metrics.
|
|
22
|
+
*/
|
|
23
|
+
export interface Meter {
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new gauge instrument. A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
|
|
26
|
+
* @param name - The name of the gauge
|
|
27
|
+
* @param options - The options for the gauge
|
|
28
|
+
*/
|
|
29
|
+
createGauge(name: MetricsType, options?: MetricOptions): Gauge;
|
|
30
|
+
/**
|
|
31
|
+
* Creates a new gauge instrument. A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
|
|
32
|
+
* @param name - The name of the gauge
|
|
33
|
+
* @param options - The options for the gauge
|
|
34
|
+
*/
|
|
35
|
+
createObservableGauge(name: MetricsType, options?: MetricOptions): ObservableGauge;
|
|
36
|
+
addBatchObservableCallback(callback: BatchObservableCallback<AttributesType>, observables: Observable<AttributesType>[]): void;
|
|
37
|
+
removeBatchObservableCallback(callback: BatchObservableCallback<AttributesType>, observables: Observable<AttributesType>[]): void;
|
|
38
|
+
/**
|
|
39
|
+
* 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.
|
|
40
|
+
* @param name - The name of the histogram
|
|
41
|
+
* @param options - The options for the histogram
|
|
42
|
+
*/
|
|
43
|
+
createHistogram(name: MetricsType, options?: MetricOptions): Histogram;
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new counter instrument. A counter can go up or down with a delta from the previous value.
|
|
46
|
+
* @param name - The name of the counter
|
|
47
|
+
* @param options - The options for the counter
|
|
48
|
+
*/
|
|
49
|
+
createUpDownCounter(name: MetricsType, options?: MetricOptions): UpDownCounter;
|
|
50
|
+
/**
|
|
51
|
+
* Creates a new gauge instrument. A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
|
|
52
|
+
* @param name - The name of the gauge
|
|
53
|
+
* @param options - The options for the gauge
|
|
54
|
+
*/
|
|
55
|
+
createObservableUpDownCounter(name: MetricsType, options?: MetricOptions): ObservableUpDownCounter;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A telemetry client that provides meters for recording metrics.
|
|
59
|
+
*/
|
|
60
|
+
export interface TelemetryClient {
|
|
61
|
+
/**
|
|
62
|
+
* Whether the client is enabled and reporting metrics.
|
|
63
|
+
**/
|
|
64
|
+
isEnabled(): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Creates a new meter
|
|
67
|
+
* @param name - The name of the meter.
|
|
68
|
+
*/
|
|
69
|
+
getMeter(name: string): Meter;
|
|
70
|
+
/**
|
|
71
|
+
* Creates a new tracer
|
|
72
|
+
* @param name - The name of the tracer.
|
|
73
|
+
*/
|
|
74
|
+
getTracer(name: string): Tracer;
|
|
75
|
+
/**
|
|
76
|
+
* Stops the telemetry client.
|
|
77
|
+
*/
|
|
78
|
+
stop(): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Flushes the telemetry client.
|
|
81
|
+
*/
|
|
82
|
+
flush(): Promise<void>;
|
|
83
|
+
}
|
|
84
|
+
/** Objects that adhere to this interface can use @trackSpan */
|
|
85
|
+
export interface Traceable {
|
|
86
|
+
tracer: Tracer;
|
|
87
|
+
}
|
|
88
|
+
type SpanDecorator<T extends Traceable, F extends (...args: any[]) => any> = (originalMethod: F, context: ClassMethodDecoratorContext<T>) => F;
|
|
89
|
+
/**
|
|
90
|
+
* Starts a new span whenever the decorated method is called.
|
|
91
|
+
* @param spanName - The name of the span to create. Can be a string or a function that returns a string.
|
|
92
|
+
* @param attributes - Initial attributes to set on the span. If a function is provided, it will be called with the arguments of the method.
|
|
93
|
+
* @param extraAttributes - Extra attributes to set on the span after the method is called. Will be called with the return value of the method. Note: if the function throws then this will not be called.
|
|
94
|
+
* @returns A decorator that wraps the method in a span.
|
|
95
|
+
*
|
|
96
|
+
* @privateRemarks
|
|
97
|
+
* This code looks complex but it's not that difficult:
|
|
98
|
+
* - decorators are functions that _replace_ a method with a different implementation
|
|
99
|
+
* - normal decorators can't take function arguments, but if we write a function that returns a decorator, we can pass arguments to that function
|
|
100
|
+
*
|
|
101
|
+
* The trackSpan function takes a span's name and some attributes and builds a decorator that wraps a method in a span with the given name and props
|
|
102
|
+
* The decorator can currently only be applied to methods on classes that have a `tracer` property. The compiler will enforce this.
|
|
103
|
+
*/
|
|
104
|
+
export declare function trackSpan<T extends Traceable, F extends (...args: any[]) => any>(spanName: string | ((this: T, ...args: Parameters<F>) => string), attributes?: AttributesType | ((this: T, ...args: Parameters<F>) => Promise<AttributesType> | AttributesType), extraAttributes?: (this: T, returnValue: Awaited<ReturnType<F>>) => AttributesType): SpanDecorator<T, F>;
|
|
105
|
+
/**
|
|
106
|
+
* Runs an event callback in a span. The span is started immediately and completes once the callback finishes running.
|
|
107
|
+
* The span will have two events added: 'callbackStart' and 'callbackEnd' to mark the start and end of the callback.
|
|
108
|
+
*
|
|
109
|
+
* @param tracer - The tracer instance to use
|
|
110
|
+
* @param spanName - The name of the span to create
|
|
111
|
+
* @param attributes - Initial attributes to set on the span
|
|
112
|
+
* @param callback - The callback to wrap in a span
|
|
113
|
+
*
|
|
114
|
+
* @returns - A new function that wraps the callback in a span
|
|
115
|
+
*/
|
|
116
|
+
export declare function wrapCallbackInSpan<F extends (...args: any[]) => any>(tracer: Tracer, spanName: string, attributes: AttributesType, callback: F): F;
|
|
117
|
+
export declare function runInSpan<A extends any[], R>(tracer: Tracer | string, spanName: string, callback: (span: Span, ...args: A) => R): (...args: A) => R;
|
|
118
|
+
//# sourceMappingURL=telemetry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../src/telemetry.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,uBAAuB,EAC5B,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,qBAAqB,IAAI,yBAAyB,EACvD,KAAK,KAAK,IAAI,SAAS,EACvB,KAAK,SAAS,IAAI,aAAa,EAC/B,KAAK,eAAe,IAAI,mBAAmB,EAC3C,KAAK,gBAAgB,IAAI,oBAAoB,EAC7C,KAAK,uBAAuB,IAAI,2BAA2B,EAC3D,KAAK,aAAa,IAAI,iBAAiB,EACvC,KAAK,IAAI,EAET,KAAK,MAAM,EACZ,MAAM,oBAAoB,CAAC;AAG5B,OAAO,KAAK,KAAK,UAAU,MAAM,iB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|
|
4
|
+
export { SpanStatusCode, ValueType } from '@opentelemetry/api';
|
|
5
|
+
/**
|
|
6
|
+
* Starts a new span whenever the decorated method is called.
|
|
7
|
+
* @param spanName - The name of the span to create. Can be a string or a function that returns a string.
|
|
8
|
+
* @param attributes - Initial attributes to set on the span. If a function is provided, it will be called with the arguments of the method.
|
|
9
|
+
* @param extraAttributes - Extra attributes to set on the span after the method is called. Will be called with the return value of the method. Note: if the function throws then this will not be called.
|
|
10
|
+
* @returns A decorator that wraps the method in a span.
|
|
11
|
+
*
|
|
12
|
+
* @privateRemarks
|
|
13
|
+
* This code looks complex but it's not that difficult:
|
|
14
|
+
* - decorators are functions that _replace_ a method with a different implementation
|
|
15
|
+
* - normal decorators can't take function arguments, but if we write a function that returns a decorator, we can pass arguments to that function
|
|
16
|
+
*
|
|
17
|
+
* The trackSpan function takes a span's name and some attributes and builds a decorator that wraps a method in a span with the given name and props
|
|
18
|
+
* The decorator can currently only be applied to methods on classes that have a `tracer` property. The compiler will enforce this.
|
|
19
|
+
*/ export function trackSpan(spanName, attributes, extraAttributes) {
|
|
20
|
+
// the return value of trackSpan is a decorator
|
|
21
|
+
return (originalMethod, _context)=>{
|
|
22
|
+
// the return value of the decorator replaces the original method
|
|
23
|
+
// in this wrapper method we start a span, call the original method, and then end the span
|
|
24
|
+
return async function replacementMethod(...args) {
|
|
25
|
+
const name = typeof spanName === 'function' ? spanName.call(this, ...args) : spanName;
|
|
26
|
+
const currentAttrs = typeof attributes === 'function' ? await attributes.call(this, ...args) : attributes;
|
|
27
|
+
// run originalMethod wrapped in an active span
|
|
28
|
+
// "active" means the span will be alive for the duration of the function execution
|
|
29
|
+
// and if any other spans are started during the execution of originalMethod, they will be children of this span
|
|
30
|
+
// behind the scenes this uses AsyncLocalStorage https://nodejs.org/dist/latest-v18.x/docs/api/async_context.html
|
|
31
|
+
return this.tracer.startActiveSpan(name, async (span)=>{
|
|
32
|
+
span.setAttributes(currentAttrs ?? {});
|
|
33
|
+
try {
|
|
34
|
+
const res = await originalMethod.call(this, ...args);
|
|
35
|
+
const extraAttrs = extraAttributes?.call(this, res);
|
|
36
|
+
span.setAttributes(extraAttrs ?? {});
|
|
37
|
+
return res;
|
|
38
|
+
} catch (err) {
|
|
39
|
+
span.setStatus({
|
|
40
|
+
code: SpanStatusCode.ERROR,
|
|
41
|
+
message: String(err)
|
|
42
|
+
});
|
|
43
|
+
throw err;
|
|
44
|
+
} finally{
|
|
45
|
+
span.end();
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Runs an event callback in a span. The span is started immediately and completes once the callback finishes running.
|
|
53
|
+
* The span will have two events added: 'callbackStart' and 'callbackEnd' to mark the start and end of the callback.
|
|
54
|
+
*
|
|
55
|
+
* @param tracer - The tracer instance to use
|
|
56
|
+
* @param spanName - The name of the span to create
|
|
57
|
+
* @param attributes - Initial attributes to set on the span
|
|
58
|
+
* @param callback - The callback to wrap in a span
|
|
59
|
+
*
|
|
60
|
+
* @returns - A new function that wraps the callback in a span
|
|
61
|
+
*/ export function wrapCallbackInSpan(tracer, spanName, attributes, callback) {
|
|
62
|
+
const span = tracer.startSpan(spanName, {
|
|
63
|
+
attributes
|
|
64
|
+
});
|
|
65
|
+
return async (...args)=>{
|
|
66
|
+
try {
|
|
67
|
+
span.addEvent('callbackStart');
|
|
68
|
+
const res = await callback(...args);
|
|
69
|
+
return res;
|
|
70
|
+
} catch (err) {
|
|
71
|
+
span.setStatus({
|
|
72
|
+
code: SpanStatusCode.ERROR,
|
|
73
|
+
message: String(err)
|
|
74
|
+
});
|
|
75
|
+
throw err;
|
|
76
|
+
} finally{
|
|
77
|
+
span.addEvent('callbackEnd');
|
|
78
|
+
span.end();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
export function runInSpan(tracer, spanName, callback) {
|
|
83
|
+
return (...args)=>{
|
|
84
|
+
const actualTracer = typeof tracer === 'string' ? getTelemetryClient().getTracer(tracer) : tracer;
|
|
85
|
+
return actualTracer.startActiveSpan(spanName, (span)=>{
|
|
86
|
+
let deferSpanEnd = false;
|
|
87
|
+
try {
|
|
88
|
+
const res = callback(span, ...args);
|
|
89
|
+
if (isPromise(res)) {
|
|
90
|
+
deferSpanEnd = true;
|
|
91
|
+
return res.catch((err)=>{
|
|
92
|
+
span.setStatus({
|
|
93
|
+
code: SpanStatusCode.ERROR,
|
|
94
|
+
message: String(err)
|
|
95
|
+
});
|
|
96
|
+
throw err;
|
|
97
|
+
}).finally(()=>{
|
|
98
|
+
span.end();
|
|
99
|
+
});
|
|
100
|
+
} else {
|
|
101
|
+
return res;
|
|
102
|
+
}
|
|
103
|
+
} catch (err) {
|
|
104
|
+
span.setStatus({
|
|
105
|
+
code: SpanStatusCode.ERROR,
|
|
106
|
+
message: String(err)
|
|
107
|
+
});
|
|
108
|
+
throw err;
|
|
109
|
+
} finally{
|
|
110
|
+
if (!deferSpanEnd) {
|
|
111
|
+
span.end();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const ATTR_JSONRPC_METHOD = "rpc.method";
|
|
2
|
+
export declare const ATTR_JSONRPC_REQUEST_ID = "rpc.jsonrpc.request_id";
|
|
3
|
+
export declare const ATTR_JSONRPC_ERROR_CODE = "rpc.jsonrpc.error_code";
|
|
4
|
+
export declare const ATTR_JSONRPC_ERROR_MSG = "rpc.jsonrpc.error_message";
|
|
5
|
+
//# sourceMappingURL=attributes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attributes.d.ts","sourceRoot":"","sources":["../../src/vendor/attributes.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,mBAAmB,eAAe,CAAC;AAChD,eAAO,MAAM,uBAAuB,2BAA2B,CAAC;AAChE,eAAO,MAAM,uBAAuB,2BAA2B,CAAC;AAChE,eAAO,MAAM,sBAAsB,8BAA8B,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// See https://opentelemetry.io/docs/specs/semconv/rpc/json-rpc/
|
|
2
|
+
export const ATTR_JSONRPC_METHOD = 'rpc.method';
|
|
3
|
+
export const ATTR_JSONRPC_REQUEST_ID = 'rpc.jsonrpc.request_id';
|
|
4
|
+
export const ATTR_JSONRPC_ERROR_CODE = 'rpc.jsonrpc.error_code';
|
|
5
|
+
export const ATTR_JSONRPC_ERROR_MSG = 'rpc.jsonrpc.error_message';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { Writable } from 'stream';
|
|
3
|
+
/**
|
|
4
|
+
* Return a function that knows how to convert the "time" field value on a
|
|
5
|
+
* Pino log record to an OTel LogRecord timestamp value.
|
|
6
|
+
*
|
|
7
|
+
* How to convert the serialized "time" on a Pino log record
|
|
8
|
+
* depends on the Logger's `Symbol(pino.time)` prop, configurable
|
|
9
|
+
* via https://getpino.io/#/docs/api?id=timestamp-boolean-function
|
|
10
|
+
*
|
|
11
|
+
* For example:
|
|
12
|
+
* const logger = pino({timestamp: pino.stdTimeFunctions.isoTime})
|
|
13
|
+
* results in log record entries of the form:
|
|
14
|
+
* ,"time":"2024-05-17T22:03:25.969Z"
|
|
15
|
+
* `otelTimestampFromTime` will be given the value of the "time" field:
|
|
16
|
+
* "2024-05-17T22:03:25.969Z"
|
|
17
|
+
* which should be parsed to a number of milliseconds since the epoch.
|
|
18
|
+
*/
|
|
19
|
+
export declare function getTimeConverter(pinoLogger: any, pinoMod: any): ((time: number) => number) | ((time: string) => number);
|
|
20
|
+
interface OTelPinoStreamOptions {
|
|
21
|
+
messageKey?: string;
|
|
22
|
+
levels: any;
|
|
23
|
+
otelTimestampFromTime?: (time: any) => number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A Pino stream for sending records to the OpenTelemetry Logs API.
|
|
27
|
+
*
|
|
28
|
+
* - This stream emits an 'unknown' event on an unprocessable pino record.
|
|
29
|
+
* The event arguments are: `logLine: string`, `err: string | Error`.
|
|
30
|
+
*/
|
|
31
|
+
export declare class OTelPinoStream extends Writable {
|
|
32
|
+
private _otelLogger;
|
|
33
|
+
private _messageKey;
|
|
34
|
+
private _levels;
|
|
35
|
+
private _otelTimestampFromTime;
|
|
36
|
+
constructor(options: OTelPinoStreamOptions);
|
|
37
|
+
_write(s: string, _encoding: string, callback: Function): void;
|
|
38
|
+
}
|
|
39
|
+
export default function (options: OTelPinoStreamOptions): OTelPinoStream;
|
|
40
|
+
export {};
|
|
41
|
+
//# sourceMappingURL=otel-pino-stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otel-pino-stream.d.ts","sourceRoot":"","sources":["../../src/vendor/otel-pino-stream.ts"],"names":[],"mappings":";AAoBA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AA8ElC;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,WAI5C,MAAM,uBAIN,MAAM,aAOvB;AAED,UAAU,qBAAqB;IAC7B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,GAAG,CAAC;IACZ,qBAAqB,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,MAAM,CAAC;CAC/C;AAED;;;;;GAKG;AACH,qBAAa,cAAe,SAAQ,QAAQ;IAC1C,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,OAAO,CAAC;IAChB,OAAO,CAAC,sBAAsB,CAAC;gBAEnB,OAAO,EAAE,qBAAqB;IAmBjC,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ;CAqGjE;AAMD,MAAM,CAAC,OAAO,WAAW,OAAO,EAAE,qBAAqB,GAAG,cAAc,CAMvE"}
|