Skip to content

Commit

Permalink
fix: authenticate memory warning logs from runner to persist (#3507)
Browse files Browse the repository at this point in the history
Making sure the correct authorization header is passed when making a
call from runner to log memory usage warnings.

It was much easier to fix than I thought I wish I had looked into it
earlier.

To test it locally you are gonna need to modify/remove the condition `if
(memoryUsagePercentage > MEMORY_WARNING_PERCENTAGE_THRESHOLD)`
  • Loading branch information
TBonnin authored Feb 11, 2025
1 parent 8b92740 commit 06b1812
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
12 changes: 7 additions & 5 deletions packages/runner/lib/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import os from 'os';
import fs from 'fs';
import { httpFetch, logger } from './utils.js';
import { idle } from './idle.js';
import { envs } from './env.js';
import type { NangoProps } from '@nangohq/types';

const MEMORY_WARNING_PERCENTAGE_THRESHOLD = 75;

export class RunnerMonitor {
private runnerId: number;
private tracked: Map<number, NangoProps> = new Map<number, NangoProps>();
Expand Down Expand Up @@ -60,7 +59,7 @@ export class RunnerMonitor {
const rss = process.memoryUsage().rss;
const total = getTotalMemoryInBytes();
const memoryUsagePercentage = (rss / total) * 100;
if (memoryUsagePercentage > MEMORY_WARNING_PERCENTAGE_THRESHOLD) {
if (memoryUsagePercentage > envs.RUNNER_MEMORY_WARNING_THRESHOLD) {
await this.reportHighMemoryUsage(memoryUsagePercentage);
}
}, 1000);
Expand All @@ -76,17 +75,20 @@ export class RunnerMonitor {
}
}
this.lastMemoryReportDate = new Date();
for (const { environmentId, activityLogId } of this.tracked.values()) {
for (const { environmentId, activityLogId, secretKey } of this.tracked.values()) {
if (!environmentId || !activityLogId) {
continue;
}
await httpFetch({
method: 'post',
url: `${this.persistServiceUrl}/environment/${environmentId}/log`,
headers: {
Authorization: `Bearer ${secretKey}`
},
data: JSON.stringify({
activityLogId: activityLogId,
level: 'warn',
msg: `Memory usage of nango scripts is high: ${memoryUsagePercentage.toFixed(2)}% of the total available memory.`
msg: `Memory usage is high: ${memoryUsagePercentage.toFixed(2)}% of the total available memory.`
})
});
}
Expand Down
15 changes: 13 additions & 2 deletions packages/runner/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ import { getLogger, stringifyError } from '@nangohq/utils';

export const logger = getLogger('Runner');

export async function httpFetch({ method, url, data = undefined }: { method: string; url: string; data?: string }): Promise<void> {
export async function httpFetch({
method,
url,
headers = undefined,
data = undefined
}: {
method: string;
url: string;
headers?: Record<string, string>;
data?: string;
}): Promise<void> {
try {
const res = await fetch(url, {
method: method,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
'Content-Type': 'application/json',
...(headers ?? {})
},
...(data && { body: data })
});
Expand Down
1 change: 1 addition & 0 deletions packages/utils/lib/environment/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const ENVS = z.object({
IDLE_MAX_DURATION_MS: z.coerce.number().default(0),
RUNNER_NODE_ID: z.coerce.number().optional(),
RUNNER_URL: z.string().url().optional(),
RUNNER_MEMORY_WARNING_THRESHOLD: z.coerce.number().optional().default(85),

// FLEET
FLEET_TIMEOUT_PENDING_MS: z.coerce
Expand Down

0 comments on commit 06b1812

Please # to comment.