@contrail/telemetry 2.2.0-alpha-tracing.0 → 2.2.0-alpha-tracing.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.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,11 @@ Versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Added
11
+
12
+ - **`traceSection(name, attrs, fn)` helper** — opens an OTel span around a section of work, sets attributes (static or computed from the span), and ends the span automatically with error recording on throw.
13
+ - **`@Traced(name, staticAttrs?)` decorator** — class-method decorator that wraps a method's invocation in a `traceSection` span. Auto-sets the OTel-spec `code.function.name` attribute to `${ClassName}.${methodName}` so the actor is searchable without callers having to set it. For dynamic attributes derived from arguments or results, set them inside the body via `trace.getActiveSpan()?.setAttribute(...)`.
14
+
10
15
  ## [2.1.0] - 2026-04-29
11
16
 
12
17
  ### Added
@@ -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; } });
package/lib/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './logger';
2
2
  export * from './semantic-conventions';
3
+ export * from './tracing';
package/lib/index.js CHANGED
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./logger"), exports);
18
18
  __exportStar(require("./semantic-conventions"), exports);
19
+ __exportStar(require("./tracing"), exports);
@@ -0,0 +1,2 @@
1
+ export * from './trace-section';
2
+ export * from './traced.decorator';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./trace-section"), exports);
18
+ __exportStar(require("./traced.decorator"), exports);
@@ -0,0 +1,4 @@
1
+ import { Attributes, Span } from '@opentelemetry/api';
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>;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.traceSection = traceSection;
4
+ const api_1 = require("@opentelemetry/api");
5
+ const tracer = api_1.trace.getTracer('contrail');
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) => {
23
+ try {
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
+ });
35
+ }
36
+ span.end();
37
+ return result;
38
+ }
39
+ catch (err) {
40
+ recordError(span, err);
41
+ span.end();
42
+ throw err;
43
+ }
44
+ });
45
+ }
@@ -0,0 +1,2 @@
1
+ import { Attributes } from '@opentelemetry/api';
2
+ export declare function Traced(name: string, staticAttributes?: Attributes): (target: any, key: string | symbol, descriptor: PropertyDescriptor) => PropertyDescriptor;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Traced = Traced;
4
+ const trace_section_1 = require("./trace-section");
5
+ function Traced(name, staticAttributes = {}) {
6
+ return function (target, key, descriptor) {
7
+ var _a;
8
+ const original = descriptor.value;
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 };
13
+ descriptor.value = function (...args) {
14
+ return (0, trace_section_1.traceSection)(name, attributes, () => original.apply(this, args));
15
+ };
16
+ return descriptor;
17
+ };
18
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/telemetry",
3
- "version": "2.2.0-alpha-tracing.0",
3
+ "version": "2.2.0-alpha-tracing.2",
4
4
  "description": "Telemetry and monitoring utilities for contrail services",
5
5
  "main": "lib/index.js",
6
6
  "browser": "lib/index.browser.js",