Skip to content

Commit

Permalink
feat: add prom metrics (#8980)
Browse files Browse the repository at this point in the history
This PR adds prometheus metrics that allows us to see whether or not
tags and namePrefix is used at all in our cloud offering.
  • Loading branch information
FredrikOseberg authored Dec 16, 2024
1 parent b1cced7 commit 39ca516
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { OpenApiService } from '../../services/openapi-service';
import { NONE } from '../../types/permissions';
import { createResponseSchema } from '../../openapi/util/create-response-schema';
import type { ClientFeaturesQuerySchema } from '../../openapi/spec/client-features-query-schema';
import type EventEmitter from 'events';
import {
clientFeatureSchema,
type ClientFeatureSchema,
Expand All @@ -33,6 +34,10 @@ import {
} from '../../openapi/spec/client-features-schema';
import type ConfigurationRevisionService from '../feature-toggle/configuration-revision-service';
import type { ClientFeatureToggleService } from './client-feature-toggle-service';
import {
CLIENT_METRICS_NAMEPREFIX,
CLIENT_METRICS_TAGS,
} from '../../internals';

const version = 2;

Expand Down Expand Up @@ -62,6 +67,8 @@ export default class FeatureController extends Controller {

private flagResolver: IFlagResolver;

private eventBus: EventEmitter;

private featuresAndSegments: (
query: IFeatureToggleQuery,
etag: string,
Expand Down Expand Up @@ -92,6 +99,7 @@ export default class FeatureController extends Controller {
this.configurationRevisionService = configurationRevisionService;
this.featureToggleService = featureToggleService;
this.flagResolver = config.flagResolver;
this.eventBus = config.eventBus;
this.logger = config.getLogger('client-api/feature.js');

this.route({
Expand Down Expand Up @@ -210,6 +218,14 @@ export default class FeatureController extends Controller {
return {};
}

if (namePrefix) {
this.eventBus.emit(CLIENT_METRICS_NAMEPREFIX);
}

if (tag) {
this.eventBus.emit(CLIENT_METRICS_TAGS);
}

const tagQuery = this.paramToArray(tag);
const projectQuery = this.paramToArray(project);
const query = await querySchema.validateAsync({
Expand Down
9 changes: 8 additions & 1 deletion src/lib/metric-events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type EventEmitter from 'events';
import { CLIENT_METRICS } from './internals';

const REQUEST_TIME = 'request_time';
const DB_TIME = 'db_time';
Expand All @@ -14,6 +15,8 @@ const USER_LOGIN = 'user-login' as const;
const EXCEEDS_LIMIT = 'exceeds-limit' as const;
const REQUEST_ORIGIN = 'request_origin' as const;
const ADDON_EVENTS_HANDLED = 'addon-event-handled' as const;
const CLIENT_METRICS_NAMEPREFIX = 'client-api-nameprefix';
const CLIENT_METRICS_TAGS = 'client-api-tags';

type MetricEvent =
| typeof REQUEST_TIME
Expand All @@ -28,7 +31,9 @@ type MetricEvent =
| typeof STAGE_ENTERED
| typeof USER_LOGIN
| typeof EXCEEDS_LIMIT
| typeof REQUEST_ORIGIN;
| typeof REQUEST_ORIGIN
| typeof CLIENT_METRICS_NAMEPREFIX
| typeof CLIENT_METRICS_TAGS;

type RequestOriginEventPayload = {
type: 'UI' | 'API';
Expand Down Expand Up @@ -76,6 +81,8 @@ export {
EXCEEDS_LIMIT,
REQUEST_ORIGIN,
ADDON_EVENTS_HANDLED,
CLIENT_METRICS_NAMEPREFIX,
CLIENT_METRICS_TAGS,
type MetricEvent,
type MetricEventPayload,
emitMetricEvent,
Expand Down
18 changes: 18 additions & 0 deletions src/lib/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,16 @@ export function registerPrometheusMetrics(
})
.set(config.rateLimiting.callSignalEndpointMaxPerSecond * 60);

const namePrefixUsed = createCounter({
name: 'nameprefix_count',
help: 'Count of nameprefix usage in client api',
});

const tagsUsed = createCounter({
name: 'tags_count',
help: 'Count of tags usage in client api',
});

const featureCreatedByMigration = createCounter({
name: 'feature_created_by_migration_count',
help: 'Feature createdBy migration count',
Expand Down Expand Up @@ -734,6 +744,14 @@ export function registerPrometheusMetrics(
mapFeaturesForClientDuration.observe(duration);
});

eventBus.on(events.CLIENT_METRICS_NAMEPREFIX, () => {
namePrefixUsed.inc();
});

eventBus.on(events.CLIENT_METRICS_TAGS, () => {
tagsUsed.inc();
});

events.onMetricEvent(
eventBus,
events.REQUEST_ORIGIN,
Expand Down

0 comments on commit 39ca516

Please sign in to comment.