Skip to content

Commit

Permalink
Added more instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ninadbstack committed Oct 14, 2024
1 parent 2300c24 commit ea7efc9
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions packages/client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,23 @@ export class PercyClient {
}

// Performs a GET request for an API endpoint with appropriate headers.
get(path) {
return request(`${this.apiUrl}/${path}`, {
headers: this.headers(),
method: 'GET'
get(path, meta) {
return logger.measure('client:get', meta.identifier, meta, () => {
return request(`${this.apiUrl}/${path}`, {
headers: this.headers(),
method: 'GET'
});
});
}

// Performs a POST request to a JSON API endpoint with appropriate headers.
post(path, body = {}) {
return request(`${this.apiUrl}/${path}`, {
headers: this.headers({ 'Content-Type': 'application/vnd.api+json' }),
method: 'POST',
body
post(path, body = {}, meta = {}) {
return logger.measure('client:post', meta.identifier || 'Unknown', meta, () => {
return request(`${this.apiUrl}/${path}`, {
headers: this.headers({ 'Content-Type': 'application/vnd.api+json' }),
method: 'POST',
body
});
});
}

Expand Down Expand Up @@ -189,7 +193,7 @@ export class PercyClient {
validateId('build', buildId);
let qs = all ? 'all-shards=true' : '';
this.log.debug(`Finalizing build ${buildId}...`);
return this.post(`builds/${buildId}/finalize?${qs}`);
return this.post(`builds/${buildId}/finalize?${qs}`, {}, { identifier: 'build.finalze' });
}

// Retrieves build data by id. Requires a read access token.
Expand Down Expand Up @@ -351,7 +355,7 @@ export class PercyClient {
'base64-content': encodedContent
}
}
});
}, { identifier: 'resource.post', ...meta });
}

// Uploads resources to the active build concurrently, two at a time.
Expand Down Expand Up @@ -439,14 +443,14 @@ export class PercyClient {
}
}
}
});
}, { identifier: 'snapshot.post', ...meta });
}

// Finalizes a snapshot.
async finalizeSnapshot(snapshotId, meta = {}) {
validateId('snapshot', snapshotId);
this.log.debug(`Finalizing snapshot ${snapshotId}...`, meta);
return this.post(`snapshots/${snapshotId}/finalize`);
return this.post(`snapshots/${snapshotId}/finalize`, {}, { identifier: 'snapshot.finalze', ...meta });
}

// Convenience method for creating a snapshot for the active build, uploading
Expand Down Expand Up @@ -532,18 +536,18 @@ export class PercyClient {
}
}
}
});
}, { identifier: 'comparison.post', ...meta });
}

async uploadComparisonTile(comparisonId, { index = 0, total = 1, filepath, content, sha } = {}) {
async uploadComparisonTile(comparisonId, { index = 0, total = 1, filepath, content, sha } = {}, meta = {}) {
validateId('comparison', comparisonId);
if (sha) {
return await this.verify(comparisonId, sha);
}
if (filepath && !content) content = await fs.promises.readFile(filepath);
let encodedContent = base64encode(content);

this.log.debug(`Uploading ${formatBytes(encodedContent.length)} comparison tile: ${index + 1}/${total} (${comparisonId})...`);
this.log.debug(`Uploading ${formatBytes(encodedContent.length)} comparison tile: ${index + 1}/${total} (${comparisonId})...`, meta);
this.mayBeLogUploadSize(encodedContent.length);

return this.post(`comparisons/${comparisonId}/tiles`, {
Expand All @@ -554,7 +558,7 @@ export class PercyClient {
index
}
}
});
}, { identifier: 'comparison.tile.post', ...meta });
}

// Convenience method for verifying if tile is present
Expand All @@ -579,9 +583,9 @@ export class PercyClient {
return true;
}

async verifyComparisonTile(comparisonId, sha) {
async verifyComparisonTile(comparisonId, sha, meta = {}) {
validateId('comparison', comparisonId);
this.log.debug(`Verifying comparison tile with sha: ${sha}`);
this.log.debug(`Verifying comparison tile with sha: ${sha}`, meta);

try {
return await this.post(`comparisons/${comparisonId}/tiles/verify`, {
Expand All @@ -591,7 +595,7 @@ export class PercyClient {
sha: sha
}
}
});
}, { identifier: 'comparison.tile.verify', ...meta });
} catch (error) {
this.log.error(error);
if (error.response.statusCode === 400) {
Expand All @@ -614,10 +618,10 @@ export class PercyClient {
}, this, 2);
}

async finalizeComparison(comparisonId) {
async finalizeComparison(comparisonId, meta = {}) {
validateId('comparison', comparisonId);
this.log.debug(`Finalizing comparison ${comparisonId}...`);
return this.post(`comparisons/${comparisonId}/finalize`);
return this.post(`comparisons/${comparisonId}/finalize`, {}, { identifier: 'comparison.finalize', ...meta });
}

async sendComparison(buildId, options) {
Expand All @@ -634,28 +638,28 @@ export class PercyClient {
return comparison;
}

async sendBuildEvents(buildId, body) {
async sendBuildEvents(buildId, body, meta = {}) {
validateId('build', buildId);
this.log.debug('Sending Build Events');
return this.post(`builds/${buildId}/send-events`, {
data: body
});
}, { identifier: 'build.send_events', ...meta });
}

async sendBuildLogs(body) {
this.log.debug('Sending Build Logs');
async sendBuildLogs(body, meta = {}) {
this.log.debug('Sending Build Logs', meta);
return this.post('logs', {
data: body
});
}, { identifier: 'build.send_logs', ...meta });
}

async getErrorAnalysis(errors) {
async getErrorAnalysis(errors, meta = {}) {
const errorLogs = formatLogErrors(errors);
this.log.debug('Sending error logs for analysis');
this.log.debug('Sending error logs for analysis', meta);

return this.post('suggestions/from_logs', {
data: errorLogs
});
}, { identifier: 'error.analysis.get', ...meta });
}

mayBeLogUploadSize(contentSize, meta = {}) {
Expand Down

0 comments on commit ea7efc9

Please sign in to comment.