UNPKG

@opencensus/nodejs-base

Version:

OpenCensus is a toolkit for collecting application performance and behavior data.

115 lines (114 loc) 4.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TracingBase = void 0; /** * Copyright 2019, 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. */ const core = require("@opencensus/core"); const core_1 = require("@opencensus/core"); const extend = require("extend"); const default_config_1 = require("./config/default-config"); const plugin_loader_1 = require("./instrumentation/plugin-loader"); const NOOP_EXPORTER = new core.NoopExporter(); /** Implements a Tracing. */ class TracingBase { /** Constructs a new TracingImpl instance. */ constructor(pluginNames = []) { /** A configuration object to start the tracing */ this.configLocal = {}; /** An object to log information to. Logs to the JS console by default. */ this.logger = core_1.logger.logger(); /** Indicates if the tracing is active */ this.activeLocal = false; this.tracer = new core.CoreTracerBase(); this.defaultPlugins = plugin_loader_1.PluginLoader.defaultPluginsFromArray(pluginNames); } /** Gets the tracing instance. */ static get instance() { return this.singletonInstance || (this.singletonInstance = new this()); } /** Gets active status */ get active() { return this.activeLocal; } /** Gets config */ get config() { return this.configLocal; } /** * Starts tracing. * @param userConfig A configuration object to start tracing. * @returns The started Tracing instance. */ start(userConfig) { this.configLocal = extend(true, {}, default_config_1.defaultConfig, { plugins: this.defaultPlugins }, userConfig); this.logger = this.configLocal.logger || core_1.logger.logger(this.configLocal.logLevel); this.configLocal.logger = this.logger; this.logger.debug('config: %o', this.configLocal); this.pluginLoader = new plugin_loader_1.PluginLoader(this.logger, this.tracer, this.configLocal.stats); this.pluginLoader.loadPlugins(this.configLocal.plugins); if (!this.configLocal.exporter) { const exporter = new core.ConsoleExporter(this.configLocal); this.registerExporter(exporter); } else { this.registerExporter(this.configLocal.exporter); } this.activeLocal = true; this.tracer.start(this.configLocal); return this; } /** Stops the tracing. */ stop() { this.activeLocal = false; this.tracer.stop(); if (this.pluginLoader) { this.pluginLoader.unloadPlugins(); } this.configLocal = {}; this.logger = core_1.logger.logger(); } /** Gets the exporter. */ get exporter() { return this.configLocal.exporter ? this.configLocal.exporter : NOOP_EXPORTER; } /** * Registers an exporter to send the collected traces to. * @param exporter The exporter to send the traces to. */ registerExporter(exporter) { if (this.configLocal.exporter) { this.unregisterExporter(this.configLocal.exporter); } if (exporter) { this.configLocal.exporter = exporter; this.tracer.registerSpanEventListener(exporter); } return this; } /** * Unregisters an exporter. * @param exporter The exporter to stop sending traces to. */ unregisterExporter(exporter) { this.tracer.unregisterSpanEventListener(exporter); this.configLocal.exporter = NOOP_EXPORTER; return this; } } exports.TracingBase = TracingBase;