Skip to content

Commit

Permalink
feat(prometheus): add getMetricsRequestHandler-method to Prometheus…
Browse files Browse the repository at this point in the history
… exporter

This commit introduces a new method on the `PrometheusExporter`-class that
allows users of server frameworks with an existing service instance to
call the `getMetricsRequestHandler`-method as part of a route to return
the collected metrics in Prometheus format.
  • Loading branch information
tapico-weyert committed Jan 27, 2021
1 parent 8f29081 commit c881208
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ export class PrometheusExporter implements MetricExporter {
});
}

/**
* Request handler that responds with the current state of metrics
* @param request Incoming HTTP request of server instance
* @param response HTTP response objet used to response to request
*/
public getMetricsRequestHandler(
_request: IncomingMessage,
response: ServerResponse
) {
this._exportMetrics(response);
}

/**
* Request handler used by http library to respond to incoming requests
* for the current state of metrics by the Prometheus backend.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,27 @@ describe('PrometheusExporter', () => {
return done();
});
});

it('should able to call getMetricsRequestHandler function to generate response with metrics', () => {
const exporter = new PrometheusExporter({ preventServerStart: true });
const mockRequest = {} as http.IncomingMessage;
let calledEnd = false;
let calledSetHeader = false;
const mockResponse = {
statusCode: 0,
setHeader: (_name: string, _value: string) => {
calledSetHeader = true;
},
end: () => {
calledEnd = true;
},
} as http.ServerResponse;

exporter.getMetricsRequestHandler(mockRequest, mockResponse);
assert.strictEqual(calledEnd, true);
assert.strictEqual(calledSetHeader, true);
assert.strictEqual(mockResponse.statusCode, 200);
});
});

describe('export', () => {
Expand Down

0 comments on commit c881208

Please sign in to comment.