Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve version metadata binding detection #125

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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