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

Detect version metadata binding, add attributes #120

Merged
merged 3 commits into from
Apr 7, 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/hot-dodos-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@microlabs/otel-cf-workers": patch
---

Add version metadata to attributes if found
7 changes: 7 additions & 0 deletions src/instrumentation/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const isServiceBinding = (item?: unknown): item is Fetcher => {
return !!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
}

const isAnalyticsEngineDataset = (item?: unknown): item is AnalyticsEngineDataset => {
return !!(item as AnalyticsEngineDataset)?.writeDataPoint
}
Expand All @@ -41,6 +45,9 @@ const instrumentEnv = (env: Record<string, unknown>): Record<string, unknown> =>
return instrumentDOBinding(item, String(prop))
} else if (isServiceBinding(item)) {
return instrumentServiceBinding(item, String(prop))
} else if (isVersionMetadata(item)) {
// we do not need to log accesses to the metadata
return item
} else if (isAnalyticsEngineDataset(item)) {
return instrumentAnalyticsEngineDataset(item, String(prop))
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/instrumentation/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { instrumentEnv } from './env.js'
import { exportSpans, proxyExecutionContext } from './common.js'
import { ResolvedTraceConfig } from '../types.js'
import { ReadableSpan } from '@opentelemetry/sdk-trace-base'
import { versionAttributes } from './version.js'

export type IncludeTraceContextFn = (request: Request) => boolean
export interface FetcherConfig {
Expand Down Expand Up @@ -140,6 +141,7 @@ export function executeFetchHandler(fetchFn: FetchHandler, [request, env, ctx]:
cold_start = false
Object.assign(attributes, gatherRequestAttributes(request))
Object.assign(attributes, gatherIncomingCfAttributes(request))
Object.assign(attributes, versionAttributes(env))
const options: SpanOptions = {
attributes,
kind: SpanKind.SERVER,
Expand Down
2 changes: 2 additions & 0 deletions src/instrumentation/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Initialiser, setConfig } from '../config.js'
import { exportSpans, proxyExecutionContext } from './common.js'
import { instrumentEnv } from './env.js'
import { unwrap, wrap } from '../wrap.js'
import { versionAttributes } from './version.js'

type QueueHandler = ExportedHandlerQueueHandler<unknown, unknown>
export type QueueHandlerArgs = Parameters<QueueHandler>
Expand Down Expand Up @@ -141,6 +142,7 @@ export function executeQueueHandler(queueFn: QueueHandler, [batch, env, ctx]: Qu
},
kind: SpanKind.CONSUMER,
}
Object.assign(options.attributes!, versionAttributes(env))
const promise = tracer.startActiveSpan(`queueHandler:${batch.queue}`, options, async (span) => {
const traceId = span.spanContext().traceId
api_context.active().setValue(traceIdSymbol, traceId)
Expand Down
2 changes: 2 additions & 0 deletions src/instrumentation/scheduled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Initialiser, setConfig } from '../config.js'
import { exportSpans, proxyExecutionContext } from './common.js'
import { instrumentEnv } from './env.js'
import { wrap } from '../wrap.js'
import { versionAttributes } from './version.js'

type ScheduledHandler = ExportedHandlerScheduledHandler<unknown>
export type ScheduledHandlerArgs = Parameters<ScheduledHandler>
Expand All @@ -23,6 +24,7 @@ export function executeScheduledHandler(
[SemanticAttributes.FAAS_TIME]: new Date(controller.scheduledTime).toISOString(),
}
cold_start = false
Object.assign(attributes, versionAttributes(env))
const options: SpanOptions = {
attributes,
kind: SpanKind.SERVER,
Expand Down
19 changes: 19 additions & 0 deletions src/instrumentation/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { isVersionMetadata } from './env.js'

export function versionAttributes(env: unknown): Record<string, string | undefined> {
const attributes = {} as Record<string, string | undefined>

if (typeof env === 'object' && env !== null) {
for (const [binding, data] of Object.entries(env)) {
if (isVersionMetadata(data)) {
attributes['cf.script_version.binding'] = binding
attributes['cf.script_version.id'] = data.id
attributes['cf.script_version.tag'] = data.tag
// Version metadata bindings are identical, so we can stop after the first one found
break
}
}
}

return attributes
}