UNPKG

4.75 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2018, OpenCensus Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18// We use 52-bits as our max number because it remains a javascript "safe
19// integer" for arithmetic and parsing while using the full hex range for
20// comparison to the lower order bytes on a traceId.
21const MAX_NUMBER = 0xfffffffffffff;
22const LOWER_BYTE_COUNT = 13;
23/** Sampler that samples every trace. */
24class AlwaysSampler {
25 constructor() {
26 this.description = 'always';
27 }
28 shouldSample(traceId) {
29 return true;
30 }
31}
32exports.AlwaysSampler = AlwaysSampler;
33/** Sampler that samples no traces. */
34class NeverSampler {
35 constructor() {
36 this.description = 'never';
37 }
38 shouldSample(traceId) {
39 return false;
40 }
41}
42exports.NeverSampler = NeverSampler;
43/** Sampler that samples a given fraction of traces. */
44class ProbabilitySampler {
45 /**
46 * Constructs a new Probability Sampler instance.
47 */
48 constructor(probability) {
49 this.description = `probability.(${probability})`;
50 this.idUpperBound = probability * MAX_NUMBER;
51 }
52 /**
53 * Checks if trace belong the sample.
54 * @param traceId Used to check the probability
55 * @returns a boolean. True if the traceId is in probability
56 * False if the traceId is not in probability.
57 */
58 shouldSample(traceId) {
59 const LOWER_BYTES = traceId
60 ? ('0000000000000' + traceId).slice(-LOWER_BYTE_COUNT)
61 : '0';
62 // tslint:disable-next-line:ban Needed to parse hexadecimal.
63 const LOWER_LONG = parseInt(LOWER_BYTES, 16);
64 if (LOWER_LONG <= this.idUpperBound) {
65 return true;
66 }
67 else {
68 return false;
69 }
70 }
71}
72exports.ProbabilitySampler = ProbabilitySampler;
73/** Builder class of Samplers */
74class SamplerBuilder {
75 /**
76 * If probability parameter is bigger then 1 return AlwaysSampler instance.
77 * If probability parameter is less than 0 returns NeverSampler instance.
78 * Else returns a Probability Sampler
79 *
80 * @param probability probability between 0 and 1
81 * @returns a Sampler object
82 */
83 static getSampler(probability) {
84 if (probability >= 1.0) {
85 return SamplerBuilder.ALWAYS;
86 }
87 else if (probability <= 0) {
88 return SamplerBuilder.NEVER;
89 }
90 return new ProbabilitySampler(probability);
91 }
92}
93SamplerBuilder.ALWAYS = new AlwaysSampler();
94SamplerBuilder.NEVER = new NeverSampler();
95exports.SamplerBuilder = SamplerBuilder;
96/**
97 * The default sampler is a Probability sampler with the probability set to
98 * 1/10000.
99 */
100exports.DEFAULT_SAMPLING_RATE = 0.0001;
101/** Default Limit for Annotations per span */
102exports.DEFAULT_SPAN_MAX_NUM_ANNOTATIONS = 32;
103/** Default limit for Message events per span */
104exports.DEFAULT_SPAN_MAX_NUM_MESSAGE_EVENTS = 128;
105/** Default limit for Attributes per span */
106exports.DEFAULT_SPAN_MAX_NUM_ATTRIBUTES = 32;
107/** Default limit for Links per span */
108exports.DEFAULT_SPAN_MAX_NUM_LINKS = 32;
109/** Builder Class of TraceParams */
110class TraceParamsBuilder {
111 static getNumberOfAnnotationEventsPerSpan(traceParameters) {
112 return !traceParameters.numberOfAnnontationEventsPerSpan
113 ? exports.DEFAULT_SPAN_MAX_NUM_ANNOTATIONS
114 : traceParameters.numberOfAnnontationEventsPerSpan;
115 }
116 static getNumberOfAttributesPerSpan(traceParameters) {
117 return !traceParameters.numberOfAttributesPerSpan
118 ? exports.DEFAULT_SPAN_MAX_NUM_ATTRIBUTES
119 : traceParameters.numberOfAttributesPerSpan;
120 }
121 static getNumberOfMessageEventsPerSpan(traceParameters) {
122 return !traceParameters.numberOfMessageEventsPerSpan
123 ? exports.DEFAULT_SPAN_MAX_NUM_MESSAGE_EVENTS
124 : traceParameters.numberOfMessageEventsPerSpan;
125 }
126 static getNumberOfLinksPerSpan(traceParameters) {
127 return !traceParameters.numberOfLinksPerSpan
128 ? exports.DEFAULT_SPAN_MAX_NUM_LINKS
129 : traceParameters.numberOfLinksPerSpan;
130 }
131}
132exports.TraceParamsBuilder = TraceParamsBuilder;
133//# sourceMappingURL=sampler.js.map
\No newline at end of file