UNPKG

@kangc/skywalking-backend-js

Version:

The NodeJS agent for Apache SkyWalking

102 lines 4.62 kB
"use strict"; /*! * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 }); var tslib_1 = require("tslib"); var AgentConfig_1 = tslib_1.__importDefault(require("../../../../config/AgentConfig")); var grpc = tslib_1.__importStar(require("@grpc/grpc-js")); var grpc_js_1 = require("@grpc/grpc-js"); var logging_1 = require("../../../../logging"); var Tracing_grpc_pb_1 = require("../../../../proto/language-agent/Tracing_grpc_pb"); var AuthInterceptor_1 = tslib_1.__importDefault(require("../AuthInterceptor")); var SegmentObjectAdapter_1 = tslib_1.__importDefault(require("../SegmentObjectAdapter")); var EventEmitter_1 = require("../../../../lib/EventEmitter"); var logger = logging_1.createLogger(__filename); var TraceReportClient = /** @class */ (function () { function TraceReportClient() { var _this = this; this.buffer = []; this.reporterClient = new Tracing_grpc_pb_1.TraceSegmentReportServiceClient(AgentConfig_1.default.collectorAddress, AgentConfig_1.default.secure ? grpc.credentials.createSsl() : grpc.credentials.createInsecure()); EventEmitter_1.emitter.on('segment-finished', function (segment) { var _a; _this.buffer.push(segment); (_a = _this.timeout) === null || _a === void 0 ? void 0 : _a.ref(); }); } Object.defineProperty(TraceReportClient.prototype, "isConnected", { get: function () { var _a; return ((_a = this.reporterClient) === null || _a === void 0 ? void 0 : _a.getChannel().getConnectivityState(true)) === grpc_js_1.connectivityState.READY; }, enumerable: false, configurable: true }); TraceReportClient.prototype.reportFunction = function (callback) { EventEmitter_1.emitter.emit('segments-sent'); // reset limiter in SpanContext try { if (this.buffer.length === 0) { if (callback) callback(); return; } var stream = this.reporterClient.collect(AuthInterceptor_1.default(), function (error, _) { if (error) { logger.error('Failed to report trace data', error); } if (callback) callback(); }); try { for (var _i = 0, _a = this.buffer; _i < _a.length; _i++) { var segment = _a[_i]; if (segment) { if (logger._isDebugEnabled) { logger.debug('Sending segment ', { segment: segment }); } stream.write(new SegmentObjectAdapter_1.default(segment)); } } } finally { this.buffer.length = 0; } stream.end(); } finally { this.timeout = setTimeout(this.reportFunction.bind(this), 1000).unref(); } }; TraceReportClient.prototype.start = function () { this.timeout = setTimeout(this.reportFunction.bind(this), 1000).unref(); }; TraceReportClient.prototype.flush = function () { // This function explicitly returns null instead of a resolved Promise in case of nothing to flush so that in this // case passing control back to the event loop can be avoided. Even a resolved Promise will run other things in // the event loop when it is awaited and before it continues. var _this = this; return this.buffer.length === 0 ? null : new Promise(function (resolve) { _this.reportFunction(resolve); }); }; return TraceReportClient; }()); exports.default = TraceReportClient; //# sourceMappingURL=TraceReportClient.js.map