text: "use strict";
  /*
   * Copyright The OpenTelemetry 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
   *
   *      https://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.
   */
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.BatchSpanProcessor = void 0;
  const api_1 = require("@opentelemetry/api");
  const core_1 = require("@opentelemetry/core");
  const DEFAULT_BUFFER_SIZE = 100;
  const DEFAULT_BUFFER_TIMEOUT_MS = 20000;
  /**
   * Implementation of the {@link SpanProcessor} that batches spans exported by
   * the SDK then pushes them to the exporter pipeline.
   */
  class BatchSpanProcessor {
      constructor(_exporter, config) {
          this._exporter = _exporter;
          this._finishedSpans = [];
          this._isShutdown = false;
          this._shuttingDownPromise = Promise.resolve();
          this._bufferSize =
              config && config.bufferSize ? config.bufferSize : DEFAULT_BUFFER_SIZE;
          this._bufferTimeout =
              config && typeof config.bufferTimeout === 'number'
                  ? config.bufferTimeout
                  : DEFAULT_BUFFER_TIMEOUT_MS;
      }
      forceFlush() {
          if (this._isShutdown) {
              return this._shuttingDownPromise;
          }
          return this._flush();
      }
      // does nothing.
      onStart(span) { }
      onEnd(span) {
          if (this._isShutdown) {
              return;
          }
          this._addToBuffer(span);
      }
      shutdown() {
          if (this._isShutdown) {
              return this._shuttingDownPromise;
          }
          this._isShutdown = true;
          this._shuttingDownPromise = new Promise((resolve, reject) => {
              Promise.resolve()
                  .then(() => {
                  return this._flush();
              })
                  .then(() => {
                  return this._exporter.shutdown();
              })
                  .then(resolve)
                  .catch(e => {
                  reject(e);
              });
          });
          return this._shuttingDownPromise;
      }
      /** Add a span in the buffer. */
      _addToBuffer(span) {
          this._finishedSpans.push(span);
          this._maybeStartTimer();
          if (this._finishedSpans.length > this._bufferSize) {
              this._flush();
          }
      }
      /** Send the span data list to exporter */
      _flush() {
          this._clearTimer();
          if (this._finishedSpans.length === 0) {
              return Promise.resolve();
          }
          return new Promise((resolve, reject) => {
              // prevent downstream exporter calls from generating spans
              api_1.context.with(api_1.suppressInstrumentation(api_1.context.active()), () => {
                  this._exporter.export(this._finishedSpans, result => {
                      this._finishedSpans = [];
                      if (result === core_1.ExportResult.SUCCESS) {
                          resolve();
                      }
                      else {
                          reject(result);
                      }
                  });
              });
          });
      }
      _maybeStartTimer() {
          if (this._timer !== undefined)
              return;
          this._timer = setTimeout(() => {
              this._flush().catch();
          }, this._bufferTimeout);
          core_1.unrefTimer(this._timer);
      }
      _clearTimer() {
          if (this._timer !== undefined) {
              clearTimeout(this._timer);
              this._timer = undefined;
          }
      }
  }
  exports.BatchSpanProcessor = BatchSpanProcessor;
  //# sourceMappingURL=BatchSpanProcessor.js.map