Skip to content

Commit

Permalink
Merge branch 'zipkin' of github.com:obecny/opentelemetry-js into zipkin
Browse files Browse the repository at this point in the history
  • Loading branch information
obecny committed Apr 12, 2021
2 parents 89aab95 + 92bbfed commit 2203326
Show file tree
Hide file tree
Showing 36 changed files with 212 additions and 129 deletions.
2 changes: 1 addition & 1 deletion metapackages/plugins-node-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
[license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
[dependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/status.svg?path=metapackages/plugins-node-core
[dependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=metapackages/plugins-node-core
[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetryplugins-node-core
[npm-url]: https://www.npmjs.com/package/@opentelemetry/plugins-node-core
[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fplugins-node-core.svg
Expand Down
2 changes: 1 addition & 1 deletion metapackages/plugins-web-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
[license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
[dependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/status.svg?path=metapackages/plugins-web-core
[dependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=metapackages/plugins-web-core
[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetryplugins-web-core
[npm-url]: https://www.npmjs.com/package/@opentelemetry/plugins-web-core
[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fplugins-web-core.svg
Expand Down
10 changes: 5 additions & 5 deletions packages/opentelemetry-api-metrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
[license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
[dependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/status.svg?path=packages/opentelemetry-api
[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-api
[devDependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/dev-status.svg?path=packages/opentelemetry-api
[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-api&type=dev
[dependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-api-metrics
[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-api-metrics
[devDependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-api-metrics&type=dev
[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-api-metrics&type=dev
[npm-url]: https://www.npmjs.com/package/@opentelemetry/api-metrics
[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fapi.svg
[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fapi-metrics.svg

[trace-api-docs]: https://open-telemetry.github.io/opentelemetry-js/classes/traceapi.html
[metrics-api-docs]: https://open-telemetry.github.io/opentelemetry-js/classes/metricsapi.html
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-context-async-hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
[license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
[dependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/status.svg?path=packages/opentelemetry-context-async-hooks
[dependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-context-async-hooks
[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-context-async-hooks
[devDependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/dev-status.svg?path=packages/opentelemetry-context-async-hooks
[devDependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-context-async-hooks&type=dev
[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-context-async-hooks&type=dev
[async-hooks-doc]: http://nodejs.org/dist/latest/docs/api/async_hooks.html
[def-context-manager]: https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-context-base/README.md
Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry-context-zone-peer-dep/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
[license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
[dependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/status.svg?path=packages/opentelemetry-context-zone-peer-dep
[dependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-context-zone-peer-dep
[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-context-zone-peer-dep
[devDependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/dev-status.svg?path=packages/opentelemetry-context-zone-peer-dep
[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-web&type=dev
[devDependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-context-zone-peer-dep&type=dev
[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-context-zone-peer-dep&type=dev
[npm-url]: https://www.npmjs.com/package/@opentelemetry/context-zone-peer-dep
[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fcontext-zone-peer-dep.svg
[zone-js]: https://www.npmjs.com/package/zone.js
Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry-context-zone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
[license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
[dependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/status.svg?path=packages/opentelemetry-context-zone
[dependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-context-zone
[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-context-zone
[devDependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/dev-status.svg?path=packages/opentelemetry-context-zone
[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-web&type=dev
[devDependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-context-zone&type=dev
[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-context-zone&type=dev
[npm-url]: https://www.npmjs.com/package/@opentelemetry/context-zone
[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fcontext-zone.svg
[zone-js]: https://www.npmjs.com/package/zone.js
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
[license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
[dependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/status.svg?path=packages/opentelemetry-core
[dependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-core
[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-core
[devDependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/dev-status.svg?path=packages/opentelemetry-core
[devDependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-core&type=dev
[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-core&type=dev
[npm-url]: https://www.npmjs.com/package/@opentelemetry/core
[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fcore.svg
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@
* limitations under the License.
*/

import { Sampler, SamplingDecision, SamplingResult } from '@opentelemetry/api';
import {
Sampler,
SamplingDecision,
SamplingResult,
isValidTraceId,
} from '@opentelemetry/api';

/** Sampler that samples a given fraction of traces based of trace id deterministically. */
export class TraceIdRatioBasedSampler implements Sampler {
private _upperBound: number;

constructor(private readonly _ratio: number = 0) {
this._ratio = this._normalize(_ratio);
this._upperBound = Math.floor(this._ratio * 0xffffffff);
}

shouldSample(context: unknown, traceId: string): SamplingResult {
let accumulation = 0;
for (let idx = 0; idx < traceId.length; idx++) {
accumulation += traceId.charCodeAt(idx);
}
const cmp = (accumulation % 100) / 100;
return {
decision:
cmp < this._ratio
isValidTraceId(traceId) && this._accumulate(traceId) < this._upperBound
? SamplingDecision.RECORD_AND_SAMPLED
: SamplingDecision.NOT_RECORD,
};
Expand All @@ -44,4 +47,14 @@ export class TraceIdRatioBasedSampler implements Sampler {
if (typeof ratio !== 'number' || isNaN(ratio)) return 0;
return ratio >= 1 ? 1 : ratio <= 0 ? 0 : ratio;
}

private _accumulate(traceId: string): number {
let accumulation = 0;
for (let i = 0; i < traceId.length / 8; i++) {
const pos = i * 8;
const part = parseInt(traceId.slice(pos, pos + 8), 16);
accumulation = (accumulation ^ part) >>> 0;
}
return accumulation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const spanContext = (traceId = '1') => ({
traceFlags: api.TraceFlags.NONE,
});

const traceId = (part: string) => ('0'.repeat(32) + part).slice(-32);

describe('TraceIdRatioBasedSampler', () => {
it('should reflect sampler name with ratio', () => {
let sampler = new TraceIdRatioBasedSampler(1.0);
Expand Down Expand Up @@ -53,93 +55,142 @@ describe('TraceIdRatioBasedSampler', () => {

it('should return a always sampler for 1', () => {
const sampler = new TraceIdRatioBasedSampler(1);
assert.deepStrictEqual(sampler.shouldSample(spanContext('1'), '1'), {
decision: api.SamplingDecision.RECORD_AND_SAMPLED,
});
assert.deepStrictEqual(
sampler.shouldSample(spanContext(traceId('1')), traceId('1')),
{
decision: api.SamplingDecision.RECORD_AND_SAMPLED,
}
);
});

it('should return a always sampler for >1', () => {
const sampler = new TraceIdRatioBasedSampler(100);
assert.deepStrictEqual(sampler.shouldSample(spanContext('1'), '1'), {
decision: api.SamplingDecision.RECORD_AND_SAMPLED,
});
assert.deepStrictEqual(
sampler.shouldSample(spanContext(traceId('1')), traceId('1')),
{
decision: api.SamplingDecision.RECORD_AND_SAMPLED,
}
);
});

it('should return a never sampler for 0', () => {
const sampler = new TraceIdRatioBasedSampler(0);
assert.deepStrictEqual(sampler.shouldSample(spanContext('1'), '1'), {
decision: api.SamplingDecision.NOT_RECORD,
});
assert.deepStrictEqual(
sampler.shouldSample(spanContext(traceId('1')), traceId('1')),
{
decision: api.SamplingDecision.NOT_RECORD,
}
);
});

it('should return a never sampler for <0', () => {
const sampler = new TraceIdRatioBasedSampler(-1);
assert.deepStrictEqual(sampler.shouldSample(spanContext('1'), '1'), {
decision: api.SamplingDecision.NOT_RECORD,
});
assert.deepStrictEqual(
sampler.shouldSample(spanContext(traceId('1')), traceId('1')),
{
decision: api.SamplingDecision.NOT_RECORD,
}
);
});

it('should handle NaN', () => {
const sampler = new TraceIdRatioBasedSampler(NaN);
assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{0}');
assert.deepStrictEqual(sampler.shouldSample(spanContext('1'), '1'), {
decision: api.SamplingDecision.NOT_RECORD,
});
assert.deepStrictEqual(
sampler.shouldSample(spanContext(traceId('1')), traceId('1')),
{
decision: api.SamplingDecision.NOT_RECORD,
}
);
});

it('should handle -NaN', () => {
const sampler = new TraceIdRatioBasedSampler(-NaN);
assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{0}');
assert.deepStrictEqual(sampler.shouldSample(spanContext('1'), '1'), {
decision: api.SamplingDecision.NOT_RECORD,
});
assert.deepStrictEqual(
sampler.shouldSample(spanContext(traceId('1')), traceId('1')),
{
decision: api.SamplingDecision.NOT_RECORD,
}
);
});

it('should handle undefined', () => {
const sampler = new TraceIdRatioBasedSampler(undefined);
assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{0}');
assert.deepStrictEqual(sampler.shouldSample(spanContext('1'), '1'), {
decision: api.SamplingDecision.NOT_RECORD,
});
assert.deepStrictEqual(
sampler.shouldSample(spanContext(traceId('1')), traceId('1')),
{
decision: api.SamplingDecision.NOT_RECORD,
}
);
});

it('should sample based on trace id', () => {
const sampler = new TraceIdRatioBasedSampler(0.2);
assert.deepStrictEqual(sampler.shouldSample(spanContext('\x00'), '\x00'), {
decision: api.SamplingDecision.RECORD_AND_SAMPLED,
});

assert.deepStrictEqual(sampler.shouldSample(spanContext('\x15'), '\x15'), {
decision: api.SamplingDecision.NOT_RECORD,
});
});

it('should sample traces that a lower sampling ratio would sample', () => {
const sampler10 = new TraceIdRatioBasedSampler(0.1);
const sampler20 = new TraceIdRatioBasedSampler(0.2);
assert.deepStrictEqual(
sampler10.shouldSample(spanContext('\x00'), '\x00'),
sampler.shouldSample(spanContext(traceId('1')), traceId('1')),
{
decision: api.SamplingDecision.RECORD_AND_SAMPLED,
}
);

assert.deepStrictEqual(
sampler20.shouldSample(spanContext('\x00'), '\x00'),
sampler.shouldSample(
spanContext(traceId('33333333')),
traceId('33333333')
),
{
decision: api.SamplingDecision.RECORD_AND_SAMPLED,
decision: api.SamplingDecision.NOT_RECORD,
}
);
});

it('should not sample with a invalid trace id', () => {
const sampler = new TraceIdRatioBasedSampler(1);
assert.deepStrictEqual(sampler.shouldSample(spanContext(''), ''), {
decision: api.SamplingDecision.NOT_RECORD,
});

assert.deepStrictEqual(
sampler10.shouldSample(spanContext('\x0a'), '\x0a'),
sampler.shouldSample(spanContext(traceId('g')), traceId('g')),
{
decision: api.SamplingDecision.NOT_RECORD,
}
);
});

it('should sample traces that a lower sampling ratio would sample', () => {
const sampler10 = new TraceIdRatioBasedSampler(0.1);
const sampler20 = new TraceIdRatioBasedSampler(0.2);

const id1 = traceId((Math.floor(0xffffffff * 0.1) - 1).toString(16));
assert.deepStrictEqual(sampler10.shouldSample(spanContext(id1), id1), {
decision: api.SamplingDecision.RECORD_AND_SAMPLED,
});
assert.deepStrictEqual(sampler20.shouldSample(spanContext(id1), id1), {
decision: api.SamplingDecision.RECORD_AND_SAMPLED,
});

const id2 = traceId((Math.floor(0xffffffff * 0.2) - 1).toString(16));
assert.deepStrictEqual(sampler10.shouldSample(spanContext(id2), id2), {
decision: api.SamplingDecision.NOT_RECORD,
});
assert.deepStrictEqual(sampler20.shouldSample(spanContext(id2), id2), {
decision: api.SamplingDecision.RECORD_AND_SAMPLED,
});

const id2delta = traceId(Math.floor(0xffffffff * 0.2).toString(16));
assert.deepStrictEqual(
sampler20.shouldSample(spanContext('\x0a'), '\x0a'),
sampler10.shouldSample(spanContext(id2delta), id2delta),
{
decision: api.SamplingDecision.RECORD_AND_SAMPLED,
decision: api.SamplingDecision.NOT_RECORD,
}
);
assert.deepStrictEqual(
sampler20.shouldSample(spanContext(id2delta), id2delta),
{
decision: api.SamplingDecision.NOT_RECORD,
}
);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-exporter-collector-grpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
[license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
[dependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/status.svg?path=packages/opentelemetry-exporter-collector-grpc
[dependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-exporter-collector-grpc
[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-exporter-collector-grpc
[devDependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/dev-status.svg?path=packages/opentelemetry-exporter-collector-grpc
[devDependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-exporter-collector-grpc&type=dev
[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-exporter-collector-grpc&type=dev
[npm-url]: https://www.npmjs.com/package/@opentelemetry/exporter-collector-grpc
[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fexporter-collector-grpc.svg
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-exporter-collector-proto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
[license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
[dependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/status.svg?path=packages/opentelemetry-exporter-collector-proto
[dependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-exporter-collector-proto
[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-exporter-collector-proto
[devDependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/dev-status.svg?path=packages/opentelemetry-exporter-collector-proto
[devDependencies-image]: https://status.david-dm.org/gh/open-telemetry/opentelemetry-js.svg?path=packages%2Fopentelemetry-exporter-collector-proto&type=dev
[devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-exporter-collector-proto&type=dev
[npm-url]: https://www.npmjs.com/package/@opentelemetry/exporter-collector-proto
[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fexporter-collector-proto.svg
Expand Down
Loading

0 comments on commit 2203326

Please sign in to comment.