You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by to...@apache.org on 2022/11/15 11:28:10 UTC

[skywalking-nodejs] branch master updated: Update AxiosPlugin for v1.0+ (#102)

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

tompytel 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 22f5352  Update AxiosPlugin for v1.0+ (#102)
22f5352 is described below

commit 22f5352a2fb085df4e4a8678ff66c5efd5a06741
Author: Tomasz Pytel <to...@gmail.com>
AuthorDate: Tue Nov 15 08:28:05 2022 -0300

    Update AxiosPlugin for v1.0+ (#102)
    
    * improved ignore path regex
    
    * fix issue #9165 express router entry duplicated
    
    * AWS DynamoDB, Lambda, SQS and SNS plugins
    
    * component IDs
    
    * update AxiosPlugin for v1.0+
---
 src/plugins/AxiosPlugin.ts  | 23 +++++++++++++++++++++--
 src/plugins/MySQL2Plugin.ts |  4 ++--
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/src/plugins/AxiosPlugin.ts b/src/plugins/AxiosPlugin.ts
index abe06f7..10a4db1 100644
--- a/src/plugins/AxiosPlugin.ts
+++ b/src/plugins/AxiosPlugin.ts
@@ -26,20 +26,39 @@ import { SpanLayer } from '../proto/language-agent/Tracing_pb';
 import DummySpan from '../trace/span/DummySpan';
 import { ignoreHttpMethodCheck } from '../config/AgentConfig';
 import PluginInstaller from '../core/PluginInstaller';
+import * as fs from 'fs';
+import * as path from 'path';
 
 class AxiosPlugin implements SwPlugin {
   readonly module = 'axios';
   readonly versions = '*';
 
+  getVersion(installer: PluginInstaller): string {
+    // TODO: this method will not work in a bundle
+    try {
+      const indexPath = installer.resolve(this.module);
+      const dirname = indexPath.slice(
+        0,
+        indexPath.lastIndexOf(`${path.sep}node_modules${path.sep}axios${path.sep}`) + 20,
+      );
+      const packageJsonStr = fs.readFileSync(`${dirname}package.json`, { encoding: 'utf-8' });
+      const pkg = JSON.parse(packageJsonStr);
+      return pkg.version;
+    } catch {
+      return '';
+    }
+  }
+
   install(installer: PluginInstaller): void {
     this.interceptClientRequest(installer);
   }
 
   private interceptClientRequest(installer: PluginInstaller): void {
-    const Axios = installer.require?.('axios/lib/core/Axios') ?? require('axios/lib/core/Axios');
+    const axios = installer.require?.('axios') ?? require('axios');
+    const Axios = axios.Axios;
     const _request = Axios.prototype.request;
 
-    Axios.prototype.request = function (url?: any, config?: any) {
+    Axios.prototype.request = axios.request = function (url?: any, config?: any) {
       if (typeof url === 'string') config = config ? { ...config, url } : { url };
       else config = url ? { ...url } : {};
 
diff --git a/src/plugins/MySQL2Plugin.ts b/src/plugins/MySQL2Plugin.ts
index cddebd7..ec5e70d 100644
--- a/src/plugins/MySQL2Plugin.ts
+++ b/src/plugins/MySQL2Plugin.ts
@@ -35,8 +35,8 @@ class MySQL2Plugin implements SwPlugin {
     // TODO: this method will not work in a bundle
     try {
       let indexPath = installer.resolve(this.module);
-      let packageSJonStr = fs.readFileSync(`${path.dirname(indexPath)}${path.sep}package.json`, { encoding: 'utf-8' });
-      const pkg = JSON.parse(packageSJonStr);
+      let packageJsonStr = fs.readFileSync(`${path.dirname(indexPath)}${path.sep}package.json`, { encoding: 'utf-8' });
+      const pkg = JSON.parse(packageJsonStr);
       return pkg.version;
     } catch {
       return '';