@datadog/sketches-js

中文文档
2.1.1发布于 2024/04/23Apache-2.0TypeScriptCommonJS零依赖近 30 天下载 594万

TypeScript implementation of DDSketch, a distributed quantile sketch algorithm

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Initialize a sketch
  4. Add values to a sketch
  5. Retrieve measurements from the sketch
  6. Merge multiple sketches
  7. References

sketches-js

Continuous Integration License

This repo contains the TypeScript implementation of the distributed quantile sketch algorithm DDSketch. DDSketch is mergeable, meaning that multiple sketches from distributed systems can be combined in a central node.

Installation

The package is under @datadog/sketches-js and can be installed through NPM or Yarn:

# NPM
npm install @datadog/sketches-js

# Yarn
yarn add @datadog/sketches-js

When using Protobuf serialization, the protobufjs module must also be installed manually:

# NPM
npm install protobufjs

# Yarn
yarn add protobufjs

Usage

Initialize a sketch

To initialize a sketch with the default parameters:

import { DDSketch } from '@datadog/sketches-js'; // or const { DDSketch } = require('@datadog/sketches-js');
const sketch = new DDSketch();

Modify the relativeAccuracy

If you want more granular control over how accurate the sketch's results will be, you can pass a relativeAccuracy parameter when initializing a sketch.

Whereas other histograms use rank error guarantees (i.e. retrieving the p99 of the histogram will give you a value between p98.9 and p99.1), DDSketch uses a relative error guarantee (if the actual value at p99 is 100, the value will be between 99 and 101 for a relativeAccuracy of 0.01).

This property makes DDSketch especially useful for long-tailed distributions of data, like measurements of latency.

import { DDSketch } from '@datadog/sketches-js';

const sketch = new DDSketch({
  relativeAccuracy: 0.01, // `relativeAccuracy` must be between 0 and 1
});

Add values to a sketch

To add a number to a sketch, call sketch.accept(value). Both positive and negative numbers are supported.

const measurementOne = 1607374726;
const measurementTwo = 0;
const measurementThree = -3.1415;

sketch.accept(measurementOne);
sketch.accept(measurementTwo);
sketch.accept(measurementThree);

Retrieve measurements from the sketch

To retrieve measurements from a sketch, use sketch.getValueAtQuantile(quantile). Any number between 0 and 1 (inclusive) can be used as a quantile.

Additionally, common summary statistics are available such as sketch.min, sketch.max, sketch.sum, and sketch.count:

const measurementOne = 1607374726;
const measurementTwo = 0;
const measurementThree = -3.1415;

sketch.accept(measurementOne);
sketch.accept(measurementTwo);
sketch.accept(measurementThree);

sketch.getValueAtQuantile(0)     // -3.1415
sketch.getValueAtQuantile(0.5)   // 0
sketch.getValueAtQuantile(0.99)  // 1607374726
sketch.getValueAtQuantile(1)     // 1607374726

sketch.min                       // -3.1415
sketch.max                       // 1607374726
sketch.count                     // 3
sketch.sum                       // 1607374722.86

Merge multiple sketches

Independent sketches can be merged together, provided that they were initialized with the same relativeAccuracy. This allows collecting and transmitting measurements in a distributed manner, and merging their results together while preserving the relativeAccuracy guarantee.

import { DDSketch } from '@datadog/sketches-js';

const sketch1 = new DDSketch();
const sketch2 = new DDSketch();

[1,2,3,4,5].forEach(value => sketch1.accept(value));
[6,7,8,9,10].forEach(value => sketch2.accept(value));

// `sketch2` is merged into `sketch1`, without modifying `sketch2`
sketch1.merge(sketch2);

sketch1.getValueAtQuantile(1) // 10

References

版本与发布记录

@datadog/sketches-js 共发布 10 个版本,首次发布于 2020/12/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.1latest2024/04/232 年前
2.1.02022/08/244 年前
2.0.02022/08/094 年前
1.0.52022/06/234 年前
1.0.42021/08/055 年前
1.0.32021/08/055 年前

关于 @datadog/sketches-js 的常见问题

@datadog/sketches-js 是什么?

TypeScript implementation of DDSketch, a distributed quantile sketch algorithm

如何安装 @datadog/sketches-js?

在项目目录运行 npm install @datadog/sketches-js,或使用 pnpm add / yarn add / bun add。

@datadog/sketches-js 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@datadog/sketches-js 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@datadog/sketches-js 可以商用吗?

@datadog/sketches-js 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@datadog/sketches-js 的下载量有多少?

根据 npm 官方下载统计,@datadog/sketches-js 在 2026/10/12 – 2026/11/10 这 30 天内被下载 5,940,271 次,日均约 198,009 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@datadog/sketches-js 还在维护吗?

最新版本 2.1.1 发布于 2024/04/23(2 年前);至今共发布 10 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/12/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。