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

feat(prometheus): add getMetricsRequestHandler-method to Prometheus… #1881

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "2.0.2",
"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": "3.9.7"
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I'm not a Prometheus user and have no knowledge of it's inner workings, but I think the name and description of this function is misleading based on what the issue is talking about and looking at the implementation.

Based on m limited understanding it looks like you are really trying to "reuse" the internal pipeline, rather than "return" (i.e. get) the MetricsRequestHandler.

As such I would think (I'll let the Prometheus experts and approvers / maintainers correct me and tell me I'm wrong) the name of the method would better be named as "handleRequest" or "publishMetrics" or something like that based on the way the internal _exportMetrics() function is used from the _requestHandler. There may also be some considerations around the batcher usage if the request is a different URL's...

_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,16 @@ 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