diff --git a/packages/opentelemetry-exporter-prometheus/package.json b/packages/opentelemetry-exporter-prometheus/package.json index abacade9c9..d3d5382046 100644 --- a/packages/opentelemetry-exporter-prometheus/package.json +++ b/packages/opentelemetry-exporter-prometheus/package.json @@ -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" diff --git a/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts b/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts index d47745980c..864e3d739d 100644 --- a/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts +++ b/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts @@ -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. diff --git a/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts b/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts index cb5ccb93bc..64bf6ec66b 100644 --- a/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts +++ b/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts @@ -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); @@ -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 = sinon.createStubInstance( + http.IncomingMessage + ); + const mockResponse: SinonStubbedInstance = 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', () => {