Skip to content
Permalink
Browse files

Add metrics for pipeline and overview

  • Loading branch information...
wpjunior committed Aug 8, 2019
1 parent 145c43b commit e35ff5a9dac6335029ba33630e518965c1d3f360
Showing with 76 additions and 18 deletions.
  1. +55 −18 lib/domain/Metric.js
  2. +4 −0 lib/http/routers/FunctionsRouter.js
  3. +17 −0 test/unit/domain/Metric.test.js
@@ -2,29 +2,42 @@ const prometheusClient = require('prom-client');

prometheusClient.collectDefaultMetrics({ prefix: 'backstage_functions_' });

const buckets = [
0.001,
0.002,
0.005,
0.010,
0.020,
0.050,
0.100,
0.200,
0.500,
1,
2,
5,
10,
15,
20,
];

const functionRunHistogram = new prometheusClient.Histogram({
name: 'backstage_functions_function_run_duration_seconds',
help: 'How many time spent to run a function in seconds',
buckets: [
0.001,
0.002,
0.005,
0.010,
0.020,
0.050,
0.100,
0.200,
0.500,
1,
2,
5,
10,
15,
20,
],
buckets,
labelNames: ['namespace', 'id'],
});

const functionOverviewRunHistogram = new prometheusClient.Histogram({
name: 'backstage_functions_overview_run_duration_seconds',
help: 'How many time spent to run all functions in seconds',
buckets,
});

const functionPipelineHistogram = new prometheusClient.Histogram({
name: 'backstage_functions_pipeline_run_duration_seconds',
help: 'How many time spent to run a function pipeline in seconds',
buckets,
});

function normalizeStatusCode(status) {
if (status < 200) {
@@ -39,12 +52,24 @@ function normalizeStatusCode(status) {
return '5xx';
}

const functionOverviewRunCounter = new prometheusClient.Counter({
name: 'backstage_functions_overview_run_total',
help: 'What the portion of executions by status codes',
labelNames: ['status'],
});

const functionRunCounter = new prometheusClient.Counter({
name: 'backstage_functions_function_run_total',
help: 'What is the status code of a function',
labelNames: ['namespace', 'id', 'status'],
});

const functionPipelineCounter = new prometheusClient.Counter({
name: 'backstage_functions_pipeline_run_total',
help: 'What is the status code of pipelines',
labelNames: ['status'],
});

const redisLeaksCounter = new prometheusClient.Counter({
name: 'backstage_functions_redis_leaks_total',
help: 'How many redis leaks in backstage functions',
@@ -58,13 +83,25 @@ class Metric {

observeFunctionRun({ namespace, id, status }) {
const spent = (Date.now() - this.start) / 1000;
const normalizedStatusCode = normalizeStatusCode(status);

functionOverviewRunHistogram.observe(spent);
functionOverviewRunCounter.labels(normalizedStatusCode).inc();

functionRunHistogram.labels(namespace, id).observe(spent);
functionRunCounter.labels(namespace, id, normalizeStatusCode(status)).inc();
functionRunCounter.labels(namespace, id, normalizedStatusCode).inc();

return spent;
}

observePipelineRun(status) {
const spent = (Date.now() - this.start) / 1000;
const normalizedStatusCode = normalizeStatusCode(status);

functionPipelineHistogram.observe(spent);
functionPipelineCounter.labels(normalizedStatusCode).inc();
}

static observeRedisLeak() {
redisLeaksCounter.inc();
}
@@ -274,6 +274,8 @@ router.put('/pipeline', bodyParser.json({ limit: bodyParserLimit }), async (req,
return { namespace, id };
});

const metric = new Metric('pipeline-run');

try {
const codes = await memoryStorage.getCodesByCache(steps, {
preCache: (code) => {
@@ -296,9 +298,11 @@ router.put('/pipeline', bodyParser.json({ limit: bodyParserLimit }), async (req,

res.set(result.headers);
res.status(result.status);
metric.observePipelineRun(result.status);
res.json(result.body);
} catch (err) {
const status = err.statusCode || 500;
metric.observePipelineRun(status);
res.status(status).json({ error: err.message });
}
});
@@ -15,6 +15,23 @@ describe('Metric', () => {
const data = prometheusClient.register.metrics();
expect(data).to.be.include('backstage_functions_function_run_total{namespace="xpto",id="blah",status="4xx"} 1');
expect(data).to.be.include('backstage_functions_function_run_duration_seconds_bucket{le="0.05",namespace="xpto",id="blah"} 1');

expect(data).to.be.include('backstage_functions_overview_run_total{status="4xx"} 1');
expect(data).to.be.include('backstage_functions_overview_run_duration_seconds_bucket{le="0.05"} 1');
});
});

describe('#observePipelineRun', () => {
beforeEach(() => {
prometheusClient.register.resetMetrics();
});

it('should increment metrics in registry', () => {
new Metric().observePipelineRun(403);
const data = prometheusClient.register.metrics();

expect(data).to.be.include('backstage_functions_pipeline_run_total{status="4xx"} 1');
expect(data).to.be.include('backstage_functions_pipeline_run_duration_seconds_bucket{le="0.05"} 1');
});
});
});

0 comments on commit e35ff5a

Please sign in to comment.
You can’t perform that action at this time.