bentoml / BentoML

The easiest way to serve AI apps and models - Build Model Inference APIs, Job queues, LLM apps, Multi-model pipelines, and more!
https://bentoml.com
Apache License 2.0
7.17k stars 792 forks source link

ValueError when mounting custom middleware #2541

Closed ALee008 closed 2 years ago

ALee008 commented 2 years ago

Describe the bug I am trying this example from the documentation about mounting custom middleware. I copy and pasted the fastapi example and served it. Upon startup I am encountering the following traceback:

Traceback (most recent call last):
  File "/appl/python3.10/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/appl/python3.10/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/bentoml/_internal/server/cli/dev_api_server.py", line 56, in <module>
    main()  # pylint: disable=no-value-for-parameter
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/bentoml/_internal/server/cli/dev_api_server.py", line 49, in main
    config = uvicorn.Config(svc.asgi_app, **uvicorn_options)
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/bentoml/_internal/service/service.py", line 209, in asgi_app
    return ServiceAppFactory(self)()
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/bentoml/_internal/server/service_app.py", line 265, in __call__
    app = super().__call__()
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/bentoml/_internal/server/base_app.py", line 56, in __call__
    return Starlette(
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/starlette/applications.py", line 76, in __init__
    self.middleware_stack = self.build_middleware_stack()
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/starlette/applications.py", line 103, in build_middleware_stack
    app = cls(app=app, **options)
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/simple_di/__init__.py", line 139, in _
    return func(*_inject_args(bind.args), **_inject_kwargs(bind.kwargs))
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/bentoml/_internal/server/instruments.py", line 37, in __init__
    self.metrics_request_duration = metrics_client.Histogram(
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/prometheus_client/metrics.py", line 544, in __init__
    super().__init__(
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/prometheus_client/metrics.py", line 134, in __init__
    registry.register(self)
  File "/appl/python-venv-bentoml/lib/python3.10/site-packages/prometheus_client/registry.py", line 29, in register
    raise ValueError(
ValueError: Duplicated timeseries in CollectorRegistry: {'BENTOML_hello_request_duration_seconds_created', 'BENTOML_hello_request_duration_seconds_count', 'BENTOML_hello_request_duration_seconds', 'BENTOML_hello_request_duration_seconds_sum', 'BENTOML_hello_request_duration_seconds_bucket'}

To Reproduce Steps to reproduce the issue:

  1. Go to https://docs.bentoml.org/en/latest/guides/server.html?highlight=fastapi#mounting-asgi-based-web-frameworks
  2. Copy and paste the FastAPI example to a service.py
  3. Run the service in the command line with bentoml serve service:svc --reload
  4. See error

Expected behavior Expecting a working example where the /hello endpoint returns {"Hello": "World"}.

Environment:

Additional context

bojiang commented 2 years ago

Hi. I just noticed that the line app = svc.asgi_app in the guidance is not needed, causing the issue we meet here.

bojiang commented 2 years ago

Just removing it from your service.py should do the trick!

ALee008 commented 2 years ago

Hey @bojiang , thanks for your reply. Removing app = svc.asgi_app solved this issue :thumbsup: