Skip to content

Commit

Permalink
fix: add a metric to track client registrations (#9314)
Browse files Browse the repository at this point in the history
Adding a counter to track every time a client registers with Unleash.
  • Loading branch information
ivarconr authored Feb 17, 2025
1 parent edd03d0 commit 3f730bb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/lib/features/metrics/instance/instance-service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type EventEmitter from 'events';
import { APPLICATION_CREATED, CLIENT_REGISTER } from '../../../types/events';
import type { IApplication, IApplicationOverview } from './models';
import type { IUnleashStores } from '../../../types/stores';
Expand All @@ -24,6 +25,7 @@ import { ALL_PROJECTS, parseStrictSemVer } from '../../../util';
import type { Logger } from '../../../logger';
import { findOutdatedSDKs, isOutdatedSdk } from './findOutdatedSdks';
import type { OutdatedSdksSchema } from '../../../openapi/spec/outdated-sdks-schema';
import { CLIENT_REGISTERED } from '../../../metric-events';

export default class ClientInstanceService {
apps = {};
Expand All @@ -48,6 +50,8 @@ export default class ClientInstanceService {

private flagResolver: IFlagResolver;

private eventBus: EventEmitter;

constructor(
{
clientMetricsStoreV2,
Expand All @@ -68,7 +72,8 @@ export default class ClientInstanceService {
{
getLogger,
flagResolver,
}: Pick<IUnleashConfig, 'getLogger' | 'flagResolver'>,
eventBus,
}: Pick<IUnleashConfig, 'getLogger' | 'flagResolver' | 'eventBus'>,
privateProjectChecker: IPrivateProjectChecker,
) {
this.clientMetricsStoreV2 = clientMetricsStoreV2;
Expand All @@ -77,6 +82,7 @@ export default class ClientInstanceService {
this.clientApplicationsStore = clientApplicationsStore;
this.clientInstanceStore = clientInstanceStore;
this.eventStore = eventStore;
this.eventBus = eventBus;
this.privateProjectChecker = privateProjectChecker;
this.flagResolver = flagResolver;
this.logger = getLogger(
Expand Down Expand Up @@ -105,6 +111,7 @@ export default class ClientInstanceService {
value.clientIp = clientIp;
value.createdBy = SYSTEM_USER.username!;
this.seenClients[this.clientKey(value)] = value;
this.eventBus.emit(CLIENT_REGISTERED, value);

if (value.sdkVersion && value.sdkVersion.indexOf(':') > -1) {
const [sdkName, sdkVersion] = value.sdkVersion.split(':');
Expand Down
2 changes: 2 additions & 0 deletions src/lib/metric-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const CLIENT_METRICS_NAMEPREFIX = 'client-api-nameprefix';
const CLIENT_METRICS_TAGS = 'client-api-tags';
const CLIENT_FEATURES_MEMORY = 'client_features_memory';
const CLIENT_DELTA_MEMORY = 'client_delta_memory';
const CLIENT_REGISTERED = 'client_registered';

type MetricEvent =
| typeof REQUEST_TIME
Expand Down Expand Up @@ -91,6 +92,7 @@ export {
CLIENT_METRICS_TAGS,
CLIENT_FEATURES_MEMORY,
CLIENT_DELTA_MEMORY,
CLIENT_REGISTERED,
type MetricEvent,
type MetricEventPayload,
emitMetricEvent,
Expand Down
8 changes: 8 additions & 0 deletions src/lib/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ export function registerPrometheusMetrics(
help: 'Number of times a feature flag has been used',
labelNames: ['toggle', 'active', 'appName'],
});
const clientRegistrationTotal = createCounter({
name: 'client_registration_total',
help: 'Number of times a an application have registered',
labelNames: ['appName', 'environment'],
});

dbMetrics.registerGaugeDbMetric({
name: 'feature_toggles_total',
Expand Down Expand Up @@ -807,6 +812,9 @@ export function registerPrometheusMetrics(
clientDeltaMemory.reset();
clientDeltaMemory.set(event.memory);
});
eventBus.on(events.CLIENT_REGISTERED, ({ appName, environment }) => {
clientRegistrationTotal.labels({ appName, environment }).inc();
});

events.onMetricEvent(
eventBus,
Expand Down

0 comments on commit 3f730bb

Please sign in to comment.