From dda8a8acc357d2d5afa617e63118fa3986857372 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 19 Sep 2023 12:06:41 +0100 Subject: [PATCH] feat: initial import --- .github/dependabot.yml | 11 + .github/workflows/automerge.yml | 8 + .github/workflows/js-test-and-release.yml | 182 +++++++++++++++ .gitignore | 9 + LICENSE | 4 + LICENSE-APACHE | 5 + LICENSE-MIT | 19 ++ README.md | 40 ++++ package.json | 54 +++++ src/index.ts | 255 ++++++++++++++++++++++ test/index.spec.ts | 31 +++ tsconfig.json | 10 + typedoc.json | 5 + 13 files changed, 633 insertions(+) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/automerge.yml create mode 100644 .github/workflows/js-test-and-release.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 LICENSE-APACHE create mode 100644 LICENSE-MIT create mode 100644 README.md create mode 100644 package.json create mode 100644 src/index.ts create mode 100644 test/index.spec.ts create mode 100644 tsconfig.json create mode 100644 typedoc.json diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000000..0bc3b42de8 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,11 @@ +version: 2 +updates: +- package-ecosystem: npm + directory: "/" + schedule: + interval: daily + time: "10:00" + open-pull-requests-limit: 10 + commit-message: + prefix: "deps" + prefix-development: "deps(dev)" diff --git a/.github/workflows/automerge.yml b/.github/workflows/automerge.yml new file mode 100644 index 0000000000..d57c2a02b6 --- /dev/null +++ b/.github/workflows/automerge.yml @@ -0,0 +1,8 @@ +name: Automerge +on: [ pull_request ] + +jobs: + automerge: + uses: protocol/.github/.github/workflows/automerge.yml@master + with: + job: 'automerge' diff --git a/.github/workflows/js-test-and-release.yml b/.github/workflows/js-test-and-release.yml new file mode 100644 index 0000000000..a15c379170 --- /dev/null +++ b/.github/workflows/js-test-and-release.yml @@ -0,0 +1,182 @@ +name: test & maybe release +on: + push: + branches: + - main + pull_request: + +jobs: + + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present lint + - run: npm run --if-present dep-check + - run: npm run --if-present doc-check + + test-node: + needs: check + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [windows-latest, ubuntu-latest, macos-latest] + node: [lts/*] + fail-fast: true + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node }} + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:node + - uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70 # v3.1.1 + with: + flags: node + + test-chrome: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:chrome + - uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70 # v3.1.1 + with: + flags: chrome + + test-chrome-webworker: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:chrome-webworker + - uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70 # v3.1.1 + with: + flags: chrome-webworker + + test-firefox: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:firefox + - uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70 # v3.1.1 + with: + flags: firefox + + test-firefox-webworker: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:firefox-webworker + - uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70 # v3.1.1 + with: + flags: firefox-webworker + + test-webkit: + needs: check + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + node: [lts/*] + fail-fast: true + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:webkit + - uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70 # v3.1.1 + with: + flags: webkit + + test-webkit-webworker: + needs: check + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + node: [lts/*] + fail-fast: true + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:webkit-webworker + - uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70 # v3.1.1 + with: + flags: webkit-webworker + + test-electron-main: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npx xvfb-maybe npm run --if-present test:electron-main + - uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70 # v3.1.1 + with: + flags: electron-main + + test-electron-renderer: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npx xvfb-maybe npm run --if-present test:electron-renderer + - uses: codecov/codecov-action@d9f34f8cd5cb3b3eb79b3e4b5dae3a16df499a70 # v3.1.1 + with: + flags: electron-renderer + + release: + needs: [test-node, test-chrome, test-chrome-webworker, test-firefox, test-firefox-webworker, test-webkit, test-webkit-webworker, test-electron-main, test-electron-renderer] + runs-on: ubuntu-latest + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - uses: ipfs/aegir/actions/docker-login@master + with: + docker-token: ${{ secrets.DOCKER_TOKEN }} + docker-username: ${{ secrets.DOCKER_USERNAME }} + - run: npm run --if-present release + env: + GITHUB_TOKEN: ${{ secrets.UCI_GITHUB_TOKEN || github.token }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..7ad9e674ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +node_modules +build +dist +.docs +.coverage +node_modules +package-lock.json +yarn.lock +.vscode diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000..20ce483c86 --- /dev/null +++ b/LICENSE @@ -0,0 +1,4 @@ +This project is dual licensed under MIT and Apache-2.0. + +MIT: https://www.opensource.org/licenses/mit +Apache-2.0: https://www.apache.org/licenses/license-2.0 diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000000..14478a3b60 --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,5 @@ +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000000..72dc60d84b --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,19 @@ +The MIT License (MIT) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000000..646db5606e --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# @libp2p/simple-metrics + +[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/) +[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io) +[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-simple-metrics.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-simple-metrics) +[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p-simple-metrics/js-test-and-release.yml?branch=main\&style=flat-square)](https://github.com/libp2p/js-libp2p-simple-metrics/actions/workflows/js-test-and-release.yml?query=branch%3Amain) + +> Simple in-memory metrics gathering for libp2p + +## Table of contents + +- [Install](#install) + - [Browser ` +``` + +## License + +Licensed under either of + +- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / ) +- MIT ([LICENSE-MIT](LICENSE-MIT) / ) + +## Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. diff --git a/package.json b/package.json new file mode 100644 index 0000000000..3bd26ea1c0 --- /dev/null +++ b/package.json @@ -0,0 +1,54 @@ +{ + "name": "@libp2p/simple-metrics", + "version": "0.0.0", + "description": "Simple in-memory metrics gathering for libp2p", + "license": "Apache-2.0 OR MIT", + "homepage": "https://github.com/libp2p/js-libp2p-simple-metrics#readme", + "repository": { + "type": "git", + "url": "git+https://github.com/libp2p/js-libp2p-simple-metrics.git" + }, + "bugs": { + "url": "https://github.com/libp2p/js-libp2p-simple-metrics/issues" + }, + "type": "module", + "types": "./dist/src/index.d.ts", + "files": [ + "src", + "dist", + "!dist/test", + "!**/*.tsbuildinfo" + ], + "exports": { + ".": { + "types": "./dist/src/index.d.ts", + "import": "./dist/src/index.js" + } + }, + "eslintConfig": { + "extends": "ipfs", + "parserOptions": { + "sourceType": "module" + } + }, + "scripts": { + "clean": "aegir clean", + "lint": "aegir lint", + "build": "aegir build", + "test": "aegir test", + "test:node": "aegir test -t node --cov", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "dep-check": "aegir dep-check -i events" + }, + "dependencies": { + "@libp2p/interface": "^0.1.2", + "@libp2p/logger": "^3.0.2" + }, + "devDependencies": { + "aegir": "^40.0.13", + "p-defer": "^4.0.0" + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000000..4e44d701c6 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,255 @@ +/** + * @packageDocumentation + * + * Stores metrics in memory and periodically invokes a configured callback + * to receive them. + * + * @example + * + * ```ts + * import { createLibp2p } from 'libp2p' + * import { simpleMetrics } from '@libp2p/simple-metrics' + * + * const node = await createLibp2p({ + * // ... other options + * metrics: simpleMetrics({ + * onMetrics: (metrics) => { + * // do something with metrics + * } + * }), + * intervalMs: 1000 // default 1s + * }) + * + * ``` + */ + +import { logger } from '@libp2p/logger' +import type { MultiaddrConnection, Stream, Connection } from '@libp2p/interface/connection' +import type { Startable } from '@libp2p/interface/dist/src/startable' +import type { Metric, MetricGroup, StopTimer, Metrics, CalculatedMetricOptions, MetricOptions, Counter, CounterGroup, CalculateMetric } from '@libp2p/interface/metrics' + +const log = logger('libp2p:simple-metrics') + +class DefaultMetric implements Metric { + public value: number = 0 + + update (value: number): void { + this.value = value + } + + increment (value: number = 1): void { + this.value += value + } + + decrement (value: number = 1): void { + this.value -= value + } + + reset (): void { + this.value = 0 + } + + timer (): StopTimer { + const start = Date.now() + + return () => { + this.value = Date.now() - start + } + } +} + +class DefaultGroupMetric implements MetricGroup { + public values: Record = {} + + update (values: Record): void { + Object.entries(values).forEach(([key, value]) => { + this.values[key] = value + }) + } + + increment (values: Record): void { + Object.entries(values).forEach(([key, value]) => { + this.values[key] = this.values[key] ?? 0 + const inc = typeof value === 'number' ? value : 1 + + this.values[key] += Number(inc) + }) + } + + decrement (values: Record): void { + Object.entries(values).forEach(([key, value]) => { + this.values[key] = this.values[key] ?? 0 + const dec = typeof value === 'number' ? value : 1 + + this.values[key] -= Number(dec) + }) + } + + reset (): void { + this.values = {} + } + + timer (key: string): StopTimer { + const start = Date.now() + + return () => { + this.values[key] = Date.now() - start + } + } +} + +export interface OnMetrics { (metrics: Record): void } + +export interface SimpleMetricsInit { + /** + * How often to invoke the onMetrics callback + */ + intervalMs?: number + + /** + * A callback periodically invoked with collected metrics + */ + onMetrics: OnMetrics +} + +class SimpleMetrics implements Metrics, Startable { + public metrics = new Map() + private started: boolean + private interval?: ReturnType + private readonly intervalMs: number + private readonly onMetrics: OnMetrics + + constructor (components: unknown, init: SimpleMetricsInit) { + this.started = false + + this._emitMetrics = this._emitMetrics.bind(this) + + this.intervalMs = init.intervalMs ?? 1000 + this.onMetrics = init.onMetrics + } + + isStarted (): boolean { + return this.started + } + + start (): void { + this.started = true + + this.interval = setInterval(this._emitMetrics, this.intervalMs) + } + + stop (): void { + this.started = false + + clearInterval(this.interval) + } + + private _emitMetrics (): void { + void Promise.resolve().then(async () => { + const output: Record = {} + + for (const [name, metric] of this.metrics.entries()) { + if (metric instanceof DefaultMetric) { + output[name] = metric.value + } else if (metric instanceof DefaultGroupMetric) { + output[name] = metric.values + } else { + output[name] = await metric() + } + } + + this.onMetrics(output) + }) + .catch(err => { + log.error('could not invoke onMetrics callback', err) + }) + } + + trackMultiaddrConnection (maConn: MultiaddrConnection): void { + + } + + trackProtocolStream (stream: Stream, connection: Connection): void { + + } + + registerMetric (name: string, opts: CalculatedMetricOptions): void + registerMetric (name: string, opts?: MetricOptions): Metric + registerMetric (name: string, opts: any = {}): any { + if (name == null ?? name.trim() === '') { + throw new Error('Metric name is required') + } + + if (opts?.calculate != null) { + // calculated metric + this.metrics.set(name, opts.calculate) + return + } + + const metric = new DefaultMetric() + this.metrics.set(name, metric) + + return metric + } + + registerMetricGroup (name: string, opts: CalculatedMetricOptions>): void + registerMetricGroup (name: string, opts?: MetricOptions): MetricGroup + registerMetricGroup (name: string, opts: any = {}): any { + if (name == null ?? name.trim() === '') { + throw new Error('Metric name is required') + } + + if (opts?.calculate != null) { + // calculated metric + this.metrics.set(name, opts.calculate) + return + } + + const metric = new DefaultMetric() + this.metrics.set(name, metric) + + return metric + } + + registerCounter (name: string, opts: CalculatedMetricOptions): void + registerCounter (name: string, opts?: MetricOptions): Counter + registerCounter (name: string, opts: any = {}): any { + if (name == null ?? name.trim() === '') { + throw new Error('Metric name is required') + } + + if (opts?.calculate != null) { + // calculated metric + this.metrics.set(name, opts.calculate) + return + } + + const metric = new DefaultGroupMetric() + this.metrics.set(name, metric) + + return metric + } + + registerCounterGroup (name: string, opts: CalculatedMetricOptions>): void + registerCounterGroup (name: string, opts?: MetricOptions): CounterGroup + registerCounterGroup (name: string, opts: any = {}): any { + if (name == null ?? name.trim() === '') { + throw new Error('Metric name is required') + } + + if (opts?.calculate != null) { + // calculated metric + this.metrics.set(name, opts.calculate) + return + } + + const metric = new DefaultGroupMetric() + this.metrics.set(name, metric) + + return metric + } +} + +export function simpleMetrics (init: SimpleMetricsInit): (components: unknown) => Metrics { + return (components: unknown) => new SimpleMetrics(components, init) +} diff --git a/test/index.spec.ts b/test/index.spec.ts new file mode 100644 index 0000000000..e3b81b4c11 --- /dev/null +++ b/test/index.spec.ts @@ -0,0 +1,31 @@ +import { start, stop } from '@libp2p/interface/startable' +import { expect } from 'aegir/chai' +import pDefer from 'p-defer' +import { simpleMetrics } from '../src/index.js' +import type { Metrics } from '@libp2p/interface/src/metrics' + +describe('simple-metrics', () => { + let s: Metrics + + afterEach(async () => { + if (s != null) { + await stop(s) + } + }) + + it('should invoke the onMetrics callback', async () => { + const deferred = pDefer() + + s = simpleMetrics({ + onMetrics: (metrics) => { + deferred.resolve(metrics) + }, + intervalMs: 10 + })({}) + + await start(s) + + const metrics = await deferred.promise + expect(metrics).to.be.ok() + }) +}) diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000000..13a3599639 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": [ + "src", + "test" + ] +} diff --git a/typedoc.json b/typedoc.json new file mode 100644 index 0000000000..f599dc728d --- /dev/null +++ b/typedoc.json @@ -0,0 +1,5 @@ +{ + "entryPoints": [ + "./src/index.ts" + ] +}