add prefix when zipping

This commit is contained in:
Yury Semikhatsky 2025-01-29 11:18:23 -08:00
parent 07b9ba9340
commit fae2800378
9 changed files with 28 additions and 18 deletions

View file

@ -96,7 +96,7 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
if (isLocal) { if (isLocal) {
const result = await this._channel.tracingStopChunk({ mode: 'entries' }); const result = await this._channel.tracingStopChunk({ mode: 'entries' });
await this._connection.localUtils()._channel.zip({ zipFile: filePath, entries: result.entries!, mode: 'write', stacksId: this._stacksId, includeSources: this._includeSources }); await this._connection.localUtils()._channel.zip({ zipFile: filePath, entries: result.entries!, mode: 'write', stacksId: this._stacksId, tracingId: this._guid, includeSources: this._includeSources });
return; return;
} }
@ -114,7 +114,7 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
await artifact.saveAs(filePath); await artifact.saveAs(filePath);
await artifact.delete(); await artifact.delete();
await this._connection.localUtils()._channel.zip({ zipFile: filePath, entries: [], mode: 'append', stacksId: this._stacksId, includeSources: this._includeSources }); await this._connection.localUtils()._channel.zip({ zipFile: filePath, entries: [], mode: 'append', stacksId: this._stacksId, tracingId: this._guid, includeSources: this._includeSources });
} }
_resetStackCounter() { _resetStackCounter() {

View file

@ -250,6 +250,7 @@ scheme.LocalUtilsZipParams = tObject({
zipFile: tString, zipFile: tString,
entries: tArray(tType('NameValue')), entries: tArray(tType('NameValue')),
stacksId: tOptional(tString), stacksId: tOptional(tString),
tracingId: tString,
mode: tEnum(['write', 'append']), mode: tEnum(['write', 'append']),
includeSources: tBoolean, includeSources: tBoolean,
}); });

View file

@ -75,18 +75,27 @@ export class LocalUtilsDispatcher extends Dispatcher<{ guid: string }, channels.
} }
}; };
for (const entry of params.entries) for (const entry of params.entries) {
addFile(entry.value, entry.name); let name = entry.name;
// Prefix network trace with the tracing(context) guid, it can be used later to deduplicate
// network traces from the same context saved for beforeAll/test/afterAll hooks. Trace
// viewer relies on .trace.trace, .trace.network and .trace.stacks files to have the same
// prefix, so we keep them consistent.
if (name === 'trace.network' || name === 'trace.trace')
name = params.tracingId + '.' + name;
addFile(entry.value, name);
}
// Add stacks and the sources. // Add stacks and the sources.
const stackSession = params.stacksId ? this._stackSessions.get(params.stacksId) : undefined; const stackSession = params.stacksId ? this._stackSessions.get(params.stacksId) : undefined;
if (stackSession?.callStacks.length) { if (stackSession?.callStacks.length) {
await stackSession.writer; await stackSession.writer;
const stacksFileName = params.tracingId + '.trace.stacks';
if (process.env.PW_LIVE_TRACE_STACKS) { if (process.env.PW_LIVE_TRACE_STACKS) {
zipFile.addFile(stackSession.file, 'trace.stacks'); zipFile.addFile(stackSession.file, stacksFileName);
} else { } else {
const buffer = Buffer.from(JSON.stringify(serializeClientSideCallMetadata(stackSession.callStacks))); const buffer = Buffer.from(JSON.stringify(serializeClientSideCallMetadata(stackSession.callStacks)));
zipFile.addBuffer(buffer, 'trace.stacks'); zipFile.addBuffer(buffer, stacksFileName);
} }
} }

View file

@ -352,10 +352,8 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
const newNetworkFile = path.join(this._state.tracesDir, this._state.traceName + `-pwnetcopy-${this._state.chunkOrdinal}.network`); const newNetworkFile = path.join(this._state.tracesDir, this._state.traceName + `-pwnetcopy-${this._state.chunkOrdinal}.network`);
const entries: NameValue[] = []; const entries: NameValue[] = [];
entries.push({ name: 'trace.trace', value: this._state.traceFile }); entries.push({ name: `trace.trace`, value: this._state.traceFile });
// Prefix network trace with the context guid, it can be used later to deduplicate entries.push({ name: `trace.network`, value: newNetworkFile });
// network traces from the same context saved for beforeAll/test/afterAll hooks.
entries.push({ name: `${this._context.guid}.trace.network`, value: newNetworkFile });
for (const sha1 of new Set([...this._state.traceSha1s, ...this._state.networkSha1s])) for (const sha1 of new Set([...this._state.traceSha1s, ...this._state.networkSha1s]))
entries.push({ name: path.join('resources', sha1), value: path.join(this._state.resourcesDir, sha1) }); entries.push({ name: path.join('resources', sha1), value: path.join(this._state.resourcesDir, sha1) });

View file

@ -342,7 +342,7 @@ async function mergeTraceFiles(fileName: string, temporaryTraceFiles: string[])
// it will contain all previous events. // it will contain all previous events.
entryName = networkTraceEntries.get(entry.fileName); entryName = networkTraceEntries.get(entry.fileName);
if (!entryName) { if (!entryName) {
entryName = i + '-trace.network'; entryName = i + '-' + entry.fileName;
networkTraceEntries.set(entry.fileName, entryName); networkTraceEntries.set(entry.fileName, entryName);
} }
} else if (entry.fileName.match(/[\d-]*trace\./)) { } else if (entry.fileName.match(/[\d-]*trace\./)) {

View file

@ -443,6 +443,7 @@ export type LocalUtilsZipParams = {
zipFile: string, zipFile: string,
entries: NameValue[], entries: NameValue[],
stacksId?: string, stacksId?: string,
tracingId: string,
mode: 'write' | 'append', mode: 'write' | 'append',
includeSources: boolean, includeSources: boolean,
}; };

View file

@ -569,6 +569,7 @@ LocalUtils:
type: array type: array
items: NameValue items: NameValue
stacksId: string? stacksId: string?
tracingId: string
mode: mode:
type: enum type: enum
literals: literals:

View file

@ -43,7 +43,7 @@ export class TraceModel {
const ordinals: string[] = []; const ordinals: string[] = [];
let hasSource = false; let hasSource = false;
for (const entryName of await this._backend.entryNames()) { for (const entryName of await this._backend.entryNames()) {
const match = entryName.match(/(.+)\.trace/); const match = entryName.match(/(.+)\.trace$/);
if (match) if (match)
ordinals.push(match[1] || ''); ordinals.push(match[1] || '');
if (entryName.includes('src@')) if (entryName.includes('src@'))

View file

@ -240,9 +240,9 @@ test('should respect tracesDir and name', async ({ browserType, server, mode },
expect(resourceNames(resources)).toEqual([ expect(resourceNames(resources)).toEqual([
'resources/XXX.css', 'resources/XXX.css',
'resources/XXX.html', 'resources/XXX.html',
'trace.network', expect.stringMatching(/trace.network$/),
'trace.stacks', expect.stringMatching(/trace.stacks$/),
'trace.trace', expect.stringMatching(/trace.trace$/),
]); ]);
} }
@ -253,9 +253,9 @@ test('should respect tracesDir and name', async ({ browserType, server, mode },
'resources/XXX.css', 'resources/XXX.css',
'resources/XXX.html', 'resources/XXX.html',
'resources/XXX.html', 'resources/XXX.html',
'trace.network', expect.stringMatching(/trace.network$/),
'trace.stacks', expect.stringMatching(/trace.stacks$/),
'trace.trace', expect.stringMatching(/trace.trace$/),
]); ]);
} }
}); });