@contrail/telemetry 2.2.0-alpha-tracing.1 → 2.2.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.
@@ -7,6 +7,7 @@
7
7
  import pino from 'pino';
8
8
  import type { Logger } from 'pino';
9
9
  export * from './semantic-conventions';
10
+ export * from './tracing';
10
11
  export { ATTR_LOG_MESSAGE, ATTR_LOG_BODY, ATTR_LOG_PAYLOAD } from './logger/semantic-conventions';
11
12
  export { PINO_LEVEL_TO_OTEL_SEVERITY, PINO_LEVEL_TO_NAME } from './logger/logger-config';
12
13
  export { parseOtelResourceAttributes } from './logger/parse-otel-resource-attributes';
@@ -31,6 +31,7 @@ exports.flushLogs = flushLogs;
31
31
  const pino_1 = __importDefault(require("pino"));
32
32
  // --- Re-export browser-safe modules as-is ---
33
33
  __exportStar(require("./semantic-conventions"), exports);
34
+ __exportStar(require("./tracing"), exports);
34
35
  var semantic_conventions_1 = require("./logger/semantic-conventions");
35
36
  Object.defineProperty(exports, "ATTR_LOG_MESSAGE", { enumerable: true, get: function () { return semantic_conventions_1.ATTR_LOG_MESSAGE; } });
36
37
  Object.defineProperty(exports, "ATTR_LOG_BODY", { enumerable: true, get: function () { return semantic_conventions_1.ATTR_LOG_BODY; } });
@@ -1,3 +1,4 @@
1
1
  import { Attributes, Span } from '@opentelemetry/api';
2
- export type TraceSectionAttrs = Attributes | ((span: Span) => Attributes | void);
3
- export declare function traceSection<T>(name: string, attrs: TraceSectionAttrs, fn: (span: Span) => Promise<T>): Promise<T>;
2
+ export type TraceSectionAttributes = Attributes | ((span: Span) => Attributes | void);
3
+ export declare function traceSection<T>(name: string, attributes: TraceSectionAttributes, fn: (span: Span) => T): T;
4
+ export declare function traceSection<T>(name: string, attributes: TraceSectionAttributes, fn: (span: Span) => Promise<T>): Promise<T>;
@@ -3,26 +3,43 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.traceSection = traceSection;
4
4
  const api_1 = require("@opentelemetry/api");
5
5
  const tracer = api_1.trace.getTracer('contrail');
6
- async function traceSection(name, attrs, fn) {
7
- return tracer.startActiveSpan(name, async (span) => {
6
+ function applyAttributes(span, attributes) {
7
+ if (typeof attributes === 'function') {
8
+ const computed = attributes(span);
9
+ if (computed)
10
+ span.setAttributes(computed);
11
+ }
12
+ else {
13
+ span.setAttributes(attributes);
14
+ }
15
+ }
16
+ function recordError(span, err) {
17
+ const message = err instanceof Error ? err.message : String(err);
18
+ span.recordException(err instanceof Error ? err : new Error(message));
19
+ span.setStatus({ code: api_1.SpanStatusCode.ERROR, message });
20
+ }
21
+ function traceSection(name, attributes, fn) {
22
+ return tracer.startActiveSpan(name, (span) => {
8
23
  try {
9
- if (typeof attrs === 'function') {
10
- const computed = attrs(span);
11
- if (computed)
12
- span.setAttributes(computed);
24
+ applyAttributes(span, attributes);
25
+ const result = fn(span);
26
+ if (result && typeof result.then === 'function') {
27
+ return result.then((val) => {
28
+ span.end();
29
+ return val;
30
+ }, (err) => {
31
+ recordError(span, err);
32
+ span.end();
33
+ throw err;
34
+ });
13
35
  }
14
- else {
15
- span.setAttributes(attrs);
16
- }
17
- return await fn(span);
36
+ span.end();
37
+ return result;
18
38
  }
19
39
  catch (err) {
20
- span.recordException(err);
21
- span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: err.message });
22
- throw err;
23
- }
24
- finally {
40
+ recordError(span, err);
25
41
  span.end();
42
+ throw err;
26
43
  }
27
44
  });
28
45
  }
@@ -1,2 +1,2 @@
1
1
  import { Attributes } from '@opentelemetry/api';
2
- export declare function Traced(name: string, staticAttrs?: Attributes): (target: any, key: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
2
+ export declare function Traced(name: string, staticAttributes?: Attributes): (target: any, key: string | symbol, descriptor: PropertyDescriptor) => PropertyDescriptor;
@@ -2,15 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Traced = Traced;
4
4
  const trace_section_1 = require("./trace-section");
5
- function Traced(name, staticAttrs = {}) {
5
+ function Traced(name, staticAttributes = {}) {
6
6
  return function (target, key, descriptor) {
7
7
  var _a;
8
8
  const original = descriptor.value;
9
- const className = (_a = target === null || target === void 0 ? void 0 : target.constructor) === null || _a === void 0 ? void 0 : _a.name;
10
- const codeFunctionName = className && className !== 'Object' ? `${className}.${key}` : key;
11
- const attrs = { ...staticAttrs, 'code.function.name': codeFunctionName };
9
+ const className = typeof target === 'function' ? target.name : (_a = target === null || target === void 0 ? void 0 : target.constructor) === null || _a === void 0 ? void 0 : _a.name;
10
+ const keyName = String(key);
11
+ const codeFunctionName = className && className !== 'Object' && className !== 'Function' ? `${className}.${keyName}` : keyName;
12
+ const attributes = { ...staticAttributes, 'code.function.name': codeFunctionName };
12
13
  descriptor.value = function (...args) {
13
- return (0, trace_section_1.traceSection)(name, attrs, () => original.apply(this, args));
14
+ return (0, trace_section_1.traceSection)(name, attributes, () => original.apply(this, args));
14
15
  };
15
16
  return descriptor;
16
17
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/telemetry",
3
- "version": "2.2.0-alpha-tracing.1",
3
+ "version": "2.2.0",
4
4
  "description": "Telemetry and monitoring utilities for contrail services",
5
5
  "main": "lib/index.js",
6
6
  "browser": "lib/index.browser.js",