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

[Plugin] check version of packages when install plugins #63

Merged
merged 5 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/Developer.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@
You can always take [the existing plugins](../skywalking/plugins) as examples, while there are some general ideas for all plugins.
1. A plugin is a module under directory [`skywalking/plugins/`](../skywalking/plugins) with an `install` method;
1. Inside the `install` method, you find out the relevant method(s) of the libraries that you plan to instrument, and create/close spans before/after those method(s).
1. You should also provide version rules in the plugin module, which means the version of package your plugin support. You should init a dict with keys `name` and `rules`. the `name` is your plugin's corresponding package's name, the `rules` is the version rules this package should follow.

You can use >, >=, ==, <=, <, and != operators in rules.

The relation between rules element in the rules array is **OR**, which means the version of the package should follow at least one rule in rules array.

You can set many version rules in one element of rules array, separate each other with a space character, the relation of rules in one rule element is **AND**, which means the version of package should follow all rules in this rule element.

For example, below `version_rule` indicates that the package version of `django` should `>=2.0 AND <=2.3 AND !=2.2.1` OR `>3.0`.
```python
version_rule = {
"name": "django",
"rules": [">=2.0 <=2.3 !=2.2.1", ">3.0"]
}
```
1. Every plugin requires a corresponding test under [`tests/plugin`](../tests/plugin) before it can be merged, refer to [the plugin test guide](PluginTest.md) when writing a plugin test.
1. Update [the supported list](Plugins.md).
1. Add the environment variables to [the list](EnvVars.md) if any.
Expand Down
74 changes: 74 additions & 0 deletions skywalking/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import logging
import pkgutil
import re
import pkg_resources

from packaging import version

from skywalking import config

Expand All @@ -38,7 +41,78 @@ def install():
continue
logger.debug('installing plugin %s', modname)
plugin = importer.find_module(modname).load_module(modname)

supported = pkg_version_check(plugin)
if not supported:
logger.debug('check version for plugin %s\'s corresponding package failed, thus '
'won\'t be installed', modname)
continue

if not hasattr(plugin, 'install') or inspect.ismethod(getattr(plugin, 'install')):
logger.warning('no `install` method in plugin %s, thus the plugin won\'t be installed', modname)
continue
plugin.install()


_operators = {
'<': lambda cv, ev: cv < ev,
'<=': lambda cv, ev: cv < ev or cv == ev,
'==': lambda cv, ev: cv == ev,
'>=': lambda cv, ev: cv > ev or cv == ev,
'>': lambda cv, ev: cv > ev,
'!=': lambda cv, ev: cv != ev
}


class VersionRuleException(Exception):
def __init__(self, message):
self.message = message


def pkg_version_check(plugin):
supported = True

# no version rules was set, no checks
if not hasattr(plugin, "version_rule"):
return supported

pkg_name = plugin.version_rule.get("name")
rules = plugin.version_rule.get("rules")

try:
current_pkg_version = pkg_resources.get_distribution(pkg_name).version
except pkg_resources.DistributionNotFound:
# when failed to get the version, we consider it as supported.
return supported

current_version = version.parse(current_pkg_version)
# pass one rule in rules (OR)
for rule in rules:
if rule.find(" ") == -1:
if check(rule, current_version):
return supported
else:
# have to pass all rule_uint in this rule (AND)
rule_units = rule.split(" ")
results = [check(unit, current_version) for unit in rule_units]
if False in results:
# check failed, try to check next rule
continue
else:
return supported

supported = False
return supported


def check(rule_unit, current_version):
idx = 2 if rule_unit[1] == '=' else 1
symbol = rule_unit[0:idx]
expect_pkg_version = rule_unit[idx:]

expect_version = version.parse(expect_pkg_version)
f = _operators.get(symbol) or None
if not f:
raise VersionRuleException("version rule {} error. only allow >,>=,==,<=,<,!= symbols".format(rule_unit))

return f(current_version, expect_version)
5 changes: 5 additions & 0 deletions skywalking/plugins/sw_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

logger = logging.getLogger(__name__)

version_rule = {
"name": "django",
"rules": [">=2.0"]
}


def install():
try:
Expand Down
13 changes: 5 additions & 8 deletions skywalking/plugins/sw_pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#

import logging
import pkg_resources
from packaging import version

from skywalking import Layer, Component, config
from skywalking.trace import tags
Expand All @@ -27,19 +25,18 @@

logger = logging.getLogger(__name__)

version_rule = {
"name": "pymongo",
"rules": [">=3.7.0"]
}


def install():
try:
from pymongo.bulk import _Bulk
from pymongo.cursor import Cursor
from pymongo.pool import SocketInfo

# check pymongo version
pymongo_version = pkg_resources.get_distribution("pymongo").version
if version.parse(pymongo_version) < version.parse("3.7.0"):
logger.warning("support pymongo version 3.7.0 or above, current version:" + pymongo_version)
raise Exception

bulk_op_map = {
0: "insert",
1: "update",
Expand Down
100 changes: 100 additions & 0 deletions tests/test_version_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#
# 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 unittest

from packaging import version

from skywalking.plugins import _operators, check


class TestVersionCheck(unittest.TestCase):
def test_operators(self):
# <
f = _operators.get("<")
v1 = version.parse("1.0.0")
v2 = version.parse("1.0.1")
self.assertTrue(f(v1, v2))
self.assertFalse(f(v2, v1))

v2 = version.parse("1.0.0")
self.assertFalse(f(v1, v2))

# <=
f = _operators.get("<=")
v1 = version.parse("1.0")
v2 = version.parse("1.0")
self.assertTrue(v1, v2)

v2 = version.parse("1.1.0")
self.assertTrue(f(v1, v2))
self.assertFalse(f(v2, v1))

# =
f = _operators.get("==")
v1 = version.parse("1.0.0")
v2 = version.parse("1.0.0")
self.assertTrue(f(v1, v2))

v2 = version.parse("1.0.1")
self.assertFalse(f(v1, v2))

# >=
f = _operators.get(">=")
v1 = version.parse("1.0.0")
v2 = version.parse("1.0.0")
self.assertTrue(f(v1, v2))

v2 = version.parse("1.0.1")
self.assertFalse(f(v1, v2))
self.assertTrue(f(v2, v1))

# >
f = _operators.get(">")
v1 = version.parse("1.0.0")
v2 = version.parse("1.0.1")
self.assertFalse(f(v1, v2))
self.assertTrue(f(v2, v1))

v2 = version.parse("1.0.0")
self.assertFalse(f(v1, v2))

# !=
f = _operators.get("!=")
v1 = version.parse("1.0.0")
v2 = version.parse("1.0.1")
self.assertTrue(f(v1, v2))

v2 = version.parse("1.0.0")
self.assertFalse(f(v1, v2))

def test_version_check(self):
current_version = version.parse("1.8.0")

self.assertTrue(check(">1.1.0", current_version))
self.assertTrue(check(">=1.0.0", current_version))
self.assertTrue(check("<2.0.0", current_version))
self.assertTrue(check("<=1.8.0", current_version))
self.assertTrue(check("==1.8.0", current_version))
self.assertTrue(check("!=1.6.0", current_version))

self.assertFalse(check(">1.9.0", current_version))
self.assertFalse(check(">=1.8.1", current_version))
self.assertFalse(check("<1.8.0", current_version))
self.assertFalse(check("<=1.7.0", current_version))
self.assertFalse(check("==1.0.0", current_version))
self.assertFalse(check("!=1.8.0", current_version))