diff --git a/guardrails_api/__init__.py b/guardrails_api/__init__.py index f102a9c..3b93d0b 100644 --- a/guardrails_api/__init__.py +++ b/guardrails_api/__init__.py @@ -1 +1 @@ -__version__ = "0.0.1" +__version__ = "0.0.2" diff --git a/guardrails_api/app.py b/guardrails_api/app.py index 92e72d8..a33fbbc 100644 --- a/guardrails_api/app.py +++ b/guardrails_api/app.py @@ -9,10 +9,12 @@ from opentelemetry.instrumentation.flask import FlaskInstrumentor from guardrails_api.clients.postgres_client import postgres_is_enabled from guardrails_api.otel import otel_is_disabled, initialize +from guardrails_api.utils.trace_server_start_if_enabled import trace_server_start_if_enabled from guardrails_api.clients.cache_client import CacheClient from rich.console import Console from rich.rule import Rule + # TODO: Move this to a separate file class OverrideJsonProvider(DefaultJSONProvider): def default(self, o): @@ -50,6 +52,7 @@ def register_config(config: Optional[str] = None): def create_app( env: Optional[str] = None, config: Optional[str] = None, port: Optional[int] = None ): + trace_server_start_if_enabled() # used to print user-facing messages during server startup console = Console() diff --git a/guardrails_api/utils/has_internet_connection.py b/guardrails_api/utils/has_internet_connection.py new file mode 100644 index 0000000..8a7099c --- /dev/null +++ b/guardrails_api/utils/has_internet_connection.py @@ -0,0 +1,10 @@ +import requests + + +def has_internet_connection() -> bool: + try: + res = requests.get("https://www.guardrailsai.com/") + res.raise_for_status() + return True + except requests.ConnectionError: + return False \ No newline at end of file diff --git a/guardrails_api/utils/trace_server_start_if_enabled.py b/guardrails_api/utils/trace_server_start_if_enabled.py new file mode 100644 index 0000000..467abd6 --- /dev/null +++ b/guardrails_api/utils/trace_server_start_if_enabled.py @@ -0,0 +1,24 @@ +import platform +from guardrails.classes.credentials import Credentials +from guardrails.version import GUARDRAILS_VERSION +from guardrails_api.utils.has_internet_connection import has_internet_connection + + +def trace_server_start_if_enabled(): + config = Credentials.from_rc_file() + if config.enable_metrics is True and has_internet_connection(): + from guardrails.utils.hub_telemetry_utils import HubTelemetry + HubTelemetry().create_new_span( + "guardrails-api/start", + [ + ("guardrails-version", GUARDRAILS_VERSION), + ("python-version", platform.python_version()), + ("system", platform.system()), + ("platform", platform.platform()), + ("arch", platform.architecture()[0]), + ("machine", platform.machine()), + ("processor", platform.processor()), + ], + True, + False, + ) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index b8078d2..8c5ae0c 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.6", "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", + "requests>=2.32.3" ] [tool.setuptools.dynamic]