milvus/shards/tracer/factory.py

51 lines
1.7 KiB
Python
Raw Normal View History

2019-10-25 20:24:03 +08:00
import os
import logging
from functools import partial
from pluginbase import PluginBase
2019-10-25 20:39:44 +08:00
from tracer import Tracer
2019-10-25 20:24:03 +08:00
logger = logging.getLogger(__name__)
here = os.path.abspath(os.path.dirname(__file__))
get_path = partial(os.path.join, here)
PLUGIN_PACKAGE_NAME = 'tracer.plugins'
plugin_base = PluginBase(package=PLUGIN_PACKAGE_NAME,
2019-10-25 20:39:44 +08:00
searchpath=[get_path('./plugins')])
2019-10-25 20:24:03 +08:00
class TracerFactory(object):
def __init__(self, searchpath=None):
self.plugin_package_name = PLUGIN_PACKAGE_NAME
self.tracer_map = {}
searchpath = searchpath if searchpath else []
searchpath = [searchpath] if isinstance(searchpath, str) else searchpath
2019-10-25 20:39:44 +08:00
self.source = plugin_base.make_plugin_source(searchpath=searchpath,
identifier=self.__class__.__name__)
2019-10-25 20:24:03 +08:00
for plugin_name in self.source.list_plugins():
plugin = self.source.load_plugin(plugin_name)
plugin.setup(self)
def on_plugin_setup(self, plugin_class):
name = getattr(plugin_class, 'name', plugin_class.__name__)
self.tracer_map[name.lower()] = plugin_class
def plugin(self, name):
return self.tracer_map.get(name, None)
def create(self,
2019-10-25 20:39:44 +08:00
tracer_type,
tracer_config,
span_decorator=None,
**kwargs):
2019-10-25 20:24:03 +08:00
if not tracer_type:
return Tracer()
plugin_class = self.plugin(tracer_type.lower())
if not plugin_class:
raise RuntimeError('Tracer Plugin \'{}\' not installed!'.format(tracer_type))
tracer = plugin_class.create(tracer_config, span_decorator=span_decorator, **kwargs)
return tracer