@grafana/faro-transport-otlp-http
Version:
Faro transport which converts the Faro data model to the Otlp data model.
65 lines • 3.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OtelPayload = void 0;
var faro_core_1 = require("@grafana/faro-core");
var transform_1 = require("./transform");
var OtelPayload = /** @class */ (function () {
function OtelPayload(_a) {
var internalLogger = _a.internalLogger, customOtlpTransform = _a.customOtlpTransform, transportItem = _a.transportItem;
this.resourceSpans = [];
this.internalLogger = internalLogger;
this.resourceLogs = [];
this.getLogTransforms = (0, transform_1.getLogTransforms)(this.internalLogger, customOtlpTransform);
this.getTraceTransforms = (0, transform_1.getTraceTransforms)(this.internalLogger);
if (transportItem) {
this.addResourceItem(transportItem);
}
}
OtelPayload.prototype.getPayload = function () {
return {
resourceLogs: this.resourceLogs,
resourceSpans: this.resourceSpans,
};
};
OtelPayload.prototype.addResourceItem = function (transportItem) {
var _a, _b, _c, _d;
var type = transportItem.type;
try {
switch (type) {
case faro_core_1.TransportItemType.LOG:
case faro_core_1.TransportItemType.EXCEPTION:
case faro_core_1.TransportItemType.EVENT:
case faro_core_1.TransportItemType.MEASUREMENT:
var _e = this.getLogTransforms, toLogRecord = _e.toLogRecord, toResourceLog = _e.toResourceLog;
// Currently the scope is fixed to '@grafana/faro-web-sdk'.
// Once we are able to drive the scope by instrumentation this will change and we need to align this function
if (this.resourceLogs.length === 0) {
this.resourceLogs = [toResourceLog(transportItem)];
}
else {
// Faro takes care of the grouping with different metadata (or OTel attributes), so we can safely
// use the just the first element of the resource.
(_b = (_a = this.resourceLogs[0]) === null || _a === void 0 ? void 0 : _a.scopeLogs[0]) === null || _b === void 0 ? void 0 : _b.logRecords.push(toLogRecord(transportItem));
}
break;
case faro_core_1.TransportItemType.TRACE:
var toResourceSpan = this.getTraceTransforms.toResourceSpan;
// We use the Otel Model as it is to avoid unnecessary resource consumption.
// This is because we don't need the same logic to add items as it is for logs.
// Also the Otel library already applies the respective protocol transforms so there is no need for additional transforms.
// We only transform the resource object to ensure that we are compliant with the respective Faro Metas which add a few more items to the resource object.
this.resourceSpans.push(toResourceSpan(transportItem));
break;
default:
(_c = this.internalLogger) === null || _c === void 0 ? void 0 : _c.error("Unknown TransportItemType: ".concat(type));
break;
}
}
catch (error) {
(_d = this.internalLogger) === null || _d === void 0 ? void 0 : _d.error(error);
}
};
return OtelPayload;
}());
exports.OtelPayload = OtelPayload;
//# sourceMappingURL=OtelPayload.js.map