Skip to content

Commit

Permalink
chore: add flag to disable summary metric collection
Browse files Browse the repository at this point in the history
  • Loading branch information
dhawal1248 committed May 21, 2024
1 parent 397df55 commit 5774bf6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
24 changes: 13 additions & 11 deletions src/util/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function appendPrefix(name) {
}

class Prometheus {
constructor() {
constructor(enableSummaryMetrics = true) {

Check warning on line 14 in src/util/prometheus.js

View check run for this annotation

Codecov / codecov/patch

src/util/prometheus.js#L14

Added line #L14 was not covered by tests
this.prometheusRegistry = new prometheusClient.Registry();
this.prometheusRegistry.setDefaultLabels(defaultLabels);
prometheusClient.collectDefaultMetrics({
Expand All @@ -21,7 +21,7 @@ class Prometheus {
prometheusClient.AggregatorRegistry.setRegistries(this.prometheusRegistry);
this.aggregatorRegistry = new prometheusClient.AggregatorRegistry();

this.createMetrics();
this.createMetrics(enableSummaryMetrics);

Check warning on line 24 in src/util/prometheus.js

View check run for this annotation

Codecov / codecov/patch

src/util/prometheus.js#L24

Added line #L24 was not covered by tests
}

async metricsController(ctx) {
Expand Down Expand Up @@ -192,7 +192,7 @@ class Prometheus {
}
}

createMetrics() {
createMetrics(enableSummaryMetrics) {

Check warning on line 195 in src/util/prometheus.js

View check run for this annotation

Codecov / codecov/patch

src/util/prometheus.js#L195

Added line #L195 was not covered by tests
const metrics = [
// Counters
{
Expand Down Expand Up @@ -1121,14 +1121,16 @@ class Prometheus {
metric.buckets,
);
} else if (metric.type === 'summary') {
this.newSummaryStat(
appendPrefix(metric.name),
metric.help,
metric.labelNames,
metric.percentiles,
metric.maxAge,
metric.ageBuckets,
);
if (enableSummaryMetrics) {
this.newSummaryStat(

Check warning on line 1125 in src/util/prometheus.js

View check run for this annotation

Codecov / codecov/patch

src/util/prometheus.js#L1125

Added line #L1125 was not covered by tests
appendPrefix(metric.name),
metric.help,
metric.labelNames,
metric.percentiles,
metric.maxAge,
metric.ageBuckets,
);
}
} else {
logger.error(
`Prometheus: Metric creation failed. Name: ${metric.name}. Invalid type: ${metric.type}`,
Expand Down
6 changes: 4 additions & 2 deletions src/util/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const logger = require('../logger');

const enableStats = process.env.ENABLE_STATS !== 'false';
const statsClientType = process.env.STATS_CLIENT || 'statsd';
// summary metrics are enabled by default. To disable set ENABLE_SUMMARY_METRICS='false'.
const enableSummaryMetrics = process.env.ENABLE_SUMMARY_METRICS !== 'false';

let statsClient;
function init() {
Expand All @@ -19,7 +21,7 @@ function init() {

case 'prometheus':
logger.info('setting up prometheus client');
statsClient = new prometheus.Prometheus();
statsClient = new prometheus.Prometheus(enableSummaryMetrics);

Check warning on line 24 in src/util/stats.js

View check run for this annotation

Codecov / codecov/patch

src/util/stats.js#L24

Added line #L24 was not covered by tests
break;

default:
Expand All @@ -40,7 +42,7 @@ const timing = (name, start, tags = {}) => {

// timingSummary is used to record observations for a summary metric
const timingSummary = (name, start, tags = {}) => {
if (!enableStats || !statsClient) {
if (!enableStats || !statsClient || !enableSummaryMetrics) {
return;

Check warning on line 46 in src/util/stats.js

View check run for this annotation

Codecov / codecov/patch

src/util/stats.js#L46

Added line #L46 was not covered by tests
}

Expand Down

0 comments on commit 5774bf6

Please sign in to comment.