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

Fixing returning NaN when rate metrics change / reset counter #121

Merged
merged 13 commits into from
Dec 5, 2023
Merged
2 changes: 2 additions & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^28.1.1",
"@jest/globals": "29.7.0",
"nodemon": "^2.0.20",
"rimraf": "^4.4.1",
"supertest": "^6.3.1",
Expand All @@ -87,6 +88,7 @@
"dev:task": "ts-node -r tsconfig-paths/register -r '@hyperdx/node-opentelemetry/build/src/tracing' ./src/tasks/index.ts",
"build": "rimraf ./build && tsc && tsc-alias",
"lint": "eslint . --ext .ts",
"lintfix": "eslint . --ext .ts --fix",
"ci:lint": "yarn lint && yarn tsc --noEmit",
"ci:int": "jest --runInBand --ci --forceExit --coverage",
"dev:int": "jest --watchAll --runInBand --detectOpenHandles"
Expand Down
26 changes: 26 additions & 0 deletions packages/api/src/clickhouse/__tests__/clickhouse.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as clickhouse from '..';
import { describe, beforeEach, jest, it, expect } from '@jest/globals';
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need to import these from globals. did you hit any issues on local ?


describe('clickhouse', () => {
beforeEach(() => {
Expand Down Expand Up @@ -42,4 +43,29 @@ describe('clickhouse', () => {
expect(clickhouse.client.insert).toHaveBeenCalledTimes(2);
expect.assertions(2);
});

it('getMetricsChart', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

would be awesome to have a bit more descriptive of a test case name

jest
.spyOn(clickhouse.client, 'query')
.mockResolvedValueOnce({ json: () => Promise.resolve({}) } as any);

await clickhouse.getMetricsChart({
aggFn: clickhouse.AggFn.AvgRate,
dataType: 'Sum',
endTime: Date.now(),
granularity: clickhouse.Granularity.OneHour,
name: 'test',
q: '',
startTime: Date.now() - 1000 * 60 * 60 * 24,
teamId: 'test',
});

expect(clickhouse.client.query).toHaveBeenCalledTimes(1);
expect(clickhouse.client.query).toHaveBeenCalledWith(
expect.objectContaining({
jaggederest marked this conversation as resolved.
Show resolved Hide resolved
format: 'JSON',
query: expect.stringContaining('isNaN(rate) = 0'),
}),
);
});
});
3 changes: 3 additions & 0 deletions packages/api/src/clickhouse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,18 @@
});

export const getLogStreamTableName = (
version: number | undefined | null,

Check warning on line 170 in packages/api/src/clickhouse/index.ts

View workflow job for this annotation

GitHub Actions / lint

'version' is defined but never used
teamId: string,

Check warning on line 171 in packages/api/src/clickhouse/index.ts

View workflow job for this annotation

GitHub Actions / lint

'teamId' is defined but never used
) => `default.${TableName.LogStream}`;

export const buildTeamLogStreamWhereCondition = (
version: number | undefined | null,

Check warning on line 175 in packages/api/src/clickhouse/index.ts

View workflow job for this annotation

GitHub Actions / lint

'version' is defined but never used
teamId: string,

Check warning on line 176 in packages/api/src/clickhouse/index.ts

View workflow job for this annotation

GitHub Actions / lint

'teamId' is defined but never used
) => SqlString.raw('(1 = 1)');

export const buildLogStreamAdditionalFilters = (
version: number | undefined | null,

Check warning on line 180 in packages/api/src/clickhouse/index.ts

View workflow job for this annotation

GitHub Actions / lint

'version' is defined but never used
teamId: string,

Check warning on line 181 in packages/api/src/clickhouse/index.ts

View workflow job for this annotation

GitHub Actions / lint

'teamId' is defined but never used
) => SettingsMap.from({});

export const healthCheck = () => client.ping();
Expand Down Expand Up @@ -415,7 +415,7 @@
// TODO: support since, until
export const fetchMetricsPropertyTypeMappings =
(intervalSecs: number) =>
async (tableVersion: number | undefined, teamId: string) => {

Check warning on line 418 in packages/api/src/clickhouse/index.ts

View workflow job for this annotation

GitHub Actions / lint

'tableVersion' is defined but never used

Check warning on line 418 in packages/api/src/clickhouse/index.ts

View workflow job for this annotation

GitHub Actions / lint

'teamId' is defined but never used
const tableName = `default.${TableName.Metric}`;
const query = SqlString.format(
`
Expand Down Expand Up @@ -704,6 +704,7 @@
startTime: number; // unix in ms
teamId: string;
}) => {
await redisClient.connect();
Copy link
Contributor

Choose a reason for hiding this comment

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

no need for this. redis client should be connected when server starts

const tableName = `default.${TableName.Metric}`;
const propertyTypeMappingsModel = await buildMetricsPropertyTypeMappingsModel(
undefined, // default version
Expand Down Expand Up @@ -803,6 +804,8 @@
(
?
)
WHERE
isNaN(rate) = 0
`.trim(),
[SqlString.raw(sumMetricSource)],
);
Expand Down
Loading