@@ -26,20 +26,39 @@ import { SpanLayer } from '../proto/language-agent/Tracing_pb';
2626import DummySpan from '../trace/span/DummySpan' ;
2727import { ignoreHttpMethodCheck } from '../config/AgentConfig' ;
2828import PluginInstaller from '../core/PluginInstaller' ;
29+ import * as fs from 'fs' ;
30+ import * as path from 'path' ;
2931
3032class AxiosPlugin implements SwPlugin {
3133 readonly module = 'axios' ;
3234 readonly versions = '*' ;
3335
36+ getVersion ( installer : PluginInstaller ) : string {
37+ // TODO: this method will not work in a bundle
38+ try {
39+ const indexPath = installer . resolve ( this . module ) ;
40+ const dirname = indexPath . slice (
41+ 0 ,
42+ indexPath . lastIndexOf ( `${ path . sep } node_modules${ path . sep } axios${ path . sep } ` ) + 20 ,
43+ ) ;
44+ const packageJsonStr = fs . readFileSync ( `${ dirname } package.json` , { encoding : 'utf-8' } ) ;
45+ const pkg = JSON . parse ( packageJsonStr ) ;
46+ return pkg . version ;
47+ } catch {
48+ return '' ;
49+ }
50+ }
51+
3452 install ( installer : PluginInstaller ) : void {
3553 this . interceptClientRequest ( installer ) ;
3654 }
3755
3856 private interceptClientRequest ( installer : PluginInstaller ) : void {
39- const Axios = installer . require ?.( 'axios/lib/core/Axios' ) ?? require ( 'axios/lib/core/Axios' ) ;
57+ const axios = installer . require ?.( 'axios' ) ?? require ( 'axios' ) ;
58+ const Axios = axios . Axios ;
4059 const _request = Axios . prototype . request ;
4160
42- Axios . prototype . request = function ( url ?: any , config ?: any ) {
61+ Axios . prototype . request = axios . request = function ( url ?: any , config ?: any ) {
4362 if ( typeof url === 'string' ) config = config ? { ...config , url } : { url } ;
4463 else config = url ? { ...url } : { } ;
4564
0 commit comments