perf: disable metrics property type mapping caching (#173)

This commit is contained in:
Warren 2024-01-02 16:46:28 -08:00 committed by GitHub
parent 3b8effea7d
commit 8d1a949124
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 44 deletions

View file

@ -0,0 +1,6 @@
---
'@hyperdx/api': patch
'@hyperdx/app': patch
---
perf: disable metrics property type mapping caching

View file

@ -438,33 +438,6 @@ export const bulkInsertTeamMetricStream = async (metrics: MetricModel[]) => {
});
};
// TODO: support since, until
export const fetchMetricsPropertyTypeMappings =
(intervalSecs: number) =>
async (tableVersion: number | undefined, teamId: string) => {
const tableName = `default.${TableName.Metric}`;
const query = SqlString.format(
`
SELECT groupUniqArrayArray(mapKeys(_string_attributes)) as strings
FROM ??
WHERE fromUnixTimestamp64Nano(_timestamp_sort_key) > now() - toIntervalSecond(?)
`,
[tableName, intervalSecs], // TODO: declare as constant
);
const ts = Date.now();
const rows = await client.query({
query,
format: 'JSON',
});
const result = await rows.json<ResponseJSON<Record<string, any[]>>>();
logger.info({
message: 'fetchMetricsPropertyTypeMappings',
query,
took: Date.now() - ts,
});
return result;
};
// since, until -> unix in ms
// TODO: what if since is like 0 or the difference is too big?
export const fetchLogsPropertyTypeMappings =
@ -578,15 +551,7 @@ export const buildLogsPropertyTypeMappingsModel = async (
export const buildMetricsPropertyTypeMappingsModel = async (
tableVersion: number | undefined,
teamId: string,
) => {
const model = new MetricsPropertyTypeMappingsModel(
tableVersion,
teamId,
fetchMetricsPropertyTypeMappings(ms('28d') / 1000),
);
await model.init();
return model;
};
) => new MetricsPropertyTypeMappingsModel(tableVersion, teamId);
// TODO: move this to PropertyTypeMappingsModel
export const doesLogsPropertyExist = (

View file

@ -58,8 +58,12 @@ export abstract class PropertyTypeMappingsModel {
return mapping;
}
private remainingTTL() {
return redisClient.pTTL(this.getCacheKey());
}
// decide if the cache is still valid
private async needsRefresh() {
protected async needsRefresh() {
return true;
}
@ -164,10 +168,6 @@ export abstract class PropertyTypeMappingsModel {
return this.currentPropertyTypeMappings.size;
}
remainingTTL() {
return redisClient.pTTL(this.getCacheKey());
}
async isAboutToExpire() {
return (await this.remainingTTL()) < ms('2h');
}
@ -183,7 +183,22 @@ export class LogsPropertyTypeMappingsModel extends PropertyTypeMappingsModel {
}
}
// FIXME: no need to cache metrics property type mappings since it's always string type
export class MetricsPropertyTypeMappingsModel extends PropertyTypeMappingsModel {
protected async needsRefresh() {
return false;
}
constructor(tableVersion: number | undefined, teamId: string) {
super(tableVersion, teamId, async () => {
// do nothing
});
}
get(property: string): 'string' | 'number' | 'bool' | undefined {
return 'string';
}
getCacheKey() {
return `metrics_property_type_mappings:${this.teamId}`;
}

View file

@ -236,9 +236,7 @@ export function mockLogsPropertyTypeMappingsModel(propertyMap: {
export function mockSpyMetricPropertyTypeMappingsModel(propertyMap: {
[property: string]: 'bool' | 'number' | 'string';
}) {
const model = new MetricsPropertyTypeMappingsModel(1, 'fake', () =>
Promise.resolve({}),
);
const model = new MetricsPropertyTypeMappingsModel(1, 'fake');
jest.spyOn(model, 'get').mockImplementation((property: string) => {
// eslint-disable-next-line security/detect-object-injection