feat: add API performance observability reporting
This commit is contained in:
1 parent
871a5fe78a
commit
88de2148ae
14 files changed
+2082
-1
No files matched your search
@@ -0,0 +1,80 @@
|
||||
import {
|
||||
buildApiRequestPerformanceEvent,
|
||||
createApiPerformanceRequestContext,
|
||||
readBoolean,
|
||||
recordMongooseQueryPerformance,
|
||||
resolveApiPerformanceConfig,
|
||||
runWithApiPerformanceContext,
|
||||
} from './performance-observer';
|
||||
|
||||
describe('performance observer config', () => {
|
||||
it('keeps observability disabled by default', () => {
|
||||
expect(resolveApiPerformanceConfig({}).enabled).toBe(false);
|
||||
expect(resolveApiPerformanceConfig({}).localEventsEnabled).toBe(false);
|
||||
});
|
||||
|
||||
it('reads common boolean values', () => {
|
||||
expect(readBoolean(undefined, true)).toBe(true);
|
||||
expect(readBoolean('false', true)).toBe(false);
|
||||
expect(readBoolean('enabled', false)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('API performance request context', () => {
|
||||
const originalEnv = process.env;
|
||||
|
||||
beforeEach(() => {
|
||||
process.env = {
|
||||
...originalEnv,
|
||||
API_PERF_OBSERVABILITY_ENABLED: 'true',
|
||||
API_PERF_LOCAL_EVENTS_ENABLED: 'false',
|
||||
API_PERF_NEW_RELIC_QUERY_EVENTS_ENABLED: 'false',
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
process.env = originalEnv;
|
||||
});
|
||||
|
||||
it('aggregates Mongoose document-return metrics into the request event', () => {
|
||||
const context = createApiPerformanceRequestContext({
|
||||
method: 'GET',
|
||||
route: '/communities/:communityId/internal-stats/matches',
|
||||
path: '/communities/abc/internal-stats/matches',
|
||||
});
|
||||
|
||||
const event = runWithApiPerformanceContext(context, () => {
|
||||
recordMongooseQueryPerformance({
|
||||
operation: 'find',
|
||||
modelName: 'matchStats',
|
||||
collectionName: 'match_stats',
|
||||
durationMs: 12,
|
||||
docsReturned: 300,
|
||||
hydratedDocsReturned: 300,
|
||||
lean: false,
|
||||
});
|
||||
recordMongooseQueryPerformance({
|
||||
operation: 'find',
|
||||
modelName: 'matchStats',
|
||||
collectionName: 'match_stats',
|
||||
durationMs: 3,
|
||||
docsReturned: 20,
|
||||
hydratedDocsReturned: 0,
|
||||
lean: true,
|
||||
});
|
||||
|
||||
return buildApiRequestPerformanceEvent({
|
||||
context,
|
||||
statusCode: 200,
|
||||
durationMs: 50,
|
||||
isError: false,
|
||||
});
|
||||
});
|
||||
|
||||
expect(event.queryCount).toBe(2);
|
||||
expect(event.mongooseDurationMs).toBe(15);
|
||||
expect(event.docsReturned).toBe(320);
|
||||
expect(event.hydratedDocsReturned).toBe(300);
|
||||
expect(event.leanQueryCount).toBe(1);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user