@contrail/telemetry 2.1.0 → 2.2.0-alpha-tracing.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.
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
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,3 @@
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>;
@@ -0,0 +1,28 @@
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
+ async function traceSection(name, attrs, fn) {
7
+ return tracer.startActiveSpan(name, async (span) => {
8
+ try {
9
+ if (typeof attrs === 'function') {
10
+ const computed = attrs(span);
11
+ if (computed)
12
+ span.setAttributes(computed);
13
+ }
14
+ else {
15
+ span.setAttributes(attrs);
16
+ }
17
+ return await fn(span);
18
+ }
19
+ catch (err) {
20
+ span.recordException(err);
21
+ span.setStatus({ code: api_1.SpanStatusCode.ERROR, message: err.message });
22
+ throw err;
23
+ }
24
+ finally {
25
+ span.end();
26
+ }
27
+ });
28
+ }
@@ -0,0 +1,2 @@
1
+ import { Attributes } from '@opentelemetry/api';
2
+ export declare function Traced(name: string, staticAttrs?: Attributes): (target: any, key: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
@@ -0,0 +1,17 @@
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, staticAttrs = {}) {
6
+ return function (target, key, descriptor) {
7
+ var _a;
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 };
12
+ descriptor.value = function (...args) {
13
+ return (0, trace_section_1.traceSection)(name, attrs, () => original.apply(this, args));
14
+ };
15
+ return descriptor;
16
+ };
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/telemetry",
3
- "version": "2.1.0",
3
+ "version": "2.2.0-alpha-tracing.1",
4
4
  "description": "Telemetry and monitoring utilities for contrail services",
5
5
  "main": "lib/index.js",
6
6
  "browser": "lib/index.browser.js",