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

trace on server start #72

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion guardrails_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.1"
__version__ = "0.0.2"
3 changes: 3 additions & 0 deletions guardrails_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()

Expand Down
9 changes: 9 additions & 0 deletions guardrails_api/utils/has_internet_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import requests


def has_internet_connection() -> bool:
try:
requests.get("https://www.guardrailsai.com/")
CalebCourier marked this conversation as resolved.
Show resolved Hide resolved
return True
except requests.ConnectionError:
return False
24 changes: 24 additions & 0 deletions guardrails_api/utils/trace_server_start_if_enabled.py
Original file line number Diff line number Diff line change
@@ -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,
)
3 changes: 2 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.6",
"flask>=3.0.3,<4",
"Flask-SQLAlchemy>=3.1.1,<4",
"Flask-Caching>=2.3.0,<3",
Expand All @@ -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]
Expand Down