diff --git a/guardrails_api/app.py b/guardrails_api/app.py index 7ac5adc..e123475 100644 --- a/guardrails_api/app.py +++ b/guardrails_api/app.py @@ -65,8 +65,6 @@ def create_app( self_endpoint = os.environ.get("SELF_ENDPOINT", f"{host}:{set_port}") os.environ["SELF_ENDPOINT"] = self_endpoint - register_config(config) - app = Flask(__name__) app.json = OverrideJsonProvider(app) @@ -84,6 +82,8 @@ def create_app( FlaskInstrumentor().instrument_app(app) initialize() + register_config(config) + # if no pg_host is set, don't set up postgres if postgres_is_enabled(): from guardrails_api.clients.postgres_client import PostgresClient diff --git a/guardrails_api/otel/traces.py b/guardrails_api/otel/traces.py index fdbcb8b..8d6c7c0 100644 --- a/guardrails_api/otel/traces.py +++ b/guardrails_api/otel/traces.py @@ -19,6 +19,14 @@ from guardrails_api.otel.constants import none +class GuardrailsInstrumentorSentinel: + pass + +try: + from openinference.instrumentation.guardrails import GuardrailsInstrumentor +except ImportError: + GuardrailsInstrumentor = GuardrailsInstrumentorSentinel() + def traces_are_disabled() -> bool: otel_traces_exporter = os.environ.get("OTEL_TRACES_EXPORTER", none) return otel_traces_exporter == none @@ -68,5 +76,9 @@ def initialize_tracer(): for exporter in trace_exporters: set_span_processors(tracer_provider, exporter, use_batch) + if not isinstance(GuardrailsInstrumentor, GuardrailsInstrumentorSentinel): + # Instrument with OpenInference + GuardrailsInstrumentor().instrument(tracer_provider=tracer_provider) + # Initialize singleton get_tracer() diff --git a/guardrails_api/start-dev.sh b/guardrails_api/start-dev.sh index a27f2d3..8c0bc1e 100755 --- a/guardrails_api/start-dev.sh +++ b/guardrails_api/start-dev.sh @@ -1 +1,2 @@ -gunicorn --bind 0.0.0.0:8000 --timeout=5 --threads=10 "guardrails_api.app:create_app()" --reload --capture-output --enable-stdio-inheritance +# TODO: Have to pass the config file to this now or it will blow up. +gunicorn --bind 0.0.0.0:8000 --timeout=5 --workers=1 "guardrails_api.app:create_app()" --reload --capture-output --enable-stdio-inheritance diff --git a/guardrails_api/start.sh b/guardrails_api/start.sh index 2696b88..d1882d5 100755 --- a/guardrails_api/start.sh +++ b/guardrails_api/start.sh @@ -1 +1,2 @@ -gunicorn --bind 0.0.0.0:8000 --timeout=5 --threads=10 "guardrails_api.app:create_app()" +# TODO: Have to pass the config file to this now or it will blow up. +gunicorn --bind 0.0.0.0:8000 --timeout=5 --workers=1 "guardrails_api.app:create_app()" diff --git a/pyproject.toml b/pyproject.toml index b8078d2..d086c20 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ readme = "README.md" keywords = ["Guardrails", "Guardrails AI", "Guardrails API", "Guardrails API"] requires-python = ">= 3.8.1" dependencies = [ - "guardrails-ai>=0.5.0a11", + "guardrails-ai>=0.5.0", "flask>=3.0.3,<4", "Flask-SQLAlchemy>=3.1.1,<4", "Flask-Caching>=2.3.0,<3", @@ -44,6 +44,10 @@ dev = [ "gunicorn>=22.0.0,<23", ] +openinference = [ + "openinference-instrumentation-guardrails>=0.1.0" +] + [tool.pytest.ini_options] minversion = "6.0" addopts = "-rP"