@inference-net/otel-cf-workers 2.2.0 → 2.2.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/dist/index.d.ts CHANGED
@@ -225,9 +225,16 @@ export declare interface LocalTrace {
225
225
  readonly spans: ReadableSpan[];
226
226
  }
227
227
 
228
- export declare interface LogAttributes {
229
- [key: string]: any;
230
- }
228
+ /**
229
+ * Log attributes must be a plain object with string keys.
230
+ * This type explicitly excludes strings to prevent accidental argument swapping.
231
+ */
232
+ export declare type LogAttributes = {
233
+ [key: string]: unknown;
234
+ } & {
235
+ length?: never;
236
+ substring?: never;
237
+ };
231
238
 
232
239
  export declare type LogBody = string | Record<string, any>;
233
240
 
@@ -237,7 +244,7 @@ export declare interface Logger {
237
244
  debug(message: string, attributes?: LogAttributes): void;
238
245
  info(message: string, attributes?: LogAttributes): void;
239
246
  warn(message: string, attributes?: LogAttributes): void;
240
- error(message: string | Error, attributes?: LogAttributes): void;
247
+ error(message: string, attributes?: LogAttributes): void;
241
248
  fatal(message: string, attributes?: LogAttributes): void;
242
249
  forceFlush(): Promise<void>;
243
250
  child(attributes: LogAttributes): Logger;
@@ -561,7 +568,7 @@ export declare class WorkerLogger implements Logger {
561
568
  debug(message: string, attributes?: LogAttributes): void;
562
569
  info(message: string, attributes?: LogAttributes): void;
563
570
  warn(message: string, attributes?: LogAttributes): void;
564
- error(message: string | Error, attributes?: LogAttributes): void;
571
+ error(message: string, attributes?: LogAttributes): void;
565
572
  fatal(message: string, attributes?: LogAttributes): void;
566
573
  forceFlush(): Promise<void>;
567
574
  /**
package/dist/index.js CHANGED
@@ -96,7 +96,7 @@ function passthroughGet(target, prop, thisArg) {
96
96
  }
97
97
  }
98
98
 
99
- const PACKAGE_VERSION = "2.2.0";
99
+ const PACKAGE_VERSION = "2.2.1";
100
100
  const PACKAGE_NAME = "@inference-net/otel-cf-workers";
101
101
  const ATTR_CLOUDFLARE_COLO = "cloudflare.colo";
102
102
  const ATTR_CLOUDFLARE_RAY_ID = "cloudflare.ray_id";
@@ -1022,6 +1022,53 @@ class WorkerTracerProvider {
1022
1022
  function millisToHr(millis) {
1023
1023
  return [Math.trunc(millis / 1e3), millis % 1e3 * 1e6];
1024
1024
  }
1025
+ function flattenError(error, prefix) {
1026
+ const result = {};
1027
+ result[`${prefix}.type`] = error.name;
1028
+ result[`${prefix}.message`] = error.message;
1029
+ if (error.stack) {
1030
+ result[`${prefix}.stacktrace`] = error.stack;
1031
+ }
1032
+ if ("cause" in error && error.cause) {
1033
+ if (error.cause instanceof Error) {
1034
+ Object.assign(result, flattenError(error.cause, `${prefix}.cause`));
1035
+ } else {
1036
+ result[`${prefix}.cause`] = String(error.cause);
1037
+ }
1038
+ }
1039
+ return result;
1040
+ }
1041
+ function flattenAttributes(attrs, prefix = "") {
1042
+ const result = {};
1043
+ for (const [key, value] of Object.entries(attrs)) {
1044
+ const fullKey = prefix ? `${prefix}.${key}` : key;
1045
+ if (value === null || value === void 0) {
1046
+ continue;
1047
+ } else if (value instanceof Error) {
1048
+ Object.assign(result, flattenError(value, fullKey));
1049
+ } else if (Array.isArray(value)) {
1050
+ const hasObjects = value.some((item) => item !== null && typeof item === "object" && !Array.isArray(item));
1051
+ if (hasObjects) {
1052
+ value.forEach((item, index) => {
1053
+ if (item instanceof Error) {
1054
+ Object.assign(result, flattenError(item, `${fullKey}.${index}`));
1055
+ } else if (item !== null && typeof item === "object" && !Array.isArray(item)) {
1056
+ Object.assign(result, flattenAttributes(item, `${fullKey}.${index}`));
1057
+ } else if (item !== null && item !== void 0) {
1058
+ result[`${fullKey}.${index}`] = item;
1059
+ }
1060
+ });
1061
+ } else {
1062
+ result[fullKey] = value;
1063
+ }
1064
+ } else if (typeof value === "object") {
1065
+ Object.assign(result, flattenAttributes(value, fullKey));
1066
+ } else {
1067
+ result[fullKey] = value;
1068
+ }
1069
+ }
1070
+ return result;
1071
+ }
1025
1072
  function getHrTime(input) {
1026
1073
  const now = Date.now();
1027
1074
  if (!input) {
@@ -1055,7 +1102,8 @@ class LogRecordImpl {
1055
1102
  this.severityNumber = init.severityNumber;
1056
1103
  this.severityText = init.severityText;
1057
1104
  this.body = init.body;
1058
- this.attributes = sanitizeAttributes(init.attributes || {});
1105
+ const flattenedAttributes = flattenAttributes(init.attributes || {});
1106
+ this.attributes = sanitizeAttributes(flattenedAttributes);
1059
1107
  this.resource = init.resource;
1060
1108
  this.instrumentationScope = init.instrumentationScope || {
1061
1109
  name: "@inference-net/otel-cf-workers"
@@ -1165,28 +1213,31 @@ class WorkerLogger {
1165
1213
  }
1166
1214
  error(message, attributes) {
1167
1215
  if (!this.shouldEmit(SEVERITY_NUMBERS.ERROR)) return;
1168
- let body;
1169
- let attrs = { ...attributes };
1170
- if (message instanceof Error) {
1171
- body = message.message;
1172
- attrs = {
1173
- ...attrs,
1174
- "exception.type": message.name,
1175
- "exception.message": message.message,
1176
- "exception.stacktrace": message.stack
1177
- };
1178
- } else {
1179
- body = message;
1216
+ const exception = attributes?.["error"] instanceof Error ? attributes["error"] : void 0;
1217
+ const activeSpan = trace.getActiveSpan();
1218
+ if (activeSpan) {
1219
+ activeSpan.setStatus({ code: SpanStatusCode.ERROR, message });
1220
+ if (exception) {
1221
+ activeSpan.recordException(exception);
1222
+ }
1180
1223
  }
1181
1224
  this.emit({
1182
1225
  severityNumber: SEVERITY_NUMBERS.ERROR,
1183
1226
  severityText: "ERROR",
1184
- body,
1185
- attributes: attrs
1227
+ body: message,
1228
+ attributes
1186
1229
  });
1187
1230
  }
1188
1231
  fatal(message, attributes) {
1189
1232
  if (!this.shouldEmit(SEVERITY_NUMBERS.FATAL)) return;
1233
+ const exception = attributes?.["error"] instanceof Error ? attributes["error"] : void 0;
1234
+ const activeSpan = trace.getActiveSpan();
1235
+ if (activeSpan) {
1236
+ activeSpan.setStatus({ code: SpanStatusCode.ERROR, message });
1237
+ if (exception) {
1238
+ activeSpan.recordException(exception);
1239
+ }
1240
+ }
1190
1241
  this.emit({
1191
1242
  severityNumber: SEVERITY_NUMBERS.FATAL,
1192
1243
  severityText: "FATAL",