File

projects/opentelemetry-interceptor/src/lib/services/instrumentation/instrumentation.service.ts

Description

InstrumentationService. Service for component to add instrumentation.

Index

Properties
Methods

Constructor

constructor(config: OpenTelemetryConfig, exporterService: IExporter, propagatorService: IPropagator, instrumentation: Instrumentation[])

Constructor

Parameters :
Name Type Optional
config OpenTelemetryConfig No
exporterService IExporter No
propagatorService IPropagator No
instrumentation Instrumentation[] No

Methods

Private convertStringToNumber
convertStringToNumber(value: string)

convert String to Number (or undefined)

Parameters :
Name Type Optional
value string No
Returns : number

number or undefined

Private defineProbabilitySampler
defineProbabilitySampler(sampleConfig: number)

define the Probability Sampler By Default, it's always (or 1)

Parameters :
Name Type Optional Description
sampleConfig number No

the sample configuration

Returns : Sampler
Public initInstrumentation
initInstrumentation()

Init instrumentation on init

Returns : void
Private insertConsoleSpanExporter
insertConsoleSpanExporter(console: boolean)

Insert in tracer the console span if config is true

Parameters :
Name Type Optional Description
console boolean No

config to insert console span

Returns : void
Private insertOrNotSpanExporter
insertOrNotSpanExporter(production: boolean, exporter: IExporter, console: boolean)

Verify to insert or not a Span Exporter

Parameters :
Name Type Optional
production boolean No
exporter IExporter No
console boolean No
Returns : void
Private insertSpanProcessorProductionMode
insertSpanProcessorProductionMode(production: boolean, exporter: IExporter)

Insert BatchSpanProcessor in production mode SimpleSpanProcessor otherwise

Parameters :
Name Type Optional
production boolean No
exporter IExporter No
Returns : void
Private loadResourceAttributes
loadResourceAttributes(commonConfig: CommonCollectorConfig)

Generate Resource Attributes

Parameters :
Name Type Optional
commonConfig CommonCollectorConfig No
Returns : Resource

Properties

Private contextManager
Default value : new ZoneContextManager()

contextManager

Private tracerProvider
Type : WebTracerProvider

tracerProvider

import { Inject, Injectable } from '@angular/core';
import { ZoneContextManager } from '@opentelemetry/context-zone-peer-dep';
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { Resource } from '@opentelemetry/resources';
import { Instrumentation, registerInstrumentations } from '@opentelemetry/instrumentation';
import {
  AlwaysOffSampler,
  AlwaysOnSampler,
  ParentBasedSampler,
  Sampler,
  TraceIdRatioBasedSampler,
  WebTracerProvider
} from '@opentelemetry/sdk-trace-web';
import { ConsoleSpanExporter, SimpleSpanProcessor, BatchSpanProcessor, NoopSpanProcessor } from '@opentelemetry/sdk-trace-base';
// eslint-disable-next-line max-len
import { OTEL_CONFIG, OpenTelemetryConfig, OTEL_INSTRUMENTATION_PLUGINS, CommonCollectorConfig } from '../../configuration/opentelemetry-config';
import { OTEL_EXPORTER, IExporter } from '../exporter/exporter.interface';
import { OTEL_PROPAGATOR, IPropagator } from '../propagator/propagator.interface';


/**
 * InstrumentationService.
 * Service for component to add instrumentation.
 */
@Injectable({
  providedIn: 'root'
})
export class InstrumentationService {

  /**
   * tracerProvider
   */
  private tracerProvider: WebTracerProvider;

  /**
   * contextManager
   */
  private contextManager = new ZoneContextManager();

  /**
   * Constructor
   *
   * @param config
   * @param exporterService
   * @param propagatorService
   */
  constructor(@Inject(OTEL_CONFIG) private config: OpenTelemetryConfig,
    @Inject(OTEL_EXPORTER)
    private exporterService: IExporter,
    @Inject(OTEL_PROPAGATOR)
    private propagatorService: IPropagator,
    @Inject(OTEL_INSTRUMENTATION_PLUGINS)
    private instrumentation: Instrumentation[]) {
    this.tracerProvider = new WebTracerProvider({
      sampler: this.defineProbabilitySampler(this.convertStringToNumber(this.config.commonConfig.probabilitySampler)),
      resource: this.loadResourceAttributes(this.config.commonConfig),
    });
  }

  /**
   * Init instrumentation on init
   */
  public initInstrumentation() {
    this.insertOrNotSpanExporter(this.config.commonConfig.production, this.exporterService, this.config.commonConfig.console);

    this.tracerProvider.register({
      contextManager: this.contextManager,
      propagator: this.propagatorService.getPropagator()
    });

    registerInstrumentations({
      instrumentations: this.instrumentation,
      tracerProvider: this.tracerProvider,
    });
  }

  /**
   * Generate Resource Attributes
   */
  private loadResourceAttributes(commonConfig: CommonCollectorConfig): Resource {
    let resourceAttributes = Resource.default().merge(
      new Resource({
        [ATTR_SERVICE_NAME]: commonConfig.serviceName,
      })
    );
    if (commonConfig.resourceAttributes !== undefined) {
      resourceAttributes = resourceAttributes.merge(new Resource(commonConfig.resourceAttributes));
    }
    return resourceAttributes;
  }

  /**
   * Verify to insert or not a Span Exporter
   */
  private insertOrNotSpanExporter(production: boolean, exporter: IExporter, console: boolean) {
    if (this.exporterService.getExporter() !== undefined) {
      this.insertSpanProcessorProductionMode(production, exporter);
      this.insertConsoleSpanExporter(console);
    } else {
      this.tracerProvider.addSpanProcessor(new NoopSpanProcessor());
    }
  }

  /**
   * Insert in tracer the console span if config is true
   *
   * @param console config to insert console span
   */
  private insertConsoleSpanExporter(console: boolean) {
    if (console) {
      this.tracerProvider.addSpanProcessor(
        new SimpleSpanProcessor(new ConsoleSpanExporter())
      );
    }
  }

  /**
   * Insert BatchSpanProcessor in production mode
   * SimpleSpanProcessor otherwise
   *
   * @param boolean production
   * @param IExporter exporter
   */
  private insertSpanProcessorProductionMode(
    production: boolean,
    exporter: IExporter
  ) {
    this.tracerProvider.addSpanProcessor(
      production
        ? new BatchSpanProcessor(exporter.getExporter())
        : new SimpleSpanProcessor(exporter.getExporter())
    );
  }

  /**
   * convert String to Number (or undefined)
   *
   * @param value
   * @returns number or undefined
   */
  private convertStringToNumber(value: string): number {
    return value !== undefined ? Number(value) : undefined;
  }

  /**
   * define the Probability Sampler
   * By Default, it's always (or 1)
   *
   * @param sampleConfig the sample configuration
   */
  private defineProbabilitySampler(sampleConfig: number): Sampler {
    if (sampleConfig >= 1) {
      return new ParentBasedSampler({ root: new AlwaysOnSampler() });
    }
    else if (sampleConfig <= 0 || sampleConfig === undefined) {
      return new ParentBasedSampler({ root: new AlwaysOffSampler() });
    } else {
      return new ParentBasedSampler({ root: new TraceIdRatioBasedSampler(sampleConfig) });
    }
  }
}


results matching ""

    No results matching ""