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

Memory leak with default labels #287

Closed
doochik opened this issue Sep 20, 2019 · 5 comments · Fixed by #288
Closed

Memory leak with default labels #287

doochik opened this issue Sep 20, 2019 · 5 comments · Fixed by #288

Comments

@doochik
Copy link
Contributor

doochik commented Sep 20, 2019

Here is code example

const promClient = require('prom-client');

// register default label
promClient.register.setDefaultLabels({
    default_label: 'default_label_value',
});

// create counter
const counter = new promClient.Counter({
    name: 'test_counter',
    help: 'test_counter errors',
    labelNames: [ 'l1', 'l2', 'l3' ],
});

// create Internal counter with labels
const intCounter = counter.labels('l1_value', 'l2_value', 'l3_value');

intCounter.inc();

// collect metrics. And this point counter labels will be extended with default labels
console.log(JSON.stringify(promClient.register.getMetricsAsJSON(), null, 2));

// Error!
intCounter.inc();

Error: Added label "default_label" is not included in initial labelset: [ 'l1', 'l2', 'l3' ]
    at Object.keys.forEach.label (node_modules/prom-client/lib/validation.js:26:10)
    at Array.forEach (<anonymous>)
    at validateLabel (node_modules/prom-client/lib/validation.js:24:22)
    at Object.inc (node_modules/prom-client/lib/counter.js:156:3)
    at Object.<anonymous> (test.js:19:12)

This happens because registry.getMetricsAs*() mutates each metric labels with defaults.

getMetricsAsJSON() {
    const metrics = [];
    const defaultLabelNames = Object.keys(this._defaultLabels);

    for (const metric of this.getMetricsAsArray()) {
        const item = metric.get();

        if (item.values) {
            for (const val of item.values) {
                for (const labelName of defaultLabelNames) {
                    //
                    // leak is here
                    //
                    val.labels[labelName] = val.labels[labelName] || this._defaultLabels[labelName];
                }
            }
        }

        metrics.push(item);
    }

    return metrics;
}
@doochik
Copy link
Contributor Author

doochik commented Sep 20, 2019

Comes from #220

@johnnyfish
Copy link

@doochik It will go in?

@doochik
Copy link
Contributor Author

doochik commented Nov 9, 2019

Still waiting for #288 to be merged

@johnnyfish
Copy link

@doochik it will be merged?

@doochik
Copy link
Contributor Author

doochik commented Nov 12, 2019

I don't know, I'm not the maintainer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants