From 39d448c25111ae2f0860dddbfb622a2da6f735c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20F=C3=B6ldi?= Date: Thu, 11 Apr 2024 22:40:44 +0200 Subject: [PATCH] Improve version metadata binding detection (#125) * Improve version metadata binding detection * Check for JSRPC bindings everywhere * Add changeset --- .changeset/tasty-ghosts-tickle.md | 5 +++++ src/instrumentation/env.ts | 16 ++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 .changeset/tasty-ghosts-tickle.md diff --git a/.changeset/tasty-ghosts-tickle.md b/.changeset/tasty-ghosts-tickle.md new file mode 100644 index 0000000..04898f4 --- /dev/null +++ b/.changeset/tasty-ghosts-tickle.md @@ -0,0 +1,5 @@ +--- +"@microlabs/otel-cf-workers": patch +--- + +Correctly detect version metadata when tag is an empty string. Also, check for RPC bindings in all cases as `isVersionMetadata` was incorrectly picking up rpc bindings too when searching for version bindings. diff --git a/src/instrumentation/env.ts b/src/instrumentation/env.ts index a939981..7a5a6b4 100644 --- a/src/instrumentation/env.ts +++ b/src/instrumentation/env.ts @@ -11,28 +11,32 @@ const isJSRPC = (item?: unknown): item is Service => { } const isKVNamespace = (item?: unknown): item is KVNamespace => { - return !!(item as KVNamespace)?.getWithMetadata + return !isJSRPC(item) && !!(item as KVNamespace)?.getWithMetadata } const isQueue = (item?: unknown): item is Queue => { - return !!(item as Queue)?.sendBatch + return !isJSRPC(item) && !!(item as Queue)?.sendBatch } const isDurableObject = (item?: unknown): item is DurableObjectNamespace => { - return !!(item as DurableObjectNamespace)?.idFromName + return !isJSRPC(item) && !!(item as DurableObjectNamespace)?.idFromName } const isServiceBinding = (item?: unknown): item is Fetcher => { const binding = item as Fetcher - return !!binding.connect || !!binding.fetch || binding.queue || binding.scheduled + return (!isJSRPC(item) && !!binding.connect) || !!binding.fetch || binding.queue || binding.scheduled } export const isVersionMetadata = (item?: unknown): item is WorkerVersionMetadata => { - return !!(item as WorkerVersionMetadata).id && !!(item as WorkerVersionMetadata).tag + return ( + !isJSRPC(item) && + typeof (item as WorkerVersionMetadata).id === 'string' && + typeof (item as WorkerVersionMetadata).tag === 'string' + ) } const isAnalyticsEngineDataset = (item?: unknown): item is AnalyticsEngineDataset => { - return !!(item as AnalyticsEngineDataset)?.writeDataPoint + return !isJSRPC(item) && !!(item as AnalyticsEngineDataset)?.writeDataPoint } const instrumentEnv = (env: Record): Record => {