@dynatrace/opentelemetry-core
Version:
Dynatrace OpenTelemetry core package
93 lines (90 loc) • 3.59 kB
JavaScript
"use strict";
/*
Copyright 2022 Dynatrace LLC
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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpanMetaData = exports.PrepareResult = exports.SendState = void 0;
const perf_hooks_1 = require("perf_hooks");
// ----------------------------------------------------------------------------
var SendState;
(function (SendState) {
SendState[SendState["New"] = 0] = "New";
SendState[SendState["Skip"] = 1] = "Skip";
SendState[SendState["Drop"] = 2] = "Drop";
SendState[SendState["Update"] = 3] = "Update";
SendState[SendState["Alive"] = 4] = "Alive";
SendState[SendState["Finished"] = 5] = "Finished";
})(SendState = exports.SendState || (exports.SendState = {}));
// ----------------------------------------------------------------------------
var PrepareResult;
(function (PrepareResult) {
PrepareResult[PrepareResult["Drop"] = 0] = "Drop";
PrepareResult[PrepareResult["Send"] = 1] = "Send";
PrepareResult[PrepareResult["Skip"] = 2] = "Skip";
})(PrepareResult = exports.PrepareResult || (exports.PrepareResult = {}));
// ----------------------------------------------------------------------------
/**
* Meta data assoziated to a Span needed by DtSpanProcessor/DtSpanExporter/Propagator.
*/
class SpanMetaData {
constructor(_options) {
this._options = _options;
this._firstSeenMs = perf_hooks_1.performance.now();
this._sendState = SendState.New;
this._lastSentMs = 0;
this._seqNr = -1;
}
prepareSend(now, isFinished) {
const shouldSend = this._prepareSend(now, isFinished);
if (shouldSend === PrepareResult.Send) {
this._lastSentMs = now;
this._seqNr++;
}
return shouldSend;
}
get seqNr() {
return this._seqNr;
}
get sendState() {
return this._sendState;
}
_prepareSend(now, isFinished) {
// always send finished spans
if (isFinished) {
this._sendState = SendState.Finished;
return PrepareResult.Send;
}
// drop outdated, non finished spans
const ageMs = now - this._firstSeenMs;
if (ageMs >= this._options.openSpanTimeoutMs) {
this._sendState = SendState.Drop;
return PrepareResult.Drop;
}
// new, non finished spans should be sent only if they are old enough
if (this._sendState === SendState.New) {
if (ageMs >= this._options.updateIntervalMs) {
this._sendState = SendState.Update;
return PrepareResult.Send;
}
return PrepareResult.Skip;
}
// Open spans should be sent as keep alive
if (now - this._lastSentMs >= this._options.keepAliveIntervalMs) {
this._sendState = SendState.Alive;
return PrepareResult.Send;
}
this._sendState = SendState.Skip;
return PrepareResult.Skip;
}
}
exports.SpanMetaData = SpanMetaData;
//# sourceMappingURL=SpanMetaData.js.map