1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-05 17:53:12 +02:00

feat: frontend api observability

This commit is contained in:
kwasniew 2024-03-06 08:39:49 +01:00
parent e7abdc6707
commit 3cb806024b
No known key found for this signature in database
GPG Key ID: 43A7CBC24C119560
3 changed files with 30 additions and 4 deletions

View File

@ -1,6 +1,7 @@
const REQUEST_TIME = 'request_time'; const REQUEST_TIME = 'request_time';
const DB_TIME = 'db_time'; const DB_TIME = 'db_time';
const SCHEDULER_JOB_TIME = 'scheduler_job_time'; const SCHEDULER_JOB_TIME = 'scheduler_job_time';
const FRONTEND_API_TIME = 'frontend_api_time';
const FEATURES_CREATED_BY_PROCESSED = 'features_created_by_processed'; const FEATURES_CREATED_BY_PROCESSED = 'features_created_by_processed';
const EVENTS_CREATED_BY_PROCESSED = 'events_created_by_processed'; const EVENTS_CREATED_BY_PROCESSED = 'events_created_by_processed';
const PROXY_REPOSITORY_CREATED = 'proxy_repository_created'; const PROXY_REPOSITORY_CREATED = 'proxy_repository_created';
@ -10,6 +11,7 @@ export {
REQUEST_TIME, REQUEST_TIME,
DB_TIME, DB_TIME,
SCHEDULER_JOB_TIME, SCHEDULER_JOB_TIME,
FRONTEND_API_TIME,
FEATURES_CREATED_BY_PROCESSED, FEATURES_CREATED_BY_PROCESSED,
EVENTS_CREATED_BY_PROCESSED, EVENTS_CREATED_BY_PROCESSED,
PROXY_REPOSITORY_CREATED, PROXY_REPOSITORY_CREATED,

View File

@ -77,6 +77,14 @@ export default class MetricsMonitor {
maxAgeSeconds: 600, maxAgeSeconds: 600,
ageBuckets: 5, ageBuckets: 5,
}); });
const frontendApiEvaluationDuration = createSummary({
name: 'frontend_api_duration_seconds',
help: 'Frontend API evaluation duration time',
labelNames: ['action'],
percentiles: [0.1, 0.5, 0.9, 0.95, 0.99],
maxAgeSeconds: 600,
ageBuckets: 5,
});
const dbDuration = createSummary({ const dbDuration = createSummary({
name: 'db_query_duration_seconds', name: 'db_query_duration_seconds',
help: 'DB query duration time', help: 'DB query duration time',
@ -401,6 +409,10 @@ export default class MetricsMonitor {
schedulerDuration.labels(jobId).observe(time); schedulerDuration.labels(jobId).observe(time);
}); });
eventBus.on(events.FRONTEND_API_TIME, ({ action, time }) => {
frontendApiEvaluationDuration.labels(action).observe(time);
});
eventBus.on(events.EVENTS_CREATED_BY_PROCESSED, ({ updated }) => { eventBus.on(events.EVENTS_CREATED_BY_PROCESSED, ({ updated }) => {
eventCreatedByMigration.inc(updated); eventCreatedByMigration.inc(updated);
}); });

View File

@ -16,7 +16,8 @@ import {
} from '../types/settings/frontend-settings'; } from '../types/settings/frontend-settings';
import { validateOrigins } from '../util'; import { validateOrigins } from '../util';
import { BadDataError, InvalidTokenError } from '../error'; import { BadDataError, InvalidTokenError } from '../error';
import { PROXY_REPOSITORY_CREATED } from '../metric-events'; import { FRONTEND_API_TIME, PROXY_REPOSITORY_CREATED } from '../metric-events';
import metricsHelper from '../util/metrics-helper';
type Config = Pick< type Config = Pick<
IUnleashConfig, IUnleashConfig,
@ -45,6 +46,8 @@ export class ProxyService {
private readonly services: Services; private readonly services: Services;
private readonly timer: Function;
/** /**
* This is intentionally a Promise becasue we want to be able to await * This is intentionally a Promise becasue we want to be able to await
* until the client (which might be being created by a different request) is ready * until the client (which might be being created by a different request) is ready
@ -60,6 +63,10 @@ export class ProxyService {
this.logger = config.getLogger('services/proxy-service.ts'); this.logger = config.getLogger('services/proxy-service.ts');
this.stores = stores; this.stores = stores;
this.services = services; this.services = services;
this.timer = (action) =>
metricsHelper.wrapTimer(config.eventBus, FRONTEND_API_TIME, {
action,
});
} }
async getProxyFeatures( async getProxyFeatures(
@ -67,11 +74,12 @@ export class ProxyService {
context: Context, context: Context,
): Promise<ProxyFeatureSchema[]> { ): Promise<ProxyFeatureSchema[]> {
const client = await this.clientForProxyToken(token); const client = await this.clientForProxyToken(token);
const stopTimer = this.timer('evaluateEnabledFeatures');
const definitions = client.getFeatureToggleDefinitions() || []; const definitions = client.getFeatureToggleDefinitions() || [];
const sessionId = context.sessionId || String(Math.random()); const sessionId = context.sessionId || String(Math.random());
const enabledFeatures = definitions
return definitions
.filter((feature) => .filter((feature) =>
client.isEnabled(feature.name, { ...context, sessionId }), client.isEnabled(feature.name, { ...context, sessionId }),
) )
@ -84,6 +92,10 @@ export class ProxyService {
}), }),
impressionData: Boolean(feature.impressionData), impressionData: Boolean(feature.impressionData),
})); }));
stopTimer();
return enabledFeatures;
} }
async registerProxyMetrics( async registerProxyMetrics(