@inference-net/otel-cf-workers 2.0.2 → 2.0.4

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
@@ -522,7 +522,10 @@ export declare class WorkerLogger implements Logger {
522
522
  private readonly name;
523
523
  private readonly version?;
524
524
  private readonly inheritedAttributes;
525
- constructor(name: string, processors: LogRecordProcessor[], resource: Resource, version?: string, inheritedAttributes?: LogAttributes);
525
+ private readonly minSeverity;
526
+ constructor(name: string, processors: LogRecordProcessor[], resource: Resource, version?: string, inheritedAttributes?: LogAttributes, minLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal');
527
+ private levelToSeverity;
528
+ private shouldEmit;
526
529
  emit(logRecord: Partial<LogRecord>): void;
527
530
  trace(message: string, attributes?: LogAttributes): void;
528
531
  debug(message: string, attributes?: LogAttributes): void;
@@ -542,7 +545,8 @@ export declare class WorkerLoggerProvider implements LoggerProvider {
542
545
  private loggers;
543
546
  private processors;
544
547
  private resource;
545
- constructor(processors: LogRecordProcessor[], resource: Resource);
548
+ private minLevel;
549
+ constructor(processors: LogRecordProcessor[], resource: Resource, minLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal');
546
550
  getLogger(name: string, version?: string, options?: LoggerOptions): Logger;
547
551
  register(): void;
548
552
  shutdown(): Promise<void>;
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.0.2";
99
+ const PACKAGE_VERSION = "2.0.4";
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";
@@ -1080,12 +1080,33 @@ class WorkerLogger {
1080
1080
  name;
1081
1081
  version;
1082
1082
  inheritedAttributes;
1083
- constructor(name, processors, resource, version, inheritedAttributes) {
1083
+ minSeverity;
1084
+ constructor(name, processors, resource, version, inheritedAttributes, minLevel = "info") {
1084
1085
  this.name = name;
1085
1086
  this.processors = processors;
1086
1087
  this.resource = resource;
1087
1088
  this.version = version;
1088
1089
  this.inheritedAttributes = inheritedAttributes || {};
1090
+ this.minSeverity = this.levelToSeverity(minLevel);
1091
+ }
1092
+ levelToSeverity(level) {
1093
+ switch (level) {
1094
+ case "trace":
1095
+ return SEVERITY_NUMBERS.TRACE;
1096
+ case "debug":
1097
+ return SEVERITY_NUMBERS.DEBUG;
1098
+ case "info":
1099
+ return SEVERITY_NUMBERS.INFO;
1100
+ case "warn":
1101
+ return SEVERITY_NUMBERS.WARN;
1102
+ case "error":
1103
+ return SEVERITY_NUMBERS.ERROR;
1104
+ case "fatal":
1105
+ return SEVERITY_NUMBERS.FATAL;
1106
+ }
1107
+ }
1108
+ shouldEmit(severityNumber) {
1109
+ return severityNumber >= this.minSeverity;
1089
1110
  }
1090
1111
  emit(logRecord) {
1091
1112
  const mergedAttributes = {
@@ -1107,6 +1128,7 @@ class WorkerLogger {
1107
1128
  });
1108
1129
  }
1109
1130
  trace(message, attributes) {
1131
+ if (!this.shouldEmit(SEVERITY_NUMBERS.TRACE)) return;
1110
1132
  this.emit({
1111
1133
  severityNumber: SEVERITY_NUMBERS.TRACE,
1112
1134
  severityText: "TRACE",
@@ -1115,6 +1137,7 @@ class WorkerLogger {
1115
1137
  });
1116
1138
  }
1117
1139
  debug(message, attributes) {
1140
+ if (!this.shouldEmit(SEVERITY_NUMBERS.DEBUG)) return;
1118
1141
  this.emit({
1119
1142
  severityNumber: SEVERITY_NUMBERS.DEBUG,
1120
1143
  severityText: "DEBUG",
@@ -1123,6 +1146,7 @@ class WorkerLogger {
1123
1146
  });
1124
1147
  }
1125
1148
  info(message, attributes) {
1149
+ if (!this.shouldEmit(SEVERITY_NUMBERS.INFO)) return;
1126
1150
  this.emit({
1127
1151
  severityNumber: SEVERITY_NUMBERS.INFO,
1128
1152
  severityText: "INFO",
@@ -1131,6 +1155,7 @@ class WorkerLogger {
1131
1155
  });
1132
1156
  }
1133
1157
  warn(message, attributes) {
1158
+ if (!this.shouldEmit(SEVERITY_NUMBERS.WARN)) return;
1134
1159
  this.emit({
1135
1160
  severityNumber: SEVERITY_NUMBERS.WARN,
1136
1161
  severityText: "WARN",
@@ -1139,6 +1164,7 @@ class WorkerLogger {
1139
1164
  });
1140
1165
  }
1141
1166
  error(message, attributes) {
1167
+ if (!this.shouldEmit(SEVERITY_NUMBERS.ERROR)) return;
1142
1168
  let body;
1143
1169
  let attrs = { ...attributes };
1144
1170
  if (message instanceof Error) {
@@ -1160,6 +1186,7 @@ class WorkerLogger {
1160
1186
  });
1161
1187
  }
1162
1188
  fatal(message, attributes) {
1189
+ if (!this.shouldEmit(SEVERITY_NUMBERS.FATAL)) return;
1163
1190
  this.emit({
1164
1191
  severityNumber: SEVERITY_NUMBERS.FATAL,
1165
1192
  severityText: "FATAL",
@@ -1180,7 +1207,14 @@ class WorkerLogger {
1180
1207
  ...this.inheritedAttributes,
1181
1208
  ...attributes
1182
1209
  };
1183
- return new WorkerLogger(this.name, this.processors, this.resource, this.version, mergedAttributes);
1210
+ let minLevel = "info";
1211
+ if (this.minSeverity <= SEVERITY_NUMBERS.TRACE) minLevel = "trace";
1212
+ else if (this.minSeverity <= SEVERITY_NUMBERS.DEBUG) minLevel = "debug";
1213
+ else if (this.minSeverity <= SEVERITY_NUMBERS.INFO) minLevel = "info";
1214
+ else if (this.minSeverity <= SEVERITY_NUMBERS.WARN) minLevel = "warn";
1215
+ else if (this.minSeverity <= SEVERITY_NUMBERS.ERROR) minLevel = "error";
1216
+ else minLevel = "fatal";
1217
+ return new WorkerLogger(this.name, this.processors, this.resource, this.version, mergedAttributes, minLevel);
1184
1218
  }
1185
1219
  }
1186
1220
 
@@ -1195,14 +1229,16 @@ class WorkerLoggerProvider {
1195
1229
  loggers = /* @__PURE__ */ new Map();
1196
1230
  processors;
1197
1231
  resource;
1198
- constructor(processors, resource) {
1232
+ minLevel;
1233
+ constructor(processors, resource, minLevel = "info") {
1199
1234
  this.processors = processors;
1200
1235
  this.resource = resource;
1236
+ this.minLevel = minLevel;
1201
1237
  }
1202
1238
  getLogger(name, version, options) {
1203
1239
  const key = `${name}@${version || ""}:${options?.schemaUrl || ""}`;
1204
1240
  if (!this.loggers.has(key)) {
1205
- this.loggers.set(key, new WorkerLogger(name, this.processors, this.resource, version));
1241
+ this.loggers.set(key, new WorkerLogger(name, this.processors, this.resource, version, void 0, this.minLevel));
1206
1242
  }
1207
1243
  return this.loggers.get(key);
1208
1244
  }
@@ -2924,6 +2960,45 @@ function instrumentSQLStorage(sql) {
2924
2960
  return wrap(sql, sqlHandler);
2925
2961
  }
2926
2962
 
2963
+ const RPC_CONTEXT_MARKER = "__otel_rpc_ctx__";
2964
+ function isRpcContextCarrier(obj) {
2965
+ return typeof obj === "object" && obj !== null && RPC_CONTEXT_MARKER in obj && obj[RPC_CONTEXT_MARKER] === true;
2966
+ }
2967
+ function injectRpcContext(ctx = context.active(), doName) {
2968
+ const carrier = {
2969
+ [RPC_CONTEXT_MARKER]: true,
2970
+ headers: {}
2971
+ };
2972
+ if (doName) {
2973
+ carrier.doName = doName;
2974
+ }
2975
+ propagation.inject(ctx, carrier.headers, {
2976
+ set: (headers, key, value) => {
2977
+ headers[key] = typeof value === "string" ? value : String(value);
2978
+ }
2979
+ });
2980
+ return carrier;
2981
+ }
2982
+ function extractRpcContext(carrier) {
2983
+ return propagation.extract(context.active(), carrier.headers, {
2984
+ get(headers, key) {
2985
+ return headers[key] || void 0;
2986
+ },
2987
+ keys(headers) {
2988
+ return Object.keys(headers);
2989
+ }
2990
+ });
2991
+ }
2992
+ function extractAndRemoveRpcContext(args) {
2993
+ if (args.length > 0 && isRpcContextCarrier(args[0])) {
2994
+ const carrier = args[0];
2995
+ const extractedContext = extractRpcContext(carrier);
2996
+ const cleanedArgs = args.slice(1);
2997
+ return [extractedContext, cleanedArgs];
2998
+ }
2999
+ return [void 0, args];
3000
+ }
3001
+
2927
3002
  function instrumentRpcMethod(method, methodName, nsName, stubId) {
2928
3003
  const tracer = trace.getTracer("do_rpc_client");
2929
3004
  const rpcHandler = {
@@ -2944,7 +3019,9 @@ function instrumentRpcMethod(method, methodName, nsName, stubId) {
2944
3019
  const spanName = `RPC ${nsName}.${methodName}`;
2945
3020
  return tracer.startActiveSpan(spanName, options, async (span) => {
2946
3021
  try {
2947
- const result = await Reflect.apply(target, thisArg, argArray);
3022
+ const contextCarrier = injectRpcContext(context.active(), stubId.name);
3023
+ const argsWithContext = [contextCarrier, ...argArray];
3024
+ const result = await Reflect.apply(target, thisArg, argsWithContext);
2948
3025
  span.setStatus({ code: SpanStatusCode.OK });
2949
3026
  return result;
2950
3027
  } catch (error) {
@@ -2991,12 +3068,24 @@ function instrumentBindingGet(getFn, nsName) {
2991
3068
  };
2992
3069
  return wrap(getFn, getHandler);
2993
3070
  }
3071
+ function instrumentBindingGetByName(getByNameFn, nsName) {
3072
+ const getByNameHandler = {
3073
+ apply(target, thisArg, argArray) {
3074
+ const stub = Reflect.apply(target, thisArg, argArray);
3075
+ return instrumentBindingStub(stub, nsName);
3076
+ }
3077
+ };
3078
+ return wrap(getByNameFn, getByNameHandler);
3079
+ }
2994
3080
  function instrumentDOBinding(ns, nsName) {
2995
3081
  const nsHandler = {
2996
3082
  get(target, prop, receiver) {
2997
3083
  if (prop === "get") {
2998
3084
  const fn = Reflect.get(ns, prop, receiver);
2999
3085
  return instrumentBindingGet(fn, nsName);
3086
+ } else if (prop === "getByName") {
3087
+ const fn = Reflect.get(ns, prop, receiver);
3088
+ return instrumentBindingGetByName(fn, nsName);
3000
3089
  } else {
3001
3090
  return passthroughGet(target, prop, receiver);
3002
3091
  }
@@ -3111,27 +3200,41 @@ function instrumentRpcHandlerMethod(fn, methodName, initialiser, env, id) {
3111
3200
  async apply(target, thisArg, argArray) {
3112
3201
  thisArg = unwrap(thisArg);
3113
3202
  const config = initialiser(env, "do-rpc");
3114
- const context$1 = setConfig(config);
3203
+ const [extractedContext, cleanedArgs] = extractAndRemoveRpcContext(argArray);
3204
+ const carrierDoName = argArray.length > 0 && extractedContext ? argArray[0].doName : void 0;
3205
+ const doName = carrierDoName || id.name || void 0;
3206
+ console.log("[DO RPC Server]", {
3207
+ methodName,
3208
+ hasExtractedContext: !!extractedContext,
3209
+ carrierDoName,
3210
+ idName: id.name,
3211
+ finalDoName: doName,
3212
+ argArrayLength: argArray.length,
3213
+ cleanedArgsLength: cleanedArgs.length
3214
+ });
3215
+ let context$1 = extractedContext || context.active();
3216
+ context$1 = setConfig(config, context$1);
3115
3217
  const tracer = trace.getTracer("do_rpc_handler");
3116
- const doName = id.name || id.toString();
3117
3218
  const executeRpc = async () => {
3118
3219
  const attributes = {
3119
3220
  [ATTR_RPC_SYSTEM]: "cloudflare_rpc",
3120
3221
  [ATTR_RPC_METHOD]: methodName,
3121
3222
  [ATTR_CLOUDFLARE_JSRPC_METHOD]: methodName,
3122
3223
  [SemanticAttributes.FAAS_TRIGGER]: "rpc",
3123
- "do.id": id.toString(),
3124
- "do.name": id.name
3224
+ "do.id": id.toString()
3125
3225
  };
3226
+ if (doName) {
3227
+ attributes["do.name"] = doName;
3228
+ }
3126
3229
  const options = {
3127
3230
  kind: SpanKind.SERVER,
3128
3231
  attributes
3129
3232
  };
3130
- const spanName = `Durable Object RPC ${doName}.${methodName}`;
3233
+ const spanName = doName ? `${doName}.${methodName}` : `${attributes["do.id"]}.${methodName}`;
3131
3234
  return tracer.startActiveSpan(spanName, options, async (span) => {
3132
3235
  try {
3133
3236
  const bound = target.bind(thisArg);
3134
- const result = await bound.apply(thisArg, argArray);
3237
+ const result = await bound.apply(thisArg, cleanedArgs);
3135
3238
  span.setStatus({ code: SpanStatusCode.OK });
3136
3239
  return result;
3137
3240
  } catch (error) {
@@ -3351,7 +3454,7 @@ function init(config, serviceConfig, propagator) {
3351
3454
  traceProvider.register();
3352
3455
  }
3353
3456
  if (config.logs && config.logs.processors.length > 0) {
3354
- const logsProvider = new WorkerLoggerProvider(config.logs.processors, resource);
3457
+ const logsProvider = new WorkerLoggerProvider(config.logs.processors, resource, config.logs.level);
3355
3458
  logsProvider.register();
3356
3459
  if (config.logs.instrumentation.instrumentConsole) {
3357
3460
  import('./console-Cy5QdByD.js').then(({ instrumentConsole }) => {
@@ -3659,7 +3762,7 @@ class ConsoleTransport {
3659
3762
  export(logs, callback) {
3660
3763
  try {
3661
3764
  for (const log of logs) {
3662
- this.printLog(log);
3765
+ this.printLog(this.options.transformLog ? this.options.transformLog(log) : log);
3663
3766
  }
3664
3767
  callback({ code: ExportResultCode.SUCCESS });
3665
3768
  } catch (error) {