@@ -26,20 +26,39 @@ import { SpanLayer } from '../proto/language-agent/Tracing_pb';
26
26
import DummySpan from '../trace/span/DummySpan' ;
27
27
import { ignoreHttpMethodCheck } from '../config/AgentConfig' ;
28
28
import PluginInstaller from '../core/PluginInstaller' ;
29
+ import * as fs from 'fs' ;
30
+ import * as path from 'path' ;
29
31
30
32
class AxiosPlugin implements SwPlugin {
31
33
readonly module = 'axios' ;
32
34
readonly versions = '*' ;
33
35
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
+
34
52
install ( installer : PluginInstaller ) : void {
35
53
this . interceptClientRequest ( installer ) ;
36
54
}
37
55
38
56
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 ;
40
59
const _request = Axios . prototype . request ;
41
60
42
- Axios . prototype . request = function ( url ?: any , config ?: any ) {
61
+ Axios . prototype . request = axios . request = function ( url ?: any , config ?: any ) {
43
62
if ( typeof url === 'string' ) config = config ? { ...config , url } : { url } ;
44
63
else config = url ? { ...url } : { } ;
45
64
0 commit comments