Skip to content

Commit

Permalink
Merge pull request #72 from guardrails-ai/anonymous-telem
Browse files Browse the repository at this point in the history
trace on server start
  • Loading branch information
CalebCourier authored Aug 29, 2024
2 parents a25f97b + d31bdcc commit d2bf8c5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
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
10 changes: 10 additions & 0 deletions guardrails_api/utils/has_internet_connection.py
Original file line number Diff line number Diff line change
@@ -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
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

0 comments on commit d2bf8c5

Please sign in to comment.