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

feat: add tracked-map #156

Merged
merged 3 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/libp2p-interfaces/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
"import": "./dist/src/keys/index.js",
"types": "./dist/src/keys/index.d.ts"
},
"./metrics": {
"import": "./dist/src/metrics/index.js",
"types": "./dist/src/metrics/index.d.ts"
},
"./peer-discovery": {
"import": "./dist/src/peer-discovery/index.js",
"types": "./dist/src/peer-discovery/index.d.ts"
Expand Down
120 changes: 120 additions & 0 deletions packages/libp2p-interfaces/src/metrics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import type { Startable } from '..'
import type { PeerId } from '../peer-id'
import type { MultiaddrConnection } from '../transport'

export interface MovingAverage {
variance: () => number
movingAverage: () => number

deviation: () => number
forecast: () => number

push: (time: number, value: number) => void
}

export interface MovingAverages {
dataReceived: MovingAverage[]
dataSent: MovingAverage[]
}

export interface StatsJSON {
dataReceived: string
dataSent: string
movingAverages: Record<string, Record<string, number>>
}

export interface Stats extends Startable {
/**
* Returns a clone of the current stats.
*/
getSnapshot: Record<string, any>

/**
* Returns a clone of the internal movingAverages
*/
getMovingAverages: () => MovingAverages

/**
* Returns a plain JSON object of the stats
*/
toJSON: () => StatsJSON

/**
* Pushes the given operation data to the queue, along with the
* current Timestamp, then resets the update timer.
*/
push: (counter: string, inc: number) => void
}

export interface Metrics extends Startable {
/**
* Returns the global `Stats` object
*/
getGlobal: () => Stats

/**
* Returns a list of `PeerId` strings currently being tracked
*/
getPeers: () => string[]

/**
* Returns the `Stats` object for the given `PeerId` whether it
* is a live peer, or in the disconnected peer LRU cache.
*/
forPeer: (peerId: PeerId) => Stats

/**
* Returns a list of all protocol strings currently being tracked.
*/
getProtocols: () => string[]

/**
* Returns the `Stats` object for the given `protocol`.
*
* @param {string} protocol
* @returns {Stats}
*/
forProtocol: (protocol: string) => Stats

/**
* Should be called when all connections to a given peer
* have closed. The `Stats` collection for the peer will
* be stopped and moved to an LRU for temporary retention.
*/
onPeerDisconnected: (peerId: PeerId) => void

/**
* Replaces the `PeerId` string with the given `peerId`.
* If stats are already being tracked for the given `peerId`, the
* placeholder stats will be merged with the existing stats.
*/
updatePlaceholder: (placeholder: PeerId, peerId: PeerId) => void

/**
* Tracks data running through a given Duplex Iterable `stream`. If
* the `peerId` is not provided, a placeholder string will be created and
* returned. This allows lazy tracking of a peer when the peer is not yet known.
* When the `PeerId` is known, `Metrics.updatePlaceholder` should be called
* with the placeholder string returned from here, and the known `PeerId`.
*/
trackStream: (data: { stream: MultiaddrConnection, remotePeer: PeerId, protocol: string }) => MultiaddrConnection
}

export interface ComponentMetricsUpdate {
system: string
component: string
metric: string
value: number
}

export interface ComponentMetricsTracker {
/**
* Returns tracked metrics key by system, component, metric, value
*/
getComponentMetrics: () => Map<string, Map<string, Map<string, string>>>

/**
* Update the stored metric value for the given system and component
*/
updateComponentMetric: (data: ComponentMetricsUpdate) => void
}
4 changes: 4 additions & 0 deletions packages/libp2p-tracked-map/LICENSE
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions packages/libp2p-tracked-map/LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 19 additions & 0 deletions packages/libp2p-tracked-map/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -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.
44 changes: 44 additions & 0 deletions packages/libp2p-tracked-map/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# libp2p-tracked-map <!-- omit in toc -->

> allows tracking metrics in libp2p

## Table of Contents <!-- omit in toc -->

- [Description](#description)
- [Example](#example)
- [Installation](#installation)
- [License](#license)
- [Contribution](#contribution)

## Description

A map that reports it's size to the libp2p [Metrics](https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/libp2p-interfaces/src/metrics#readme) system.

If metrics are disabled a regular map is used.

## Example

```JavaScript
import { trackedMap } from '@libp2p/tracked-map'

const map = trackedMap<string, string>({ metrics })

map.set('key', 'value')
```

## Installation

```console
$ npm i @libp2p/tracked-map
```

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

### 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.
141 changes: 141 additions & 0 deletions packages/libp2p-tracked-map/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
{
"name": "@libp2p/tracked-map",
"version": "0.0.0",
"description": "Allows tracking of statistics while libp2p is running",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/libp2p-tracked-map#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/libp2p/js-libp2p-interfaces.git"
},
"bugs": {
"url": "https://github.com/libp2p/js-libp2p-interfaces/issues"
},
"keywords": [
"IPFS"
],
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist/src",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"import": "./dist/src/index.js"
}
},
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}
},
"release": {
"branches": [
"master"
],
"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": "chore",
"release": "patch"
},
{
"type": "docs",
"release": "patch"
},
{
"type": "test",
"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": "Trivial Changes"
},
{
"type": "test",
"section": "Tests"
}
]
}
}
],
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/github",
"@semantic-release/git"
]
},
"scripts": {
"lint": "aegir lint",
"dep-check": "aegir dep-check dist/src/**/*.js",
"build": "tsc",
"pretest": "npm run build",
"test": "aegir test -f ./dist/test/**/*.js",
"test:chrome": "npm run test -- -t browser",
"test:chrome-webworker": "npm run test -- -t webworker",
"test:firefox": "npm run test -- -t browser -- --browser firefox",
"test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox",
"test:node": "npm run test -- -t node --cov",
"test:electron-main": "npm run test -- -t electron-main"
},
"dependencies": {
"@libp2p/interfaces": "^1.0.0"
},
"devDependencies": {
"aegir": "^36.1.3",
"sinon": "^13.0.1"
}
}
Loading