@blaxel/telemetry 0.2.23-dev.168 → 0.2.23-dev.170

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/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  import { blaxelTelemetry } from "./telemetry";
2
+ export { setJsonLogger } from "./json_logger";
2
3
  export { blaxelTelemetry };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.blaxelTelemetry = void 0;
3
+ exports.blaxelTelemetry = exports.setJsonLogger = void 0;
4
4
  const core_1 = require("@blaxel/core");
5
5
  const json_logger_1 = require("./json_logger");
6
6
  const legacy_logger_1 = require("./legacy_logger");
@@ -13,3 +13,5 @@ if (core_1.settings.loggerType === "http") {
13
13
  else if (core_1.settings.loggerType === "json") {
14
14
  (0, json_logger_1.setJsonLogger)();
15
15
  }
16
+ var json_logger_2 = require("./json_logger");
17
+ Object.defineProperty(exports, "setJsonLogger", { enumerable: true, get: function () { return json_logger_2.setJsonLogger; } });
@@ -43,16 +43,83 @@ const taskIndex = core_1.env.BL_TASK_KEY || 'TASK_INDEX';
43
43
  const taskPrefix = core_1.env.BL_TASK_PREFIX || '';
44
44
  const executionKey = core_1.env.BL_EXECUTION_KEY || 'BL_EXECUTION_ID';
45
45
  const executionPrefix = core_1.env.BL_EXECUTION_PREFIX || '';
46
+ // Validate environment variables to prevent issues
47
+ function validateEnvVar(value, defaultValue, varName) {
48
+ if (!value || value.trim() === '') {
49
+ exports.originalLogger.warn(`Warning: ${varName} environment variable is empty, using default: ${defaultValue}`);
50
+ return defaultValue;
51
+ }
52
+ return value;
53
+ }
54
+ const validatedLabelsName = validateEnvVar(labelsName, 'labels', 'BL_LOGGER_LABELS');
55
+ // Enhanced error serialization to capture all properties
56
+ function serializeError(error) {
57
+ const serialized = {
58
+ message: error.message,
59
+ name: error.name,
60
+ stack: error.stack
61
+ };
62
+ // Capture any additional properties on the error object
63
+ for (const key of Object.keys(error)) {
64
+ if (!(key in serialized)) {
65
+ try {
66
+ const value = error[key];
67
+ // Avoid circular references by limiting depth
68
+ serialized[key] = typeof value === 'object' ? (0, core_1.stringify)(value, 2) : value;
69
+ }
70
+ catch {
71
+ serialized[key] = '[Unserializable]';
72
+ }
73
+ }
74
+ }
75
+ return serialized;
76
+ }
77
+ // Enhanced stringify function with better error handling
78
+ function enhancedStringify(obj, maxDepth = 2) {
79
+ if (obj instanceof Error) {
80
+ return JSON.stringify(serializeError(obj));
81
+ }
82
+ // Handle circular references by using a simple set to track seen objects
83
+ const seen = new WeakSet();
84
+ const stringifyWithCircularCheck = (value, depth = 0) => {
85
+ if (value === null || value === undefined)
86
+ return value;
87
+ if (typeof value !== 'object')
88
+ return value;
89
+ if (seen.has(value)) {
90
+ return '[Circular Reference]';
91
+ }
92
+ if (depth >= maxDepth) {
93
+ return Array.isArray(value) ? '[Array]' : '[Object]';
94
+ }
95
+ seen.add(value);
96
+ if (Array.isArray(value)) {
97
+ return value.map(item => stringifyWithCircularCheck(item, depth + 1));
98
+ }
99
+ const result = {};
100
+ for (const [key, val] of Object.entries(value)) {
101
+ result[key] = stringifyWithCircularCheck(val, depth + 1);
102
+ }
103
+ return result;
104
+ };
105
+ try {
106
+ const processed = stringifyWithCircularCheck(obj);
107
+ return JSON.stringify(processed);
108
+ }
109
+ catch {
110
+ return (0, core_1.stringify)(obj, maxDepth);
111
+ }
112
+ }
46
113
  // Format a log message with appropriate color and prefix
47
114
  function formatLogMessage(severity, message, args) {
48
- const messageStr = typeof message === "string" ? message : (0, core_1.stringify)(message, 2);
49
- const argsStr = args.map(arg => typeof arg === "string" ? arg : (0, core_1.stringify)(arg, 2)).join(" ");
115
+ const messageStr = typeof message === "string" ? message : enhancedStringify(message, 2);
116
+ const argsStr = args.map(arg => typeof arg === "string" ? arg : enhancedStringify(arg, 2)).join(" ");
50
117
  const msg = `${messageStr}${argsStr ? " " + argsStr : ""}`;
51
118
  const logEntry = {
52
119
  message: msg,
53
120
  severity
54
121
  };
55
- logEntry[labelsName] = {};
122
+ logEntry[validatedLabelsName] = {};
56
123
  const currentSpan = api_1.trace.getActiveSpan();
57
124
  if (currentSpan) {
58
125
  const { traceId, spanId } = currentSpan.spanContext();
@@ -61,11 +128,28 @@ function formatLogMessage(severity, message, args) {
61
128
  }
62
129
  const taskId = core_1.env[taskIndex] || null;
63
130
  if (taskId) {
64
- logEntry[labelsName]['blaxel-task'] = `${taskPrefix}${taskId}`;
131
+ logEntry[validatedLabelsName]['blaxel-task'] = `${taskPrefix}${taskId}`;
65
132
  }
66
133
  const executionId = core_1.env[executionKey] || null;
67
134
  if (executionId) {
68
- logEntry[labelsName]['blaxel-execution'] = `${executionPrefix}${executionId.split('-').pop()}`;
135
+ logEntry[validatedLabelsName]['blaxel-execution'] = `${executionPrefix}${executionId.split('-').pop()}`;
136
+ }
137
+ try {
138
+ return JSON.stringify(logEntry);
139
+ }
140
+ catch (error) {
141
+ // Fallback for serialization errors
142
+ const fallbackEntry = {
143
+ message: `JSON serialization failed: ${msg}`,
144
+ severity,
145
+ error: error instanceof Error ? error.message : String(error)
146
+ };
147
+ try {
148
+ return JSON.stringify(fallbackEntry);
149
+ }
150
+ catch {
151
+ // Last resort fallback
152
+ return `{"message":"${severity}: ${msg.replace(/"/g, '\\"')}","severity":"${severity}","error":"Failed to serialize log entry"}`;
153
+ }
69
154
  }
70
- return JSON.stringify(logEntry);
71
155
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaxel/telemetry",
3
- "version": "0.2.23-dev.168",
3
+ "version": "0.2.23-dev.170",
4
4
  "description": "Blaxel SDK for TypeScript",
5
5
  "license": "MIT",
6
6
  "author": "Blaxel, INC (https://blaxel.ai)",
@@ -71,7 +71,7 @@
71
71
  "@opentelemetry/sdk-trace-base": "^2.0.0",
72
72
  "@opentelemetry/sdk-trace-node": "^2.0.0",
73
73
  "ai": "^4.3.13",
74
- "@blaxel/core": "0.2.23-dev.168"
74
+ "@blaxel/core": "0.2.23-dev.170"
75
75
  },
76
76
  "devDependencies": {
77
77
  "@eslint/js": "^9.26.0",