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

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
@@ -84,6 +84,7 @@ export declare interface ConsoleTransportConfig {
84
84
  pretty?: boolean;
85
85
  colors?: boolean;
86
86
  includeTimestamp?: boolean;
87
+ transformLog?: (logRecord: ReadableLogRecord) => ReadableLogRecord;
87
88
  }
88
89
 
89
90
  /**
@@ -244,6 +245,7 @@ export declare interface LogsConfig {
244
245
  transports?: LogTransport[];
245
246
  batching?: BatchConfig;
246
247
  instrumentation?: LogsInstrumentationOptions;
248
+ level?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
247
249
  }
248
250
 
249
251
  export declare interface LogsInstrumentationOptions {
@@ -354,6 +356,7 @@ export declare type ResolveConfigFn<Env = any> = (env: Env, trigger: Trigger) =>
354
356
  export declare interface ResolvedLogsConfig {
355
357
  processors: LogRecordProcessor[];
356
358
  instrumentation: LogsInstrumentationOptions;
359
+ level: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
357
360
  }
358
361
 
359
362
  export declare interface ResolvedTraceConfig extends TraceConfigBase {
@@ -509,7 +512,7 @@ declare class TraceState {
509
512
  private exportSpans;
510
513
  }
511
514
 
512
- export declare type Trigger = Request | MessageBatch | ScheduledController | DOConstructorTrigger | 'do-alarm' | ForwardableEmailMessage;
515
+ export declare type Trigger = Request | MessageBatch | ScheduledController | DOConstructorTrigger | 'do-alarm' | 'do-rpc' | ForwardableEmailMessage;
513
516
 
514
517
  export declare function withNextSpan(attrs: Attributes): void;
515
518
 
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.0";
99
+ const PACKAGE_VERSION = "2.0.2";
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";
@@ -224,6 +224,10 @@ const ATTR_CLOUDFLARE_IMAGES_REQUIRE_SIGNED_URLS = "cloudflare.images.require_si
224
224
  const ATTR_CLOUDFLARE_RATE_LIMIT_KEY = "cloudflare.rate_limit.key";
225
225
  const ATTR_CLOUDFLARE_RATE_LIMIT_ALLOWED = "cloudflare.rate_limit.allowed";
226
226
  const ATTR_CLOUDFLARE_RATE_LIMIT_SUCCESS = "cloudflare.rate_limit.success";
227
+ const ATTR_CLOUDFLARE_JSRPC_METHOD = "cloudflare.jsrpc.method";
228
+ const ATTR_RPC_SYSTEM = "rpc.system";
229
+ const ATTR_RPC_SERVICE = "rpc.service";
230
+ const ATTR_RPC_METHOD = "rpc.method";
227
231
  const DEFAULT_OTLP_HEADERS = {
228
232
  accept: "application/json",
229
233
  "content-type": "application/json",
@@ -596,7 +600,8 @@ function parseLogsConfig(supplied) {
596
600
  processors,
597
601
  instrumentation: {
598
602
  instrumentConsole: supplied.instrumentation?.instrumentConsole ?? false
599
- }
603
+ },
604
+ level: supplied.level ?? "info"
600
605
  };
601
606
  }
602
607
 
@@ -2919,6 +2924,41 @@ function instrumentSQLStorage(sql) {
2919
2924
  return wrap(sql, sqlHandler);
2920
2925
  }
2921
2926
 
2927
+ function instrumentRpcMethod(method, methodName, nsName, stubId) {
2928
+ const tracer = trace.getTracer("do_rpc_client");
2929
+ const rpcHandler = {
2930
+ apply: async (target, thisArg, argArray) => {
2931
+ const attributes = {
2932
+ [ATTR_RPC_SYSTEM]: "cloudflare_rpc",
2933
+ [ATTR_RPC_SERVICE]: nsName,
2934
+ [ATTR_RPC_METHOD]: methodName,
2935
+ [ATTR_CLOUDFLARE_JSRPC_METHOD]: methodName,
2936
+ "do.namespace": nsName,
2937
+ "do.id": stubId.toString(),
2938
+ "do.id.name": stubId.name
2939
+ };
2940
+ const options = {
2941
+ kind: SpanKind.CLIENT,
2942
+ attributes
2943
+ };
2944
+ const spanName = `RPC ${nsName}.${methodName}`;
2945
+ return tracer.startActiveSpan(spanName, options, async (span) => {
2946
+ try {
2947
+ const result = await Reflect.apply(target, thisArg, argArray);
2948
+ span.setStatus({ code: SpanStatusCode.OK });
2949
+ return result;
2950
+ } catch (error) {
2951
+ span.recordException(error);
2952
+ span.setStatus({ code: SpanStatusCode.ERROR });
2953
+ throw error;
2954
+ } finally {
2955
+ span.end();
2956
+ }
2957
+ });
2958
+ }
2959
+ };
2960
+ return wrap(method, rpcHandler);
2961
+ }
2922
2962
  function instrumentBindingStub(stub, nsName) {
2923
2963
  const stubHandler = {
2924
2964
  get(target, prop, receiver) {
@@ -2932,7 +2972,11 @@ function instrumentBindingStub(stub, nsName) {
2932
2972
  };
2933
2973
  return instrumentClientFetch(fetcher, () => ({ includeTraceContext: true }), attrs);
2934
2974
  } else {
2935
- return passthroughGet(target, prop, receiver);
2975
+ const value = passthroughGet(target, prop, receiver);
2976
+ if (typeof value === "function" && typeof prop === "string") {
2977
+ return instrumentRpcMethod(value, prop, nsName, target.id);
2978
+ }
2979
+ return value;
2936
2980
  }
2937
2981
  }
2938
2982
  };
@@ -3061,19 +3105,45 @@ function instrumentAlarmFn(alarmFn, initialiser, env, id) {
3061
3105
  };
3062
3106
  return wrap(alarmFn, alarmHandler);
3063
3107
  }
3064
- function instrumentAnyFn(fn, initialiser, env, _id) {
3065
- if (!fn) return void 0;
3108
+ function instrumentRpcHandlerMethod(fn, methodName, initialiser, env, id) {
3109
+ if (!fn) return fn;
3066
3110
  const fnHandler = {
3067
3111
  async apply(target, thisArg, argArray) {
3068
3112
  thisArg = unwrap(thisArg);
3069
- const config = initialiser(env, "do-alarm");
3113
+ const config = initialiser(env, "do-rpc");
3070
3114
  const context$1 = setConfig(config);
3071
- try {
3072
- const bound = target.bind(unwrap(thisArg));
3073
- return await context.with(context$1, () => bound.apply(thisArg, argArray), void 0);
3074
- } catch (error) {
3075
- throw error;
3076
- }
3115
+ const tracer = trace.getTracer("do_rpc_handler");
3116
+ const doName = id.name || id.toString();
3117
+ const executeRpc = async () => {
3118
+ const attributes = {
3119
+ [ATTR_RPC_SYSTEM]: "cloudflare_rpc",
3120
+ [ATTR_RPC_METHOD]: methodName,
3121
+ [ATTR_CLOUDFLARE_JSRPC_METHOD]: methodName,
3122
+ [SemanticAttributes.FAAS_TRIGGER]: "rpc",
3123
+ "do.id": id.toString(),
3124
+ "do.name": id.name
3125
+ };
3126
+ const options = {
3127
+ kind: SpanKind.SERVER,
3128
+ attributes
3129
+ };
3130
+ const spanName = `Durable Object RPC ${doName}.${methodName}`;
3131
+ return tracer.startActiveSpan(spanName, options, async (span) => {
3132
+ try {
3133
+ const bound = target.bind(thisArg);
3134
+ const result = await bound.apply(thisArg, argArray);
3135
+ span.setStatus({ code: SpanStatusCode.OK });
3136
+ return result;
3137
+ } catch (error) {
3138
+ span.recordException(error);
3139
+ span.setStatus({ code: SpanStatusCode.ERROR });
3140
+ throw error;
3141
+ } finally {
3142
+ span.end();
3143
+ }
3144
+ });
3145
+ };
3146
+ return await context.with(context$1, executeRpc);
3077
3147
  }
3078
3148
  };
3079
3149
  return wrap(fn, fnHandler);
@@ -3093,9 +3163,9 @@ function instrumentDurableObject(doObj, initialiser, env, state, classStyle) {
3093
3163
  return instrumentAlarmFn(alarmFn, initialiser, env, state.id);
3094
3164
  } else {
3095
3165
  const result = Reflect.get(target, prop);
3096
- if (typeof result === "function") {
3166
+ if (typeof result === "function" && typeof prop === "string") {
3097
3167
  result.bind(doObj);
3098
- return instrumentAnyFn(result, initialiser, env, state.id);
3168
+ return instrumentRpcHandlerMethod(result, prop, initialiser, env, state.id);
3099
3169
  }
3100
3170
  return result;
3101
3171
  }
@@ -3582,7 +3652,8 @@ class ConsoleTransport {
3582
3652
  this.options = {
3583
3653
  pretty: options.pretty ?? true,
3584
3654
  colors: options.colors ?? false,
3585
- includeTimestamp: options.includeTimestamp ?? true
3655
+ includeTimestamp: options.includeTimestamp ?? true,
3656
+ transformLog: options.transformLog ?? ((l) => l)
3586
3657
  };
3587
3658
  }
3588
3659
  export(logs, callback) {