skywalking-backend-js-netease
Version:
The NodeJS agent for Apache SkyWalking
86 lines • 4.24 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 grpc = tslib_1.__importStar(require("@grpc/grpc-js"));
const grpc_js_1 = require("@grpc/grpc-js");
const packageInfo = tslib_1.__importStar(require("../../../../../package.json"));
const logging_1 = require("../../../../logging");
const Management_grpc_pb_1 = require("../../../../proto/management/Management_grpc_pb");
const AuthInterceptor_1 = tslib_1.__importDefault(require("../AuthInterceptor"));
const Management_pb_1 = require("../../../../proto/management/Management_pb");
const AgentConfig_1 = tslib_1.__importDefault(require("../../../../config/AgentConfig"));
const Common_pb_1 = require("../../../../proto/common/Common_pb");
const os = tslib_1.__importStar(require("os"));
const CommandExecute_1 = require("../../command/CommandExecute");
const logger = logging_1.createLogger(__filename);
class HeartbeatClient {
constructor() {
this.managementServiceClient = new Management_grpc_pb_1.ManagementServiceClient(AgentConfig_1.default.collectorAddress, AgentConfig_1.default.secure ? grpc.credentials.createSsl() : grpc.credentials.createInsecure());
}
get isConnected() {
return this.managementServiceClient.getChannel().getConnectivityState(true) === grpc_js_1.connectivityState.READY;
}
start() {
if (this.heartbeatTimer) {
logger.warn(`
The heartbeat timer has already been scheduled,
this may be a potential bug, please consider reporting
this to ${packageInfo.bugs.url}
`);
return;
}
const keepAlivePkg = new Management_pb_1.InstancePingPkg()
.setService(AgentConfig_1.default.serviceName)
.setServiceinstance(AgentConfig_1.default.serviceInstance);
const instanceProperties = new Management_pb_1.InstanceProperties()
.setService(AgentConfig_1.default.serviceName)
.setServiceinstance(AgentConfig_1.default.serviceInstance)
.setPropertiesList([
new Common_pb_1.KeyStringValuePair().setKey('language').setValue('NodeJS'),
new Common_pb_1.KeyStringValuePair().setKey('OS Name').setValue(os.platform()),
new Common_pb_1.KeyStringValuePair().setKey('hostname').setValue(os.hostname()),
new Common_pb_1.KeyStringValuePair().setKey('Process No.').setValue(`${process.pid}`),
]);
this.heartbeatTimer = setInterval(() => {
this.managementServiceClient.reportInstanceProperties(instanceProperties, AuthInterceptor_1.default(), (error, resp) => {
if (error) {
logger.error('Failed to send heartbeat', error);
}
// do with resp commands
CommandExecute_1.execute(resp);
});
this.managementServiceClient.keepAlive(keepAlivePkg, AuthInterceptor_1.default(), (error, resp) => {
if (error) {
logger.error('Failed to send heartbeat', error);
return;
}
// do with resp commands
CommandExecute_1.execute(resp);
});
}, 20000).unref();
}
flush() {
logger.warn('HeartbeatClient does not need flush().');
return null;
}
}
exports.default = HeartbeatClient;
//# sourceMappingURL=HeartbeatClient.js.map