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

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.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.3";
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";
@@ -2924,6 +2924,42 @@ function instrumentSQLStorage(sql) {
2924
2924
  return wrap(sql, sqlHandler);
2925
2925
  }
2926
2926
 
2927
+ const RPC_CONTEXT_MARKER = "__otel_rpc_ctx__";
2928
+ function isRpcContextCarrier(obj) {
2929
+ return typeof obj === "object" && obj !== null && RPC_CONTEXT_MARKER in obj && obj[RPC_CONTEXT_MARKER] === true;
2930
+ }
2931
+ function injectRpcContext(ctx = context.active()) {
2932
+ const carrier = {
2933
+ [RPC_CONTEXT_MARKER]: true,
2934
+ headers: {}
2935
+ };
2936
+ propagation.inject(ctx, carrier.headers, {
2937
+ set: (headers, key, value) => {
2938
+ headers[key] = typeof value === "string" ? value : String(value);
2939
+ }
2940
+ });
2941
+ return carrier;
2942
+ }
2943
+ function extractRpcContext(carrier) {
2944
+ return propagation.extract(context.active(), carrier.headers, {
2945
+ get(headers, key) {
2946
+ return headers[key] || void 0;
2947
+ },
2948
+ keys(headers) {
2949
+ return Object.keys(headers);
2950
+ }
2951
+ });
2952
+ }
2953
+ function extractAndRemoveRpcContext(args) {
2954
+ if (args.length > 0 && isRpcContextCarrier(args[0])) {
2955
+ const carrier = args[0];
2956
+ const extractedContext = extractRpcContext(carrier);
2957
+ const cleanedArgs = args.slice(1);
2958
+ return [extractedContext, cleanedArgs];
2959
+ }
2960
+ return [void 0, args];
2961
+ }
2962
+
2927
2963
  function instrumentRpcMethod(method, methodName, nsName, stubId) {
2928
2964
  const tracer = trace.getTracer("do_rpc_client");
2929
2965
  const rpcHandler = {
@@ -2944,7 +2980,9 @@ function instrumentRpcMethod(method, methodName, nsName, stubId) {
2944
2980
  const spanName = `RPC ${nsName}.${methodName}`;
2945
2981
  return tracer.startActiveSpan(spanName, options, async (span) => {
2946
2982
  try {
2947
- const result = await Reflect.apply(target, thisArg, argArray);
2983
+ const contextCarrier = injectRpcContext(context.active());
2984
+ const argsWithContext = [contextCarrier, ...argArray];
2985
+ const result = await Reflect.apply(target, thisArg, argsWithContext);
2948
2986
  span.setStatus({ code: SpanStatusCode.OK });
2949
2987
  return result;
2950
2988
  } catch (error) {
@@ -3111,7 +3149,9 @@ function instrumentRpcHandlerMethod(fn, methodName, initialiser, env, id) {
3111
3149
  async apply(target, thisArg, argArray) {
3112
3150
  thisArg = unwrap(thisArg);
3113
3151
  const config = initialiser(env, "do-rpc");
3114
- const context$1 = setConfig(config);
3152
+ const [extractedContext, cleanedArgs] = extractAndRemoveRpcContext(argArray);
3153
+ let context$1 = extractedContext || context.active();
3154
+ context$1 = setConfig(config, context$1);
3115
3155
  const tracer = trace.getTracer("do_rpc_handler");
3116
3156
  const doName = id.name || id.toString();
3117
3157
  const executeRpc = async () => {
@@ -3131,7 +3171,7 @@ function instrumentRpcHandlerMethod(fn, methodName, initialiser, env, id) {
3131
3171
  return tracer.startActiveSpan(spanName, options, async (span) => {
3132
3172
  try {
3133
3173
  const bound = target.bind(thisArg);
3134
- const result = await bound.apply(thisArg, argArray);
3174
+ const result = await bound.apply(thisArg, cleanedArgs);
3135
3175
  span.setStatus({ code: SpanStatusCode.OK });
3136
3176
  return result;
3137
3177
  } catch (error) {