Skip to content
Permalink
Browse files

Instrument application with AWS X-Ray

- Add aws-xray-sdk to requirements.txt
- Add X-Ray middleware and configuration to settings.py
- Add Docker container to run X-Ray daemon in Production
  • Loading branch information...
rbreslow authored and Jean Cochrane committed Oct 26, 2018
1 parent c6948e5 commit 8cfa35f61c961c585cf9903d1dba369dffd25650
@@ -20,3 +20,11 @@ MAILER_PORT=587
MAILER_USER=
MAILER_PASSWORD=
MAILER_USE_TLS_OR_SSL=tls
ROLLBAR_SERVER_TOKEN=
ROLLBAR_CLIENT_TOKEN=
AWS_XRAY_TRACING_NAME=district-builder-dtl-pa
AWS_XRAY_DAEMON_ADDRESS=xray.districtbuilder.internal:2000
AWS_XRAY_CONTEXT_MISSING=LOG_ERROR
AWS_REGION=us-east-1
XRAY_DAEMON_HOST=xray.districtbuilder.internal
XRAY_DAEMON_PORT=2000
@@ -17,6 +17,8 @@
import logging.config
import logging
from . import REDIS_URL
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch_all

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -36,6 +38,7 @@

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.getenv('DEBUG', False)
ENVIRONMENT = os.getenv('ENVIRONMENT', 'Development')

ALLOWED_HOSTS = os.getenv('HOST').split(',')

@@ -76,6 +79,29 @@
'django.middleware.locale.LocaleMiddleware',
]

if ENVIRONMENT in ['Staging', 'Production']:
xray_recorder.configure(plugins=["EC2Plugin"])
patch_all()
MIDDLEWARE = ['aws_xray_sdk.ext.django.middleware.XRayMiddleware'] + MIDDLEWARE
INSTALLED_APPS += ['aws_xray_sdk.ext.django']

# Rollbar setup
ROLLBAR_SERVER_TOKEN = os.getenv('ROLLBAR_SERVER_TOKEN', None)
ROLLBAR_CLIENT_TOKEN = os.getenv('ROLLBAR_CLIENT_TOKEN', None)
ROLLBAR = None
if ROLLBAR_SERVER_TOKEN and not DEBUG:
ROLLBAR = {
'access_token': ROLLBAR_SERVER_TOKEN,
'environment': 'production',
'branch': 'master',
'root': '/usr/src/app',
}
MIDDLEWARE.append('rollbar.contrib.django.middleware.RollbarNotifierMiddleware')

SERIALIZATION_MODULES = {
"geojson": "django.contrib.gis.serializers.geojson",
}

ROOT_URLCONF = 'publicmapping.urls'

TEMPLATES = [
@@ -29,3 +29,4 @@ python-dateutil==2.6.0
requests==2.18.4
git+git://github.com/PublicMapping/district-builder-config.git@0.1.0
django-watchman==0.15.0
aws-xray-sdk==2.2.0
@@ -39,7 +39,3 @@ services:
geoserver:
links:
- postgres:${DATABASE_HOST}

languages:
volumes:
- ./django/publicmapping/:/usr/src/app/
@@ -25,6 +25,18 @@ services:
- /opt/district-builder/user-data/districtbuilder_data.zip:/data/districtbuilder_data.zip
- /opt/district-builder/user-data/config_settings.py:/usr/src/app/publicmapping/config_settings.py
- /opt/district-builder/user-data/config.xml:/usr/src/app/config/config.xml
command:
- "--workers=2"
- "--timeout=60"
- "--bind=0.0.0.0:${WEB_APP_PORT}"
- "--log-level=info"
- "--access-logfile=-"
- "--error-logfile=-"
- "--timeout=300"
- "-kgevent"
- "publicmapping.wsgi"
links:
- xray:${XRAY_DAEMON_HOST}
logging:
driver: syslog
options:
@@ -50,15 +62,18 @@ services:
driver: syslog
options:
tag: districtbuilder-geoserver

languages:
image: districtbuilder-languages:${GIT_COMMIT:-latest}
container_name: districtbuilder-languages
xray:
image: districtbuilder-xray:${GIT_COMMIT:-latest}
container_name: districtbuilder-xray
restart: on-failure
volumes:
- /opt/district-builder/user-data/config_settings.py:/usr/src/app/publicmapping/config_settings.py
- /opt/district-builder/user-data/config.xml:/usr/src/app/config/config.xml
build:
context: ./xray
env_file:
- .env
ports:
- "${XRAY_DAEMON_PORT}:${XRAY_DAEMON_PORT}"
- "${XRAY_DAEMON_PORT}:${XRAY_DAEMON_PORT}/udp"
logging:
driver: syslog
options:
tag: districtbuilder-languages
tag: districtbuilder-xray
@@ -0,0 +1,6 @@
FROM amazonlinux
RUN yum install -y unzip
RUN curl -o daemon.zip https://s3.dualstack.us-east-2.amazonaws.com/aws-xray-assets.us-east-2/xray-daemon/aws-xray-daemon-linux-3.x.zip
RUN unzip daemon.zip && cp xray /usr/bin/xray
ENTRYPOINT ["/usr/bin/xray", "-b", "0.0.0.0:2000", "-t", "0.0.0.0:2000", "-o", "-l", "info"]
EXPOSE 2000/udp

0 comments on commit 8cfa35f

Please sign in to comment.
You can’t perform that action at this time.