fix(tracing): avoid trace file name collisions (#24191)
We have been optionally adding `-<number>` in multiple places, and these might collide in various circumstances, for example: two contexts at the same time, one of them has the second trace chunk. References #23387.
This commit is contained in:
parent
53bf1995db
commit
53feeaf270
|
@ -166,17 +166,13 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
|
||||||
throw new Error('Cannot start a trace chunk while stopping');
|
throw new Error('Cannot start a trace chunk while stopping');
|
||||||
|
|
||||||
const state = this._state;
|
const state = this._state;
|
||||||
const suffix = state.chunkOrdinal ? `-${state.chunkOrdinal}` : ``;
|
|
||||||
state.chunkOrdinal++;
|
|
||||||
state.traceFile = {
|
|
||||||
file: path.join(state.tracesDir, `${state.traceName}${suffix}.trace`),
|
|
||||||
buffer: [],
|
|
||||||
};
|
|
||||||
state.recording = true;
|
state.recording = true;
|
||||||
state.callIds.clear();
|
state.callIds.clear();
|
||||||
|
|
||||||
if (options.name && options.name !== this._state.traceName)
|
if (options.name && options.name !== state.traceName)
|
||||||
this._changeTraceName(this._state, options.name);
|
this._changeTraceName(state, options.name);
|
||||||
|
else
|
||||||
|
this._allocateNewTraceFile(state);
|
||||||
|
|
||||||
this._appendTraceOperation(async () => {
|
this._appendTraceOperation(async () => {
|
||||||
await mkdirIfNeeded(state.traceFile.file);
|
await mkdirIfNeeded(state.traceFile.file);
|
||||||
|
@ -213,6 +209,15 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
|
||||||
page.setScreencastOptions(null);
|
page.setScreencastOptions(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _allocateNewTraceFile(state: RecordingState) {
|
||||||
|
const suffix = state.chunkOrdinal ? `-chunk${state.chunkOrdinal}` : ``;
|
||||||
|
state.chunkOrdinal++;
|
||||||
|
state.traceFile = {
|
||||||
|
file: path.join(state.tracesDir, `${state.traceName}${suffix}.trace`),
|
||||||
|
buffer: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private async _changeTraceName(state: RecordingState, name: string) {
|
private async _changeTraceName(state: RecordingState, name: string) {
|
||||||
await this._appendTraceOperation(async () => {
|
await this._appendTraceOperation(async () => {
|
||||||
await flushTraceFile(state.traceFile);
|
await flushTraceFile(state.traceFile);
|
||||||
|
@ -220,10 +225,8 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
|
||||||
|
|
||||||
const oldNetworkFile = state.networkFile;
|
const oldNetworkFile = state.networkFile;
|
||||||
state.traceName = name;
|
state.traceName = name;
|
||||||
state.traceFile = {
|
state.chunkOrdinal = 0; // Reset ordinal for the new name.
|
||||||
file: path.join(state.tracesDir, name + '.trace'),
|
this._allocateNewTraceFile(state);
|
||||||
buffer: [],
|
|
||||||
};
|
|
||||||
state.networkFile = {
|
state.networkFile = {
|
||||||
file: path.join(state.tracesDir, name + '.network'),
|
file: path.join(state.tracesDir, name + '.network'),
|
||||||
buffer: [],
|
buffer: [],
|
||||||
|
|
|
@ -698,9 +698,9 @@ class ArtifactsRecorder {
|
||||||
private async _startTraceChunkOnContextCreation(tracing: Tracing) {
|
private async _startTraceChunkOnContextCreation(tracing: Tracing) {
|
||||||
if (this._captureTrace) {
|
if (this._captureTrace) {
|
||||||
const title = [path.relative(this._testInfo.project.testDir, this._testInfo.file) + ':' + this._testInfo.line, ...this._testInfo.titlePath.slice(1)].join(' › ');
|
const title = [path.relative(this._testInfo.project.testDir, this._testInfo.file) + ':' + this._testInfo.line, ...this._testInfo.titlePath.slice(1)].join(' › ');
|
||||||
const ordinalSuffix = this._traceOrdinal ? `-${this._traceOrdinal}` : '';
|
const ordinalSuffix = this._traceOrdinal ? `-context${this._traceOrdinal}` : '';
|
||||||
++this._traceOrdinal;
|
++this._traceOrdinal;
|
||||||
const retrySuffix = this._testInfo.retry ? `-${this._testInfo.retry}` : '';
|
const retrySuffix = this._testInfo.retry ? `-retry${this._testInfo.retry}` : '';
|
||||||
const name = `${this._testInfo.testId}${retrySuffix}${ordinalSuffix}`;
|
const name = `${this._testInfo.testId}${retrySuffix}${ordinalSuffix}`;
|
||||||
if (!(tracing as any)[kTracingStarted]) {
|
if (!(tracing as any)[kTracingStarted]) {
|
||||||
await tracing.start({ ...this._traceOptions, title, name });
|
await tracing.start({ ...this._traceOptions, title, name });
|
||||||
|
|
Loading…
Reference in New Issue