@daytonaio/sdk 0.139.0 → 0.141.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.
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Configuration options for span instrumentation
3
+ */
4
+ export interface SpanConfig {
5
+ /**
6
+ * Custom name for the span. If not provided, uses `ClassName.methodName` format
7
+ */
8
+ name?: string;
9
+ /**
10
+ * Additional attributes to attach to the span
11
+ */
12
+ attributes?: Record<string, string>;
13
+ }
14
+ /**
15
+ * Configuration options for metric instrumentation
16
+ */
17
+ export interface MetricConfig {
18
+ /**
19
+ * Custom name for the metric. If not provided, uses `ClassName.methodName` format
20
+ */
21
+ name?: string;
22
+ /**
23
+ * Description for the metrics being collected
24
+ */
25
+ description?: string;
26
+ /**
27
+ * Additional labels to attach to the metrics
28
+ */
29
+ labels?: Record<string, string>;
30
+ }
31
+ /**
32
+ * Configuration options for the combined instrumentation decorator
33
+ */
34
+ export interface InstrumentationConfig {
35
+ /**
36
+ * Custom name for the span and metric. If not provided, uses `ClassName.methodName` format
37
+ */
38
+ name?: string;
39
+ /**
40
+ * Description for the metrics being collected
41
+ */
42
+ description?: string;
43
+ /**
44
+ * Additional labels/attributes to attach to spans and metrics
45
+ */
46
+ labels?: Record<string, string>;
47
+ /**
48
+ * Enable trace collection (default: true)
49
+ */
50
+ enableTraces?: boolean;
51
+ /**
52
+ * Enable metrics collection (default: true)
53
+ */
54
+ enableMetrics?: boolean;
55
+ }
56
+ /**
57
+ * Decorator for instrumenting methods with OpenTelemetry spans (traces only)
58
+ *
59
+ * @param config - Configuration object or string name for the span
60
+ *
61
+ */
62
+ export declare function WithSpan(config?: string | SpanConfig): (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void;
63
+ /**
64
+ * Decorator for instrumenting methods with OpenTelemetry metrics (metrics only)
65
+ *
66
+ * Collects two metrics:
67
+ * - Counter: `{name}_executions` - tracks number of executions with status (success/error)
68
+ * - Histogram: `{name}_duration` - tracks execution duration in milliseconds
69
+ *
70
+ * @param config - Configuration object or string name for the metric
71
+ *
72
+ */
73
+ export declare function WithMetric(config?: string | MetricConfig): (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void;
74
+ /**
75
+ * Decorator for instrumenting methods with both OpenTelemetry traces and metrics
76
+ *
77
+ * This decorator composes @WithSpan and @WithMetric to provide both trace and metric collection.
78
+ * You can selectively enable/disable traces or metrics using the config options.
79
+ *
80
+ * @param config - Configuration object or string name for the instrumentation
81
+ */
82
+ export declare function WithInstrumentation(config?: string | InstrumentationConfig): MethodDecorator;
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2025 Daytona Platforms Inc.
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.WithSpan = WithSpan;
8
+ exports.WithMetric = WithMetric;
9
+ exports.WithInstrumentation = WithInstrumentation;
10
+ const api_1 = require("@opentelemetry/api");
11
+ // Lazy initialization to ensure SDK is started before getting tracer/meter
12
+ const getTracer = () => api_1.trace.getTracer('');
13
+ const getMeter = () => api_1.metrics.getMeter('');
14
+ const executionHistograms = new Map();
15
+ /**
16
+ * Converts a string to snake_case for Prometheus-friendly metric names
17
+ */
18
+ function toSnakeCase(str) {
19
+ return str
20
+ .replace(/([A-Z])/g, '_$1')
21
+ .toLowerCase()
22
+ .replace(/^_/, '')
23
+ .replace(/\./g, '_');
24
+ }
25
+ /**
26
+ * Decorator for instrumenting methods with OpenTelemetry spans (traces only)
27
+ *
28
+ * @param config - Configuration object or string name for the span
29
+ *
30
+ */
31
+ function WithSpan(config) {
32
+ return (target, propertyKey, descriptor) => {
33
+ const originalMethod = descriptor.value;
34
+ const methodName = String(propertyKey);
35
+ descriptor.value = async function (...args) {
36
+ const cfg = typeof config === 'string' ? { name: config } : config || {};
37
+ const { name, attributes = {} } = cfg;
38
+ const spanName = name || `${target.constructor.name}.${methodName}`;
39
+ const allAttributes = {
40
+ component: target.constructor.name,
41
+ method: methodName,
42
+ ...attributes,
43
+ };
44
+ const span = getTracer().startSpan(spanName, {
45
+ attributes: allAttributes,
46
+ }, api_1.context.active());
47
+ return api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), async () => {
48
+ try {
49
+ const result = await originalMethod.apply(this, args);
50
+ span.setStatus({ code: api_1.SpanStatusCode.OK });
51
+ return result;
52
+ }
53
+ catch (error) {
54
+ span.setStatus({
55
+ code: api_1.SpanStatusCode.ERROR,
56
+ message: error instanceof Error ? error.message : String(error),
57
+ });
58
+ span.recordException(error instanceof Error ? error : new Error(String(error)));
59
+ throw error;
60
+ }
61
+ finally {
62
+ span.end();
63
+ }
64
+ });
65
+ };
66
+ };
67
+ }
68
+ /**
69
+ * Decorator for instrumenting methods with OpenTelemetry metrics (metrics only)
70
+ *
71
+ * Collects two metrics:
72
+ * - Counter: `{name}_executions` - tracks number of executions with status (success/error)
73
+ * - Histogram: `{name}_duration` - tracks execution duration in milliseconds
74
+ *
75
+ * @param config - Configuration object or string name for the metric
76
+ *
77
+ */
78
+ function WithMetric(config) {
79
+ return (target, propertyKey, descriptor) => {
80
+ const originalMethod = descriptor.value;
81
+ const methodName = String(propertyKey);
82
+ descriptor.value = async function (...args) {
83
+ const cfg = typeof config === 'string' ? { name: config } : config || {};
84
+ const { name, description, labels = {} } = cfg;
85
+ const metricName = toSnakeCase(name || `${target.constructor.name}.${methodName}`);
86
+ const allLabels = {
87
+ component: target.constructor.name,
88
+ method: methodName,
89
+ ...labels,
90
+ };
91
+ // Get or create histogram for this method
92
+ if (!executionHistograms.has(metricName)) {
93
+ executionHistograms.set(metricName, getMeter().createHistogram(`${metricName}_duration`, {
94
+ description: description || `Duration of executions for ${metricName}`,
95
+ unit: 'ms',
96
+ }));
97
+ }
98
+ const histogram = executionHistograms.get(metricName);
99
+ if (!histogram) {
100
+ throw new Error(`Histogram not found for metric: ${metricName}`);
101
+ }
102
+ const startTime = Date.now();
103
+ let status = 'success';
104
+ try {
105
+ const result = await originalMethod.apply(this, args);
106
+ return result;
107
+ }
108
+ catch (error) {
109
+ status = 'error';
110
+ throw error;
111
+ }
112
+ finally {
113
+ const duration = Date.now() - startTime;
114
+ histogram.record(duration, { ...allLabels, status });
115
+ }
116
+ };
117
+ };
118
+ }
119
+ /**
120
+ * Decorator for instrumenting methods with both OpenTelemetry traces and metrics
121
+ *
122
+ * This decorator composes @WithSpan and @WithMetric to provide both trace and metric collection.
123
+ * You can selectively enable/disable traces or metrics using the config options.
124
+ *
125
+ * @param config - Configuration object or string name for the instrumentation
126
+ */
127
+ function WithInstrumentation(config) {
128
+ const cfg = typeof config === 'string' ? { name: config } : config || {};
129
+ const { enableTraces = true, enableMetrics = true, name, description, labels } = cfg;
130
+ const decorators = [];
131
+ if (enableTraces) {
132
+ decorators.push(WithSpan({ name, attributes: labels }));
133
+ }
134
+ if (enableMetrics) {
135
+ decorators.push(WithMetric({ name, description, labels }));
136
+ }
137
+ return (target, propertyKey, descriptor) => {
138
+ decorators.forEach((decorator) => decorator(target, propertyKey, descriptor));
139
+ };
140
+ }
141
+ //# sourceMappingURL=otel.decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"otel.decorator.js","sourceRoot":"","sources":["../../../../../libs/sdk-typescript/src/utils/otel.decorator.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAqFH,4BA2CC;AAYD,gCA8CC;AAUD,kDAiBC;AAnND,4CAAuF;AAEvF,2EAA2E;AAC3E,MAAM,SAAS,GAAG,GAAG,EAAE,CAAC,WAAK,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;AAC3C,MAAM,QAAQ,GAAG,GAAG,EAAE,CAAC,aAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;AAE3C,MAAM,mBAAmB,GAAG,IAAI,GAAG,EAAqB,CAAA;AA4DxD;;GAEG;AACH,SAAS,WAAW,CAAC,GAAW;IAC9B,OAAO,GAAG;SACP,OAAO,CAAC,UAAU,EAAE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