Skip to content

Commit

Permalink
[Issue 2665] Add temp logging to help troubleshoot db connection fail…
Browse files Browse the repository at this point in the history
…ures (#2816)

## Summary
Partially Fixes #2665

### Time to review: __1 min__

## Changes proposed
> What was added, updated, or removed in this PR.

Added `sslmode` to Postgres connection string. Also added some temporary
logging to help troubleshoot db connection failures from analytics code.
I will remove the logging in a future PR, as soon as I find the cause of
the db connection failures.

## Context for reviewers
> Testing instructions, background context, more in-depth details of the
implementation, and anything else you'd like to call out or ask
reviewers. Explain how the changes were verified.

Analytics step functions have been deployed but when they execute their
attempted connections to Postgres always fail.

## Additional information
> Screenshots, GIF demos, code examples or output to help show the
changes working as expected.
  • Loading branch information
DavidDudas-Intuitial authored Nov 13, 2024
1 parent 2fb61a6 commit 7f943c5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion analytics/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# reads environment variables from .env files defaulting to "local.env"
class PydanticBaseEnvConfig(BaseSettings):
model_config = SettingsConfigDict(env_file="%s.env" % os.getenv("ENVIRONMENT", "local"), extra="ignore") # set extra to ignore so that it ignores variables irrelevant to the database config (e.g. metabase settings)
model_config = SettingsConfigDict(env_file="%s.env" % os.getenv("ENVIRONMENT", "local"), extra="allow")

class DBSettings(PydanticBaseEnvConfig):
db_host: str = Field(alias="DB_HOST")
Expand Down
10 changes: 8 additions & 2 deletions analytics/src/analytics/integrations/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# pylint: disable=invalid-name, line-too-long
"""Get a connection to the database using a SQLAlchemy engine object."""

import os

import boto3
from sqlalchemy import Engine, create_engine

Expand All @@ -25,11 +27,15 @@ def get_db() -> Engine:
db = get_db_settings()
# inspired by simpler-grants-gov/blob/main/api/src/adapters/db/clients/postgres_client.py
token = db.password if db.local_env is True else generate_iam_auth_token(db)
url = f"postgresql+psycopg://{db.user}:{token}@{db.db_host}:{db.port}?sslmode={db.ssl_mode}"
print(f"TEMP DEBUG: environment = {os.getenv('ENVIRONMENT', 'local')}")
print(f"TEMP DEBUG: db settings = {db}")
print(f"TEMP DEBUG: token has non-zero len? {len(str(token)) > 0}")

return create_engine(
f"postgresql+psycopg://{db.user}:{token}@{db.db_host}:{db.port}",
url,
pool_pre_ping=True,
hide_parameters=True,
hide_parameters=False,
)


Expand Down

0 comments on commit 7f943c5

Please sign in to comment.