Skip to content

Commit

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

Co-authored-by: Weyert de Boer <[email protected]>
Co-authored-by: Valentin Marchaud <[email protected]>
  • Loading branch information
3 people authored Feb 8, 2021
1 parent 9cfa92c commit a299b1d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/opentelemetry-exporter-prometheus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@
"devDependencies": {
"@types/mocha": "8.2.0",
"@types/node": "14.14.20",
"@types/sinon": "9.0.10",
"codecov": "3.8.1",
"gts": "3.1.0",
"mocha": "7.2.0",
"nyc": "15.1.0",
"rimraf": "3.0.2",
"sinon": "9.2.3",
"ts-mocha": "8.0.0",
"ts-node": "9.1.1",
"typescript": "4.1.3"
Expand Down
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 @@ -24,9 +24,11 @@ import {
HistogramAggregator,
} from '@opentelemetry/metrics';
import * as assert from 'assert';
import * as sinon from 'sinon';
import * as http from 'http';
import { PrometheusExporter } from '../src';
import { mockAggregator, mockedHrTimeMs } from './util';
import { SinonStubbedInstance } from 'sinon';

describe('PrometheusExporter', () => {
mockAggregator(SumAggregator);
Expand Down Expand Up @@ -171,6 +173,23 @@ describe('PrometheusExporter', () => {
return done();
});
});

it('should able to call getMetricsRequestHandler function to generate response with metrics', () => {
const exporter = new PrometheusExporter({ preventServerStart: true });
const mockRequest: SinonStubbedInstance<http.IncomingMessage> = sinon.createStubInstance(
http.IncomingMessage
);
const mockResponse: SinonStubbedInstance<http.ServerResponse> = sinon.createStubInstance(
http.ServerResponse
);
exporter.getMetricsRequestHandler(
(mockRequest as unknown) as http.IncomingMessage,
(mockResponse as unknown) as http.ServerResponse
);
sinon.assert.calledOnce(mockResponse.setHeader);
sinon.assert.calledOnce(mockResponse.end);
assert.strictEqual(mockResponse.statusCode, 200);
});
});

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

0 comments on commit a299b1d

Please sign in to comment.