skywalking-backend-js-netease
Version:
The NodeJS agent for Apache SkyWalking
102 lines • 4.48 kB
JavaScript
;
/*!
*
* 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 });
const tslib_1 = require("tslib");
const AgentConfig_1 = tslib_1.__importDefault(require("../../../../config/AgentConfig"));
const grpc = tslib_1.__importStar(require("@grpc/grpc-js"));
const grpc_js_1 = require("@grpc/grpc-js");
const logging_1 = require("../../../../logging");
const Tracing_grpc_pb_1 = require("../../../../proto/language-agent/Tracing_grpc_pb");
const AuthInterceptor_1 = tslib_1.__importDefault(require("../AuthInterceptor"));
const SegmentObjectAdapter_1 = tslib_1.__importDefault(require("../SegmentObjectAdapter"));
const EventEmitter_1 = require("../../../../lib/EventEmitter");
const CommandExecute_1 = require("../../command/CommandExecute");
const logger = logging_1.createLogger(__filename);
class TraceReportClient {
constructor() {
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', (segment) => {
var _a;
this.buffer.push(segment);
(_a = this.timeout) === null || _a === void 0 ? void 0 : _a.ref();
});
}
get isConnected() {
var _a;
return ((_a = this.reporterClient) === null || _a === void 0 ? void 0 : _a.getChannel().getConnectivityState(true)) === grpc_js_1.connectivityState.READY;
}
reportFunction(callback) {
EventEmitter_1.emitter.emit('segments-sent'); // reset limiter in SpanContext
try {
if (this.buffer.length === 0) {
if (callback)
callback();
return;
}
const stream = this.reporterClient.collect(AuthInterceptor_1.default(), (error, resp) => {
if (error) {
logger.error('Failed to report trace data', error);
}
// do with resp commands
CommandExecute_1.execute(resp);
if (callback)
callback();
});
stream.on('error', (err) => {
if (err) {
logger.error('Failed to report trace data', err);
}
});
try {
for (const segment of this.buffer) {
if (segment) {
if (logger._isDebugEnabled) {
logger.debug('Sending 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();
}
}
start() {
this.timeout = setTimeout(this.reportFunction.bind(this), 1000).unref();
}
flush() {
// 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.
return this.buffer.length === 0
? null
: new Promise((resolve) => {
this.reportFunction(resolve);
});
}
}
exports.default = TraceReportClient;
//# sourceMappingURL=TraceReportClient.js.map