You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2020/12/10 16:52:18 UTC

[skywalking-nodejs] branch master updated: chore: clean up unused codes and simplify (#10)

This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-nodejs.git


The following commit(s) were added to refs/heads/master by this push:
     new e06e5c5  chore: clean up unused codes and simplify (#10)
e06e5c5 is described below

commit e06e5c5f95478d5f02079d5f689516e0379bfbe3
Author: Tomasz Pytel <to...@gmail.com>
AuthorDate: Thu Dec 10 13:51:02 2020 -0300

    chore: clean up unused codes and simplify (#10)
    
    * Cleanup: removed Context.capture() and .restore()
    
    * Cleanup: cleared up Span.inject(), other misc
---
 src/config/AgentConfig.ts         | 14 +++++++-------
 src/plugins/AxiosPlugin.ts        |  2 +-
 src/plugins/HttpPlugin.ts         | 11 ++++++++---
 src/trace/context/Context.ts      |  7 +------
 src/trace/context/DummyContext.ts | 17 +----------------
 src/trace/context/SegmentRef.ts   | 15 ---------------
 src/trace/context/Snapshot.ts     | 27 ---------------------------
 src/trace/context/SpanContext.ts  | 26 ++------------------------
 src/trace/span/Span.ts            |  4 ++++
 9 files changed, 24 insertions(+), 99 deletions(-)

diff --git a/src/config/AgentConfig.ts b/src/config/AgentConfig.ts
index cdae60f..2eaa156 100644
--- a/src/config/AgentConfig.ts
+++ b/src/config/AgentConfig.ts
@@ -34,14 +34,14 @@ export type AgentConfig = {
 export function finalizeConfig(config: AgentConfig): void {
   const escapeRegExp = (s: string) => s.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
 
-  const ignoreSuffix =`^.+(?:${config.ignoreSuffix!.split(',').map(s => escapeRegExp(s.trim())).join('|')})$`;
+  const ignoreSuffix =`^.+(?:${config.ignoreSuffix!.split(',').map((s) => escapeRegExp(s.trim())).join('|')})$`;
   const ignorePath = '^(?:' + config.traceIgnorePath!.split(',').map(
-    s1 => s1.trim().split('**').map(
-      s2 => s2.split('*').map(
-        s3 => s3.split('?').map(escapeRegExp).join('[^/]')  // replaces "**"
-      ).join('[^/]*')                                       // replaces "*"
-    ).join('(?:(?:[^/]+\.)*[^/]+)?')                        // replaces "?"
-  ).join('|') + ')$';                                       // replaces ","
+    (s1) => s1.trim().split('**').map(
+      (s2) => s2.split('*').map(
+        (s3) => s3.split('?').map(escapeRegExp).join('[^/]')  // replaces "**"
+      ).join('[^/]*')                                         // replaces "*"
+    ).join('(?:(?:[^/]+\.)*[^/]+)?')                          // replaces "?"
+  ).join('|') + ')$';                                         // replaces ","
 
   config.reIgnoreOperation = RegExp(`${ignoreSuffix}|${ignorePath}`);
 }
diff --git a/src/plugins/AxiosPlugin.ts b/src/plugins/AxiosPlugin.ts
index 09ff877..37e330c 100644
--- a/src/plugins/AxiosPlugin.ts
+++ b/src/plugins/AxiosPlugin.ts
@@ -97,7 +97,7 @@ class AxiosPlugin implements SwPlugin {
       const span = ContextManager.current.newExitSpan(operation, host).start();
 
       try {
-        span.component = Component.UNKNOWN;
+        span.component = Component.HTTP;  // TODO: add Component.AXIOS (to main Skywalking project)
         span.layer = SpanLayer.HTTP;
         span.peer = host;
         span.tag(Tag.httpURL(host + operation));
diff --git a/src/plugins/HttpPlugin.ts b/src/plugins/HttpPlugin.ts
index b237c8b..2984cc5 100644
--- a/src/plugins/HttpPlugin.ts
+++ b/src/plugins/HttpPlugin.ts
@@ -70,11 +70,16 @@ class HttpPlugin implements SwPlugin {
       const span: ExitSpan = ContextManager.current.newExitSpan(operation, host).start() as ExitSpan;
 
       try {
-        if (span.depth === 1) {
+        if (span.depth === 1) {  // only set HTTP if this span is not overridden by a higher level one
           span.component = Component.HTTP;
           span.layer = SpanLayer.HTTP;
+        }
+        if (!span.peer) {
           span.peer = host;
-          span.tag(Tag.httpURL(host + pathname));
+        }
+        const httpURL = host + pathname;
+        if (!span.hasTag(httpURL)) {
+          span.tag(Tag.httpURL(httpURL));
         }
 
         const request: ClientRequest = _request.apply(this, arguments);
@@ -104,7 +109,7 @@ class HttpPlugin implements SwPlugin {
         return request;
 
       } catch (e) {
-        if (!stopped) {
+        if (!stopped) {  // don't want to set error if exception occurs after clean close
           span.error(e);
           stopIfNotStopped();
         }
diff --git a/src/trace/context/Context.ts b/src/trace/context/Context.ts
index d1b234e..8a23252 100644
--- a/src/trace/context/Context.ts
+++ b/src/trace/context/Context.ts
@@ -19,7 +19,6 @@
 
 import Span from '../../trace/span/Span';
 import Segment from '../../trace/context/Segment';
-import Snapshot from '../../trace/context/Snapshot';
 import { ContextCarrier } from './ContextCarrier';
 
 export default interface Context {
@@ -29,7 +28,7 @@ export default interface Context {
 
   newEntrySpan(operation: string, carrier?: ContextCarrier): Span;
 
-  newExitSpan(operation: string, peer: string, carrier?: ContextCarrier): Span;
+  newExitSpan(operation: string, peer: string): Span;
 
   start(span: Span): Context;
 
@@ -45,8 +44,4 @@ export default interface Context {
   resync(span: Span): void;
 
   currentSpan(): Span | undefined;
-
-  capture(): Snapshot;
-
-  restore(snapshot: Snapshot): void;
 }
diff --git a/src/trace/context/DummyContext.ts b/src/trace/context/DummyContext.ts
index 68c44cf..b64a1dd 100644
--- a/src/trace/context/DummyContext.ts
+++ b/src/trace/context/DummyContext.ts
@@ -22,8 +22,6 @@ import Span from '../../trace/span/Span';
 import DummySpan from '../../trace/span/DummySpan';
 import Segment from '../../trace/context/Segment';
 import { SpanType } from '../../proto/language-agent/Tracing_pb';
-import Snapshot from '../../trace/context/Snapshot';
-import ID from '../../trace/ID';
 import { ContextCarrier } from './ContextCarrier';
 
 export default class DummyContext implements Context {
@@ -39,7 +37,7 @@ export default class DummyContext implements Context {
     return this.span;
   }
 
-  newExitSpan(operation: string, peer: string, carrier?: ContextCarrier): Span {
+  newExitSpan(operation: string, peer: string): Span {
     return this.span;
   }
 
@@ -67,17 +65,4 @@ export default class DummyContext implements Context {
   currentSpan(): Span {
     throw new Error('DummyContext.currentSpan() should never be called!');
   }
-
-  capture(): Snapshot {
-    return {
-      parentEndpoint: '',
-      segmentId: new ID(),
-      spanId: 0,
-      traceId: new ID(),
-    };
-  }
-
-  restore(snapshot: Snapshot) {
-    return;
-  }
 }
diff --git a/src/trace/context/SegmentRef.ts b/src/trace/context/SegmentRef.ts
index 2cc8cad..430579e 100644
--- a/src/trace/context/SegmentRef.ts
+++ b/src/trace/context/SegmentRef.ts
@@ -17,9 +17,7 @@
  *
  */
 
-import Snapshot from '../../trace/context/Snapshot';
 import ID from '../../trace/ID';
-import config from '../../config/AgentConfig';
 import { ContextCarrier } from './ContextCarrier';
 
 export default class SegmentRef {
@@ -54,17 +52,4 @@ export default class SegmentRef {
       carrier.clientAddress!,
     );
   }
-
-  static fromSnapshot(snapshot: Snapshot): SegmentRef {
-    return new SegmentRef(
-      'CrossThread',
-      new ID(snapshot.traceId.toString()),
-      new ID(snapshot.segmentId.toString()),
-      snapshot.spanId,
-      config.serviceName,
-      config.serviceInstance,
-      snapshot.parentEndpoint,
-      '',
-    );
-  }
 }
diff --git a/src/trace/context/Snapshot.ts b/src/trace/context/Snapshot.ts
deleted file mode 100644
index e5de84b..0000000
--- a/src/trace/context/Snapshot.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-/*!
- *
- * 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.
- *
- */
-
-import ID from '../../trace/ID';
-
-export default interface Snapshot {
-  segmentId: ID;
-  spanId: number;
-  traceId: ID;
-  parentEndpoint: string;
-}
diff --git a/src/trace/context/SpanContext.ts b/src/trace/context/SpanContext.ts
index f1daaa3..196dd51 100644
--- a/src/trace/context/SpanContext.ts
+++ b/src/trace/context/SpanContext.ts
@@ -29,8 +29,6 @@ import LocalSpan from '../../trace/span/LocalSpan';
 import buffer from '../../agent/Buffer';
 import { createLogger } from '../../logging';
 import { executionAsyncId } from 'async_hooks';
-import Snapshot from '../../trace/context/Snapshot';
-import SegmentRef from '../../trace/context/SegmentRef';
 import { ContextCarrier } from './ContextCarrier';
 import ContextManager from './ContextManager';
 import { SpanType } from '../../proto/language-agent/Tracing_pb';
@@ -101,7 +99,7 @@ export default class SpanContext implements Context {
     return span;
   }
 
-  newExitSpan(operation: string, peer: string, carrier?: ContextCarrier): Span {
+  newExitSpan(operation: string, peer: string): Span {
     if (logger.isDebugEnabled()) {
       logger.debug('Creating exit span', {
         parentId: this.parentId,
@@ -128,10 +126,6 @@ export default class SpanContext implements Context {
         peer,
         operation,
       });
-
-      // if (carrier && carrier.isValid()) {  // is this right?
-      //   Object.assign(carrier, span.inject());
-      // }
     }
 
     return span;
@@ -181,7 +175,7 @@ export default class SpanContext implements Context {
       ContextManager.spans.splice(idx, 1);
     }
 
-    if (--this.nSpans == 0) {
+    if (--this.nSpans === 0) {
       buffer.put(this.segment);
       ContextManager.clear();
       return true;
@@ -217,20 +211,4 @@ export default class SpanContext implements Context {
   currentSpan(): Span | undefined {
     return ContextManager.spans[ContextManager.spans.length - 1];
   }
-
-  capture(): Snapshot {
-    return {
-      segmentId: this.segment.segmentId,
-      spanId: this.currentSpan()?.id ?? -1,
-      traceId: this.segment.relatedTraces[0],
-      parentEndpoint: ContextManager.spans[0].operation,
-    };
-  }
-
-  restore(snapshot: Snapshot) {
-    const ref = SegmentRef.fromSnapshot(snapshot);
-    this.segment.refer(ref);
-    this.currentSpan()?.refer(ref);
-    this.segment.relate(ref.traceId);
-  }
 }
diff --git a/src/trace/span/Span.ts b/src/trace/span/Span.ts
index 3e245a2..f97cb0a 100644
--- a/src/trace/span/Span.ts
+++ b/src/trace/span/Span.ts
@@ -117,6 +117,10 @@ export default abstract class Span {
     return this;
   }
 
+  hasTag(key: string) {
+    return !this.tags.every((t) => t.key !== key);
+  }
+
   tag(tag: Tag): this {
     if (!tag.overridable) {
       this.tags.push(Object.assign({}, tag));