@ms-cloudpack/telemetry 0.4.0 → 0.4.1

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.
@@ -1 +1 @@
1
- {"version":3,"file":"AppInsightsTelemetryClient.d.ts","sourceRoot":"","sources":["../../src/AppInsights/AppInsightsTelemetryClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAmC,KAAK,IAAI,EAAE,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE7F,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAIrE,qBAAa,0BAA2B,YAAW,eAAe;IAChE,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAsB;IACnD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAO;IACjC,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAA8B;IACnE,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAqB;gBAExC,OAAO,EAAE,gBAAgB;IAoBrC,IAAW,WAAW,IAAI,mBAAmB,CAE5C;IAED,IAAW,QAAQ,IAAI,IAAI,CAE1B;IAED;;;;OAIG;IACI,sBAAsB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAIxD;;OAEG;IACU,QAAQ;IAMrB;;;OAGG;IACI,SAAS,IAAI,MAAM;CAG3B"}
1
+ {"version":3,"file":"AppInsightsTelemetryClient.d.ts","sourceRoot":"","sources":["../../src/AppInsights/AppInsightsTelemetryClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAmC,KAAK,IAAI,EAAE,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE7F,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAIrE,qBAAa,0BAA2B,YAAW,eAAe;IAChE,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAsB;IACnD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAO;IACjC,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAA8B;IACnE,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAqB;gBAExC,OAAO,EAAE,gBAAgB;IAoBrC,IAAW,WAAW,IAAI,mBAAmB,CAE5C;IAED,IAAW,QAAQ,IAAI,IAAI,CAE1B;IAED;;;;OAIG;IACI,sBAAsB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAIxD;;OAEG;IACU,QAAQ;IAOrB;;;OAGG;IACI,SAAS,IAAI,MAAM;CAG3B"}
@@ -36,6 +36,7 @@ export class AppInsightsTelemetryClient {
36
36
  * Flushes all telemetry and shuts down the telemetry client.
37
37
  */
38
38
  async shutdown() {
39
+ this._performance.shutdown();
39
40
  this.rootSpan.end();
40
41
  await this._traceProvider.forceFlush();
41
42
  shutdownAzureMonitor();
@@ -1 +1 @@
1
- {"version":3,"file":"AppInsightsTelemetryClient.js","sourceRoot":"","sources":["../../src/AppInsights/AppInsightsTelemetryClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AACrF,OAAO,EAAE,KAAK,EAAoD,MAAM,oBAAoB,CAAC;AAE7F,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAGhE,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,MAAM,OAAO,0BAA0B;IAMrC,YAAY,OAAyB;QAHpB,yBAAoB,GAA2B,EAAE,CAAC;QAIjE,MAAM,MAAM,GAAG,oBAAoB,CAAC,OAAO,CAAC,CAAC;QAE7C,kDAAkD;QAClD,eAAe,CAAC,MAAM,CAAC,CAAC;QAExB,IAAI,CAAC,cAAc,GAAI,KAAK,CAAC,iBAAiB,EAA0B,CAAC,WAAW,EAAwB,CAAC;QAE7G,wBAAwB;QACxB,IAAI,CAAC,sBAAsB,CAAC,SAAS,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC;QAE/D,mEAAmE;QACnE,IAAI,CAAC,cAAc,CAAC,gBAAgB,CAAC,IAAI,sBAAsB,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC,CAAC;QAElG,mBAAmB;QACnB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,YAAY,IAAI,MAAM,CAAC,CAAC;QAE5E,IAAI,CAAC,YAAY,GAAG,IAAI,mBAAmB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC9D,CAAC;IAED,IAAW,WAAW;QACpB,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED,IAAW,QAAQ;QACjB,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED;;;;OAIG;IACI,sBAAsB,CAAC,GAAW,EAAE,KAAa;QACtD,IAAI,CAAC,oBAAoB,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACzC,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,QAAQ;QACnB,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC;QACpB,MAAM,IAAI,CAAC,cAAc,CAAC,UAAU,EAAE,CAAC;QACvC,oBAAoB,EAAE,CAAC;IACzB,CAAC;IAED;;;OAGG;IACI,SAAS;QACd,OAAO,KAAK,CAAC,SAAS,CAAC,kBAAkB,CAAC,CAAC;IAC7C,CAAC;CACF","sourcesContent":["import { shutdownAzureMonitor, useAzureMonitor } from '@azure/monitor-opentelemetry';\nimport { trace, type ProxyTracerProvider, type Span, type Tracer } from '@opentelemetry/api';\nimport type { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';\nimport { PerformanceRecorder } from '../PerformanceRecorder.js';\nimport type { TelemetryClient } from '../types/TelemetryClient.js';\nimport type { TelemetryOptions } from '../types/TelemetryOptions.js';\nimport { SpanEnrichingProcessor } from './SpanEnrichingProcessor.js';\nimport { getAppInsightsConfig } from './getAppInsightsConfig.js';\n\nexport class AppInsightsTelemetryClient implements TelemetryClient {\n private readonly _performance: PerformanceRecorder;\n private readonly _rootSpan: Span;\n private readonly sharedSpanAttributes: Record<string, string> = {};\n private readonly _traceProvider: NodeTracerProvider;\n\n constructor(options: TelemetryOptions) {\n const config = getAppInsightsConfig(options);\n\n // Set up azure monitor (aka application insights)\n useAzureMonitor(config);\n\n this._traceProvider = (trace.getTracerProvider() as ProxyTracerProvider).getDelegate() as NodeTracerProvider;\n\n // Set shared attributes\n this.setSharedSpanAttribute('version', options.productVersion);\n\n // Set SpanEnrichingProcessor to add shared attributes to all spans\n this._traceProvider.addSpanProcessor(new SpanEnrichingProcessor(() => this.sharedSpanAttributes));\n\n // Create root span\n this._rootSpan = this.getTracer().startSpan(options.rootSpanName || 'root');\n\n this._performance = new PerformanceRecorder(this._rootSpan);\n }\n\n public get performance(): PerformanceRecorder {\n return this._performance;\n }\n\n public get rootSpan(): Span {\n return this._rootSpan;\n }\n\n /**\n * Sets a shared attribute that will be added to all spans.\n * @param key - key of the attribute\n * @param value - value of the attribute\n */\n public setSharedSpanAttribute(key: string, value: string) {\n this.sharedSpanAttributes[key] = value;\n }\n\n /**\n * Flushes all telemetry and shuts down the telemetry client.\n */\n public async shutdown() {\n this.rootSpan.end();\n await this._traceProvider.forceFlush();\n shutdownAzureMonitor();\n }\n\n /**\n * Gets the OpenTelemetry tracer.\n * @returns the OpenTelemetry tracer\n */\n public getTracer(): Tracer {\n return trace.getTracer('cloudpack-tracer');\n }\n}\n"]}
1
+ {"version":3,"file":"AppInsightsTelemetryClient.js","sourceRoot":"","sources":["../../src/AppInsights/AppInsightsTelemetryClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AACrF,OAAO,EAAE,KAAK,EAAoD,MAAM,oBAAoB,CAAC;AAE7F,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAGhE,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,MAAM,OAAO,0BAA0B;IAMrC,YAAY,OAAyB;QAHpB,yBAAoB,GAA2B,EAAE,CAAC;QAIjE,MAAM,MAAM,GAAG,oBAAoB,CAAC,OAAO,CAAC,CAAC;QAE7C,kDAAkD;QAClD,eAAe,CAAC,MAAM,CAAC,CAAC;QAExB,IAAI,CAAC,cAAc,GAAI,KAAK,CAAC,iBAAiB,EAA0B,CAAC,WAAW,EAAwB,CAAC;QAE7G,wBAAwB;QACxB,IAAI,CAAC,sBAAsB,CAAC,SAAS,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC;QAE/D,mEAAmE;QACnE,IAAI,CAAC,cAAc,CAAC,gBAAgB,CAAC,IAAI,sBAAsB,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC,CAAC;QAElG,mBAAmB;QACnB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,YAAY,IAAI,MAAM,CAAC,CAAC;QAE5E,IAAI,CAAC,YAAY,GAAG,IAAI,mBAAmB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC9D,CAAC;IAED,IAAW,WAAW;QACpB,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED,IAAW,QAAQ;QACjB,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED;;;;OAIG;IACI,sBAAsB,CAAC,GAAW,EAAE,KAAa;QACtD,IAAI,CAAC,oBAAoB,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACzC,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,QAAQ;QACnB,IAAI,CAAC,YAAY,CAAC,QAAQ,EAAE,CAAC;QAC7B,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC;QACpB,MAAM,IAAI,CAAC,cAAc,CAAC,UAAU,EAAE,CAAC;QACvC,oBAAoB,EAAE,CAAC;IACzB,CAAC;IAED;;;OAGG;IACI,SAAS;QACd,OAAO,KAAK,CAAC,SAAS,CAAC,kBAAkB,CAAC,CAAC;IAC7C,CAAC;CACF","sourcesContent":["import { shutdownAzureMonitor, useAzureMonitor } from '@azure/monitor-opentelemetry';\nimport { trace, type ProxyTracerProvider, type Span, type Tracer } from '@opentelemetry/api';\nimport type { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';\nimport { PerformanceRecorder } from '../PerformanceRecorder.js';\nimport type { TelemetryClient } from '../types/TelemetryClient.js';\nimport type { TelemetryOptions } from '../types/TelemetryOptions.js';\nimport { SpanEnrichingProcessor } from './SpanEnrichingProcessor.js';\nimport { getAppInsightsConfig } from './getAppInsightsConfig.js';\n\nexport class AppInsightsTelemetryClient implements TelemetryClient {\n private readonly _performance: PerformanceRecorder;\n private readonly _rootSpan: Span;\n private readonly sharedSpanAttributes: Record<string, string> = {};\n private readonly _traceProvider: NodeTracerProvider;\n\n constructor(options: TelemetryOptions) {\n const config = getAppInsightsConfig(options);\n\n // Set up azure monitor (aka application insights)\n useAzureMonitor(config);\n\n this._traceProvider = (trace.getTracerProvider() as ProxyTracerProvider).getDelegate() as NodeTracerProvider;\n\n // Set shared attributes\n this.setSharedSpanAttribute('version', options.productVersion);\n\n // Set SpanEnrichingProcessor to add shared attributes to all spans\n this._traceProvider.addSpanProcessor(new SpanEnrichingProcessor(() => this.sharedSpanAttributes));\n\n // Create root span\n this._rootSpan = this.getTracer().startSpan(options.rootSpanName || 'root');\n\n this._performance = new PerformanceRecorder(this._rootSpan);\n }\n\n public get performance(): PerformanceRecorder {\n return this._performance;\n }\n\n public get rootSpan(): Span {\n return this._rootSpan;\n }\n\n /**\n * Sets a shared attribute that will be added to all spans.\n * @param key - key of the attribute\n * @param value - value of the attribute\n */\n public setSharedSpanAttribute(key: string, value: string) {\n this.sharedSpanAttributes[key] = value;\n }\n\n /**\n * Flushes all telemetry and shuts down the telemetry client.\n */\n public async shutdown() {\n this._performance.shutdown();\n this.rootSpan.end();\n await this._traceProvider.forceFlush();\n shutdownAzureMonitor();\n }\n\n /**\n * Gets the OpenTelemetry tracer.\n * @returns the OpenTelemetry tracer\n */\n public getTracer(): Tracer {\n return trace.getTracer('cloudpack-tracer');\n }\n}\n"]}
@@ -0,0 +1,18 @@
1
+ import type { MetricEvent } from './types/MetricEvent.js';
2
+ import type { MetricEventAggregator } from './types/MetricEventAggregator.js';
3
+ export declare class MetricEventAggregatorRegistry {
4
+ private readonly _aggregators;
5
+ /**
6
+ * Registers a new aggregator. If an aggregator already exists, it will be return the aggregator.
7
+ * @param name - The name of the metric.
8
+ * @param aggregate - The aggregator function.
9
+ * @returns The aggregator that is passed in.
10
+ */
11
+ register<T>(aggregator: MetricEventAggregator<T>): MetricEventAggregator<T>;
12
+ /**
13
+ * Gets all the aggregated events from all the registered aggregators.
14
+ * @returns The aggregated events.
15
+ */
16
+ getAggregatedEvents(): MetricEvent[];
17
+ }
18
+ //# sourceMappingURL=MetricEventAggregatorRegistry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MetricEventAggregatorRegistry.d.ts","sourceRoot":"","sources":["../src/MetricEventAggregatorRegistry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AAE9E,qBAAa,6BAA6B;IACxC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAA6C;IAE1E;;;;;OAKG;IACH,QAAQ,CAAC,CAAC,EAAE,UAAU,EAAE,qBAAqB,CAAC,CAAC,CAAC;IAQhD;;;OAGG;IACH,mBAAmB,IAAI,WAAW,EAAE;CAQrC"}
@@ -0,0 +1,29 @@
1
+ export class MetricEventAggregatorRegistry {
2
+ constructor() {
3
+ this._aggregators = new Set();
4
+ }
5
+ /**
6
+ * Registers a new aggregator. If an aggregator already exists, it will be return the aggregator.
7
+ * @param name - The name of the metric.
8
+ * @param aggregate - The aggregator function.
9
+ * @returns The aggregator that is passed in.
10
+ */
11
+ register(aggregator) {
12
+ if (!this._aggregators.has(aggregator)) {
13
+ this._aggregators.add(aggregator);
14
+ }
15
+ return aggregator;
16
+ }
17
+ /**
18
+ * Gets all the aggregated events from all the registered aggregators.
19
+ * @returns The aggregated events.
20
+ */
21
+ getAggregatedEvents() {
22
+ const events = [];
23
+ for (const aggregator of this._aggregators) {
24
+ events.push(...aggregator.getAggregatedEvents());
25
+ }
26
+ return events;
27
+ }
28
+ }
29
+ //# sourceMappingURL=MetricEventAggregatorRegistry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MetricEventAggregatorRegistry.js","sourceRoot":"","sources":["../src/MetricEventAggregatorRegistry.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,6BAA6B;IAA1C;QACmB,iBAAY,GAAG,IAAI,GAAG,EAAkC,CAAC;IA4B5E,CAAC;IA1BC;;;;;OAKG;IACH,QAAQ,CAAI,UAAoC;QAC9C,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE;YACtC,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;SACnC;QAED,OAAO,UAAU,CAAC;IACpB,CAAC;IAED;;;OAGG;IACH,mBAAmB;QACjB,MAAM,MAAM,GAAkB,EAAE,CAAC;QACjC,KAAK,MAAM,UAAU,IAAI,IAAI,CAAC,YAAY,EAAE;YAC1C,MAAM,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC,mBAAmB,EAAE,CAAC,CAAC;SAClD;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF","sourcesContent":["import type { MetricEvent } from './types/MetricEvent.js';\nimport type { MetricEventAggregator } from './types/MetricEventAggregator.js';\n\nexport class MetricEventAggregatorRegistry {\n private readonly _aggregators = new Set<MetricEventAggregator<unknown>>();\n\n /**\n * Registers a new aggregator. If an aggregator already exists, it will be return the aggregator.\n * @param name - The name of the metric.\n * @param aggregate - The aggregator function.\n * @returns The aggregator that is passed in.\n */\n register<T>(aggregator: MetricEventAggregator<T>) {\n if (!this._aggregators.has(aggregator)) {\n this._aggregators.add(aggregator);\n }\n\n return aggregator;\n }\n\n /**\n * Gets all the aggregated events from all the registered aggregators.\n * @returns The aggregated events.\n */\n getAggregatedEvents(): MetricEvent[] {\n const events: MetricEvent[] = [];\n for (const aggregator of this._aggregators) {\n events.push(...aggregator.getAggregatedEvents());\n }\n\n return events;\n }\n}\n"]}
@@ -1,12 +1,21 @@
1
1
  import type { Span } from '@opentelemetry/api';
2
- import type { SpanAttributes } from './types/SpanAttributes.js';
3
2
  import type { PerformanceTracker } from './types/PerformanceTracker.js';
3
+ import type { SpanAttributes } from './types/SpanAttributes.js';
4
+ import type { MetricEventAggregator } from './types/MetricEventAggregator.js';
4
5
  /**
5
6
  * PerformanceRecorder is a utility class for recording performance metrics into telemetry.
6
7
  */
7
8
  export declare class PerformanceRecorder {
8
9
  private readonly span;
10
+ private readonly _metricEventAggregatorRegistry;
9
11
  constructor(span: Span);
12
+ /**
13
+ * Registers a new aggregator for a given metric.
14
+ * If an aggregator already exists, it will be skipped.
15
+ * @param name - The name of the metric.
16
+ * @param aggregator - The aggregator function.
17
+ */
18
+ registerAggregator<T>(aggregator: MetricEventAggregator<T>): MetricEventAggregator<T>;
10
19
  /**
11
20
  * Records a performance metric into telemetry. The metric will be recorded as an event on the span and logged to the console.
12
21
  * It can be used for any situation where a performance metric (ie: how long does it take run something, or any other purpose) is needed.
@@ -24,5 +33,9 @@ export declare class PerformanceRecorder {
24
33
  * @returns - The result of the `execute` function.
25
34
  */
26
35
  track<T>(name: string, execute: (tracker: PerformanceTracker) => Promise<T>): Promise<T>;
36
+ /**
37
+ * Shuts down the performance recorder and records any aggregated events.
38
+ */
39
+ shutdown(): void;
27
40
  }
28
41
  //# sourceMappingURL=PerformanceRecorder.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"PerformanceRecorder.d.ts","sourceRoot":"","sources":["../src/PerformanceRecorder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE/C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AAExE;;GAEG;AACH,qBAAa,mBAAmB;IAClB,OAAO,CAAC,QAAQ,CAAC,IAAI;gBAAJ,IAAI,EAAE,IAAI;IAEvC;;;;OAIG;IACH,MAAM,CAAC,UAAU,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,oBAAoB,CAAC,EAAE,cAAc,CAAA;KAAE,GAAG,IAAI;IAMhG;;;;;OAKG;IACG,KAAK,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,OAAO,EAAE,kBAAkB,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAqB/F"}
1
+ {"version":3,"file":"PerformanceRecorder.d.ts","sourceRoot":"","sources":["../src/PerformanceRecorder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAG/C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACxE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AAE9E;;GAEG;AACH,qBAAa,mBAAmB;IAGlB,OAAO,CAAC,QAAQ,CAAC,IAAI;IAFjC,OAAO,CAAC,QAAQ,CAAC,8BAA8B,CAAuC;gBAEzD,IAAI,EAAE,IAAI;IAEvC;;;;;OAKG;IACH,kBAAkB,CAAC,CAAC,EAAE,UAAU,EAAE,qBAAqB,CAAC,CAAC,CAAC;IAI1D;;;;OAIG;IACH,MAAM,CAAC,UAAU,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,oBAAoB,CAAC,EAAE,cAAc,CAAA;KAAE,GAAG,IAAI;IAMhG;;;;;OAKG;IACG,KAAK,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,OAAO,EAAE,kBAAkB,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAsB9F;;OAEG;IACH,QAAQ,IAAI,IAAI;CAKjB"}
@@ -1,10 +1,21 @@
1
1
  import { performance } from 'perf_hooks';
2
+ import { MetricEventAggregatorRegistry } from './MetricEventAggregatorRegistry.js';
2
3
  /**
3
4
  * PerformanceRecorder is a utility class for recording performance metrics into telemetry.
4
5
  */
5
6
  export class PerformanceRecorder {
6
7
  constructor(span) {
7
8
  this.span = span;
9
+ this._metricEventAggregatorRegistry = new MetricEventAggregatorRegistry();
10
+ }
11
+ /**
12
+ * Registers a new aggregator for a given metric.
13
+ * If an aggregator already exists, it will be skipped.
14
+ * @param name - The name of the metric.
15
+ * @param aggregator - The aggregator function.
16
+ */
17
+ registerAggregator(aggregator) {
18
+ return this._metricEventAggregatorRegistry.register(aggregator);
8
19
  }
9
20
  /**
10
21
  * Records a performance metric into telemetry. The metric will be recorded as an event on the span and logged to the console.
@@ -14,7 +25,7 @@ export class PerformanceRecorder {
14
25
  record(attributes) {
15
26
  const { name, value, additionalProperties = {} } = attributes;
16
27
  this.span.addEvent('Performance-Metric', { ...additionalProperties, name, value });
17
- console.debug(`Performance-Metric: ${name} ${value} ms`);
28
+ console.debug(`Performance-Metric: ${name} ${value}`);
18
29
  }
19
30
  /**
20
31
  * Tracks the execution time of a given `execute` function and report it to telemetry.
@@ -38,5 +49,13 @@ export class PerformanceRecorder {
38
49
  this.record({ name, value: performance.now() - startTime, additionalProperties });
39
50
  return result;
40
51
  }
52
+ /**
53
+ * Shuts down the performance recorder and records any aggregated events.
54
+ */
55
+ shutdown() {
56
+ for (const { name, value, attributes } of this._metricEventAggregatorRegistry.getAggregatedEvents()) {
57
+ this.record({ name, value, additionalProperties: attributes });
58
+ }
59
+ }
41
60
  }
42
61
  //# sourceMappingURL=PerformanceRecorder.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"PerformanceRecorder.js","sourceRoot":"","sources":["../src/PerformanceRecorder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAIzC;;GAEG;AACH,MAAM,OAAO,mBAAmB;IAC9B,YAA6B,IAAU;QAAV,SAAI,GAAJ,IAAI,CAAM;IAAG,CAAC;IAE3C;;;;OAIG;IACH,MAAM,CAAC,UAAkF;QACvF,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,oBAAoB,GAAG,EAAE,EAAE,GAAG,UAAU,CAAC;QAC9D,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,oBAAoB,EAAE,EAAE,GAAG,oBAAoB,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QACnF,OAAO,CAAC,KAAK,CAAC,uBAAuB,IAAI,IAAI,KAAK,KAAK,CAAC,CAAC;IAC3D,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,KAAK,CAAI,IAAY,EAAE,OAAoD;QAC/E,MAAM,oBAAoB,GAAmB,EAAE,CAAC;QAEhD,MAAM,SAAS,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;QAEpC,MAAM,OAAO,GAAuB;YAClC,IAAI;YACJ,SAAS;YACT,aAAa,EAAE,CAAC,UAA0B,EAAE,EAAE;gBAC5C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE;oBACrD,oBAAoB,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;iBACnC;YACH,CAAC;SACF,CAAC;QAEF,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;QAEtC,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,WAAW,CAAC,GAAG,EAAE,GAAG,SAAS,EAAE,oBAAoB,EAAE,CAAC,CAAC;QAElF,OAAO,MAAM,CAAC;IAChB,CAAC;CACF","sourcesContent":["import type { Span } from '@opentelemetry/api';\nimport { performance } from 'perf_hooks';\nimport type { SpanAttributes } from './types/SpanAttributes.js';\nimport type { PerformanceTracker } from './types/PerformanceTracker.js';\n\n/**\n * PerformanceRecorder is a utility class for recording performance metrics into telemetry.\n */\nexport class PerformanceRecorder {\n constructor(private readonly span: Span) {}\n\n /**\n * Records a performance metric into telemetry. The metric will be recorded as an event on the span and logged to the console.\n * It can be used for any situation where a performance metric (ie: how long does it take run something, or any other purpose) is needed.\n * @param attributes\n */\n record(attributes: { name: string; value: number; additionalProperties?: SpanAttributes }): void {\n const { name, value, additionalProperties = {} } = attributes;\n this.span.addEvent('Performance-Metric', { ...additionalProperties, name, value });\n console.debug(`Performance-Metric: ${name} ${value} ms`);\n }\n\n /**\n * Tracks the execution time of a given `execute` function and report it to telemetry.\n * @param name - The name of the performance metric.\n * @param execute - The function to execute and track.\n * @returns - The result of the `execute` function.\n */\n async track<T>(name: string, execute: (tracker: PerformanceTracker) => Promise<T>): Promise<T> {\n const additionalProperties: SpanAttributes = {};\n\n const startTime = performance.now();\n\n const tracker: PerformanceTracker = {\n name,\n startTime,\n setProperties: (properties: SpanAttributes) => {\n for (const [key, value] of Object.entries(properties)) {\n additionalProperties[key] = value;\n }\n },\n };\n\n const result = await execute(tracker);\n\n this.record({ name, value: performance.now() - startTime, additionalProperties });\n\n return result;\n }\n}\n"]}
1
+ {"version":3,"file":"PerformanceRecorder.js","sourceRoot":"","sources":["../src/PerformanceRecorder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EAAE,6BAA6B,EAAE,MAAM,oCAAoC,CAAC;AAKnF;;GAEG;AACH,MAAM,OAAO,mBAAmB;IAG9B,YAA6B,IAAU;QAAV,SAAI,GAAJ,IAAI,CAAM;QAFtB,mCAA8B,GAAG,IAAI,6BAA6B,EAAE,CAAC;IAE5C,CAAC;IAE3C;;;;;OAKG;IACH,kBAAkB,CAAI,UAAoC;QACxD,OAAO,IAAI,CAAC,8BAA8B,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;IAClE,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,UAAkF;QACvF,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,oBAAoB,GAAG,EAAE,EAAE,GAAG,UAAU,CAAC;QAC9D,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,oBAAoB,EAAE,EAAE,GAAG,oBAAoB,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QACnF,OAAO,CAAC,KAAK,CAAC,uBAAuB,IAAI,IAAI,KAAK,EAAE,CAAC,CAAC;IACxD,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,KAAK,CAAI,IAAY,EAAE,OAAoD;QAC/E,MAAM,oBAAoB,GAAmB,EAAE,CAAC;QAEhD,MAAM,SAAS,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;QAEpC,MAAM,OAAO,GAAuB;YAClC,IAAI;YACJ,SAAS;YACT,aAAa,EAAE,CAAC,UAA0B,EAAE,EAAE;gBAC5C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE;oBACrD,oBAAoB,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;iBACnC;YACH,CAAC;SACF,CAAC;QAEF,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;QAEtC,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,WAAW,CAAC,GAAG,EAAE,GAAG,SAAS,EAAE,oBAAoB,EAAE,CAAC,CAAC;QAElF,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;OAEG;IACH,QAAQ;QACN,KAAK,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,IAAI,CAAC,8BAA8B,CAAC,mBAAmB,EAAE,EAAE;YACnG,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,oBAAoB,EAAE,UAAU,EAAE,CAAC,CAAC;SAChE;IACH,CAAC;CACF","sourcesContent":["import type { Span } from '@opentelemetry/api';\nimport { performance } from 'perf_hooks';\nimport { MetricEventAggregatorRegistry } from './MetricEventAggregatorRegistry.js';\nimport type { PerformanceTracker } from './types/PerformanceTracker.js';\nimport type { SpanAttributes } from './types/SpanAttributes.js';\nimport type { MetricEventAggregator } from './types/MetricEventAggregator.js';\n\n/**\n * PerformanceRecorder is a utility class for recording performance metrics into telemetry.\n */\nexport class PerformanceRecorder {\n private readonly _metricEventAggregatorRegistry = new MetricEventAggregatorRegistry();\n\n constructor(private readonly span: Span) {}\n\n /**\n * Registers a new aggregator for a given metric.\n * If an aggregator already exists, it will be skipped.\n * @param name - The name of the metric.\n * @param aggregator - The aggregator function.\n */\n registerAggregator<T>(aggregator: MetricEventAggregator<T>) {\n return this._metricEventAggregatorRegistry.register(aggregator);\n }\n\n /**\n * Records a performance metric into telemetry. The metric will be recorded as an event on the span and logged to the console.\n * It can be used for any situation where a performance metric (ie: how long does it take run something, or any other purpose) is needed.\n * @param attributes\n */\n record(attributes: { name: string; value: number; additionalProperties?: SpanAttributes }): void {\n const { name, value, additionalProperties = {} } = attributes;\n this.span.addEvent('Performance-Metric', { ...additionalProperties, name, value });\n console.debug(`Performance-Metric: ${name} ${value}`);\n }\n\n /**\n * Tracks the execution time of a given `execute` function and report it to telemetry.\n * @param name - The name of the performance metric.\n * @param execute - The function to execute and track.\n * @returns - The result of the `execute` function.\n */\n async track<T>(name: string, execute: (tracker: PerformanceTracker) => Promise<T>): Promise<T> {\n const additionalProperties: SpanAttributes = {};\n\n const startTime = performance.now();\n\n const tracker: PerformanceTracker = {\n name,\n startTime,\n setProperties: (properties: SpanAttributes) => {\n for (const [key, value] of Object.entries(properties)) {\n additionalProperties[key] = value;\n }\n },\n };\n\n const result = await execute(tracker);\n\n this.record({ name, value: performance.now() - startTime, additionalProperties });\n\n return result;\n }\n\n /**\n * Shuts down the performance recorder and records any aggregated events.\n */\n shutdown(): void {\n for (const { name, value, attributes } of this._metricEventAggregatorRegistry.getAggregatedEvents()) {\n this.record({ name, value, additionalProperties: attributes });\n }\n }\n}\n"]}
package/lib/index.d.ts CHANGED
@@ -7,4 +7,6 @@ export { createNoOpTelemetryClient } from './createNoOpTelemetryClient.js';
7
7
  export type { PerformanceTracker } from './types/PerformanceTracker.js';
8
8
  export type { SpanAttributes } from './types/SpanAttributes.js';
9
9
  export type { PerformanceRecorder } from './PerformanceRecorder.js';
10
+ export type { MetricEvent } from './types/MetricEvent.js';
11
+ export type { MetricEventAggregator } from './types/MetricEventAggregator.js';
10
12
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,YAAY,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACpD,YAAY,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAClE,YAAY,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AACpE,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAC3E,YAAY,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACxE,YAAY,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAChE,YAAY,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,YAAY,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACpD,YAAY,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAClE,YAAY,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AACpE,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAC3E,YAAY,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACxE,YAAY,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAChE,YAAY,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AACpE,YAAY,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAC1D,YAAY,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC"}
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAInE,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC","sourcesContent":["export type { Span, Tracer } from '@opentelemetry/api';\nexport { createTelemetryClient } from './createTelemetryClient.js';\nexport type { LogLevel } from './types/LogLevel.js';\nexport type { TelemetryClient } from './types/TelemetryClient.js';\nexport type { TelemetryOptions } from './types/TelemetryOptions.js';\nexport { createNoOpTelemetryClient } from './createNoOpTelemetryClient.js';\nexport type { PerformanceTracker } from './types/PerformanceTracker.js';\nexport type { SpanAttributes } from './types/SpanAttributes.js';\nexport type { PerformanceRecorder } from './PerformanceRecorder.js';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAInE,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC","sourcesContent":["export type { Span, Tracer } from '@opentelemetry/api';\nexport { createTelemetryClient } from './createTelemetryClient.js';\nexport type { LogLevel } from './types/LogLevel.js';\nexport type { TelemetryClient } from './types/TelemetryClient.js';\nexport type { TelemetryOptions } from './types/TelemetryOptions.js';\nexport { createNoOpTelemetryClient } from './createNoOpTelemetryClient.js';\nexport type { PerformanceTracker } from './types/PerformanceTracker.js';\nexport type { SpanAttributes } from './types/SpanAttributes.js';\nexport type { PerformanceRecorder } from './PerformanceRecorder.js';\nexport type { MetricEvent } from './types/MetricEvent.js';\nexport type { MetricEventAggregator } from './types/MetricEventAggregator.js';\n"]}
@@ -0,0 +1,7 @@
1
+ import type { SpanAttributes } from './SpanAttributes.js';
2
+ export interface MetricEvent {
3
+ attributes?: SpanAttributes;
4
+ value: number;
5
+ name: string;
6
+ }
7
+ //# sourceMappingURL=MetricEvent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MetricEvent.d.ts","sourceRoot":"","sources":["../../src/types/MetricEvent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE1D,MAAM,WAAW,WAAW;IAC1B,UAAU,CAAC,EAAE,cAAc,CAAC;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;CACd"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=MetricEvent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MetricEvent.js","sourceRoot":"","sources":["../../src/types/MetricEvent.ts"],"names":[],"mappings":"","sourcesContent":["import type { SpanAttributes } from './SpanAttributes.js';\n\nexport interface MetricEvent {\n attributes?: SpanAttributes;\n value: number;\n name: string;\n}\n"]}
@@ -0,0 +1,6 @@
1
+ import type { MetricEvent } from '../index.js';
2
+ export interface MetricEventAggregator<T> {
3
+ record(event: T): void;
4
+ getAggregatedEvents(): MetricEvent[];
5
+ }
6
+ //# sourceMappingURL=MetricEventAggregator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MetricEventAggregator.d.ts","sourceRoot":"","sources":["../../src/types/MetricEventAggregator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,MAAM,WAAW,qBAAqB,CAAC,CAAC;IACtC,MAAM,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IACvB,mBAAmB,IAAI,WAAW,EAAE,CAAC;CACtC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=MetricEventAggregator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MetricEventAggregator.js","sourceRoot":"","sources":["../../src/types/MetricEventAggregator.ts"],"names":[],"mappings":"","sourcesContent":["import type { MetricEvent } from '../index.js';\n\nexport interface MetricEventAggregator<T> {\n record(event: T): void;\n getAggregatedEvents(): MetricEvent[];\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/telemetry",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Helpers for logging tasks to the console.",
5
5
  "license": "MIT",
6
6
  "type": "module",