Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IP metrics to REST #2174

Merged
merged 17 commits into from
Jun 28, 2021
1 change: 1 addition & 0 deletions hedera-mirror-rest/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ hedera:
password: password
username: metrics
uriPath: '/swagger'
ipMetrics: false
openapi:
specFileName: 'openapi'
swaggerUIPath: 'docs'
Expand Down
15 changes: 15 additions & 0 deletions hedera-mirror-rest/middleware/metricsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

// ext libraries
const extend = require('extend');
const client = require('prom-client');
const swStats = require('swagger-stats');

// files
Expand Down Expand Up @@ -52,6 +53,20 @@ const onMetricsAuthenticate = async (req, username, password) => {
});
};

const ipEndpointHistogram = new client.Histogram({
steven-sheehy marked this conversation as resolved.
Show resolved Hide resolved
name: 'mirror_ip_endpoints_histogram',
steven-sheehy marked this conversation as resolved.
Show resolved Hide resolved
help: 'a histogram mapping ip addresses to the endpoints they hit',
buckets: [0.1, 5, 15, 50, 100, 500],
labelNames: ['endpoint', 'ip'],
});

const recordIpAndEndpoint = async (req, res, next) => {
if (req.route !== undefined) {
ipEndpointHistogram.labels(req.route.path, req.ip).observe(1);
steven-sheehy marked this conversation as resolved.
Show resolved Hide resolved
}
};

module.exports = {
metricsHandler,
recordIpAndEndpoint,
};
7 changes: 6 additions & 1 deletion hedera-mirror-rest/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const tokens = require('./tokens');
const topicmessage = require('./topicmessage');
const transactions = require('./transactions');
const {handleError} = require('./middleware/httpErrorHandler');
const {metricsHandler} = require('./middleware/metricsHandler');
const {metricsHandler, recordIpAndEndpoint} = require('./middleware/metricsHandler');
const {serveSwaggerDocs} = require('./middleware/openapiHandler');
const {responseHandler} = require('./middleware/responseHandler');
const {requestLogger, requestQueryParser} = require('./middleware/requestHandler');
Expand Down Expand Up @@ -165,6 +165,11 @@ app.getAsync(`${apiPrefix}/schedules/:id`, schedules.getScheduleById);
app.getAsync(`${apiPrefix}/transactions`, transactions.getTransactions);
app.getAsync(`${apiPrefix}/transactions/:id`, transactions.getOneTransaction);

// record ip metrics if enabled
if (config.metrics.ipMetrics) {
app.useAsync(recordIpAndEndpoint);
Nana-EC marked this conversation as resolved.
Show resolved Hide resolved
}

// response data handling middleware
app.useAsync(responseHandler);

Expand Down
1 change: 0 additions & 1 deletion hedera-mirror-rest/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ const getTransactions = async (req, res) => {
logger.debug(`getTransactions returning ${ret.transactions.length} entries`);
res.locals[constants.responseDataLabel] = ret;
};

steven-sheehy marked this conversation as resolved.
Show resolved Hide resolved
/**
* Gets the scheduled db query from the scheduled param in the HTTP request query. The last scheduled value is honored.
* If not present, returns empty string.
Expand Down