From e5ebaaae4a0d69bfd5179664efa0ec4702d5bf88 Mon Sep 17 00:00:00 2001 From: ruleeeer Date: Wed, 9 Mar 2022 23:43:18 +0800 Subject: [PATCH] Fix mysql2 plugin install error --- src/core/OptionMethods.ts | 24 ++++++++++++++++++++++++ src/core/PluginInstaller.ts | 11 ++++++++--- src/core/SwPlugin.ts | 3 ++- src/plugins/MySQL2Plugin.ts | 11 ++++++++++- 4 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/core/OptionMethods.ts diff --git a/src/core/OptionMethods.ts b/src/core/OptionMethods.ts new file mode 100644 index 0000000..9fe0456 --- /dev/null +++ b/src/core/OptionMethods.ts @@ -0,0 +1,24 @@ +/*! + * + * 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 PluginInstaller from './PluginInstaller'; + +export default interface OptionMethods { + getVersion?(installer: PluginInstaller): string; +} diff --git a/src/core/PluginInstaller.ts b/src/core/PluginInstaller.ts index f6f285f..58e8e82 100644 --- a/src/core/PluginInstaller.ts +++ b/src/core/PluginInstaller.ts @@ -40,7 +40,7 @@ while (topModule.parent) { export default class PluginInstaller { private readonly pluginDir: string; readonly require: (name: string) => any = topModule.require.bind(topModule); - private readonly resolve = (request: string) => (module.constructor as any)._resolveFilename(request, topModule); + readonly resolve = (request: string) => (module.constructor as any)._resolveFilename(request, topModule); constructor() { this.pluginDir = path.resolve(__dirname, '..', 'plugins'); @@ -64,8 +64,13 @@ export default class PluginInstaller { }; } - const packageJsonPath = this.resolve(`${plugin.module}/package.json`); - const version = this.require(packageJsonPath).version; + let version = null; + try { + const packageJsonPath = this.resolve(`${plugin.module}/package.json`); + version = this.require(packageJsonPath).version; + } catch (e) { + version = plugin.getVersion?.(this); + } if (!semver.satisfies(version, plugin.versions)) { logger.info(`Plugin ${plugin.module} ${version} doesn't satisfy the supported version ${plugin.versions}`); diff --git a/src/core/SwPlugin.ts b/src/core/SwPlugin.ts index d369097..7816fc4 100644 --- a/src/core/SwPlugin.ts +++ b/src/core/SwPlugin.ts @@ -19,8 +19,9 @@ import PluginInstaller from './PluginInstaller'; import Span from '../trace/span/Span'; +import OptionMethods from './OptionMethods'; -export default interface SwPlugin { +export default interface SwPlugin extends OptionMethods { readonly module: string; readonly versions: string; diff --git a/src/plugins/MySQL2Plugin.ts b/src/plugins/MySQL2Plugin.ts index e1497ba..1adc5dd 100644 --- a/src/plugins/MySQL2Plugin.ts +++ b/src/plugins/MySQL2Plugin.ts @@ -24,13 +24,22 @@ import Tag from '../Tag'; import { SpanLayer } from '../proto/language-agent/Tracing_pb'; import PluginInstaller from '../core/PluginInstaller'; import agentConfig from '../config/AgentConfig'; +import * as fs from 'fs'; +import * as path from 'path'; class MySQL2Plugin implements SwPlugin { readonly module = 'mysql2'; readonly versions = '*'; + getVersion(installer: PluginInstaller): string { + 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); + return pkg.version; + } + install(installer: PluginInstaller): void { - const Connection = installer.require('mysql2/lib/connection'); + const Connection = installer.require('mysql2').Connection; const _query = Connection.prototype.query; Connection.prototype.query = function (sql: any, values: any, cb: any) {