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

fix(prometheus): metrics.ts leaks to global registry #1202 #1203

Merged
merged 2 commits into from
Aug 12, 2021

Conversation

petermetz
Copy link
Contributor

@petermetz petermetz commented Aug 10, 2021

fix(prometheus): metrics.ts leaks to global registry #1202

  1. Specified a register property of the gauges as an empty
    array so that it does not pollute the global namespace. This
    is how this is supposed to be done as per the docs of prom-client.

  2. Once the change from 1) took place, the issue became that
    the metrics gathering code was still trying to hit up the
    global scope for the metrics, e.g. calling the get metrics
    methods directly on the promClient object instead of the
    registry that we create for each prmoetheus exporter object
    separately. So a little additional refactor ensued to fix this
    as well by making sure that we grab a reference of the registry
    object at construction time and then re-use that wherever needed
    instead of going through the global promClient object.

  3. Added missing .startMetricsCollection calls in the plugin
    constructors to ensure that the prometheus exporter object
    gets initialized properly (this is where the registry gets
    created as well so without this there are crashes happening
    when one tries to access the metrics through the registry)

Why though?
The problem was that the metrics.ts file that we have for all the
plugin's metrics constructs a new Metric (Gauge) object at import
time which then defaults to registering the metric in the global
registry of prom-client by default.

The latter was causing crashes when separate versions of the same
metrics.ts file are imported in a scenario were the API server
imports plugins from a different directory (this issue is coming
from the branch where I'm working on plugin sandboxing via the
live-plugin-manager).

Fixes #1202

Signed-off-by: Peter Somogyvari peter.somogyvari@accenture.com

@petermetz petermetz added the bug Something isn't working label Aug 10, 2021
@petermetz petermetz added this to the v0.8.0 milestone Aug 10, 2021
@petermetz petermetz requested a review from takeutak August 10, 2021 22:34
@petermetz petermetz self-assigned this Aug 10, 2021
@petermetz
Copy link
Contributor Author

cc: @jagpreetsinghsasan

…i#1202

1. Specified a `register` property of the gauges as an empty
array so that it does not pollute the global namespace. This
is how this is supposed to be done as per the docs of prom-client.

2. Once the change from 1) took place, the issue became that
the metrics gathering code was still trying to hit up the
global scope for the metrics, e.g. calling the get metrics
methods directly on the promClient object instead of the
registry that we create for each prmoetheus exporter object
separately. So a little additional refactor ensued to fix this
as well by making sure that we grab a reference of the registry
object at construction time and then re-use that wherever needed
instead of going through the global promClient object.

3. Added missing .startMetricsCollection calls in the plugin
constructors to ensure that the prometheus exporter object
gets initialized properly (this is where the registry gets
created as well so without this there are crashes happening
when one tries to access the metrics through the registry)

Why though?
The problem was that the metrics.ts file that we have for all the
plugin's metrics constructs a new Metric (Gauge) object at import
time which then defaults to registering the metric in the global
registry of prom-client by default.

The latter was causing crashes when separate versions of the same
metrics.ts file are imported in a scenario were the API server
imports plugins from a different directory (this issue is coming
from the branch where I'm working on plugin sandboxing via the
live-plugin-manager).

Fixes hyperledger-cacti#1202

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
petermetz added a commit to petermetz/cacti that referenced this pull request Aug 10, 2021
…er-cacti#1192

Migrates to the live-plugin-manager package to install plugins
instead of doing it via vanilla npm which was causing problems
with conflicting dependency versions where the API server would
want semver 7.x and one of the plugins (through some transient
dependency of the plugin itself) would install semver 5.x which
would then cause the API server to break down at runtime due to
the breaking changes between semver 7 and 5.

The hope with the new live-plugin-manager package is that using
this will provide sufficient isolation so that these kind of issues
are non-existent and also that it does not introduce other different
types of issues stemming from exactly said isolation. With that said
if isolation problems do occur we'll have to fix that anyway because
the plugins should not depend on the API server and vica versa.

Fixes hyperledger-cacti#1192

Depends on hyperledger-cacti#1203

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
@codecov-commenter
Copy link

codecov-commenter commented Aug 11, 2021

Codecov Report

Merging #1203 (6f3bdcf) into main (0e4c7c5) will increase coverage by 0.19%.
The diff coverage is 100.00%.

❗ Current head 6f3bdcf differs from pull request most recent head ab9c6d5. Consider uploading reports for the commit ab9c6d5 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1203      +/-   ##
==========================================
+ Coverage   71.76%   71.95%   +0.19%     
==========================================
  Files         270      270              
  Lines        9420     9431      +11     
  Branches     1113     1113              
==========================================
+ Hits         6760     6786      +26     
+ Misses       2087     2072      -15     
  Partials      573      573              
Impacted Files Coverage Δ
...src/main/typescript/prometheus-exporter/metrics.ts 100.00% <ø> (ø)
...src/main/typescript/prometheus-exporter/metrics.ts 100.00% <ø> (ø)
...src/main/typescript/prometheus-exporter/metrics.ts 100.00% <ø> (ø)
...src/main/typescript/prometheus-exporter/metrics.ts 100.00% <ø> (ø)
...src/main/typescript/prometheus-exporter/metrics.ts 100.00% <ø> (ø)
...src/main/typescript/prometheus-exporter/metrics.ts 100.00% <ø> (ø)
...src/main/typescript/prometheus-exporter/metrics.ts 100.00% <ø> (ø)
...src/main/typescript/prometheus-exporter/metrics.ts 100.00% <ø> (ø)
...s-cmd-api-server/src/main/typescript/api-server.ts 85.52% <100.00%> (+0.04%) ⬆️
...escript/prometheus-exporter/prometheus-exporter.ts 93.75% <100.00%> (+20.41%) ⬆️
... and 12 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0e4c7c5...ab9c6d5. Read the comment docs.

@jagpreetsinghsasan
Copy link
Contributor

LGTM

Copy link
Contributor

@jonathan-m-hamilton jonathan-m-hamilton left a comment

Choose a reason for hiding this comment

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

LGTM

@petermetz petermetz merged commit ce076d7 into hyperledger-cacti:main Aug 12, 2021
@petermetz petermetz deleted the fix-1202 branch August 12, 2021 19:10
petermetz added a commit to petermetz/cacti that referenced this pull request Aug 12, 2021
…er-cacti#1192

Migrates to the lmify package to install plugins at runtime
instead of doing it via vanilla npm which was causing problems
with conflicting dependency versions where the API server would
want semver 7.x and one of the plugins (through some transient
dependency of the plugin itself) would install semver 5.x which
would then cause the API server to break down at runtime due to
the breaking changes between semver 7 and 5.

The magic sauce is the --prefix option of npm which, when specified
instructs npm to ignore the usual parent directory traversal algorithm
when evaluating/resolving dependency trees and instead just do a full
installation to the specified directory path as dictated by the
--prefix option. This means that we can install each plugin in their
own directory the code being isolated from the API server and also
from other plugins that might also interfere.

Fixes hyperledger-cacti#1192

Depends on hyperledger-cacti#1203

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
petermetz added a commit that referenced this pull request Aug 13, 2021
Migrates to the lmify package to install plugins at runtime
instead of doing it via vanilla npm which was causing problems
with conflicting dependency versions where the API server would
want semver 7.x and one of the plugins (through some transient
dependency of the plugin itself) would install semver 5.x which
would then cause the API server to break down at runtime due to
the breaking changes between semver 7 and 5.

The magic sauce is the --prefix option of npm which, when specified
instructs npm to ignore the usual parent directory traversal algorithm
when evaluating/resolving dependency trees and instead just do a full
installation to the specified directory path as dictated by the
--prefix option. This means that we can install each plugin in their
own directory the code being isolated from the API server and also
from other plugins that might also interfere.

Fixes #1192

Depends on #1203

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
hanxu12 pushed a commit to hanxu12/cactus that referenced this pull request Aug 14, 2021
…er-cacti#1192

Migrates to the lmify package to install plugins at runtime
instead of doing it via vanilla npm which was causing problems
with conflicting dependency versions where the API server would
want semver 7.x and one of the plugins (through some transient
dependency of the plugin itself) would install semver 5.x which
would then cause the API server to break down at runtime due to
the breaking changes between semver 7 and 5.

The magic sauce is the --prefix option of npm which, when specified
instructs npm to ignore the usual parent directory traversal algorithm
when evaluating/resolving dependency trees and instead just do a full
installation to the specified directory path as dictated by the
--prefix option. This means that we can install each plugin in their
own directory the code being isolated from the API server and also
from other plugins that might also interfere.

Fixes hyperledger-cacti#1192

Depends on hyperledger-cacti#1203

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
Signed-off-by: hxlaf <xuhan@lafayette.edu>
maramih pushed a commit to maramih/cactus that referenced this pull request Aug 17, 2021
…er-cacti#1192

Migrates to the lmify package to install plugins at runtime
instead of doing it via vanilla npm which was causing problems
with conflicting dependency versions where the API server would
want semver 7.x and one of the plugins (through some transient
dependency of the plugin itself) would install semver 5.x which
would then cause the API server to break down at runtime due to
the breaking changes between semver 7 and 5.

The magic sauce is the --prefix option of npm which, when specified
instructs npm to ignore the usual parent directory traversal algorithm
when evaluating/resolving dependency trees and instead just do a full
installation to the specified directory path as dictated by the
--prefix option. This means that we can install each plugin in their
own directory the code being isolated from the API server and also
from other plugins that might also interfere.

Fixes hyperledger-cacti#1192

Depends on hyperledger-cacti#1203

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
Leeyoungone pushed a commit to Leeyoungone/cactus that referenced this pull request Aug 27, 2021
…er-cacti#1192

Migrates to the lmify package to install plugins at runtime
instead of doing it via vanilla npm which was causing problems
with conflicting dependency versions where the API server would
want semver 7.x and one of the plugins (through some transient
dependency of the plugin itself) would install semver 5.x which
would then cause the API server to break down at runtime due to
the breaking changes between semver 7 and 5.

The magic sauce is the --prefix option of npm which, when specified
instructs npm to ignore the usual parent directory traversal algorithm
when evaluating/resolving dependency trees and instead just do a full
installation to the specified directory path as dictated by the
--prefix option. This means that we can install each plugin in their
own directory the code being isolated from the API server and also
from other plugins that might also interfere.

Fixes hyperledger-cacti#1192

Depends on hyperledger-cacti#1203

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
elenaizaguirre pushed a commit to elenaizaguirre/cactus that referenced this pull request Sep 20, 2021
…er-cacti#1192

Migrates to the live-plugin-manager package to install plugins
instead of doing it via vanilla npm which was causing problems
with conflicting dependency versions where the API server would
want semver 7.x and one of the plugins (through some transient
dependency of the plugin itself) would install semver 5.x which
would then cause the API server to break down at runtime due to
the breaking changes between semver 7 and 5.

The hope with the new live-plugin-manager package is that using
this will provide sufficient isolation so that these kind of issues
are non-existent and also that it does not introduce other different
types of issues stemming from exactly said isolation. With that said
if isolation problems do occur we'll have to fix that anyway because
the plugins should not depend on the API server and vica versa.

Fixes hyperledger-cacti#1192

Depends on hyperledger-cacti#1203

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
RafaelAPB pushed a commit to RafaelAPB/blockchain-integration-framework that referenced this pull request Mar 9, 2022
…er-cacti#1192

Migrates to the lmify package to install plugins at runtime
instead of doing it via vanilla npm which was causing problems
with conflicting dependency versions where the API server would
want semver 7.x and one of the plugins (through some transient
dependency of the plugin itself) would install semver 5.x which
would then cause the API server to break down at runtime due to
the breaking changes between semver 7 and 5.

The magic sauce is the --prefix option of npm which, when specified
instructs npm to ignore the usual parent directory traversal algorithm
when evaluating/resolving dependency trees and instead just do a full
installation to the specified directory path as dictated by the
--prefix option. This means that we can install each plugin in their
own directory the code being isolated from the API server and also
from other plugins that might also interfere.

Fixes hyperledger-cacti#1192

Depends on hyperledger-cacti#1203

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fix(prometheus): metrics.ts leaks to global registry
5 participants