/**
 * Copyright 2018, OpenCensus Authors
 *
 * 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.
 */ import { TraceParams } from '../config/types' ; import { Sampler } from './types' ; const MAX_NUMBER = 0xfffffffffffff ; const LOWER_BYTE_COUNT = 13 ; /**  Sampler that samples every trace. */ export class AlwaysSampler implements Sampler { readonly description = 'always' ; shouldSample ( traceId ) { return true ; } } /** Sampler that samples no traces. */ export class NeverSampler implements Sampler { readonly description = 'never' ; shouldSample ( traceId ) { return false ; } } /** Sampler that samples a given fraction of traces. */ export class ProbabilitySampler implements Sampler { private idUpperBound : number ; readonly description : string ; /**
     * Constructs a new Probability Sampler instance.
     */ constructor ( probability ) { this . description = `probability.(${ probability })` ; this . idUpperBound = probability * MAX_NUMBER ; } param traceId returns shouldSample ( traceId ) { const LOWER_BYTES = traceId ? ( '0000000000000' + traceId ) . slice ( - LOWER_BYTE_COUNT ) : '0' ; const LOWER_LONG = parseInt ( LOWER_BYTES , 16 ) ; if ( LOWER_LONG <= this . idUpperBound ) { return true ; } else { return false ; } } } /** Builder class of Samplers */ export class SamplerBuilder { private static readonly ALWAYS = new AlwaysSampler ( ) ; private static readonly NEVER = new NeverSampler ( ) ; param probability returns static getSampler ( probability ) { if ( probability >= 1.0 ) { return SamplerBuilder . ALWAYS ; } else if ( probability <= 0 ) { return SamplerBuilder . NEVER ; } return new ProbabilitySampler ( probability ) ; } } /**
 * The default sampler is a Probability sampler with the probability set to
 * 1/10000.
 */ export const DEFAULT_SAMPLING_RATE = 0.0001 ; /** Default Limit for Annotations per span */ export const DEFAULT_SPAN_MAX_NUM_ANNOTATIONS = 32 ; /** Default limit for Message events per span */ export const DEFAULT_SPAN_MAX_NUM_MESSAGE_EVENTS = 128 ; /** Default limit for Attributes per span */ export const DEFAULT_SPAN_MAX_NUM_ATTRIBUTES = 32 ; /** Default limit for Links per span */ export const DEFAULT_SPAN_MAX_NUM_LINKS = 32 ; /** Builder Class of TraceParams */ export class TraceParamsBuilder { static getNumberOfAnnotationEventsPerSpan ( traceParameters ) { return ! traceParameters . numberOfAnnontationEventsPerSpan ? DEFAULT_SPAN_MAX_NUM_ANNOTATIONS : traceParameters . numberOfAnnontationEventsPerSpan ; } static getNumberOfAttributesPerSpan ( traceParameters ) { return ! traceParameters . numberOfAttributesPerSpan ? DEFAULT_SPAN_MAX_NUM_ATTRIBUTES : traceParameters . numberOfAttributesPerSpan ; } static getNumberOfMessageEventsPerSpan ( traceParameters ) { return ! traceParameters . numberOfMessageEventsPerSpan ? DEFAULT_SPAN_MAX_NUM_MESSAGE_EVENTS : traceParameters . numberOfMessageEventsPerSpan ; } static getNumberOfLinksPerSpan ( traceParameters ) { return ! traceParameters . numberOfLinksPerSpan ? DEFAULT_SPAN_MAX_NUM_LINKS : traceParameters . numberOfLinksPerSpan ; } }