-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathapplication.py
More file actions
76 lines (55 loc) · 2.35 KB
/
application.py
File metadata and controls
76 lines (55 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
from __future__ import print_function
import os
from apig_wsgi import make_lambda_handler
from dotenv import load_dotenv
from environs import Env
from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix
from app import create_app
load_dotenv()
env = Env()
ff_enable_otel = env.bool("FF_ENABLE_OTEL", default=False)
if not ff_enable_otel:
from aws_xray_sdk.core import patch_all, xray_recorder
from aws_xray_sdk.ext.flask.middleware import XRayMiddleware
from app.aws.xray.context import NotifyContext
# Patch all supported libraries for X-Ray
# Used to trace requests and responses through the stack
patch_all()
is_lambda = os.environ.get("AWS_LAMBDA_RUNTIME_API") is not None
enable_newrelic = env.bool("ENABLE_NEW_RELIC", default=False) and not ff_enable_otel
print("is_lambda =", is_lambda)
print("enable_newrelic =", enable_newrelic)
if is_lambda and enable_newrelic:
import newrelic.agent
# Initialize New Relic early, before creating the Flask app
print("Lambda detected, and New Relic enabled - initializing New Relic agent")
environment = os.getenv("NOTIFY_ENVIRONMENT", "dev")
newrelic.agent.initialize("newrelic.ini", environment=environment)
application = Flask("api")
application.wsgi_app = ProxyFix(application.wsgi_app) # type: ignore
app = create_app(application)
if not ff_enable_otel:
# Configure X-Ray after app creation
xray_recorder.configure(service="Notify-API", context=NotifyContext())
XRayMiddleware(app, xray_recorder)
# It's annoying that we have to do this here, but order matters
# so we need to check if is lambda twice.
if is_lambda and enable_newrelic:
# Wrap the Flask app with New Relic's WSGI wrapper
app = newrelic.agent.WSGIApplicationWrapper(app)
apig_wsgi_handler = make_lambda_handler(
app, binary_support=True, non_binary_content_type_prefixes=["application/yaml", "application/json"]
)
if os.environ.get("USE_LOCAL_JINJA_TEMPLATES") == "True":
print("")
print("========================================================")
print("")
print("WARNING: USING LOCAL JINJA from /jinja_templates FOLDER!")
print(".env USE_LOCAL_JINJA_TEMPLATES=True")
print("")
print("========================================================")
print("")
def handler(event, context):
return apig_wsgi_handler(event, context)