From 40f47f8370ca37e4fc3c66ae57a0d6f36027579f Mon Sep 17 00:00:00 2001 From: Caleb Courier Date: Thu, 18 Jul 2024 09:17:36 -0500 Subject: [PATCH 1/2] Add openinference instrumentation --- guardrails_api/app.py | 4 ++-- guardrails_api/otel/traces.py | 4 ++++ guardrails_api/start-dev.sh | 3 ++- guardrails_api/start.sh | 3 ++- pyproject.toml | 3 ++- 5 files changed, 12 insertions(+), 5 deletions(-) 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..ef2f1e7 100644 --- a/guardrails_api/otel/traces.py +++ b/guardrails_api/otel/traces.py @@ -16,6 +16,7 @@ from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( OTLPSpanExporter as GrpcSpanExporter, ) +from openinference.instrumentation.guardrails import GuardrailsInstrumentor from guardrails_api.otel.constants import none @@ -68,5 +69,8 @@ def initialize_tracer(): for exporter in trace_exporters: set_span_processors(tracer_provider, exporter, use_batch) + # 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..9e5992f 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", @@ -27,6 +27,7 @@ dependencies = [ "opentelemetry-exporter-otlp-proto-grpc>=1.0.0,<2", "opentelemetry-exporter-otlp-proto-http>=1.0.0,<2", "opentelemetry-instrumentation-flask>=0.12b0,<1", + "openinference-instrumentation-guardrails>=0.1.0" ] [tool.setuptools.dynamic] From a4b3060f2e9c7642fbdecbbf81e8abca78070291 Mon Sep 17 00:00:00 2001 From: Caleb Courier Date: Thu, 18 Jul 2024 13:34:09 -0500 Subject: [PATCH 2/2] make openinference optional --- guardrails_api/otel/traces.py | 14 +++++++++++--- pyproject.toml | 5 ++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/guardrails_api/otel/traces.py b/guardrails_api/otel/traces.py index ef2f1e7..8d6c7c0 100644 --- a/guardrails_api/otel/traces.py +++ b/guardrails_api/otel/traces.py @@ -16,10 +16,17 @@ from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( OTLPSpanExporter as GrpcSpanExporter, ) -from openinference.instrumentation.guardrails import GuardrailsInstrumentor 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 @@ -69,8 +76,9 @@ def initialize_tracer(): for exporter in trace_exporters: set_span_processors(tracer_provider, exporter, use_batch) - # Instrument with OpenInference - GuardrailsInstrumentor().instrument(tracer_provider=tracer_provider) + if not isinstance(GuardrailsInstrumentor, GuardrailsInstrumentorSentinel): + # Instrument with OpenInference + GuardrailsInstrumentor().instrument(tracer_provider=tracer_provider) # Initialize singleton get_tracer() diff --git a/pyproject.toml b/pyproject.toml index 9e5992f..d086c20 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,6 @@ dependencies = [ "opentelemetry-exporter-otlp-proto-grpc>=1.0.0,<2", "opentelemetry-exporter-otlp-proto-http>=1.0.0,<2", "opentelemetry-instrumentation-flask>=0.12b0,<1", - "openinference-instrumentation-guardrails>=0.1.0" ] [tool.setuptools.dynamic] @@ -45,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"