Skip to content
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

Use structured logging and log validation errors in usage service #6115

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nice-birds-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'hive': patch
---

Move information about target and organization to logger's metadata in usage service
40 changes: 21 additions & 19 deletions packages/services/usage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ async function main() {
return;
}

const authenticatedRequestLogger = req.log.child({
token: maskedToken,
target: tokenInfo.target,
organization: tokenInfo.organization,
});

stopTokensDurationTimer({
status: 'success',
});
Expand All @@ -163,8 +169,8 @@ async function main() {
entityType: 'target',
})
.catch(error => {
req.log.error('Failed to check rate limit (target=%s)', tokenInfo.target);
req.log.error(error);
authenticatedRequestLogger.error('Failed to check rate limit');
authenticatedRequestLogger.error(error);
Sentry.captureException(error, {
level: 'error',
});
Expand All @@ -176,8 +182,8 @@ async function main() {
droppedReports
.labels({ targetId: tokenInfo.target, orgId: tokenInfo.organization })
.inc();
req.log.info(
'Rate limited (token=%s, target=%s, organization=%s)',
authenticatedRequestLogger.info(
'Rate limited',
maskedToken,
tokenInfo.target,
tokenInfo.organization,
Expand All @@ -189,26 +195,21 @@ async function main() {

const retentionInfo =
(await rateLimit?.getRetentionForTargetId?.(tokenInfo.target).catch(error => {
req.log.error(error);
authenticatedRequestLogger.error(error);
Sentry.captureException(error, {
level: 'error',
});
return null;
})) || null;

if (typeof retentionInfo !== 'number') {
req.log.error(
'Failed to get retention info (token=%s, target=%s, organization=%s)',
maskedToken,
tokenInfo.target,
tokenInfo.organization,
);
authenticatedRequestLogger.error('Failed to get retention info');
}

const stopTimer = collectDuration.startTimer();
try {
if (readiness() === false) {
req.log.warn('Not ready to collect report (token=%s)', maskedToken);
authenticatedRequestLogger.warn('Not ready to collect report');
stopTimer({
status: 'not_ready',
});
Expand Down Expand Up @@ -236,6 +237,11 @@ async function main() {
stopTimer({
status: 'error',
});
authenticatedRequestLogger.info(
'Report validation failed (errors=%j)',
result.errors.map(error => error.path + ': ' + error.message),
);

void res.status(400).send({
errors: result.errors,
});
Expand All @@ -251,6 +257,7 @@ async function main() {
operations: result.operations,
});
} else {
authenticatedRequestLogger.debug("Invalid 'x-api-version' header value.");
stopTimer({
status: 'error',
});
Expand All @@ -260,13 +267,8 @@ async function main() {
stopTimer({
status: 'error',
});
req.log.error(
'Failed to collect report (token=%s, target=%s, organization=%s)',
maskedToken,
tokenInfo.target,
tokenInfo.organization,
);
req.log.error(error, 'Failed to collect');
authenticatedRequestLogger.error('Failed to collect report');
authenticatedRequestLogger.error(error, 'Failed to collect');
Sentry.captureException(error, {
level: 'error',
});
Expand Down
Loading