-
Notifications
You must be signed in to change notification settings - Fork 1
Postgres fixes: Concurrency issue, connection leak, and fixes to guard CRUD operations #83
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
Open
AlejandroEsquivel
wants to merge
14
commits into
main
Choose a base branch
from
fix/db-fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4da9e8d
fix: added pg advisory lock due to worker init concurrency issues (wh…
AlejandroEsquivel 6e2a84d
fix for http_error class interface
AlejandroEsquivel b31e845
support for uvicorn factory method creation
AlejandroEsquivel 3877ac7
updated requirements lock
AlejandroEsquivel b21d4b5
fixes for dockerfile
AlejandroEsquivel a328b7c
updates to docker compose file for testing with pg locally
AlejandroEsquivel a7aed37
fix lock reference
AlejandroEsquivel 01e8e0d
ported over pg guard client
AlejandroEsquivel 7e03f54
Fix tests due to update pgguard client interface
AlejandroEsquivel d901ba8
revert changes to postgres_client init
AlejandroEsquivel 19f6813
Add comment
AlejandroEsquivel 20c664a
Update guardrails_api/app.py
AlejandroEsquivel 8950985
Update Dockerfile
AlejandroEsquivel f700707
Update compose.yml
AlejandroEsquivel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,13 +20,16 @@ | |
|
||
from starlette.middleware.base import BaseHTTPMiddleware | ||
|
||
GR_ENV_FILE = os.environ.get("GR_ENV_FILE", None) | ||
GR_CONFIG_FILE_PATH = os.environ.get("GR_CONFIG_FILE_PATH", None) | ||
PORT = int(os.environ.get("PORT", 8000)) | ||
|
||
class RequestInfoMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request: Request, call_next): | ||
tracer = trace.get_tracer(__name__) | ||
# Get the current context and attach it to this task | ||
with tracer.start_as_current_span("request_info") as span: | ||
client_ip = request.client.host | ||
client_ip = request.client.host if request.client else None | ||
user_agent = request.headers.get("user-agent", "unknown") | ||
referrer = request.headers.get("referrer", "unknown") | ||
user_id = request.headers.get("x-user-id", "unknown") | ||
|
@@ -40,13 +43,15 @@ async def dispatch(self, request: Request, call_next): | |
context.attach(baggage.set_baggage("organization", organization)) | ||
context.attach(baggage.set_baggage("app", app)) | ||
|
||
span.set_attribute("client.ip", client_ip) | ||
span.set_attribute("http.user_agent", user_agent) | ||
span.set_attribute("http.referrer", referrer) | ||
span.set_attribute("user.id", user_id) | ||
span.set_attribute("organization", organization) | ||
span.set_attribute("app", app) | ||
|
||
if client_ip: | ||
span.set_attribute("client.ip", client_ip) | ||
|
||
response = await call_next(request) | ||
return response | ||
|
||
|
@@ -70,9 +75,13 @@ def register_config(config: Optional[str] = None): | |
config_module = importlib.util.module_from_spec(spec) | ||
spec.loader.exec_module(config_module) | ||
|
||
return config_file_path | ||
|
||
# Support for providing env vars as uvicorn does not support supplying args to create_app | ||
# - Usage: GR_CONFIG_FILE_PATH=config.py GR_ENV_FILE=.env PORT=8080 uvicorn --factory 'guardrails_api.app:create_app' --host 0.0.0.0 --port $PORT --workers 2 --timeout-keep-alive 90 | ||
# - Usage: gunicorn -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:$PORT --timeout=90 --workers=2 "guardrails_api.app:create_app(None, None, $PORT)" | ||
Comment on lines
+80
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's rough. We'll have to address this in the OSS's cli as well since it calls this. |
||
def create_app( | ||
env: Optional[str] = None, config: Optional[str] = None, port: Optional[int] = None | ||
env: Optional[str] = GR_ENV_FILE, config: Optional[str] = GR_CONFIG_FILE_PATH, port: Optional[int] = PORT | ||
): | ||
trace_server_start_if_enabled() | ||
# used to print user-facing messages during server startup | ||
|
@@ -89,12 +98,12 @@ def create_app( | |
env_file_path = os.path.abspath(env) | ||
load_dotenv(env_file_path, override=True) | ||
|
||
set_port = port or os.environ.get("PORT", 8000) | ||
set_port = port or PORT | ||
host = os.environ.get("HOST", "http://localhost") | ||
self_endpoint = os.environ.get("SELF_ENDPOINT", f"{host}:{set_port}") | ||
os.environ["SELF_ENDPOINT"] = self_endpoint | ||
|
||
register_config(config) | ||
resolved_config_file_path = register_config(config) | ||
|
||
app = FastAPI(openapi_url="") | ||
|
||
|
@@ -159,6 +168,10 @@ async def value_error_handler(request: Request, exc: ValueError): | |
) | ||
|
||
console.print("") | ||
console.print("Using the following configuration:") | ||
console.print(f"- Guardrails Log Level: {guardrails_log_level}") | ||
console.print(f"- Self Endpoint: {self_endpoint}") | ||
console.print(f"- Config File Path: {resolved_config_file_path} [Provided: {config}]") | ||
console.print( | ||
Rule("[bold grey]Server Logs[/bold grey]", characters="=", style="white") | ||
) | ||
|
@@ -170,4 +183,4 @@ async def value_error_handler(request: Request, exc: ValueError): | |
import uvicorn | ||
|
||
app = create_app() | ||
uvicorn.run(app, host="0.0.0.0", port=8000) | ||
uvicorn.run(app, host="0.0.0.0", port=PORT) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm starting to think we should just have two different Dockfiles and compose files to run with PG vs config.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed 👌🏼