Avoid init the client twice, make sure the config take effect (#12)

Co-authored-by: kezhenxu94 <kezhenxu94@apache.org>
diff --git a/src/agent/protocol/grpc/clients/HeartbeatClient.ts b/src/agent/protocol/grpc/clients/HeartbeatClient.ts
old mode 100644
new mode 100755
index 8873d3d..bd36b7a
--- a/src/agent/protocol/grpc/clients/HeartbeatClient.ts
+++ b/src/agent/protocol/grpc/clients/HeartbeatClient.ts
@@ -33,20 +33,18 @@
 const logger = createLogger(__filename);
 
 class HeartbeatClient implements Client {
-  heartbeatClient: ManagementServiceClient;
+  heartbeatClient?: ManagementServiceClient;
   heartbeatTimer?: NodeJS.Timeout;
 
-  constructor() {
-    this.heartbeatClient = new ManagementServiceClient(config.collectorAddress, grpc.credentials.createInsecure(), {
-      interceptors: [AuthInterceptor],
-    });
-  }
-
   get isConnected(): boolean {
-    return this.heartbeatClient.getChannel().getConnectivityState(true) === connectivityState.READY;
+    return this.heartbeatClient?.getChannel().getConnectivityState(true) === connectivityState.READY;
   }
 
   start() {
+    this.heartbeatClient = new ManagementServiceClient(config.collectorAddress, grpc.credentials.createInsecure(), {
+      interceptors: [AuthInterceptor],
+    });
+
     if (this.heartbeatTimer) {
       logger.warn(`
         The heartbeat timer has already been scheduled,
@@ -71,7 +69,7 @@
     ]);
 
     this.heartbeatTimer = setInterval(() => {
-      this.heartbeatClient.reportInstanceProperties(
+      this.heartbeatClient?.reportInstanceProperties(
         instanceProperties,
 
         (error, _) => {
@@ -80,7 +78,7 @@
           }
         },
       );
-      this.heartbeatClient.keepAlive(
+      this.heartbeatClient?.keepAlive(
         keepAlivePkg,
 
         (error, _) => {
diff --git a/src/agent/protocol/grpc/clients/TraceReportClient.ts b/src/agent/protocol/grpc/clients/TraceReportClient.ts
old mode 100644
new mode 100755
index 028ddbf..9a18032
--- a/src/agent/protocol/grpc/clients/TraceReportClient.ts
+++ b/src/agent/protocol/grpc/clients/TraceReportClient.ts
@@ -30,19 +30,11 @@
 const logger = createLogger(__filename);
 
 class TraceReportClient implements Client {
-  reporterClient: TraceSegmentReportServiceClient;
+  reporterClient?: TraceSegmentReportServiceClient;
   timeout: any;
 
-  constructor() {
-    this.reporterClient = new TraceSegmentReportServiceClient(
-      config.collectorAddress,
-      grpc.credentials.createInsecure(),
-      { interceptors: [AuthInterceptor] },
-    );
-  }
-
   get isConnected(): boolean {
-    return this.reporterClient.getChannel().getConnectivityState(true) === connectivityState.READY;
+    return this.reporterClient?.getChannel().getConnectivityState(true) === connectivityState.READY;
   }
 
   ref() {
@@ -50,9 +42,14 @@
   }
 
   start() {
+    this.reporterClient = new TraceSegmentReportServiceClient(
+      config.collectorAddress,
+      grpc.credentials.createInsecure(),
+      { interceptors: [AuthInterceptor] },
+    );
     const reportFunction = () => {
       try {
-        if (buffer.length === 0) {
+        if (buffer.length === 0 || !this.reporterClient) {
           return;
         }