feat: add deadline for trace request (#118)

diff --git a/README.md b/README.md
index 935a5cd..4b19e74 100644
--- a/README.md
+++ b/README.md
@@ -69,6 +69,8 @@
 | `SW_AWS_LAMBDA_CHAIN` | Pass trace ID to AWS Lambda function in its parameters (to allow linking). Only use if both caller and callee will be instrumented. | `false` |
 | `SW_AWS_SQS_CHECK_BODY` | Incoming SQS messages check inside the body for trace ID in order to allow linking outgoing SNS messages to incoming SQS. | `false` |
 | `SW_AGENT_MAX_BUFFER_SIZE` | The maximum buffer size before sending the segment data to backend | `'1000'` |
+| `SW_AGENT_TRACE_TIMEOUT` | The timeout for trace requests to backend services | `'10000'` |
+
 
 Note that the various ignore options like `SW_IGNORE_SUFFIX`, `SW_TRACE_IGNORE_PATH` and `SW_HTTP_IGNORE_METHOD` as well as endpoints which are not recorded due to exceeding `SW_AGENT_MAX_BUFFER_SIZE` all propagate their ignored status downstream to any other endpoints they may call. If that endpoint is running the Node Skywalking agent then regardless of its ignore settings it will not be recorded since its upstream parent was not recorded. This allows the elimination of entire trees of endpoints you are not interested in as well as eliminating partial traces if a span in the chain is ignored but calls out to other endpoints which are recorded as children of ROOT instead of the actual parent.
 
diff --git a/src/agent/protocol/grpc/clients/TraceReportClient.ts b/src/agent/protocol/grpc/clients/TraceReportClient.ts
index 1ef0003..3f6f102 100755
--- a/src/agent/protocol/grpc/clients/TraceReportClient.ts
+++ b/src/agent/protocol/grpc/clients/TraceReportClient.ts
@@ -60,13 +60,17 @@
         return;
       }
 
-      const stream = this.reporterClient.collect(AuthInterceptor(), (error, _) => {
-        if (error) {
-          logger.error('Failed to report trace data', error);
-        }
+      const stream = this.reporterClient.collect(
+        AuthInterceptor(),
+        { deadline: Date.now() + config.traceTimeout },
+        (error, _) => {
+          if (error) {
+            logger.error('Failed to report trace data', error);
+          }
 
-        if (callback) callback();
-      });
+          if (callback) callback();
+        },
+      );
 
       try {
         for (const segment of this.buffer) {
diff --git a/src/config/AgentConfig.ts b/src/config/AgentConfig.ts
index ad82b59..ca33587 100644
--- a/src/config/AgentConfig.ts
+++ b/src/config/AgentConfig.ts
@@ -42,6 +42,7 @@
   reDisablePlugins?: RegExp;
   reIgnoreOperation?: RegExp;
   reHttpIgnoreMethod?: RegExp;
+  traceTimeout?: number;
 };
 
 export function finalizeConfig(config: AgentConfig): void {
@@ -136,6 +137,9 @@
   reDisablePlugins: RegExp(''), // temporary placeholder so Typescript doesn't throw a fit
   reIgnoreOperation: RegExp(''),
   reHttpIgnoreMethod: RegExp(''),
+  traceTimeout: Number.isSafeInteger(process.env.SW_AGENT_TRACE_TIMEOUT)
+    ? Number.parseInt(process.env.SW_AGENT_TRACE_TIMEOUT as string, 10)
+    : 10 * 1000,
 };
 
 export default _config;