# Path: drift/utils.py
# def get_tier_name(fail_hard=True):
#     """
#     Get tier name from environment
#     """
#     if 'DRIFT_TIER' in os.environ:
#         return os.environ['DRIFT_TIER']
# 
#     if fail_hard:
#         raise RuntimeError(
#             "No tier specified. Specify one in "
#             "'DRIFT_TIER' environment variable, or use the --tier command "
#             "line argument."
#         )
# 
# Path: drift/core/extensions/logging.py
# def get_log_details():
#     details = OrderedDict()
#     tenant_name = None
#     tier_name = get_tier_name()
#     remote_addr = None
# 
#     try:
#         remote_addr = request.remote_addr
#     except Exception:
#         pass
# 
#     try:
#         if hasattr(g, "conf"):
#             tenant_name = (
#                 g.conf.tenant_name["tenant_name"] if g.conf.tenant_name else "(none)"
#             )
#     except RuntimeError as e:
#         if "Working outside of application context" in repr(e):
#             pass
#         else:
#             raise
#     log_context = {}
#     log_context["created"] = datetime.datetime.utcnow().isoformat() + "Z"
#     log_context["tenant"] = tenant_name
#     log_context["tier"] = tier_name
#     log_context["remote_addr"] = remote_addr
#     details["logger"] = log_context
#     jwt_context = {}
#     try:
#         fields = set(
#             [
#                 "user_id",
#                 "player_id",
#                 "roles",
#                 "jti",
#                 "user_name",
#                 "player_name",
#                 "client_id",
#                 "identity_id",
#             ]
#         )
#         for k, v in current_user.items():
#             if k in fields:
#                 key = "{}".format(k)
#                 jwt_context[key] = v
#             if k == "roles" and v:
#                 jwt_context[k] = ",".join(v)
#     except Exception as e:
#         pass
#     if jwt_context:
#         details["user"] = jwt_context
# 
#     # add Drift-Log-Context" request headers to the logs
#     try:
#         details["client"] = json.loads(request.headers.get("Drift-Log-Context"))
#     except Exception:
#         pass
# 
#     return details


import logging
import os, sys
import sentry_sdk
from flask import current_app, g
from driftconfig.util import get_drift_config
from drift.utils import get_tier_name
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from drift.core.extensions.logging import get_log_details
"""
Sentry integration
"""


log = logging.getLogger(__name__)


TIER_DEFAULTS = {"dsn": "<PLEASE FILL IN>"}


# Initialize Sentry at file scope to catch'em all.
def _init_sentry(app):
    dsn = os.environ.get('SENTRY_DSN')

# Path: drift/utils.py
# def get_tier_name(fail_hard=True):
#     """
#     Get tier name from environment
#     """
#     if 'DRIFT_TIER' in os.environ:
#         return os.environ['DRIFT_TIER']
# 
#     if fail_hard:
#         raise RuntimeError(
#             "No tier specified. Specify one in "
#             "'DRIFT_TIER' environment variable, or use the --tier command "
#             "line argument."
#         )
# 
# Path: drift/core/extensions/logging.py
# def get_log_details():
#     details = OrderedDict()
#     tenant_name = None
#     tier_name = get_tier_name()
#     remote_addr = None
# 
#     try:
#         remote_addr = request.remote_addr
#     except Exception:
#         pass
# 
#     try:
#         if hasattr(g, "conf"):
#             tenant_name = (
#                 g.conf.tenant_name["tenant_name"] if g.conf.tenant_name else "(none)"
#             )
#     except RuntimeError as e:
#         if "Working outside of application context" in repr(e):
#             pass
#         else:
#             raise
#     log_context = {}
#     log_context["created"] = datetime.datetime.utcnow().isoformat() + "Z"
#     log_context["tenant"] = tenant_name
#     log_context["tier"] = tier_name
#     log_context["remote_addr"] = remote_addr
#     details["logger"] = log_context
#     jwt_context = {}
#     try:
#         fields = set(
#             [
#                 "user_id",
#                 "player_id",
#                 "roles",
#                 "jti",
#                 "user_name",
#                 "player_name",
#                 "client_id",
#                 "identity_id",
#             ]
#         )
#         for k, v in current_user.items():
#             if k in fields:
#                 key = "{}".format(k)
#                 jwt_context[key] = v
#             if k == "roles" and v:
#                 jwt_context[k] = ",".join(v)
#     except Exception as e:
#         pass
#     if jwt_context:
#         details["user"] = jwt_context
# 
#     # add Drift-Log-Context" request headers to the logs
#     try:
#         details["client"] = json.loads(request.headers.get("Drift-Log-Context"))
#     except Exception:
#         pass
# 
#     return details


# Path: drift/core/resources/sentry.py
import logging
import os, sys
import sentry_sdk
from flask import current_app, g
from driftconfig.util import get_drift_config
from drift.utils import get_tier_name
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from drift.core.extensions.logging import get_log_details

"""
Sentry integration
"""


log = logging.getLogger(__name__)


TIER_DEFAULTS = {"dsn": "<PLEASE FILL IN>"}


# Initialize Sentry at file scope to catch'em all.
def _init_sentry(app):
    dsn = os.environ.get('SENTRY_DSN')

    tier_name = get_tier_name()