From 285d0c0159849e180ee075917921c6d26b5fefd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20F=C3=B6ldi?= Date: Mon, 29 Apr 2024 14:21:38 +0200 Subject: [PATCH] Consistent instrumented span naming (#131) --- .changeset/rotten-radios-hope.md | 5 +++++ src/instrumentation/analytics-engine.ts | 2 +- src/instrumentation/cache.ts | 2 +- src/instrumentation/do-storage.ts | 2 +- src/instrumentation/do.ts | 4 ++-- src/instrumentation/fetch.ts | 8 ++++---- src/instrumentation/kv.ts | 2 +- src/instrumentation/queue.ts | 6 +++--- src/instrumentation/scheduled.ts | 2 +- 9 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 .changeset/rotten-radios-hope.md diff --git a/.changeset/rotten-radios-hope.md b/.changeset/rotten-radios-hope.md new file mode 100644 index 0000000..80d866d --- /dev/null +++ b/.changeset/rotten-radios-hope.md @@ -0,0 +1,5 @@ +--- +"@microlabs/otel-cf-workers": minor +--- + +[Breaking] Rename some instrumented spans for consistency diff --git a/src/instrumentation/analytics-engine.ts b/src/instrumentation/analytics-engine.ts index 08c245e..62feaff 100644 --- a/src/instrumentation/analytics-engine.ts +++ b/src/instrumentation/analytics-engine.ts @@ -34,7 +34,7 @@ function instrumentAEFn(fn: Function, name: string, operation: string) { kind: SpanKind.CLIENT, attributes, } - return tracer.startActiveSpan(`${name} ${operation}`, options, async (span) => { + return tracer.startActiveSpan(`Analytics Engine ${name} ${operation}`, options, async (span) => { const result = await Reflect.apply(target, thisArg, argArray) const extraAttrsFn = AEAttributes[operation] const extraAttrs = extraAttrsFn ? extraAttrsFn(argArray, result) : {} diff --git a/src/instrumentation/cache.ts b/src/instrumentation/cache.ts index f5acb4c..ae58d5e 100644 --- a/src/instrumentation/cache.ts +++ b/src/instrumentation/cache.ts @@ -19,7 +19,7 @@ function instrumentFunction(fn: T, cacheName: string, op: st 'cache.operation': op, } const options: SpanOptions = { kind: SpanKind.CLIENT, attributes } - return tracer.startActiveSpan(`cache:${cacheName}:${op}`, options, async (span) => { + return tracer.startActiveSpan(`Cache ${cacheName} ${op}`, options, async (span) => { const result = await Reflect.apply(target, thisArg, argArray) if (op === 'match') { span.setAttribute('cache.hit', !!result) diff --git a/src/instrumentation/do-storage.ts b/src/instrumentation/do-storage.ts index 21a1064..8d798b3 100644 --- a/src/instrumentation/do-storage.ts +++ b/src/instrumentation/do-storage.ts @@ -176,7 +176,7 @@ function instrumentStorageFn(fn: Function, operation: string) { operation, }, } - return tracer.startActiveSpan(`do:storage:${operation}`, options, async (span) => { + return tracer.startActiveSpan(`Durable Object Storage ${operation}`, options, async (span) => { const result = await Reflect.apply(target, thisArg, argArray) const extraAttrsFn = StorageAttributes[operation] const extraAttrs = extraAttrsFn ? extraAttrsFn(argArray, result) : {} diff --git a/src/instrumentation/do.ts b/src/instrumentation/do.ts index 59ac084..031804d 100644 --- a/src/instrumentation/do.ts +++ b/src/instrumentation/do.ts @@ -97,7 +97,7 @@ export function executeDOFetch(fetchFn: FetchFn, request: Request, id: DurableOb } const name = id.name || '' - const promise = tracer.startActiveSpan(`do.fetchHandler:${name}`, options, spanContext, async (span) => { + const promise = tracer.startActiveSpan(`Durable Object Fetch ${name}`, options, spanContext, async (span) => { try { const response: Response = await fetchFn(request) if (response.ok) { @@ -121,7 +121,7 @@ export function executeDOAlarm(alarmFn: NonNullable, id: DurableObjectI const tracer = trace.getTracer('DO alarmHandler') const name = id.name || '' - const promise = tracer.startActiveSpan(`do.alarmHandler:${name}`, async (span) => { + const promise = tracer.startActiveSpan(`Durable Object Alarm ${name}`, async (span) => { span.setAttribute(SemanticAttributes.FAAS_COLDSTART, cold_start) cold_start = false span.setAttribute('do.id', id.toString()) diff --git a/src/instrumentation/fetch.ts b/src/instrumentation/fetch.ts index 3f0f268..2641275 100644 --- a/src/instrumentation/fetch.ts +++ b/src/instrumentation/fetch.ts @@ -148,20 +148,20 @@ export function executeFetchHandler(fetchFn: FetchHandler, [request, env, ctx]: } const method = request.method.toUpperCase() - const promise = tracer.startActiveSpan(method, options, spanContext, async (span) => { + const promise = tracer.startActiveSpan(`fetchHandler ${method}`, options, spanContext, async (span) => { const readable = span as unknown as ReadableSpan try { const response: Response = await fetchFn(request, env, ctx) span.setAttributes(gatherResponseAttributes(response)) if (readable.attributes['http.route']) { - span.updateName(`${method} ${readable.attributes['http.route']}`) + span.updateName(`fetchHandler ${method} ${readable.attributes['http.route']}`) } span.end() return response } catch (error) { if (readable.attributes['http.route']) { - span.updateName(`${method} ${readable.attributes['http.route']}`) + span.updateName(`fetchHandler ${method} ${readable.attributes['http.route']}`) } span.recordException(error as Exception) span.setStatus({ code: SpanStatusCode.ERROR }) @@ -215,7 +215,7 @@ export function instrumentClientFetch( const host = new URL(request.url).host const method = request.method.toUpperCase() - const spanName = typeof attrs?.['name'] === 'string' ? attrs?.['name'] : `${method}: ${host}` + const spanName = typeof attrs?.['name'] === 'string' ? attrs?.['name'] : `fetch ${method} ${host}` const promise = tracer.startActiveSpan(spanName, options, async (span) => { const includeTraceContext = typeof config.includeTraceContext === 'function' diff --git a/src/instrumentation/kv.ts b/src/instrumentation/kv.ts index 0943737..182f78f 100644 --- a/src/instrumentation/kv.ts +++ b/src/instrumentation/kv.ts @@ -80,7 +80,7 @@ function instrumentKVFn(fn: Function, name: string, operation: string) { kind: SpanKind.CLIENT, attributes, } - return tracer.startActiveSpan(`${name} ${operation}`, options, async (span) => { + return tracer.startActiveSpan(`KV ${name} ${operation}`, options, async (span) => { const result = await Reflect.apply(target, thisArg, argArray) const extraAttrsFn = KVAttributes[operation] const extraAttrs = extraAttrsFn ? extraAttrsFn(argArray, result) : {} diff --git a/src/instrumentation/queue.ts b/src/instrumentation/queue.ts index 40d1f10..585e46c 100644 --- a/src/instrumentation/queue.ts +++ b/src/instrumentation/queue.ts @@ -143,7 +143,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 promise = tracer.startActiveSpan(`queueHandler ${batch.queue}`, options, async (span) => { const traceId = span.spanContext().traceId api_context.active().setValue(traceIdSymbol, traceId) try { @@ -191,7 +191,7 @@ function instrumentQueueSend(fn: Queue['send'], name: string): Queue['send']> = { apply: (target, thisArg, argArray) => { - return tracer.startActiveSpan(`queueSend: ${name}`, async (span) => { + return tracer.startActiveSpan(`Queues ${name} send`, async (span) => { span.setAttribute('queue.operation', 'send') await Reflect.apply(target, unwrap(thisArg), argArray) span.end() @@ -205,7 +205,7 @@ function instrumentQueueSendBatch(fn: Queue['sendBatch'], name: string) const tracer = trace.getTracer('queueSender') const handler: ProxyHandler['sendBatch']> = { apply: (target, thisArg, argArray) => { - return tracer.startActiveSpan(`queueSendBatch: ${name}`, async (span) => { + return tracer.startActiveSpan(`Queues ${name} sendBatch`, async (span) => { span.setAttribute('queue.operation', 'sendBatch') await Reflect.apply(target, unwrap(thisArg), argArray) span.end() diff --git a/src/instrumentation/scheduled.ts b/src/instrumentation/scheduled.ts index 64e53fa..0dcbe37 100644 --- a/src/instrumentation/scheduled.ts +++ b/src/instrumentation/scheduled.ts @@ -30,7 +30,7 @@ export function executeScheduledHandler( kind: SpanKind.SERVER, } - const promise = tracer.startActiveSpan('scheduledHandler', options, async (span) => { + const promise = tracer.startActiveSpan(`scheduledHandler ${controller.cron}`, options, async (span) => { const traceId = span.spanContext().traceId api_context.active().setValue(traceIdSymbol, traceId) try {