1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-31 00:16:47 +01:00
unleash.unleash/src/lib/metric-events.ts
Nuno Góis 49fecb2005
chore: request origin prom metrics (#7709)
https://linear.app/unleash/issue/2-2501/adapt-origin-middleware-to-stop-logging-ui-requests-and-start

This adapts the new origin middleware to stop logging UI requests (too
noisy) and adds new Prometheus metrics.

<img width="745" alt="image"
src="https://github.com/user-attachments/assets/d0c7f51d-feb6-4ff5-b856-77661be3b5a9">

This should allow us to better analyze this data. If we see a lot of API
requests, we can dive into the logs for that instance and check the
logged data, like the user agent.

This PR adds some helper methods to make listening and emitting metric
events more strict in terms of types. I think it's a positive change
aligned with our scouting principle, but if you think it's complex and
does not belong here I'm happy with dropping it.
2024-07-31 13:52:39 +01:00

78 lines
2.2 KiB
TypeScript

import type EventEmitter from 'events';
const REQUEST_TIME = 'request_time';
const DB_TIME = 'db_time';
const FUNCTION_TIME = 'function_time';
const SCHEDULER_JOB_TIME = 'scheduler_job_time';
const FEATURES_CREATED_BY_PROCESSED = 'features_created_by_processed';
const EVENTS_CREATED_BY_PROCESSED = 'events_created_by_processed';
const FRONTEND_API_REPOSITORY_CREATED = 'frontend_api_repository_created';
const PROXY_REPOSITORY_CREATED = 'proxy_repository_created';
const PROXY_FEATURES_FOR_TOKEN_TIME = 'proxy_features_for_token_time';
const STAGE_ENTERED = 'stage-entered' as const;
const EXCEEDS_LIMIT = 'exceeds-limit' as const;
const REQUEST_ORIGIN = 'request_origin' as const;
type MetricEvent =
| typeof REQUEST_TIME
| typeof DB_TIME
| typeof FUNCTION_TIME
| typeof SCHEDULER_JOB_TIME
| typeof FEATURES_CREATED_BY_PROCESSED
| typeof EVENTS_CREATED_BY_PROCESSED
| typeof FRONTEND_API_REPOSITORY_CREATED
| typeof PROXY_REPOSITORY_CREATED
| typeof PROXY_FEATURES_FOR_TOKEN_TIME
| typeof STAGE_ENTERED
| typeof EXCEEDS_LIMIT
| typeof REQUEST_ORIGIN;
type RequestOriginEventPayload = {
type: 'UI' | 'API';
method: Request['method'];
};
type MetricEventPayloads = {
[key: string]: unknown;
[REQUEST_ORIGIN]: RequestOriginEventPayload;
};
type MetricEventPayload<T extends MetricEvent> = MetricEventPayloads[T];
type MetricEventListener<T extends MetricEvent> = (
payload: MetricEventPayload<T>,
) => void;
const emitMetricEvent = <T extends MetricEvent>(
eventBus: EventEmitter,
event: T,
payload: MetricEventPayload<T>,
) => eventBus.emit(event, payload);
const onMetricEvent = <T extends MetricEvent>(
eventBus: EventEmitter,
event: T,
listener: MetricEventListener<T>,
) => {
eventBus.on(event, listener);
};
export {
REQUEST_TIME,
DB_TIME,
SCHEDULER_JOB_TIME,
FUNCTION_TIME,
FEATURES_CREATED_BY_PROCESSED,
EVENTS_CREATED_BY_PROCESSED,
FRONTEND_API_REPOSITORY_CREATED,
PROXY_REPOSITORY_CREATED,
PROXY_FEATURES_FOR_TOKEN_TIME,
STAGE_ENTERED,
EXCEEDS_LIMIT,
REQUEST_ORIGIN,
type MetricEvent,
type MetricEventPayload,
emitMetricEvent,
onMetricEvent,
};