laixintao/prometheus-http-sd

Remove extra parameters in run_python function

Closed this issue · 1 comments

Currently, our run_python implementation passes the entire dict into the generate_targets function. However, if the generate_target function doesn't accept any parameter or user passes extra parameters will cause some TypeError Exception.

Therefore, should we remove some extra parameters before we call the target function? For example, we can write some code like this:

def run_python(generator_path, **extra_args) -> TargetList:
    logger.debug(f"start to import module {generator_path}...")

    loader = importlib.machinery.SourceFileLoader("mymodule", generator_path)
    spec = importlib.util.spec_from_loader("mymodule", loader)
    if spec:
        mymodule = importlib.util.module_from_spec(spec)
        loader.exec_module(mymodule)
    else:
        raise Exception("Load a None module!")

    parameters = {}
    signature = inspect.signature(mymodule.generate_targets)
    for key in signature.parameters:
        if key in extra_args:
            parameters[key] = extra_args[key]

    return mymodule.generate_targets(**parameters)

I think user can both control their:

  • sd function
  • sd url

if they set their sd function to accept NO arguments, but they give arguments when set the url, they should see this error instead of missing the set urls implicitly in functions