Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update AxiosPlugin for v1.0+ #102

Merged
merged 12 commits into from
Nov 15, 2022
23 changes: 21 additions & 2 deletions src/plugins/AxiosPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } : {};

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/MySQL2Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand Down