@blaxel/telemetry 0.2.49-dev.214 → 0.2.49-dev1

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,44 @@
1
+ import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
2
+ import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
3
+ import { ResourceMetrics } from "@opentelemetry/sdk-metrics";
4
+ import { ReadableSpan, SpanExporter } from "@opentelemetry/sdk-trace-node";
5
+ /**
6
+ * SpanExporter that refreshes authentication before each export.
7
+ * This is necessary for long-running containers where tokens may expire.
8
+ */
9
+ export declare class AuthRefreshingSpanExporter implements SpanExporter {
10
+ private createExporter;
11
+ constructor(createExporter: () => SpanExporter);
12
+ private currentExporter;
13
+ export(spans: ReadableSpan[], resultCallback: (result: {
14
+ code: number;
15
+ error?: Error;
16
+ }) => void): void;
17
+ private doExport;
18
+ shutdown(): Promise<void>;
19
+ forceFlush(): Promise<void>;
20
+ }
21
+ /**
22
+ * MetricExporter that refreshes authentication before each export.
23
+ * This is necessary for long-running containers where tokens may expire.
24
+ */
25
+ export declare class AuthRefreshingMetricExporter {
26
+ private createExporter;
27
+ constructor(createExporter: () => OTLPMetricExporter);
28
+ private currentExporter;
29
+ export(metrics: ResourceMetrics, resultCallback: (result: {
30
+ code: number;
31
+ error?: Error;
32
+ }) => void): void;
33
+ private doExport;
34
+ shutdown(): Promise<void>;
35
+ forceFlush(): Promise<void>;
36
+ }
37
+ /**
38
+ * Creates an OTLP Trace Exporter with the current auth headers
39
+ */
40
+ export declare function createTraceExporter(): OTLPTraceExporter;
41
+ /**
42
+ * Creates an OTLP Metric Exporter with the current auth headers
43
+ */
44
+ export declare function createMetricExporter(): OTLPMetricExporter;
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthRefreshingMetricExporter = exports.AuthRefreshingSpanExporter = void 0;
4
+ exports.createTraceExporter = createTraceExporter;
5
+ exports.createMetricExporter = createMetricExporter;
6
+ const core_1 = require("@blaxel/core");
7
+ const exporter_metrics_otlp_http_1 = require("@opentelemetry/exporter-metrics-otlp-http");
8
+ const exporter_trace_otlp_http_1 = require("@opentelemetry/exporter-trace-otlp-http");
9
+ /**
10
+ * SpanExporter that refreshes authentication before each export.
11
+ * This is necessary for long-running containers where tokens may expire.
12
+ */
13
+ class AuthRefreshingSpanExporter {
14
+ createExporter;
15
+ constructor(createExporter) {
16
+ this.createExporter = createExporter;
17
+ core_1.logger.debug("[AuthRefreshingSpanExporter] Initialized");
18
+ }
19
+ currentExporter = null;
20
+ export(spans, resultCallback) {
21
+ core_1.logger.debug(`[AuthRefreshingSpanExporter] Exporting ${spans.length} spans`);
22
+ // Execute async operations but return void as required by interface
23
+ this.doExport(spans, resultCallback).catch(error => {
24
+ core_1.logger.error("[AuthRefreshingSpanExporter] Fatal error in export:", error);
25
+ resultCallback({ code: 1, error: error });
26
+ });
27
+ }
28
+ async doExport(spans, resultCallback) {
29
+ try {
30
+ core_1.logger.debug("[AuthRefreshingSpanExporter] Starting authentication refresh");
31
+ const startTime = Date.now();
32
+ // Always refresh auth before export
33
+ await (0, core_1.authenticate)();
34
+ const authTime = Date.now() - startTime;
35
+ core_1.logger.debug(`[AuthRefreshingSpanExporter] Authentication completed in ${authTime}ms`);
36
+ // Log current auth status
37
+ if (core_1.settings.authorization) {
38
+ core_1.logger.debug("[AuthRefreshingSpanExporter] Authorization token is present");
39
+ }
40
+ else {
41
+ core_1.logger.warn("[AuthRefreshingSpanExporter] No authorization token after authentication!");
42
+ }
43
+ core_1.logger.debug("[AuthRefreshingSpanExporter] Creating new exporter");
44
+ // Create new exporter with fresh headers
45
+ this.currentExporter = this.createExporter();
46
+ core_1.logger.debug("[AuthRefreshingSpanExporter] New exporter created with fresh auth headers");
47
+ // Export using the fresh exporter
48
+ if (this.currentExporter && this.currentExporter.export) {
49
+ core_1.logger.debug("[AuthRefreshingSpanExporter] Calling export on fresh exporter");
50
+ this.currentExporter.export(spans, resultCallback);
51
+ }
52
+ else {
53
+ const error = new Error('Exporter not initialized');
54
+ core_1.logger.error("[AuthRefreshingSpanExporter] Exporter not properly initialized", error);
55
+ resultCallback({ code: 1, error });
56
+ }
57
+ }
58
+ catch (error) {
59
+ core_1.logger.error("[AuthRefreshingSpanExporter] Error during authentication or export:", error);
60
+ core_1.logger.error("[AuthRefreshingSpanExporter] Error details:", {
61
+ message: error.message,
62
+ stack: error.stack
63
+ });
64
+ resultCallback({ code: 1, error: error });
65
+ }
66
+ }
67
+ async shutdown() {
68
+ core_1.logger.debug("[AuthRefreshingSpanExporter] Shutting down");
69
+ if (this.currentExporter) {
70
+ return this.currentExporter.shutdown();
71
+ }
72
+ }
73
+ async forceFlush() {
74
+ core_1.logger.debug("[AuthRefreshingSpanExporter] Force flushing");
75
+ if (this.currentExporter && this.currentExporter.forceFlush) {
76
+ return this.currentExporter.forceFlush();
77
+ }
78
+ }
79
+ }
80
+ exports.AuthRefreshingSpanExporter = AuthRefreshingSpanExporter;
81
+ /**
82
+ * MetricExporter that refreshes authentication before each export.
83
+ * This is necessary for long-running containers where tokens may expire.
84
+ */
85
+ class AuthRefreshingMetricExporter {
86
+ createExporter;
87
+ constructor(createExporter) {
88
+ this.createExporter = createExporter;
89
+ core_1.logger.debug("[AuthRefreshingMetricExporter] Initialized");
90
+ }
91
+ currentExporter = null;
92
+ export(metrics, resultCallback) {
93
+ core_1.logger.debug("[AuthRefreshingMetricExporter] Exporting metrics");
94
+ // Execute async operations but return void
95
+ this.doExport(metrics, resultCallback).catch(error => {
96
+ core_1.logger.error("[AuthRefreshingMetricExporter] Fatal error in export:", error);
97
+ resultCallback({ code: 1, error: error });
98
+ });
99
+ }
100
+ async doExport(metrics, resultCallback) {
101
+ try {
102
+ core_1.logger.debug("[AuthRefreshingMetricExporter] Starting authentication refresh");
103
+ const startTime = Date.now();
104
+ // Always refresh auth before export
105
+ await (0, core_1.authenticate)();
106
+ const authTime = Date.now() - startTime;
107
+ core_1.logger.debug(`[AuthRefreshingMetricExporter] Authentication completed in ${authTime}ms`);
108
+ // Log current auth status
109
+ if (core_1.settings.authorization) {
110
+ core_1.logger.debug("[AuthRefreshingMetricExporter] Authorization token is present");
111
+ }
112
+ else {
113
+ core_1.logger.warn("[AuthRefreshingMetricExporter] No authorization token after authentication!");
114
+ }
115
+ core_1.logger.debug("[AuthRefreshingMetricExporter] Creating new exporter");
116
+ // Create new exporter with fresh headers
117
+ this.currentExporter = this.createExporter();
118
+ core_1.logger.debug("[AuthRefreshingMetricExporter] New exporter created with fresh auth headers");
119
+ // Export using the fresh exporter
120
+ if (this.currentExporter && this.currentExporter.export) {
121
+ core_1.logger.debug("[AuthRefreshingMetricExporter] Calling export on fresh exporter");
122
+ this.currentExporter.export(metrics, resultCallback);
123
+ }
124
+ else {
125
+ const error = new Error('Exporter not initialized');
126
+ core_1.logger.error("[AuthRefreshingMetricExporter] Exporter not properly initialized", error);
127
+ resultCallback({ code: 1, error });
128
+ }
129
+ }
130
+ catch (error) {
131
+ core_1.logger.error("[AuthRefreshingMetricExporter] Error during authentication or export:", error);
132
+ core_1.logger.error("[AuthRefreshingMetricExporter] Error details:", {
133
+ message: error.message,
134
+ stack: error.stack
135
+ });
136
+ resultCallback({ code: 1, error: error });
137
+ }
138
+ }
139
+ async shutdown() {
140
+ core_1.logger.debug("[AuthRefreshingMetricExporter] Shutting down");
141
+ if (this.currentExporter) {
142
+ return this.currentExporter.shutdown();
143
+ }
144
+ }
145
+ async forceFlush() {
146
+ core_1.logger.debug("[AuthRefreshingMetricExporter] Force flushing");
147
+ if (this.currentExporter && this.currentExporter.forceFlush) {
148
+ return this.currentExporter.forceFlush();
149
+ }
150
+ }
151
+ }
152
+ exports.AuthRefreshingMetricExporter = AuthRefreshingMetricExporter;
153
+ /**
154
+ * Creates an OTLP Trace Exporter with the current auth headers
155
+ */
156
+ function createTraceExporter() {
157
+ const headers = {};
158
+ if (core_1.settings.authorization) {
159
+ headers["x-blaxel-authorization"] = core_1.settings.authorization;
160
+ core_1.logger.debug("[createTraceExporter] Added authorization header");
161
+ }
162
+ else {
163
+ core_1.logger.warn("[createTraceExporter] No authorization available");
164
+ }
165
+ if (core_1.settings.workspace) {
166
+ headers["x-blaxel-workspace"] = core_1.settings.workspace;
167
+ core_1.logger.debug(`[createTraceExporter] Added workspace header: ${core_1.settings.workspace}`);
168
+ }
169
+ else {
170
+ core_1.logger.warn("[createTraceExporter] No workspace available");
171
+ }
172
+ core_1.logger.debug("[createTraceExporter] Creating OTLPTraceExporter with headers:", Object.keys(headers));
173
+ return new exporter_trace_otlp_http_1.OTLPTraceExporter({
174
+ headers,
175
+ });
176
+ }
177
+ /**
178
+ * Creates an OTLP Metric Exporter with the current auth headers
179
+ */
180
+ function createMetricExporter() {
181
+ const headers = {};
182
+ if (core_1.settings.authorization) {
183
+ headers["x-blaxel-authorization"] = core_1.settings.authorization;
184
+ core_1.logger.debug("[createMetricExporter] Added authorization header");
185
+ }
186
+ else {
187
+ core_1.logger.warn("[createMetricExporter] No authorization available");
188
+ }
189
+ if (core_1.settings.workspace) {
190
+ headers["x-blaxel-workspace"] = core_1.settings.workspace;
191
+ core_1.logger.debug(`[createMetricExporter] Added workspace header: ${core_1.settings.workspace}`);
192
+ }
193
+ else {
194
+ core_1.logger.warn("[createMetricExporter] No workspace available");
195
+ }
196
+ core_1.logger.debug("[createMetricExporter] Creating OTLPMetricExporter with headers:", Object.keys(headers));
197
+ return new exporter_metrics_otlp_http_1.OTLPMetricExporter({
198
+ headers,
199
+ });
200
+ }