Skip to content

Commit

Permalink
chore: update project
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed May 16, 2024
1 parent af31a78 commit 1cc6a94
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 118 deletions.
63 changes: 49 additions & 14 deletions packages/metrics-simple/README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,75 @@
# @libp2p/simple-metrics <!-- omit in toc -->
# @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)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p)
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=main\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amain)

> Simple in-memory metrics gathering for libp2p
## Table of contents <!-- omit in toc -->
# About

- [Install](#install)
- [Browser `<script>` tag](#browser-script-tag)
- [License](#license)
- [Contribution](#contribution)
<!--
## Install
!IMPORTANT!
Everything in this README between "# About" and "# Install" is automatically
generated and will be overwritten the next time the doc generator is run.
To make changes to this section, please update the @packageDocumentation section
of src/index.js or src/index.ts
To experiment with formatting, please run "npm run docs" from the root of this
repo and examine the changes made.
-->

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
})

```

# Install

```console
$ npm i @libp2p/simple-metrics
```

### Browser `<script>` tag
## Browser `<script>` tag

Loading this module through a script tag will make it's exports available as `Libp2pSimpleMetrics` in the global namespace.

```html
<script src="https://unpkg.com/@libp2p/simple-metrics/dist/index.min.js"></script>
```

## License
# API Docs

- <https://libp2p.github.io/js-libp2p/modules/_libp2p_simple_metrics.html>

# License

Licensed under either of

- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
- Apache 2.0, ([LICENSE-APACHE](https://github.com/libp2p/js-libp2p/blob/main/packages/metrics-simple/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](https://github.com/libp2p/js-libp2p/blob/main/packages/metrics-simple/LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

## Contribution
# 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.
102 changes: 11 additions & 91 deletions packages/metrics-simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
"version": "1.0.1",
"description": "Simple in-memory metrics gathering for libp2p",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/libp2p/js-libp2p-simple-metrics#readme",
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/metrics-simple#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/libp2p/js-libp2p-simple-metrics.git"
"url": "git+https://github.com/libp2p/js-libp2p.git"
},
"bugs": {
"url": "https://github.com/libp2p/js-libp2p-simple-metrics/issues"
"url": "https://github.com/libp2p/js-libp2p/issues"
},
"publishConfig": {
"access": "public",
"provenance": true
},
"type": "module",
"types": "./dist/src/index.d.ts",
Expand All @@ -28,94 +32,10 @@
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"project": true,
"sourceType": "module"
}
},
"release": {
"branches": [
"main"
],
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"preset": "conventionalcommits",
"releaseRules": [
{
"breaking": true,
"release": "major"
},
{
"revert": true,
"release": "patch"
},
{
"type": "feat",
"release": "minor"
},
{
"type": "fix",
"release": "patch"
},
{
"type": "docs",
"release": "patch"
},
{
"type": "test",
"release": "patch"
},
{
"type": "deps",
"release": "patch"
},
{
"scope": "no-release",
"release": false
}
]
}
],
[
"@semantic-release/release-notes-generator",
{
"preset": "conventionalcommits",
"presetConfig": {
"types": [
{
"type": "feat",
"section": "Features"
},
{
"type": "fix",
"section": "Bug Fixes"
},
{
"type": "chore",
"section": "Trivial Changes"
},
{
"type": "docs",
"section": "Documentation"
},
{
"type": "deps",
"section": "Dependencies"
},
{
"type": "test",
"section": "Tests"
}
]
}
}
],
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/github",
"@semantic-release/git"
]
},
"scripts": {
"clean": "aegir clean",
"lint": "aegir lint",
Expand All @@ -130,13 +50,13 @@
"dep-check": "aegir dep-check -i events"
},
"dependencies": {
"@libp2p/interface": "^0.1.2",
"@libp2p/logger": "^3.0.2",
"@libp2p/interface": "^1.3.1",
"@libp2p/logger": "^4.0.12",
"it-foreach": "^2.0.4",
"it-stream-types": "^2.0.1"
},
"devDependencies": {
"aegir": "^40.0.13",
"aegir": "^42.2.11",
"p-defer": "^4.0.0"
}
}
20 changes: 9 additions & 11 deletions packages/metrics-simple/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @packageDocumentation
*
* Stores metrics in memory and periodically invokes a configured callback
* to receive them.
* Stores metrics in memory and periodically invokes a configured callback to
* receive them.
*
* @example
*
Expand All @@ -25,10 +25,8 @@

import { logger } from '@libp2p/logger'
import each from 'it-foreach'
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'
import type { Duplex, Source } from 'it-stream-types'
import type { Startable, MultiaddrConnection, Stream, Connection, Metric, MetricGroup, StopTimer, Metrics, CalculatedMetricOptions, MetricOptions, Counter, CounterGroup, CalculateMetric } from '@libp2p/interface'
import type { Duplex } from 'it-stream-types'

const log = logger('libp2p:simple-metrics')

Expand Down Expand Up @@ -185,7 +183,7 @@ class SimpleMetrics implements Metrics, Startable {
* Override the sink/source of the stream to count the bytes
* in and out
*/
_track (stream: Duplex<Source<any>>, name: string): void {
_track (stream: Duplex<AsyncGenerator<any>>, name: string): void {
const self = this

const sink = stream.sink
Expand Down Expand Up @@ -218,7 +216,7 @@ class SimpleMetrics implements Metrics, Startable {
registerMetric (name: string, opts: CalculatedMetricOptions): void
registerMetric (name: string, opts?: MetricOptions): Metric
registerMetric (name: string, opts: any = {}): any {
if (name == null ?? name.trim() === '') {
if (name == null || name.trim() === '') {
throw new Error('Metric name is required')
}

Expand All @@ -237,7 +235,7 @@ class SimpleMetrics implements Metrics, Startable {
registerMetricGroup (name: string, opts: CalculatedMetricOptions<Record<string, number>>): void
registerMetricGroup (name: string, opts?: MetricOptions): MetricGroup
registerMetricGroup (name: string, opts: any = {}): any {
if (name == null ?? name.trim() === '') {
if (name == null || name.trim() === '') {
throw new Error('Metric name is required')
}

Expand All @@ -256,7 +254,7 @@ class SimpleMetrics implements Metrics, Startable {
registerCounter (name: string, opts: CalculatedMetricOptions): void
registerCounter (name: string, opts?: MetricOptions): Counter
registerCounter (name: string, opts: any = {}): any {
if (name == null ?? name.trim() === '') {
if (name == null || name.trim() === '') {
throw new Error('Metric name is required')
}

Expand All @@ -275,7 +273,7 @@ class SimpleMetrics implements Metrics, Startable {
registerCounterGroup (name: string, opts: CalculatedMetricOptions<Record<string, number>>): void
registerCounterGroup (name: string, opts?: MetricOptions): CounterGroup
registerCounterGroup (name: string, opts: any = {}): any {
if (name == null ?? name.trim() === '') {
if (name == null || name.trim() === '') {
throw new Error('Metric name is required')
}

Expand Down
4 changes: 2 additions & 2 deletions packages/metrics-simple/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { start, stop } from '@libp2p/interface/startable'
import { start, stop } from '@libp2p/interface'
import { expect } from 'aegir/chai'
import pDefer from 'p-defer'
import { simpleMetrics } from '../src/index.js'
import type { Metrics } from '@libp2p/interface/src/metrics'
import type { Metrics } from '@libp2p/interface'

describe('simple-metrics', () => {
let s: Metrics
Expand Down
8 changes: 8 additions & 0 deletions packages/metrics-simple/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@
"include": [
"src",
"test"
],
"references": [
{
"path": "../interface"
},
{
"path": "../logger"
}
]
}

0 comments on commit 1cc6a94

Please sign in to comment.