Skip to content

Commit 478a20b

Browse files
committed
Split resource and span attributes
1 parent 5abfd2e commit 478a20b

File tree

7 files changed

+58
-59
lines changed

7 files changed

+58
-59
lines changed

apps/webapp/app/v3/otlpExporter.server.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,7 @@ function convertLogsToCreateableEvents(resourceLog: ResourceLogs): Array<Creatab
286286
function convertSpansToCreateableEvents(resourceSpan: ResourceSpans): Array<CreatableEvent> {
287287
const resourceAttributes = resourceSpan.resource?.attributes ?? [];
288288

289-
const resourceProperties = extractEventProperties(
290-
resourceAttributes,
291-
SemanticInternalAttributes.METADATA
292-
);
289+
const resourceProperties = extractEventProperties(resourceAttributes);
293290

294291
return resourceSpan.scopeSpans.flatMap((scopeSpan) => {
295292
return scopeSpan.spans

packages/cli-v3/src/dev/taskRunProcessPool.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class TaskRunProcessPool {
3232
serverWorker: ServerBackgroundWorker,
3333
machineResources: MachinePresetResources,
3434
env?: Record<string, string>
35-
): Promise<TaskRunProcess> {
35+
): Promise<{ taskRunProcess: TaskRunProcess; isReused: boolean }> {
3636
// Try to reuse an existing process if enabled
3737
if (this.options.enableProcessReuse) {
3838
const reusableProcess = this.findReusableProcess();
@@ -44,7 +44,7 @@ export class TaskRunProcessPool {
4444

4545
this.availableProcesses = this.availableProcesses.filter((p) => p !== reusableProcess);
4646
this.busyProcesses.add(reusableProcess);
47-
return reusableProcess;
47+
return { taskRunProcess: reusableProcess, isReused: true };
4848
} else {
4949
logger.debug("[TaskRunProcessPool] No reusable process found", {
5050
availableCount: this.availableProcesses.length,
@@ -71,7 +71,7 @@ export class TaskRunProcessPool {
7171
}).initialize();
7272

7373
this.busyProcesses.add(newProcess);
74-
return newProcess;
74+
return { taskRunProcess: newProcess, isReused: false };
7575
}
7676

7777
async returnProcess(process: TaskRunProcess): Promise<void> {

packages/cli-v3/src/entryPoints/dev-run-controller.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ export class DevRunController {
608608
this.snapshotPoller.start();
609609

610610
// Get process from pool instead of creating new one
611-
this.taskRunProcess = await this.opts.taskRunProcessPool.getProcess(
611+
const { taskRunProcess, isReused } = await this.opts.taskRunProcessPool.getProcess(
612612
this.opts.worker.manifest,
613613
{
614614
id: "unmanaged",
@@ -623,26 +623,31 @@ export class DevRunController {
623623
}
624624
);
625625

626+
this.taskRunProcess = taskRunProcess;
627+
626628
// Update the process environment for this specific run
627629
// Note: We may need to enhance TaskRunProcess to support updating env vars
628630
logger.debug("executing task run process from pool", {
629631
attemptNumber: execution.attempt.number,
630632
runId: execution.run.id,
631633
});
632634

633-
const completion = await this.taskRunProcess.execute({
634-
payload: {
635-
execution,
636-
traceContext: execution.run.traceContext ?? {},
637-
metrics,
638-
},
639-
messageId: run.friendlyId,
640-
env: {
641-
...sanitizeEnvVars(envVars ?? {}),
642-
...sanitizeEnvVars(this.opts.worker.params.env),
643-
TRIGGER_PROJECT_REF: execution.project.ref,
635+
const completion = await this.taskRunProcess.execute(
636+
{
637+
payload: {
638+
execution,
639+
traceContext: execution.run.traceContext ?? {},
640+
metrics,
641+
},
642+
messageId: run.friendlyId,
643+
env: {
644+
...sanitizeEnvVars(envVars ?? {}),
645+
...sanitizeEnvVars(this.opts.worker.params.env),
646+
TRIGGER_PROJECT_REF: execution.project.ref,
647+
},
644648
},
645-
});
649+
isReused
650+
);
646651

647652
logger.debug("Completed run", completion);
648653

packages/core/src/v3/otel/tracingSDK.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class AsyncResourceDetector implements DetectorSync {
5757
});
5858
}
5959

60+
get isResolved() {
61+
return this._resolved;
62+
}
63+
6064
detect(_config?: ResourceDetectionConfig): Resource {
6165
return new Resource({}, this._promise);
6266
}
@@ -70,6 +74,8 @@ class AsyncResourceDetector implements DetectorSync {
7074
return;
7175
}
7276

77+
console.log("resolving async resource detector", attributes);
78+
7379
this._resolved = true;
7480
this._resolver(attributes);
7581
}
@@ -114,7 +120,7 @@ export class TracingSDK {
114120
: {};
115121

116122
const commonResources = detectResourcesSync({
117-
detectors: [processDetectorSync],
123+
detectors: [this.asyncResourceDetector, processDetectorSync],
118124
})
119125
.merge(
120126
new Resource({

packages/core/src/v3/taskContext/index.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ export class TaskContextAPI {
4747
return {};
4848
}
4949

50+
get resourceAttributes(): Attributes {
51+
if (this.ctx) {
52+
return {
53+
[SemanticInternalAttributes.ENVIRONMENT_ID]: this.ctx.environment.id,
54+
[SemanticInternalAttributes.ENVIRONMENT_TYPE]: this.ctx.environment.type,
55+
[SemanticInternalAttributes.ORGANIZATION_ID]: this.ctx.organization.id,
56+
[SemanticInternalAttributes.PROJECT_ID]: this.ctx.project.id,
57+
[SemanticInternalAttributes.PROJECT_REF]: this.ctx.project.ref,
58+
[SemanticInternalAttributes.PROJECT_NAME]: this.ctx.project.name,
59+
[SemanticInternalAttributes.ORGANIZATION_SLUG]: this.ctx.organization.slug,
60+
[SemanticInternalAttributes.ORGANIZATION_NAME]: this.ctx.organization.name,
61+
[SemanticInternalAttributes.MACHINE_PRESET_NAME]: this.ctx.machine?.name,
62+
[SemanticInternalAttributes.MACHINE_PRESET_CPU]: this.ctx.machine?.cpu,
63+
[SemanticInternalAttributes.MACHINE_PRESET_MEMORY]: this.ctx.machine?.memory,
64+
[SemanticInternalAttributes.MACHINE_PRESET_CENTS_PER_MS]: this.ctx.machine?.centsPerMs,
65+
};
66+
}
67+
68+
return {};
69+
}
70+
5071
get workerAttributes(): Attributes {
5172
if (this.worker) {
5273
return {
@@ -68,22 +89,10 @@ export class TaskContextAPI {
6889
[SemanticInternalAttributes.TASK_EXPORT_NAME]: this.ctx.task.exportName,
6990
[SemanticInternalAttributes.QUEUE_NAME]: this.ctx.queue.name,
7091
[SemanticInternalAttributes.QUEUE_ID]: this.ctx.queue.id,
71-
[SemanticInternalAttributes.ENVIRONMENT_ID]: this.ctx.environment.id,
72-
[SemanticInternalAttributes.ENVIRONMENT_TYPE]: this.ctx.environment.type,
73-
[SemanticInternalAttributes.ORGANIZATION_ID]: this.ctx.organization.id,
74-
[SemanticInternalAttributes.PROJECT_ID]: this.ctx.project.id,
75-
[SemanticInternalAttributes.PROJECT_REF]: this.ctx.project.ref,
76-
[SemanticInternalAttributes.PROJECT_NAME]: this.ctx.project.name,
7792
[SemanticInternalAttributes.RUN_ID]: this.ctx.run.id,
7893
[SemanticInternalAttributes.RUN_IS_TEST]: this.ctx.run.isTest,
79-
[SemanticInternalAttributes.ORGANIZATION_SLUG]: this.ctx.organization.slug,
80-
[SemanticInternalAttributes.ORGANIZATION_NAME]: this.ctx.organization.name,
8194
[SemanticInternalAttributes.BATCH_ID]: this.ctx.batch?.id,
8295
[SemanticInternalAttributes.IDEMPOTENCY_KEY]: this.ctx.run.idempotencyKey,
83-
[SemanticInternalAttributes.MACHINE_PRESET_NAME]: this.ctx.machine?.name,
84-
[SemanticInternalAttributes.MACHINE_PRESET_CPU]: this.ctx.machine?.cpu,
85-
[SemanticInternalAttributes.MACHINE_PRESET_MEMORY]: this.ctx.machine?.memory,
86-
[SemanticInternalAttributes.MACHINE_PRESET_CENTS_PER_MS]: this.ctx.machine?.centsPerMs,
8796
};
8897
}
8998

packages/core/src/v3/taskContext/otelProcessors.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,7 @@ export class TaskContextSpanProcessor implements SpanProcessor {
1919
onStart(span: Span, parentContext: Context): void {
2020
if (taskContext.ctx) {
2121
span.setAttributes(
22-
flattenAttributes(
23-
{
24-
...taskContext.attributes,
25-
[SemanticInternalAttributes.ATTEMPT_ID]: taskContext.ctx.attempt.id,
26-
[SemanticInternalAttributes.ATTEMPT_NUMBER]: taskContext.ctx.attempt.number,
27-
},
28-
SemanticInternalAttributes.METADATA
29-
)
22+
flattenAttributes(taskContext.attributes, SemanticInternalAttributes.METADATA)
3023
);
3124
}
3225

@@ -76,13 +69,7 @@ function createPartialSpan(tracer: Tracer, span: Span, parentContext: Context) {
7669

7770
if (taskContext.ctx) {
7871
partialSpan.setAttributes(
79-
flattenAttributes(
80-
{
81-
[SemanticInternalAttributes.ATTEMPT_ID]: taskContext.ctx.attempt.id,
82-
[SemanticInternalAttributes.ATTEMPT_NUMBER]: taskContext.ctx.attempt.number,
83-
},
84-
SemanticInternalAttributes.METADATA
85-
)
72+
flattenAttributes(taskContext.attributes, SemanticInternalAttributes.METADATA)
8673
);
8774
}
8875

@@ -108,14 +95,7 @@ export class TaskContextLogProcessor implements LogRecordProcessor {
10895
// Adds in the context attributes to the log record
10996
if (taskContext.ctx) {
11097
logRecord.setAttributes(
111-
flattenAttributes(
112-
{
113-
...taskContext.attributes,
114-
[SemanticInternalAttributes.ATTEMPT_ID]: taskContext.ctx.attempt.id,
115-
[SemanticInternalAttributes.ATTEMPT_NUMBER]: taskContext.ctx.attempt.number,
116-
},
117-
SemanticInternalAttributes.METADATA
118-
)
98+
flattenAttributes(taskContext.attributes, SemanticInternalAttributes.METADATA)
11999
);
120100
}
121101

packages/core/src/v3/workers/taskExecutor.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ export class TaskExecutor {
112112
runMetadata.enterWithMetadata(execution.run.metadata);
113113
}
114114

115-
// this._tracingSDK.asyncResourceDetector.resolveWithAttributes({
116-
// ...taskContext.attributes,
117-
// });
115+
if (!this._tracingSDK.asyncResourceDetector.isResolved) {
116+
this._tracingSDK.asyncResourceDetector.resolveWithAttributes({
117+
...taskContext.resourceAttributes,
118+
});
119+
}
118120

119121
const result = await this._tracer.startActiveSpan(
120122
attemptMessage,

0 commit comments

Comments
 (0)