it('supports static labels', done => {
    const metric = new promClient.Counter({
      name: 'test_counter',
      help: 'help',
      labelNames: ['label1', 'label2']
    });

    const rp = requestPlus()
      .plus.wrap(promDecorator, {
        metric: metric,
        labels: {
          label1: 'foo',
          label2: 'bar'
        }
      });

     nock('http://example.com')
      .get('/prom-decorator')
      .reply(200, 'ok');

    rp('http://example.com/prom-decorator')
      .then(() => {
        const labels = metric.get().values[0].labels;
        expect(labels.label1).toBe('foo');
        expect(labels.label2).toBe('bar');
        done();
      })
      .catch(done.fail);
  });
Beispiel #2
0
  async fetchPath(method, path, payload, accessToken) {
    const options = this.getOptions(method, path, payload, accessToken)
    const [response, body] = await request(options)
    const {statusCode} = response || {}

    CallCounter.inc({type: 'total'}, 1)
    // 300 codes are for github membership checks
    if ([200, 201, 202, 203, 204, 300, 301, 302].indexOf(statusCode) < 0) {
      error(`${statusCode} ${method} ${path}`, response.body)
      if (statusCode >= 400 && statusCode <= 499) {
        CallCounter.inc({type: '4xx'}, 1)
      } else if (statusCode >= 500 && statusCode <= 599) {
        CallCounter.inc({ type: '5xx'}, 1)
      }
      throw new GithubServiceError(response)
    }
    else {
      CallCounter.inc({type: 'success'}, 1)
      return body
    }
  }
Beispiel #3
0
const server = http.createServer((req, res) => {
  switch (req.url) {
    case '/':
      // Set label values and increase counter
      counter.labels(req.url, req.method, 200).inc()

      // A bit hacky, just for demo purposes!
      const currentValue = counter.hashMap['method:GET,path:/,statusCode:200'].value
      return res.end(`Website has been loaded ${currentValue} times since the webserver has been started.`)

    case '/metrics':
      // Expose service metrics
      res.setHeader('Content-Type', prometheus.contentType)
      return res.end(prometheus.register.metrics())

    default:
      res.statusCode = 404

      // Set label values and increase counter
      counter.labels(req.url, req.method, res.statusCode).inc()

      return res.end()
  }
})
 .then(() => {
   const labels = metric.get().values[0].labels;
   expect(labels.label1).toBe('foo');
   expect(labels.label2).toBe('bar');
   done();
 })