Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

feat!: return metrics objects from register instead of updating with an options object #310

Merged
merged 6 commits into from
Nov 5, 2022

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented Nov 4, 2022

Removes the component update methods in favour of registering metrics once with methods that return objects which can wrap metric gathering implementations.

This lets us (for example) return a wrapped prometheus gauge and avoid concatenating strings, etc when extracting metrics.

Register methods for metrics (prometheus gauges) and counters are present, extra registerHistogram etc methods can be added at a later date when required.

Single metrics

Before:

metrics.updateComponentMetric({
  system: 'libp2p',
  component: 'connection-manager',
  metric: 'incoming-connections',
  value: 5
})

After:

const metric = metrics.registerMetric('libp2p_connection_manager_incoming_connections')

// call repeatedly
metric.update(5)

// or
metric.increment()

// or
metric.increment(5)

// or
const stopTimer = metric.timer()
// later
stopTimer()

// or
metric.reset()

// etc

Metric groups

Before:

metrics.updateComponentMetric({
  system: 'libp2p',
  component: 'connection-manager',
  metric: 'connections',
  value: {
    incoming: 5,
    outgoing: 10
  }
})

After:

const metric = metrics.registerMetricGroup('libp2p_connection_manager_connections')

// call repeatedly
metric.update({
  incoming: 5,
  outgoing: 10
})

Extracting metrics

This has got much simpler, this example is from an upcoming PR to js-ipfs:

import client from 'prom-client'

// Endpoints for handling debug metrics
export default [{
  method: 'GET',
  path: '/debug/metrics/prometheus',
  /**
   * @param {import('../../types').Request} request
   * @param {import('@hapi/hapi').ResponseToolkit} h
   */
  async handler (request, h) {
    if (!process.env.IPFS_MONITORING) {
      throw Boom.notImplemented('Monitoring is disabled. Enable it by setting environment variable IPFS_MONITORING')
    }

    return h.response(await client.register.metrics())
      .type(client.register.contentType)
  }
}]

BREAKING CHANGE: the global/per-peer moving average tracking has been removed from the interface as it's expensive and requires lots of timers - this functionality can be replicated by implementations if it's desirable. It's better to have simple counters instead and let an external system like Prometheus or Graphana calculate the values over time

…options object

Removes the component update methods in favour of registering metrics once
with methods that return objects which can wrap metric gathering implementations.

This lets us return a wrapped prometheus gauge and avoid concatenating strings, etc
when extracting metrics.

Arbitrary metrics for generating line graphs are present as before, extra
`registerHistogram` etc methods can be added at a later date when required.
achingbrain added a commit to libp2p/js-libp2p-tcp that referenced this pull request Nov 5, 2022
Uses new metrics interface from libp2p/js-libp2p-interfaces#310
to report useful connection metrics.

Similar to #217 but it adds the listening host/port to the metrics
name to allow multiple TCP listeners to report metrics separately.
@achingbrain achingbrain merged commit 3b106ce into master Nov 5, 2022
@achingbrain achingbrain deleted the feat/simplify-metrics-interface branch November 5, 2022 17:18
github-actions bot pushed a commit that referenced this pull request Nov 5, 2022
## [@libp2p/interface-metrics-v4.0.0](https://github.com/libp2p/js-libp2p-interfaces/compare/@libp2p/interface-metrics-v3.0.0...@libp2p/interface-metrics-v4.0.0) (2022-11-05)

### ⚠ BREAKING CHANGES

* the global/per-peer moving average tracking has been removed from the interface as it's expensive and requires lots of timers - this functionality can be replicated by implementations if it's desirable.  It's better to have simple counters instead and let an external system like Prometheus or Graphana calculate the values over time

### Features

* return metrics objects from register instead of updating with an options object ([#310](#310)) ([3b106ce](3b106ce))
@github-actions
Copy link

github-actions bot commented Nov 5, 2022

🎉 This PR is included in version @libp2p/interface-metrics-v4.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

achingbrain added a commit to libp2p/js-libp2p-tcp that referenced this pull request Nov 5, 2022
Uses new metrics interface from libp2p/js-libp2p-interfaces#310 to report useful connection metrics.

Similar to #217 but it adds the listening host/port to the metrics name to allow multiple TCP listeners to report metrics separately.

BREAKING CHANGE: requires metrics interface v4
github-actions bot pushed a commit that referenced this pull request Nov 5, 2022
## [@libp2p/interface-mocks-v8.0.0](https://github.com/libp2p/js-libp2p-interfaces/compare/@libp2p/interface-mocks-v7.1.0...@libp2p/interface-mocks-v8.0.0) (2022-11-05)

### ⚠ BREAKING CHANGES

* the global/per-peer moving average tracking has been removed from the interface as it's expensive and requires lots of timers - this functionality can be replicated by implementations if it's desirable.  It's better to have simple counters instead and let an external system like Prometheus or Graphana calculate the values over time

### Features

* return metrics objects from register instead of updating with an options object ([#310](#310)) ([3b106ce](3b106ce))

### Bug Fixes

* update project config ([#311](#311)) ([27dd0ce](27dd0ce))

### Dependencies

* update sibling dependencies ([6f41152](6f41152))
@github-actions
Copy link

github-actions bot commented Nov 5, 2022

🎉 This PR is included in version @libp2p/interface-mocks-v8.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

1 participant