Skip to content

Commit

Permalink
Detect version metadata binding, add attributes (#120)
Browse files Browse the repository at this point in the history
* Detect version metadata binding, add attributes

* Export isVersionMetadata

* Rename attribute to script_version
  • Loading branch information
DaniFoldi authored Apr 7, 2024
1 parent 89cb900 commit 051118f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
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 @@ -27,6 +27,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 @@ -49,6 +53,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
}

0 comments on commit 051118f

Please sign in to comment.