Repository navigation
feat: opt-in deterministic trace and job span IDs #219
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,6 +72,36 @@ GitHub Actions summary for easy access: | |
|
|
||
|  | ||
|
|
||
| ## Deterministic Trace IDs | ||
|
|
||
| By default, trace and span IDs are random. Set `DETERMINISTIC_TRACE_IDS: true` | ||
| to derive them from the workflow run instead, so another tool (for example, a | ||
| step that exports test results) can add spans to the same trace without talking | ||
| to this action: | ||
|
|
||
| ```yaml | ||
| with: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| DETERMINISTIC_TRACE_IDS: true | ||
| ``` | ||
|
|
||
| Each ID is the leading bytes of the hex SHA-256 of a key: | ||
|
|
||
| | Span | Key | Length | | ||
| | ----------------------------------- | --------------------------------------- | -------- | | ||
| | Trace ID | `<owner>/<repo>:<run_id>:<run_attempt>` | 16 bytes | | ||
| | Workflow (root) span | `workflow:<run_id>:<run_attempt>` | 8 bytes | | ||
| | `<job>` span | `job:<job_id>` | 8 bytes | | ||
| | `<job> with time of waiting runner` | `job:<job_id>:with-waiting` | 8 bytes | | ||
| | `waiting runner for <job>` | `job:<job_id>:waiting` | 8 bytes | | ||
|
|
||
| Step spans keep random IDs. For example, to parent a span under a job in shell: | ||
|
|
||
| ```bash | ||
| trace_id=$(printf '%s' "$GITHUB_REPOSITORY:$RUN_ID:$RUN_ATTEMPT" | sha256sum | cut -c1-32) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example does not hash the same key the implementation uses.
Use |
||
| parent_span_id=$(printf '%s' "job:$JOB_ID" | sha256sum | cut -c1-16) | ||
| ``` | ||
|
|
||
| ## How it works | ||
|
|
||
| This action creates metrics and traces of GitHub Actions workflows and sends | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -314,6 +314,32 @@ describe('should export expected spans', () => { | |||||||
| expect(traceId).toBe(rootSpan.spanContext().traceId) | ||||||||
| }) | ||||||||
|
|
||||||||
| test('should keep random IDs unless deterministic IDs are enabled', async () => { | ||||||||
| const traceId = await createTrace(workflowRunResults) | ||||||||
| expect(traceId).toMatch(/^[0-9a-f]{32}$/) | ||||||||
| expect(traceId).not.toBe('9b622c3f42e4e04dcc07e34075e07913') | ||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nits] It should be wrote :)
Suggested change
|
||||||||
| }) | ||||||||
|
|
||||||||
| test('should derive trace and job span IDs from the run and job IDs', async () => { | ||||||||
| settings.deterministicTraceIds = true | ||||||||
| const traceId = await createTrace(workflowRunResults) | ||||||||
| await forceFlush() | ||||||||
| settings.deterministicTraceIds = false | ||||||||
|
|
||||||||
| // Hard-coded: these are the documented contract other tools compute against. | ||||||||
| expect(traceId).toBe('9b622c3f42e4e04dcc07e34075e07913') | ||||||||
| const spans = exporter.getFinishedSpans() | ||||||||
| const spanId = (name: string): string => | ||||||||
| findSpanByName(spans, name).spanContext().spanId | ||||||||
| expect(spanId(workflowRun.name)).toBe('a20d690c4ccb758d') | ||||||||
| expect(spanId('job1')).toBe('d45e31f76cac183d') | ||||||||
| expect(spanId('job1 with time of waiting runner')).toBe('df18a27dfc12d482') | ||||||||
| expect(spanId('waiting runner for job1')).toBe('4a0b581f5a1cf0af') | ||||||||
| expect(new Set(spans.map(span => span.spanContext().spanId)).size).toBe( | ||||||||
| spans.length | ||||||||
| ) | ||||||||
| }) | ||||||||
|
|
||||||||
| test('should return empty string when trace feature is disabled', async () => { | ||||||||
| settings.FeatureFlagTrace = false | ||||||||
| const traceId = await createTrace(workflowRunResults) | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { createHash } from 'node:crypto' | ||
| import { IdGenerator, RandomIdGenerator } from '@opentelemetry/sdk-trace-base' | ||
|
|
||
| export type SpanIds = { traceId?: string; spanId?: string } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [imo] |
||
|
|
||
| const hashId = (key: string, bytes: number): string => | ||
| createHash('sha256') | ||
| .update(key) | ||
| .digest('hex') | ||
| .slice(0, bytes * 2) | ||
|
|
||
| // Deterministic so other tools can attach spans to a workflow run's trace | ||
| // without talking to this action. See "Deterministic trace IDs" in README.md. | ||
| export const workflowTraceId = ( | ||
| repository: string, | ||
| runId: number, | ||
| runAttempt: number | ||
| ): string => hashId(`${repository}:${runId}:${runAttempt}`, 16) | ||
|
|
||
| export const workflowSpanId = (runId: number, runAttempt: number): string => | ||
| hashId(`workflow:${runId}:${runAttempt}`, 8) | ||
|
|
||
| export const jobSpanId = (jobId: number): string => hashId(`job:${jobId}`, 8) | ||
|
|
||
| export const jobWithWaitingSpanId = (jobId: number): string => | ||
| hashId(`job:${jobId}:with-waiting`, 8) | ||
|
|
||
| export const jobWaitingSpanId = (jobId: number): string => | ||
| hashId(`job:${jobId}:waiting`, 8) | ||
|
|
||
| // The tracer asks the provider's IdGenerator for ids without saying which span | ||
| // they are for, so createSpan presets them immediately around startSpan. | ||
| class PresetIdGenerator implements IdGenerator { | ||
| private readonly random = new RandomIdGenerator() | ||
| private preset: SpanIds = {} | ||
|
|
||
| withIds<T>(ids: SpanIds, start: () => T): T { | ||
| this.preset = ids | ||
| try { | ||
| return start() | ||
| } finally { | ||
| this.preset = {} | ||
| } | ||
| } | ||
|
|
||
| generateTraceId(): string { | ||
| return this.preset.traceId ?? this.random.generateTraceId() | ||
| } | ||
|
|
||
| generateSpanId(): string { | ||
| return this.preset.spanId ?? this.random.generateSpanId() | ||
| } | ||
| } | ||
|
|
||
| export const idGenerator = new PresetIdGenerator() | ||
Uh oh!
There was an error while loading. Please reload this page.