Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Openinference Instrumentation Proof-Of-Concept #60

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions guardrails_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions guardrails_api/otel/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
3 changes: 2 additions & 1 deletion guardrails_api/start-dev.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion guardrails_api/start.sh
Original file line number Diff line number Diff line change
@@ -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()"
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down