Repository navigation
feat(worker): export worker and workflow spans over OTLP (closes #61) #352
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
Closed
rishiiicreates
wants to merge
1
commit into
oll4com:main
from
rishiiicreates:feat/worker-otlp-spans-issue-61
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import { | ||
| defaultPayloadConverter, | ||
| proxySinks, | ||
| uuid4, | ||
| workflowInfo, | ||
| type ActivityInput, | ||
| type ContinueAsNewInput, | ||
| type StartChildWorkflowExecutionInput, | ||
| type WorkflowExecuteInput, | ||
| type WorkflowInboundCallsInterceptor, | ||
| type WorkflowInterceptors, | ||
| type WorkflowOutboundCallsInterceptor | ||
| } from "@temporalio/workflow"; | ||
| import { formatTraceparent, parseTraceparent, type ParsedTraceparent } from "@space/contracts"; | ||
| import type { WorkflowTraceSinks } from "./workflow-sinks.js"; | ||
|
|
||
| const { trace } = proxySinks<WorkflowTraceSinks>(); | ||
|
|
||
| interface ActiveTraceContext { | ||
| traceId: string; | ||
| spanId: string; | ||
| parentSpanId: string | null; | ||
| } | ||
|
|
||
| export class TracingWorkflowInboundInterceptor implements WorkflowInboundCallsInterceptor { | ||
| private activeContext: ActiveTraceContext | null = null; | ||
|
|
||
| getActiveContext(): ActiveTraceContext | null { | ||
| return this.activeContext; | ||
| } | ||
|
|
||
| async execute(input: WorkflowExecuteInput, next: (input: WorkflowExecuteInput) => Promise<unknown>): Promise<unknown> { | ||
| const info = workflowInfo(); | ||
| const startedAtMs = info.runStartTime.getTime(); | ||
|
|
||
| let parentParsed: ParsedTraceparent | null = null; | ||
|
|
||
| if (input.headers.traceparent) { | ||
| try { | ||
| const raw = defaultPayloadConverter.fromPayload(input.headers.traceparent); | ||
| parentParsed = parseTraceparent(raw); | ||
| } catch {} | ||
| } | ||
|
|
||
| if (!parentParsed && typeof info.memo?.traceparent === "string") { | ||
| parentParsed = parseTraceparent(info.memo.traceparent); | ||
| } | ||
|
|
||
| if (!parentParsed && input.args[0] && typeof input.args[0] === "object") { | ||
| const arg = input.args[0] as Record<string, unknown>; | ||
| const candidate = arg.traceparent ?? (arg.turn as Record<string, unknown> | undefined)?.traceparent ?? (arg.bridge as Record<string, unknown> | undefined)?.traceparent; | ||
| if (typeof candidate === "string") { | ||
| parentParsed = parseTraceparent(candidate); | ||
| } | ||
| } | ||
|
|
||
| if (!parentParsed && input.headers.continuationTraceparent) { | ||
| try { | ||
| const raw = defaultPayloadConverter.fromPayload(input.headers.continuationTraceparent); | ||
| parentParsed = parseTraceparent(raw); | ||
| } catch {} | ||
| } | ||
|
|
||
| if (parentParsed) { | ||
| const workflowSpanId = uuid4().replace(/-/g, "").slice(0, 16); | ||
| this.activeContext = { | ||
| traceId: parentParsed.traceId, | ||
| spanId: workflowSpanId, | ||
| parentSpanId: parentParsed.parentId | ||
| }; | ||
|
|
||
| trace.recordWorkflowStart({ | ||
| traceId: this.activeContext.traceId, | ||
| spanId: this.activeContext.spanId, | ||
| parentSpanId: this.activeContext.parentSpanId, | ||
| workflowType: info.workflowType, | ||
| workflowId: info.workflowId, | ||
| runId: info.runId, | ||
| startedAtMs | ||
| }); | ||
| } | ||
|
|
||
| try { | ||
| const result = await next(input); | ||
| if (this.activeContext) { | ||
| trace.recordWorkflowSpan({ | ||
| ...this.activeContext, | ||
| workflowType: info.workflowType, | ||
| workflowId: info.workflowId, | ||
| runId: info.runId, | ||
| startedAtMs, | ||
| endedAtMs: Date.now(), | ||
| status: "COMPLETED" | ||
| }); | ||
| } | ||
| return result; | ||
| } catch (error) { | ||
| if (this.activeContext) { | ||
| trace.recordWorkflowSpan({ | ||
| ...this.activeContext, | ||
| workflowType: info.workflowType, | ||
| workflowId: info.workflowId, | ||
| runId: info.runId, | ||
| startedAtMs, | ||
| endedAtMs: Date.now(), | ||
| status: "FAILED", | ||
| errorName: error instanceof Error ? error.name : "WorkflowError" | ||
| }); | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export class TracingWorkflowOutboundInterceptor implements WorkflowOutboundCallsInterceptor { | ||
| constructor(private readonly inbound: TracingWorkflowInboundInterceptor) {} | ||
|
|
||
| scheduleActivity(input: ActivityInput, next: (input: ActivityInput) => Promise<unknown>): Promise<unknown> { | ||
| const ctx = this.inbound.getActiveContext(); | ||
| if (ctx) { | ||
| const traceparent = formatTraceparent(ctx.traceId, ctx.spanId); | ||
| const headers = { | ||
| ...input.headers, | ||
| traceparent: defaultPayloadConverter.toPayload(traceparent) | ||
| }; | ||
| return next({ ...input, headers }); | ||
| } | ||
| return next(input); | ||
| } | ||
|
|
||
| startChildWorkflowExecution( | ||
| input: StartChildWorkflowExecutionInput, | ||
| next: (input: StartChildWorkflowExecutionInput) => Promise<[Promise<string>, Promise<unknown>]> | ||
| ): Promise<[Promise<string>, Promise<unknown>]> { | ||
| const ctx = this.inbound.getActiveContext(); | ||
| if (ctx) { | ||
| const traceparent = formatTraceparent(ctx.traceId, ctx.spanId); | ||
| const headers = { | ||
| ...input.headers, | ||
| traceparent: defaultPayloadConverter.toPayload(traceparent) | ||
| }; | ||
| return next({ ...input, headers }); | ||
| } | ||
| return next(input); | ||
| } | ||
|
|
||
| continueAsNew(input: ContinueAsNewInput, next: (input: ContinueAsNewInput) => Promise<never>): Promise<never> { | ||
| const ctx = this.inbound.getActiveContext(); | ||
| if (ctx) { | ||
| const traceparent = formatTraceparent(ctx.traceId, ctx.spanId); | ||
| const headers = { | ||
| ...input.headers, | ||
| continuationTraceparent: defaultPayloadConverter.toPayload(traceparent) | ||
| }; | ||
| return next({ ...input, headers }); | ||
| } | ||
| return next(input); | ||
| } | ||
| } | ||
|
|
||
| export function interceptors(): WorkflowInterceptors { | ||
| const inbound = new TracingWorkflowInboundInterceptor(); | ||
| const outbound = new TracingWorkflowOutboundInterceptor(inbound); | ||
| return { | ||
| inbound: [inbound], | ||
| outbound: [outbound] | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Record
ContinueAsNewasCONTINUED_AS_NEW, notFAILED.continueAsNew()throws aContinueAsNewerror out of the workflow function. Thiscatchblock receives that error. It records the run asFAILEDwitherrorName: "ContinueAsNew". The exporter then marks the span as an error, withspace.outcome=FAILED. Every continued run shows a false error in the trace.WorkflowSpanPayload.statusalready allows"CONTINUED_AS_NEW", but no code sets it. Add that branch. Add a test for theexecutepath. The current tests only cover the outbound interceptor.Proposed fix
import { defaultPayloadConverter, + ContinueAsNew, proxySinks,} catch (error) { if (this.activeContext) { trace.recordWorkflowSpan({ ...this.activeContext, workflowType: info.workflowType, workflowId: info.workflowId, runId: info.runId, startedAtMs, endedAtMs: Date.now(), - status: "FAILED", - errorName: error instanceof Error ? error.name : "WorkflowError" + ...(error instanceof ContinueAsNew + ? { status: "CONTINUED_AS_NEW" as const } + : { status: "FAILED" as const, errorName: error instanceof Error ? error.name : "WorkflowError" }) }); } throw error;