prometheus / prometheus/client_js

Override globalRegistry

Open
#265 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
3.5k
Forks
429
Avg merge
3d 3h
Merged PRs (30d)
22

Description

I am trying to export metrics on file and then export it using other worker to metric collector.
right now, I have to pass file register in each metrics manually to get them to dump in file. forgetting that might lead to loosing those metrics.

So, is there anyway to override globalRegistry in code so that all metrics goes in file register by default instead of global default register ?

Full code snippet i am trying to use:

const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const client = require('prom-client');
const Registry = client.Registry;

class FileRegistry extends Registry {

    constructor(opts) {
        opts = opts || {};
        opts.metricsDir = opts.metricsDir || './metrics.pom';
        opts.saveInterval = opts.saveInterval || 4000; // in ms
        super();
        this.fileOpts = opts;
        this.startFlushLoop();
    }

    startFlushLoop() {
        setInterval(() => {
            this.writeToFile();
        }, this.fileOpts.saveInternal);
    }

    writeToFile() {
        const { metricsDir } = this.fileOpts;
        const pomStr = this.metrics();
        this.resetMetrics();
        console.log('pomStr', pomStr);
        appendFileAsync(metricsDir, pomStr, { flags: 'a+' })
            .catch(err => {
               // something really bad happened 
                console.error(err);
                process.exit(1);
            });

    }
}

// Current possible solution
const fileRegister = new FileRegistry();
const counter = new client.Counter({
    name: 'metric_name',
    help: 'metric_help',
    registers: [fileRegister]
});

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the client.Registry/globalRegistry behavior and the client.Counter construction shown in the issue, then examine how registers are selected when none are supplied. Done means the supported approach lets metrics use the file registry by default without manually passing it to every metric, with behavior documented and covered by relevant tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
observability
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.