-
Notifications
You must be signed in to change notification settings - Fork 825
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
refactor(metrics): export pipeline #1017
Closed
Closed
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
af75f12
refactor(metrics): metrics provider implements controller
legendecas 963fb8c
refactor: make PushController a variant of PullController
legendecas 5ada775
Merge branch 'master' into pull-controller
legendecas e80e541
feat: add server start callback
legendecas e7afc5f
feat: prometheus export pipeline
legendecas 4b22cfb
Merge branch 'master' into pull-controller
legendecas 60af018
Merge branch 'master' into pull-controller
legendecas 5dda52a
feat: take `ConsoleMetricExporter` as examples
legendecas bb9a2be
Merge branch 'master' into pull-controller
legendecas 2194b8c
Merge branch 'master' into pull-controller
legendecas 15b03b0
test: removing assertion on deprecated labelKeys and monotonic attrib…
legendecas 902454c
test: replace file headers
legendecas 3dd9ebd
test: lint markdown autofix
legendecas b3a5ddc
test: add test coverage for PullController
legendecas c88bcec
Merge branch 'master' into pull-controller
legendecas 58eddf2
test: await asynchronous collect
legendecas afbf0b2
docs: controllers can be installed as global meter provider
legendecas d4a45bc
feat(prometheus): add support for async metric collection
legendecas b679073
Merge remote-tracking branch 'upstream/master' into pull-controller
legendecas 8440c53
refactor: fix naming confusion issue
legendecas 30e883b
style: autofix
legendecas 65c2e46
feat: add export pipeline installer interface
legendecas 87a5981
Merge remote-tracking branch 'upstream/master' into pull-controller
legendecas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/opentelemetry-exporter-prometheus/src/export/export-pipeline.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/*! | ||
* Copyright 2020, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as api from '@opentelemetry/api'; | ||
import { PullController } from '@opentelemetry/metrics'; | ||
import { PrometheusExporter } from '../prometheus'; | ||
import { ExporterConfig } from './types'; | ||
|
||
/** | ||
* Install Prometheus export pipeline to global metrics api. | ||
* @param exporterConfig Exporter configuration | ||
* @param callback Callback to be called after a server was started | ||
* @param controllerConfig Default meter configuration | ||
*/ | ||
export function installExportPipeline( | ||
exporterConfig?: ExporterConfig, | ||
callback?: () => void, | ||
controllerConfig?: ConstructorParameters<typeof PullController>[0] | ||
) { | ||
const exporter = new PrometheusExporter(exporterConfig, callback); | ||
const pullController = new PullController({ ...controllerConfig, exporter }); | ||
exporter.setPullCallback(() => pullController.collect()); | ||
api.metrics.setGlobalMeterProvider(pullController); | ||
return { exporter, controller: pullController }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ | |
|
||
export * from './prometheus'; | ||
export * from './export/types'; | ||
export * from './export/export-pipeline'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/opentelemetry-exporter-prometheus/test/export/export-pipeline.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/*! | ||
* Copyright 2020, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as api from '@opentelemetry/api'; | ||
import * as assert from 'assert'; | ||
import { installExportPipeline, PrometheusExporter } from '../../src'; | ||
import { get } from '../request-util'; | ||
import { mockedTimeMS } from '../sandbox'; | ||
import { PullController } from '@opentelemetry/metrics'; | ||
|
||
describe('Prometheus Export Pipeline', () => { | ||
let exporter: PrometheusExporter; | ||
|
||
afterEach(done => { | ||
exporter?.shutdown(done); | ||
}); | ||
|
||
it('should install to global metrics api', async () => { | ||
let controller: PullController; | ||
await new Promise(resolve => { | ||
({ exporter, controller } = installExportPipeline( | ||
{ startServer: true }, | ||
resolve | ||
)); | ||
}); | ||
|
||
assert.strictEqual(api.metrics.getMeterProvider(), controller!); | ||
|
||
const counter = api.metrics.getMeter('test').createCounter('counter', { | ||
description: 'a test description', | ||
labelKeys: ['key1'], | ||
}); | ||
const boundCounter = counter.bind({ key1: 'labelValue1' }); | ||
boundCounter.add(10); | ||
|
||
const res = await get('http://localhost:9464/metrics'); | ||
assert.strictEqual(res.statusCode, 200); | ||
assert(res.body != null); | ||
const lines = res.body!.split('\n'); | ||
|
||
assert.strictEqual(lines[0], '# HELP counter a test description'); | ||
|
||
assert.deepStrictEqual(lines, [ | ||
'# HELP counter a test description', | ||
'# TYPE counter counter', | ||
`counter{key1="labelValue1"} 10 ${mockedTimeMS}`, | ||
'', | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/opentelemetry-exporter-prometheus/test/request-util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/*! | ||
* Copyright 2020, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as http from 'http'; | ||
|
||
export interface TestMessage { | ||
statusCode?: number; | ||
body?: string; | ||
} | ||
|
||
export function get(url: string): Promise<TestMessage> { | ||
return new Promise((resolve, reject) => { | ||
http | ||
.get(url, (res: http.IncomingMessage) => { | ||
res.on('error', reject); | ||
|
||
res.setEncoding('utf8'); | ||
|
||
let body = ''; | ||
res.on('data', data => { | ||
body += data; | ||
}); | ||
res.on('end', () => { | ||
resolve({ statusCode: res.statusCode, body }); | ||
}); | ||
}) | ||
.on('error', reject); | ||
}); | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/opentelemetry-exporter-prometheus/test/sandbox.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2020, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { Point, CounterSumAggregator } from '@opentelemetry/metrics'; | ||
import { HrTime } from '@opentelemetry/api'; | ||
|
||
export const mockedHrTime: HrTime = [1586347902211, 0]; | ||
export const mockedTimeMS = 1586347902211000; | ||
|
||
let toPoint: () => Point; | ||
before(() => { | ||
toPoint = CounterSumAggregator.prototype.toPoint; | ||
CounterSumAggregator.prototype.toPoint = function (): Point { | ||
const point = toPoint.apply(this); | ||
point.timestamp = mockedHrTime; | ||
return point; | ||
}; | ||
}); | ||
after(() => { | ||
CounterSumAggregator.prototype.toPoint = toPoint; | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
installExportPipeline
for me it looks kind of magic from outside, not sure what is happening inside and how the exporter is being bound with meter and what if I want to change it. If we want to have similar pattern for other exporters (imho we should have those things unified) I would moveinstallExportPipeline
to a core or metrics and allow setting exporter as dependency injection. This way it will look a bit more clear for me and I will still be able to use the same pattern for any other exporter that I want. WDYT ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key point for
installExportPipeline
is that the export policy mainly depends on how the exporter works. For example, the prometheus exporter (which starts a server awaiting incoming scrapping) would prefer a pulling strategy and the console exporter would prefer a periodically print of the metric records. For this reason, theinstallExportPipeline
was implemented for each exporter and in theinstallExportPipeline
there is nothing magic other than installing a metric exporting strategy (controller) as the global metric provider as the metric provider is the role who holds all meters/metrics.If the
installExportPipeline
is implemented as a method in opentelemetry/metric or core, there is no significant difference with the current approach since the exporting strategy still needs to be determined by who installs the pipeline which can be probably the end-user who uses the exporter rather than the exporter themselves.e.g.
The assumption is based on that the end-user doesn't have to care about the strategy the exporter used. They do care about which exporter they are going to work with.