@ms-cloudpack/telemetry 0.8.0 → 0.8.2

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.
@@ -21,7 +21,7 @@ import {
21
21
  require_src2,
22
22
  require_src3,
23
23
  require_src4
24
- } from "./chunk-NCK3Y7B7.js";
24
+ } from "./chunk-6K6C2B4T.js";
25
25
 
26
26
  // ../../node_modules/.store/@azure-core-rest-pipeline-npm-1.17.0-aa1ea9ace7/package/dist/commonjs/pipeline.js
27
27
  var require_pipeline = __commonJS({
@@ -6,8 +6,8 @@ const __filename = topLevelUrl.fileURLToPath(import.meta.url);
6
6
  const __dirname = topLevelPath.dirname(__filename);
7
7
  import {
8
8
  NoOpTelemetryClient
9
- } from "./chunk-6ZXUGFN2.js";
10
- import "./chunk-NCK3Y7B7.js";
9
+ } from "./chunk-7OGMQ6JV.js";
10
+ import "./chunk-6K6C2B4T.js";
11
11
  export {
12
12
  NoOpTelemetryClient
13
13
  };
@@ -4965,11 +4965,11 @@ var require_ExportResult = __commonJS({
4965
4965
  "use strict";
4966
4966
  Object.defineProperty(exports, "__esModule", { value: true });
4967
4967
  exports.ExportResultCode = void 0;
4968
- var ExportResultCode;
4969
- (function(ExportResultCode2) {
4970
- ExportResultCode2[ExportResultCode2["SUCCESS"] = 0] = "SUCCESS";
4971
- ExportResultCode2[ExportResultCode2["FAILED"] = 1] = "FAILED";
4972
- })(ExportResultCode = exports.ExportResultCode || (exports.ExportResultCode = {}));
4968
+ var ExportResultCode2;
4969
+ (function(ExportResultCode3) {
4970
+ ExportResultCode3[ExportResultCode3["SUCCESS"] = 0] = "SUCCESS";
4971
+ ExportResultCode3[ExportResultCode3["FAILED"] = 1] = "FAILED";
4972
+ })(ExportResultCode2 = exports.ExportResultCode || (exports.ExportResultCode = {}));
4973
4973
  }
4974
4974
  });
4975
4975
 
@@ -6988,8 +6988,8 @@ var require_BatchSpanProcessor = __commonJS({
6988
6988
  }
6989
6989
  };
6990
6990
  __name(_BatchSpanProcessor, "BatchSpanProcessor");
6991
- var BatchSpanProcessor = _BatchSpanProcessor;
6992
- exports.BatchSpanProcessor = BatchSpanProcessor;
6991
+ var BatchSpanProcessor2 = _BatchSpanProcessor;
6992
+ exports.BatchSpanProcessor = BatchSpanProcessor2;
6993
6993
  }
6994
6994
  });
6995
6995
 
@@ -8418,7 +8418,7 @@ var require_ConsoleSpanExporter = __commonJS({
8418
8418
  Object.defineProperty(exports, "__esModule", { value: true });
8419
8419
  exports.ConsoleSpanExporter = void 0;
8420
8420
  var core_1 = require_src2();
8421
- var _ConsoleSpanExporter = class _ConsoleSpanExporter {
8421
+ var _ConsoleSpanExporter2 = class _ConsoleSpanExporter2 {
8422
8422
  /**
8423
8423
  * Export spans.
8424
8424
  * @param spans
@@ -8479,9 +8479,9 @@ var require_ConsoleSpanExporter = __commonJS({
8479
8479
  }
8480
8480
  }
8481
8481
  };
8482
- __name(_ConsoleSpanExporter, "ConsoleSpanExporter");
8483
- var ConsoleSpanExporter = _ConsoleSpanExporter;
8484
- exports.ConsoleSpanExporter = ConsoleSpanExporter;
8482
+ __name(_ConsoleSpanExporter2, "ConsoleSpanExporter");
8483
+ var ConsoleSpanExporter2 = _ConsoleSpanExporter2;
8484
+ exports.ConsoleSpanExporter = ConsoleSpanExporter2;
8485
8485
  }
8486
8486
  });
8487
8487
 
@@ -12359,6 +12359,154 @@ function getVersion(importUrl) {
12359
12359
  }
12360
12360
  __name(getVersion, "getVersion");
12361
12361
 
12362
+ // src/ConsoleSpanExporter.ts
12363
+ var import_core = __toESM(require_src2(), 1);
12364
+ var _ConsoleSpanExporter = class _ConsoleSpanExporter {
12365
+ /**
12366
+ * Export spans.
12367
+ */
12368
+ export(spans, resultCallback) {
12369
+ return this._sendSpans(spans, resultCallback);
12370
+ }
12371
+ /**
12372
+ * Shutdown the exporter.
12373
+ */
12374
+ shutdown() {
12375
+ this._sendSpans([]);
12376
+ return this.forceFlush();
12377
+ }
12378
+ /**
12379
+ * Exports any pending spans in exporter
12380
+ */
12381
+ forceFlush() {
12382
+ return Promise.resolve();
12383
+ }
12384
+ /**
12385
+ * converts span info into more readable format
12386
+ */
12387
+ _exportInfo(span) {
12388
+ return [
12389
+ `Telemetry Span: ${span.name} in ${span.duration}ms`,
12390
+ span.events.length > 0 ? `with ${span.events.length} events` : void 0
12391
+ ].filter((text) => !!text).join(" ");
12392
+ }
12393
+ /**
12394
+ * Showing spans in console
12395
+ */
12396
+ _sendSpans(spans, done) {
12397
+ for (const span of spans) {
12398
+ console.debug(this._exportInfo(span));
12399
+ }
12400
+ if (done) {
12401
+ return done({ code: import_core.ExportResultCode.SUCCESS });
12402
+ }
12403
+ }
12404
+ };
12405
+ __name(_ConsoleSpanExporter, "ConsoleSpanExporter");
12406
+ var ConsoleSpanExporter = _ConsoleSpanExporter;
12407
+
12408
+ // src/proxies/createEndGuardedSpan.ts
12409
+ var hasEnded = Symbol("hasEnded");
12410
+ function createEndGuardedSpan(span) {
12411
+ return new Proxy(span, {
12412
+ get(target, prop) {
12413
+ if (prop === "hasEnded") {
12414
+ return () => !!Reflect.get(target, hasEnded);
12415
+ }
12416
+ if (prop === "end") {
12417
+ return () => {
12418
+ if (!Reflect.get(target, hasEnded)) {
12419
+ target.end();
12420
+ Reflect.set(target, hasEnded, true);
12421
+ }
12422
+ };
12423
+ }
12424
+ return Reflect.get(target, prop);
12425
+ },
12426
+ set(target, prop, value) {
12427
+ if (prop === "end") {
12428
+ throw new Error("Cannot overwrite 'end' method on Span.");
12429
+ }
12430
+ return Reflect.set(target, prop, value);
12431
+ }
12432
+ });
12433
+ }
12434
+ __name(createEndGuardedSpan, "createEndGuardedSpan");
12435
+
12436
+ // src/proxies/createSpanFunctionWrapper.ts
12437
+ function createSpanFunctionWrapper(fn) {
12438
+ return function(span) {
12439
+ const guardedSpan = createEndGuardedSpan(span);
12440
+ try {
12441
+ return fn(guardedSpan);
12442
+ } catch (error) {
12443
+ if (!guardedSpan.hasEnded()) {
12444
+ guardedSpan.recordException(error instanceof Error || typeof error === "string" ? error : String(error));
12445
+ } else {
12446
+ console.info("OpenTelemetry:startActiveSpan: span has already ended, skipping recording exception.");
12447
+ }
12448
+ throw error;
12449
+ } finally {
12450
+ guardedSpan.end();
12451
+ }
12452
+ };
12453
+ }
12454
+ __name(createSpanFunctionWrapper, "createSpanFunctionWrapper");
12455
+
12456
+ // src/proxies/createImprovedStartActiveSpan.ts
12457
+ function parseParameters(...params) {
12458
+ const [name, optionsOrFn, contextOrFn, maybeFn] = params;
12459
+ if (params.length === 2) {
12460
+ return { name, fn: optionsOrFn };
12461
+ }
12462
+ if (params.length === 3) {
12463
+ return { name, options: optionsOrFn, fn: contextOrFn };
12464
+ }
12465
+ if (params.length === 4) {
12466
+ return {
12467
+ name,
12468
+ options: optionsOrFn,
12469
+ context: contextOrFn,
12470
+ fn: maybeFn
12471
+ };
12472
+ }
12473
+ throw new Error("Number of arguments is not supported for startActiveSpan");
12474
+ }
12475
+ __name(parseParameters, "parseParameters");
12476
+ function createImprovedStartActiveSpan(target) {
12477
+ return (...params) => {
12478
+ const { fn, name, context: context2, options } = parseParameters(...params);
12479
+ const wrappedSpanFunction = createSpanFunctionWrapper(fn);
12480
+ if (context2) {
12481
+ return target.startActiveSpan(name, options, context2, wrappedSpanFunction);
12482
+ }
12483
+ if (options) {
12484
+ return target.startActiveSpan(name, options, wrappedSpanFunction);
12485
+ }
12486
+ return target.startActiveSpan(name, wrappedSpanFunction);
12487
+ };
12488
+ }
12489
+ __name(createImprovedStartActiveSpan, "createImprovedStartActiveSpan");
12490
+
12491
+ // src/proxies/createProxiedTracer.ts
12492
+ var isProxy = Symbol("isProxy");
12493
+ function createProxiedTracer(tracer) {
12494
+ if (Reflect.has(tracer, isProxy)) {
12495
+ return tracer;
12496
+ }
12497
+ const proxiedTracer = new Proxy(tracer, {
12498
+ get(target, prop) {
12499
+ if (prop === "startActiveSpan") {
12500
+ return createImprovedStartActiveSpan(target);
12501
+ }
12502
+ return Reflect.get(target, prop);
12503
+ }
12504
+ });
12505
+ Reflect.set(proxiedTracer, isProxy, true);
12506
+ return proxiedTracer;
12507
+ }
12508
+ __name(createProxiedTracer, "createProxiedTracer");
12509
+
12362
12510
  // src/BaseTelemetryClient.ts
12363
12511
  var _BaseTelemetryClient = class _BaseTelemetryClient {
12364
12512
  constructor(options) {
@@ -12375,6 +12523,9 @@ var _BaseTelemetryClient = class _BaseTelemetryClient {
12375
12523
  "cloudpack.telemetry.version": getVersion(import.meta.url)
12376
12524
  })
12377
12525
  );
12526
+ if (options.logLevel === "DEBUG") {
12527
+ this._tracerProvider.addSpanProcessor(new import_sdk_trace_node.BatchSpanProcessor(new ConsoleSpanExporter()));
12528
+ }
12378
12529
  this._rootSpan = this.tracer.startSpan(options.rootSpanName || "root");
12379
12530
  this._performance = new PerformanceRecorder(this._rootSpan);
12380
12531
  diag2.info("TelemetryClient is initialized.");
@@ -12395,7 +12546,7 @@ var _BaseTelemetryClient = class _BaseTelemetryClient {
12395
12546
  * @returns the OpenTelemetry tracer
12396
12547
  */
12397
12548
  get tracer() {
12398
- return this._tracerProvider.getTracer("cloudpack-tracer");
12549
+ return createProxiedTracer(this._tracerProvider.getTracer("cloudpack-tracer"));
12399
12550
  }
12400
12551
  get performance() {
12401
12552
  return this._performance;
@@ -7,7 +7,7 @@ const __dirname = topLevelPath.dirname(__filename);
7
7
  import {
8
8
  BaseTelemetryClient,
9
9
  __name
10
- } from "./chunk-NCK3Y7B7.js";
10
+ } from "./chunk-6K6C2B4T.js";
11
11
 
12
12
  // src/NoOpTelemetryClient.ts
13
13
  var _NoOpTelemetryClient = class _NoOpTelemetryClient extends BaseTelemetryClient {
package/dist/index.js CHANGED
@@ -6,28 +6,28 @@ const __filename = topLevelUrl.fileURLToPath(import.meta.url);
6
6
  const __dirname = topLevelPath.dirname(__filename);
7
7
  import {
8
8
  NoOpTelemetryClient
9
- } from "./chunk-6ZXUGFN2.js";
9
+ } from "./chunk-7OGMQ6JV.js";
10
10
  import {
11
11
  DiagConsoleLogger,
12
12
  DiagLogLevel,
13
13
  __name,
14
14
  diag,
15
15
  init_esm
16
- } from "./chunk-NCK3Y7B7.js";
16
+ } from "./chunk-6K6C2B4T.js";
17
17
 
18
18
  // src/createTelemetryClient.ts
19
19
  init_esm();
20
20
  async function createTelemetryClient(options) {
21
21
  if (!options.connectionString) {
22
22
  console.debug("No connection string found. Telemetry will not be sent.");
23
- const { NoOpTelemetryClient: NoOpTelemetryClient2 } = await import("./NoOpTelemetryClient-M2CYNHPI.js");
23
+ const { NoOpTelemetryClient: NoOpTelemetryClient2 } = await import("./NoOpTelemetryClient-IZOMUV4R.js");
24
24
  return new NoOpTelemetryClient2();
25
25
  }
26
26
  diag.setLogger(new DiagConsoleLogger(), {
27
27
  logLevel: options.logLevel || DiagLogLevel.WARN,
28
28
  suppressOverrideMessage: true
29
29
  });
30
- const { AppInsightsTelemetryClient } = await import("./AppInsightsTelemetryClient-VHBDVKM4.js");
30
+ const { AppInsightsTelemetryClient } = await import("./AppInsightsTelemetryClient-DIN7NSX7.js");
31
31
  return new AppInsightsTelemetryClient(options);
32
32
  }
33
33
  __name(createTelemetryClient, "createTelemetryClient");
@@ -0,0 +1,37 @@
1
+ /**
2
+ * A customized version of the OpenTelemetry `ConsoleSpanExporter`.
3
+ * Original source: https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-base/src/export/ConsoleSpanExporter.ts
4
+ *
5
+ * This modified exporter adjusts the format of the output for improved readability.
6
+ */
7
+ import type { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-base';
8
+ import type { ExportResult } from '@opentelemetry/core';
9
+ /**
10
+ * This is implementation of {@link SpanExporter} that prints spans to the
11
+ * console. This class can be used for diagnostic purposes.
12
+ *
13
+ * NOTE: This {@link SpanExporter} is intended for diagnostics use only, output rendered to the console may change at any time.
14
+ */
15
+ export declare class ConsoleSpanExporter implements SpanExporter {
16
+ /**
17
+ * Export spans.
18
+ */
19
+ export(spans: ReadableSpan[], resultCallback: (result: ExportResult) => void): void;
20
+ /**
21
+ * Shutdown the exporter.
22
+ */
23
+ shutdown(): Promise<void>;
24
+ /**
25
+ * Exports any pending spans in exporter
26
+ */
27
+ forceFlush(): Promise<void>;
28
+ /**
29
+ * converts span info into more readable format
30
+ */
31
+ private _exportInfo;
32
+ /**
33
+ * Showing spans in console
34
+ */
35
+ private _sendSpans;
36
+ }
37
+ //# sourceMappingURL=ConsoleSpanExporter.d.ts.map
@@ -0,0 +1,12 @@
1
+ import type { Span } from '@opentelemetry/api';
2
+ /**
3
+ * A span that can be ended only once
4
+ */
5
+ export type EndGuardedSpan = Span & {
6
+ hasEnded: () => boolean;
7
+ };
8
+ /**
9
+ * Wraps a Span in a Proxy to intercept calls to `end` and ensure it’s only called once.
10
+ */
11
+ export declare function createEndGuardedSpan(span: Span): EndGuardedSpan;
12
+ //# sourceMappingURL=createEndGuardedSpan.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=createEndGuardedSpan.test.d.ts.map
@@ -0,0 +1,4 @@
1
+ import type { Tracer, Span, SpanOptions, Context } from '@opentelemetry/api';
2
+ export type SpanFunction<T> = (span: Span) => T;
3
+ export declare function createImprovedStartActiveSpan(target: Tracer): <T>(name: string, optionsOrFn?: SpanOptions | SpanFunction<unknown> | undefined, contextOrFn?: Context | SpanFunction<unknown> | undefined, maybeFn?: SpanFunction<unknown> | undefined) => T;
4
+ //# sourceMappingURL=createImprovedStartActiveSpan.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=createImprovedStartActiveSpan.test.d.ts.map
@@ -0,0 +1,10 @@
1
+ import type { Tracer } from '@opentelemetry/api';
2
+ /**
3
+ * Creates a proxied tracer that will automatically do the following things;
4
+ * - End the span when the function ends
5
+ * - Record any exceptions that are thrown
6
+ * @param tracer - The tracer to proxy
7
+ * @returns - Proxied tracer
8
+ */
9
+ export declare function createProxiedTracer(tracer: Tracer): Tracer;
10
+ //# sourceMappingURL=createProxiedTracer.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=createProxiedTracer.test.d.ts.map
@@ -0,0 +1,9 @@
1
+ import type { Span } from '@opentelemetry/api';
2
+ import type { SpanFunction } from './createImprovedStartActiveSpan.js';
3
+ /**
4
+ * Wraps a span function to ensure that the span is ended and any exceptions are recorded.
5
+ * @param fn - The span function to wrap
6
+ * @returns - Wrapped span function
7
+ */
8
+ export declare function createSpanFunctionWrapper<T>(fn: SpanFunction<T>): (span: Span) => T;
9
+ //# sourceMappingURL=createSpanFunctionWrapper.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=createSpanFunctionWrapper.test.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/telemetry",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "description": "Helpers for reporting telemetry in Cloudpack.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -16,9 +16,10 @@
16
16
  "devDependencies": {
17
17
  "@azure/monitor-opentelemetry-exporter": "1.0.0-beta.26",
18
18
  "@ms-cloudpack/eslint-plugin-internal": "^0.0.1",
19
- "@ms-cloudpack/package-utilities": "^10.2.6",
19
+ "@ms-cloudpack/package-utilities": "^10.2.8",
20
20
  "@ms-cloudpack/scripts": "^0.0.1",
21
21
  "@opentelemetry/api": "~1.9.0",
22
+ "@opentelemetry/core": "~1.26.0",
22
23
  "@opentelemetry/resources": "~1.26.0",
23
24
  "@opentelemetry/sdk-trace-base": "~1.26.0",
24
25
  "@opentelemetry/sdk-trace-node": "~1.26.0",
@@ -31,7 +32,10 @@
31
32
  "build:watch": "cloudpack-scripts build-watch",
32
33
  "build": "cloudpack-scripts build && cloudpack-scripts bundle-node",
33
34
  "lint:update": "cloudpack-scripts lint-update",
34
- "lint": "cloudpack-scripts lint"
35
+ "lint": "cloudpack-scripts lint",
36
+ "test:update": "cloudpack-scripts test-update",
37
+ "test:watch": "cloudpack-scripts test-watch",
38
+ "test": "cloudpack-scripts test"
35
39
  },
36
40
  "files": [
37
41
  "dist",