Skip to content

Commit

Permalink
Add support for custom metrics
Browse files Browse the repository at this point in the history
- Users can import `metricClient` function from openapm and use it any
of the files in app code to define custom metrics.
- This function internally uses prom-client's client so the same DSL
is supported.
  • Loading branch information
prathamesh-sonpatki committed May 19, 2024
1 parent edf8550 commit dcd1759
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
13 changes: 12 additions & 1 deletion playground/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
* */
require('dotenv').config();
const express = require('express');
const { OpenAPM, setOpenAPMLabels } = require('../dist/src/index.js');
const {
OpenAPM,
setOpenAPMLabels,
metricClient
} = require('../dist/src/index.js');
const mysql2 = require('mysql2');

const openapm = new OpenAPM({
Expand Down Expand Up @@ -37,6 +41,12 @@ const pool = mysql2.createPool(
'mysql://express-app:password@127.0.0.1/express' // If this throws an error, Change the db url to the one you're running on your machine locally or the testing instance you might have hosted.
);

const client = metricClient();
const counter = new client.Counter({
name: 'cancelation_calls',
help: 'no. of times cancel operation is called'
});

app.get('/result', (req, res) => {
pool.getConnection((err, conn) => {
conn.query(
Expand All @@ -59,6 +69,7 @@ app.get('/organizations/:org/users', (req, res) => {
});

app.get('/cancel/:ids', (req, res) => {
counter.inc();
res.status(200).json({});
});

Expand Down
4 changes: 4 additions & 0 deletions src/OpenAPM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,4 +471,8 @@ export class OpenAPM extends LevitateEvents {
}
}

export function metricClient() {
return promClient;
}

export default OpenAPM;
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { default as OpenAPM } from './OpenAPM';
export { default as OpenAPM, metricClient } from './OpenAPM';
export { setOpenAPMLabels } from './async-local-storage.http';
export type { OpenAPMOptions } from './OpenAPM';
9 changes: 9 additions & 0 deletions tests/express.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ describe('REDMiddleware', () => {
).toBe(NUMBER_OF_REQUESTS);
});

test('Captures Custom Counter Metrics - App', async () => {
expect(
parseInt(
parsedData?.find((m) => m.name === 'custom_counter')?.metrics[0]
.value ?? '0'
)
).toBe(NUMBER_OF_REQUESTS);
});

test('Captures Counter Metrics - Router', async () => {
expect(
parseInt(
Expand Down
7 changes: 7 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ import request from 'supertest';
import express from 'express';
import type { Express } from 'express';
import { setOpenAPMLabels } from '../src/async-local-storage.http';
import { metricClient } from '../src/OpenAPM';

export const addRoutes = (app: Express) => {
const router = express.Router();
const client = metricClient();
const counter = new client.Counter({
name: 'custom_counter',
help: 'no. of times operation is called'
});

router.get('/:id', (req, res) => {
const { id } = req.params;
Expand All @@ -14,6 +20,7 @@ export const addRoutes = (app: Express) => {
app.use('/api/router/', router);
app.get('/api/:id', (req, res) => {
const { id } = req.params;
counter.inc();
res.status(200).send(id);
});

Expand Down

0 comments on commit dcd1759

Please sign in to comment.