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

feat: cache recent prometheus queries in memory #1643

Merged
merged 5 commits into from
Feb 21, 2025
Merged
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
54 changes: 46 additions & 8 deletions src/shadowbox/server/manager_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
PrometheusClient,
PrometheusMetric,
PrometheusValue,
QueryResultData,
} from '../infrastructure/prometheus_scraper';
import {DataUsageByUser, DataUsageTimeframe} from '../model/metrics';

Expand Down Expand Up @@ -111,6 +112,8 @@ export class PrometheusManagerMetrics implements ManagerMetrics {
Math.ceil(now / PROMETHEUS_RANGE_QUERY_STEP_SECONDS) * PROMETHEUS_RANGE_QUERY_STEP_SECONDS;
const start = end - timeframe.seconds;

this.prunePrometheusCache();

const [
bandwidth,
bandwidthRange,
Expand All @@ -121,34 +124,34 @@ export class PrometheusManagerMetrics implements ManagerMetrics {
dataTransferredByAccessKeyRange,
tunnelTimeByAccessKeyRange,
] = await Promise.all([
this.prometheusClient.query(
this.cachedPrometheusClient.query(
`sum(rate(shadowsocks_data_bytes_per_location{dir=~"c<p|p>t"}[${PROMETHEUS_RANGE_QUERY_STEP_SECONDS}s]))`
),
this.prometheusClient.queryRange(
this.cachedPrometheusClient.queryRange(
`sum(rate(shadowsocks_data_bytes_per_location{dir=~"c<p|p>t"}[${PROMETHEUS_RANGE_QUERY_STEP_SECONDS}s]))`,
start,
end,
`${PROMETHEUS_RANGE_QUERY_STEP_SECONDS}s`
),
this.prometheusClient.query(
this.cachedPrometheusClient.query(
`sum(increase(shadowsocks_data_bytes_per_location{dir=~"c<p|p>t"}[${timeframe.seconds}s])) by (location, asn, asorg)`
),
this.prometheusClient.query(
this.cachedPrometheusClient.query(
`sum(increase(shadowsocks_tunnel_time_seconds_per_location[${timeframe.seconds}s])) by (location, asn, asorg)`
),
this.prometheusClient.query(
this.cachedPrometheusClient.query(
`sum(increase(shadowsocks_data_bytes{dir=~"c<p|p>t"}[${timeframe.seconds}s])) by (access_key)`
),
this.prometheusClient.query(
this.cachedPrometheusClient.query(
`sum(increase(shadowsocks_tunnel_time_seconds[${timeframe.seconds}s])) by (access_key)`
),
this.prometheusClient.queryRange(
this.cachedPrometheusClient.queryRange(
`sum(increase(shadowsocks_data_bytes{dir=~"c<p|p>t"}[${PROMETHEUS_RANGE_QUERY_STEP_SECONDS}s])) by (access_key)`,
start,
end,
`${PROMETHEUS_RANGE_QUERY_STEP_SECONDS}s`
),
this.prometheusClient.queryRange(
this.cachedPrometheusClient.queryRange(
`sum(increase(shadowsocks_tunnel_time_seconds[${PROMETHEUS_RANGE_QUERY_STEP_SECONDS}s])) by (access_key)`,
start,
end,
Expand Down Expand Up @@ -231,6 +234,41 @@ export class PrometheusManagerMetrics implements ManagerMetrics {
accessKeys: Array.from(accessKeyMap.values()),
};
}

private prometheusCache = new Map<string, {timestamp: number; result: QueryResultData}>();

private get cachedPrometheusClient() {
return new Proxy(this.prometheusClient, {
get: (target, prop) => {
if (typeof target[prop] !== 'function') {
return target[prop];
}

return async (query, ...args) => {
const cacheId = `${String(prop)}: ${query} (args: ${args.join(', ')}))`;

if (this.prometheusCache.has(cacheId)) {
return this.prometheusCache.get(cacheId).result;
}

const result = await (target[prop] as Function)(query, ...args);

this.prometheusCache.set(cacheId, {timestamp: Date.now(), result});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also consider writing temp files to disk


return result;
};
},
});
}

private prunePrometheusCache() {
const now = Date.now();
for (const [key, value] of this.prometheusCache) {
if (now - value.timestamp > PROMETHEUS_RANGE_QUERY_STEP_SECONDS * 1000) {
this.prometheusCache.delete(key);
}
}
}
}

function getServerMetricsLocationEntry(
Expand Down
Loading