Skip to content

Commit

Permalink
Improve version metadata binding detection (#125)
Browse files Browse the repository at this point in the history
* Improve version metadata binding detection

* Check for JSRPC bindings everywhere

* Add changeset
  • Loading branch information
DaniFoldi authored Apr 11, 2024
1 parent f87fad8 commit 39d448c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-ghosts-tickle.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 10 additions & 6 deletions src/instrumentation/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown> => {
return !!(item as Queue<unknown>)?.sendBatch
return !isJSRPC(item) && !!(item as Queue<unknown>)?.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<string, unknown>): Record<string, unknown> => {
Expand Down

0 comments on commit 39d448c

Please sign in to comment.