1 | ;
|
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 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.ExporterBuffer = void 0;
|
19 | const logger = require("../common/console-logger");
|
20 | const DEFAULT_BUFFER_SIZE = 100;
|
21 | const DEFAULT_BUFFER_TIMEOUT = 20000;
|
22 | /** Controls the sending of traces to exporters. */
|
23 | class ExporterBuffer {
|
24 | /**
|
25 | * Constructs a new Buffer instance.
|
26 | * @param exporter The service to send the collected spans.
|
27 | * @param config A buffer configuration object to create a buffer.
|
28 | */
|
29 | constructor(exporter, config) {
|
30 | /** Manage when the buffer timeout needs to be reseted */
|
31 | this.resetTimeout = false;
|
32 | /** Indicates when the buffer timeout is running */
|
33 | this.bufferTimeoutInProgress = false;
|
34 | /** Trace queue of a buffer */
|
35 | this.queue = [];
|
36 | this.exporter = exporter;
|
37 | this.logger = config.logger || logger.logger();
|
38 | this.bufferSize = isNaN(Number(config.bufferSize))
|
39 | ? DEFAULT_BUFFER_SIZE
|
40 | : Number(config.bufferSize);
|
41 | this.bufferTimeout = isNaN(Number(config.bufferTimeout))
|
42 | ? DEFAULT_BUFFER_TIMEOUT
|
43 | : Number(config.bufferTimeout);
|
44 | return this;
|
45 | }
|
46 | /**
|
47 | * Set the buffer size value.
|
48 | * @param bufferSize The new buffer size.
|
49 | */
|
50 | setBufferSize(bufferSize) {
|
51 | this.bufferSize = bufferSize;
|
52 | return this;
|
53 | }
|
54 | getBufferSize() {
|
55 | return this.bufferSize;
|
56 | }
|
57 | getQueue() {
|
58 | return this.queue;
|
59 | }
|
60 | /**
|
61 | * Add a span in the buffer.
|
62 | * @param span Span to be added in the buffer.
|
63 | */
|
64 | addToBuffer(span) {
|
65 | this.queue.push(span);
|
66 | this.logger.debug('ExporterBuffer: added new span');
|
67 | if (this.queue.length > this.bufferSize) {
|
68 | this.flush();
|
69 | }
|
70 | if (this.bufferTimeoutInProgress) {
|
71 | this.resetBufferTimeout();
|
72 | }
|
73 | else {
|
74 | this.setBufferTimeout();
|
75 | }
|
76 | return this;
|
77 | }
|
78 | /** Reset the buffer timeout */
|
79 | resetBufferTimeout() {
|
80 | this.logger.debug('ExporterBuffer: reset timeout');
|
81 | this.resetTimeout = true;
|
82 | }
|
83 | /** Start the buffer timeout, when finished calls flush method */
|
84 | setBufferTimeout() {
|
85 | this.logger.debug('ExporterBuffer: set timeout');
|
86 | this.bufferTimeoutInProgress = true;
|
87 | const timer = setTimeout(() => {
|
88 | if (this.queue.length === 0) {
|
89 | return;
|
90 | }
|
91 | if (this.resetTimeout) {
|
92 | this.resetTimeout = false;
|
93 | this.setBufferTimeout();
|
94 | }
|
95 | else {
|
96 | this.bufferTimeoutInProgress = false;
|
97 | this.flush();
|
98 | }
|
99 | }, this.bufferTimeout);
|
100 | // Don't let this timer be the only thing keeping the process alive
|
101 | timer.unref();
|
102 | }
|
103 | /** Send the trace queue to all exporters */
|
104 | flush() {
|
105 | this.exporter.publish(this.queue);
|
106 | this.queue = [];
|
107 | return this;
|
108 | }
|
109 | }
|
110 | exports.ExporterBuffer = ExporterBuffer;
|
111 | //# sourceMappingURL=exporter-buffer.js.map |
\ | No newline at end of file |