diff --git a/.gitignore b/.gitignore index 15609f5..6819b88 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ /sentry-sdk-benchmark /FrameworkBenchmarks/ + +__pycache__ \ No newline at end of file diff --git a/README.md b/README.md index 13c8e7f..59f97c8 100644 --- a/README.md +++ b/README.md @@ -127,3 +127,27 @@ docker images -f "dangling=true" -q | xargs -tn10 docker rmi -f ## Adding More Platforms See [platform/README.md](platform/README.md). + +## Licensing +This repostitory may contain code from https://github.com/nylas/nylas-perftools, which is licensed under the following license: +> The MIT License (MIT) +> +> Copyright (c) 2014 Nylas +> +> Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/platform/python/django/instrumented_nylas/django-postgresql.dockerfile b/platform/python/django/instrumented_nylas/django-postgresql.dockerfile new file mode 100644 index 0000000..ad90bbc --- /dev/null +++ b/platform/python/django/instrumented_nylas/django-postgresql.dockerfile @@ -0,0 +1,13 @@ +FROM python:3.9.1-buster + +WORKDIR /django + +COPY requirements.txt ./ +RUN pip install -r requirements.txt +COPY requirements-sentry.txt ./ +RUN pip install -r requirements-sentry.txt +COPY . ./ + +EXPOSE 8080 + +CMD gunicorn --pid=gunicorn.pid hello.wsgi:application -c gunicorn_conf.py --env DJANGO_DB=postgresql diff --git a/platform/python/django/instrumented_nylas/gunicorn_conf.py b/platform/python/django/instrumented_nylas/gunicorn_conf.py new file mode 100644 index 0000000..fb41f03 --- /dev/null +++ b/platform/python/django/instrumented_nylas/gunicorn_conf.py @@ -0,0 +1,26 @@ +import multiprocessing +import os +import sys + +_is_pypy = hasattr(sys, 'pypy_version_info') +_is_travis = os.environ.get('TRAVIS') == 'true' + +workers = multiprocessing.cpu_count() * 3 +if _is_travis: + workers = 2 + +bind = "0.0.0.0:8080" +keepalive = 120 +errorlog = '-' +pidfile = 'gunicorn.pid' +pythonpath = 'hello' + +if _is_pypy: + worker_class = "tornado" +else: + worker_class = "meinheld.gmeinheld.MeinheldWorker" + + def post_fork(server, worker): + # Disalbe access log + import meinheld.server + meinheld.server.set_access_logger(None) diff --git a/platform/python/django/instrumented_nylas/hello/hello/__init__.py b/platform/python/django/instrumented_nylas/hello/hello/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/platform/python/django/instrumented_nylas/hello/hello/nylas_middleware.py b/platform/python/django/instrumented_nylas/hello/hello/nylas_middleware.py new file mode 100644 index 0000000..41580e6 --- /dev/null +++ b/platform/python/django/instrumented_nylas/hello/hello/nylas_middleware.py @@ -0,0 +1,13 @@ +import hello.nylas_profiler as nylas_profiler + +class NylasMiddleware: + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + profiler = nylas_profiler.Sampler('test') + profiler.start() + response = self.get_response(request) + profiler.stop() + + return response diff --git a/platform/python/django/instrumented_nylas/hello/hello/nylas_profiler.py b/platform/python/django/instrumented_nylas/hello/hello/nylas_profiler.py new file mode 100644 index 0000000..27d7d63 --- /dev/null +++ b/platform/python/django/instrumented_nylas/hello/hello/nylas_profiler.py @@ -0,0 +1,180 @@ +""" +This file contains code from https://github.com/nylas/nylas-perftools, which is published under the following license: + +The MIT License (MIT) + +Copyright (c) 2014 Nylas + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +""" + +import atexit +import os +import signal +import time +import threading +import json + +def nanosecond_time(): + return int(time.perf_counter() * 1e9) + +class FrameData: + def __init__(self, frame): + self.function_name = frame.f_code.co_name + self.module = frame.f_globals['__name__'] + + # Depending on Python version, frame.f_code.co_filename either stores just the file name or the entire absolute path. + self.file_name = frame.f_code.co_filename + self.line_number = frame.f_code.co_firstlineno + + @property + def _attribute_tuple(self): + """Returns a tuple of the attributes used in comparison""" + return (self.function_name, self.module, self.file_name, self.line_number) + + def __eq__(self, other): + if isinstance(other, FrameData): + return self._attribute_tuple == other._attribute_tuple + return False + + def __hash__(self): + return hash(self._attribute_tuple) + + def __str__(self): + return f'{self.function_name}({self.module}) in {self.file_name}:{self.line_number}' + +class StackSample: + def __init__(self, top_frame, profiler_start_time, frame_indices): + self.sample_time = nanosecond_time() - profiler_start_time + self.stack = [] + self._add_all_frames(top_frame, frame_indices) + + def _add_all_frames(self, top_frame, frame_indices): + frame = top_frame + while frame is not None: + frame_data = FrameData(frame) + if frame_data not in frame_indices: + frame_indices[frame_data] = len(frame_indices) + self.stack.append(frame_indices[frame_data]) + frame = frame.f_back + self.stack = list(reversed(self.stack)) + + def __str__(self): + return f'Time: {self.sample_time}; Stack: {[str(frame) for frame in reversed(self.stack)]}' + +class Sampler(object): + """ + A simple stack sampler for low-overhead CPU profiling: samples the call + stack every `interval` seconds and keeps track of counts by frame. Because + this uses signals, it only works on the main thread. + """ + def __init__(self, transaction, interval=0.01): + self.interval = interval + self.stack_samples = [] + self._transaction = transaction + + def __enter__(self): + self.start() + + def __exit__(self, *_): + self.stop() + # if len(self.stack_samples) > 0: + # with open('test_profile.json', 'w') as f: + # f.write(self.to_json()) + + def start(self): + self._start_time = nanosecond_time() + self.stack_samples = [] + self._frame_indices = dict() + try: + signal.signal(signal.SIGVTALRM, self._sample) + except ValueError: + raise ValueError('cannot run on non main thread') + return + + signal.setitimer(signal.ITIMER_VIRTUAL, self.interval) + atexit.register(self.stop) + + def sample_weights(self): + """ + Return the weights of each sample (difference between the sample's and previous sample's timestamp). + """ + if self.stack_samples == []: + return [] + + return [self.stack_samples[0].sample_time, *(sample.sample_time - prev_sample.sample_time for sample, prev_sample in zip(self.stack_samples[1:], self.stack_samples))] + + def _sample(self, _, frame): + self.stack_samples.append(StackSample(frame, self._start_time, self._frame_indices)) + signal.setitimer(signal.ITIMER_VIRTUAL, self.interval) + + def to_json(self): + """ + Exports this object to a JSON format compatible with Sentry's profiling visualizer + """ + return json.dumps(self, cls=self.JSONEncoder) + + def frame_list(self): + # Build frame array from the frame indices + frames = [None] * len(self._frame_indices) + for frame, index in self._frame_indices.items(): + frames[index] = frame + return frames + + def samples(self): + return len(self.stack_samples) + + def __str__(self): + return '\n'.join([str(sample) for sample in self.stack_samples]) + + def stop(self): + signal.setitimer(signal.ITIMER_VIRTUAL, 0) + + def __del__(self): + self.stop() + + @property + def transaction_name(self): + return self._transaction.name + + class JSONEncoder(json.JSONEncoder): + def default(self, o): + if isinstance(o, Sampler): + return { + 'transactionName': o.transaction_name, + 'profiles': [{ + 'weights': o.sample_weights(), + 'samples': [sample.stack for sample in o.stack_samples], + 'type': 'sampled', + 'endValue': o.stack_samples[-1].sample_time, # end ts + 'startValue': 0, # start ts + 'name': 'main', + 'unit': 'nanoseconds', + 'threadID': threading.get_ident() + }], + 'shared': { + 'frames': [{ + 'name': frame.function_name, + 'file': frame.file_name, + 'line': frame.line_number + } for frame in o.frame_list()] # TODO: Add all elements + # 'frames': [{ + # 'key': string | number, + # 'name': string, + # 'file': string, + # 'line': number, + # 'column': number, + # 'is_application': boolean, + # 'image': string, + # 'resource': string, + # 'threadId': number + # }] # TODO: Add all elements + } + } + + else: + return json.JSONEncoder.default(self, o) diff --git a/platform/python/django/instrumented_nylas/hello/hello/settings.py b/platform/python/django/instrumented_nylas/hello/hello/settings.py new file mode 100644 index 0000000..2ec6e32 --- /dev/null +++ b/platform/python/django/instrumented_nylas/hello/hello/settings.py @@ -0,0 +1,71 @@ +import os +import sentry_sdk +from sentry_sdk.integrations.django import DjangoIntegration + +sentry_sdk.init( + integrations=[DjangoIntegration()], + traces_sample_rate=1.0, + send_default_pii=True, + # debug=True, +) + +DEBUG = False + +SECRET_KEY = '_7mb6#v4yf@qhc(r(zbyh&z_iby-na*7wz&-v6pohsul-d#y5f' +ADMINS = () + +MANAGERS = ADMINS + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.' + os.environ['DJANGO_DB'], # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. + 'NAME': 'hello_world', # Or path to database file if using sqlite3. + 'USER': 'benchmarkdbuser', # Not used with sqlite3. + 'PASSWORD': 'benchmarkdbpass', # Not used with sqlite3. + 'HOST': 'tfb-database', # Set to empty string for localhost. Not used with sqlite3. + 'PORT': '', # Set to empty string for default. Not used with sqlite3. + 'CONN_MAX_AGE': 30, + } +} + +TIME_ZONE = 'America/Chicago' +LANGUAGE_CODE = 'en-us' +USE_I18N = False +USE_L10N = False +USE_TZ = False + +MEDIA_ROOT = '' +MEDIA_URL = '' +STATIC_ROOT = '' +STATIC_URL = '/static/' +STATICFILES_DIRS = () +STATICFILES_FINDERS = () +MIDDLEWARE = ['hello.nylas_middleware.NylasMiddleware'] + +ROOT_URLCONF = 'hello.urls' +WSGI_APPLICATION = 'hello.wsgi.application' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': {}, + }, +] + +INSTALLED_APPS = ( + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'world', +) + +LOGGING = { + 'version': 1, + 'disable_existing_loggers': True, + 'handlers': {}, + 'loggers': {}, + +} + +ALLOWED_HOSTS = ['*'] diff --git a/platform/python/django/instrumented_nylas/hello/hello/urls.py b/platform/python/django/instrumented_nylas/hello/hello/urls.py new file mode 100644 index 0000000..fbf2fbb --- /dev/null +++ b/platform/python/django/instrumented_nylas/hello/hello/urls.py @@ -0,0 +1,11 @@ +from django.conf.urls import url +from world.views import plaintext, json, db, dbs, fortunes, update + +urlpatterns = [ + url(r'^plaintext$', plaintext), + url(r'^json$', json), + url(r'^db$', db), + url(r'^dbs$', dbs), + url(r'^fortunes$', fortunes), + url(r'^update$', update), +] diff --git a/platform/python/django/instrumented_nylas/hello/hello/wsgi.py b/platform/python/django/instrumented_nylas/hello/hello/wsgi.py new file mode 100644 index 0000000..717367e --- /dev/null +++ b/platform/python/django/instrumented_nylas/hello/hello/wsgi.py @@ -0,0 +1,21 @@ +""" +WSGI config for hello project. + +This module contains the WSGI application used by Django's development server +and any production WSGI deployments. It should expose a module-level variable +named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover +this application via the ``WSGI_APPLICATION`` setting. + +Usually you will have the standard Django WSGI application here, but it also +might make sense to replace the whole Django WSGI application with a custom one +that later delegates to the Django one. For example, you could introduce WSGI +middleware here, or combine a Django application with an application of another +framework. + +""" +import os + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello.settings") + +from django.core.wsgi import get_wsgi_application +application = get_wsgi_application() diff --git a/platform/python/django/instrumented_nylas/hello/manage.py b/platform/python/django/instrumented_nylas/hello/manage.py new file mode 100644 index 0000000..96d6d94 --- /dev/null +++ b/platform/python/django/instrumented_nylas/hello/manage.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello.settings") + + from django.core.management import execute_from_command_line + + execute_from_command_line(sys.argv) diff --git a/platform/python/django/instrumented_nylas/hello/world/__init__.py b/platform/python/django/instrumented_nylas/hello/world/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/platform/python/django/instrumented_nylas/hello/world/models.py b/platform/python/django/instrumented_nylas/hello/world/models.py new file mode 100644 index 0000000..4756e2b --- /dev/null +++ b/platform/python/django/instrumented_nylas/hello/world/models.py @@ -0,0 +1,15 @@ +from django.db import models + + +class World(models.Model): + randomnumber = models.IntegerField() + + class Meta: + db_table = 'world' + + +class Fortune(models.Model): + message = models.CharField(max_length=65535) + + class Meta: + db_table = 'fortune' diff --git a/platform/python/django/instrumented_nylas/hello/world/templates/base.html b/platform/python/django/instrumented_nylas/hello/world/templates/base.html new file mode 100644 index 0000000..8a23e83 --- /dev/null +++ b/platform/python/django/instrumented_nylas/hello/world/templates/base.html @@ -0,0 +1,9 @@ + + + +Fortunes + + + {% block content %}{% endblock %} + + \ No newline at end of file diff --git a/platform/python/django/instrumented_nylas/hello/world/templates/fortunes.html b/platform/python/django/instrumented_nylas/hello/world/templates/fortunes.html new file mode 100644 index 0000000..8b8bed8 --- /dev/null +++ b/platform/python/django/instrumented_nylas/hello/world/templates/fortunes.html @@ -0,0 +1,16 @@ +{% extends "base.html" %} + +{% block content %} + + + + + +{% for fortune in fortunes %} + + + + +{% endfor %} +
idmessage
{{ fortune.id }}{{ fortune.message }}
+{% endblock %} diff --git a/platform/python/django/instrumented_nylas/hello/world/views.py b/platform/python/django/instrumented_nylas/hello/world/views.py new file mode 100644 index 0000000..5dd6a35 --- /dev/null +++ b/platform/python/django/instrumented_nylas/hello/world/views.py @@ -0,0 +1,76 @@ +import random +from operator import itemgetter +from functools import partial +from ujson import dumps as uj_dumps + +from django.http import HttpResponse +from django.shortcuts import render + +from world.models import World, Fortune + + +_random_int = partial(random.randint, 1, 10000) + + +def _get_queries(request): + try: + queries = int(request.GET.get('queries', 1)) + except Exception: + queries = 1 + if queries < 1: + queries = 1 + if queries > 500: + queries = 500 + return queries + + +def plaintext(request): + return HttpResponse("Hello, World!", content_type="text/plain") + + +def json(request): + return HttpResponse( + uj_dumps({"message": "Hello, World!"}), + content_type="application/json" + ) + + +def db(request): + r = _random_int() + world = uj_dumps({ + 'id': r, + 'randomNumber': World.objects.get(id=r).randomnumber + }) + return HttpResponse(world, content_type="application/json") + + +def dbs(request): + queries = _get_queries(request) + + def caller(input_): + int_ = _random_int() + return {'id': int_, 'randomNumber': World.objects.get(id=int_).randomnumber} + worlds = tuple(map(caller, range(queries))) + + return HttpResponse(uj_dumps(worlds), content_type="application/json") + + +def fortunes(request): + fortunes = list(Fortune.objects.values('id', 'message')) + fortunes.append({"id": 0, 'message': "Additional fortune added at request time."}) + fortunes.sort(key=itemgetter('message')) + + return render(request, 'fortunes.html', {'fortunes': fortunes}) + + +def update(request): + queries = _get_queries(request) + + def caller(input_): + w = World.objects.get(id=_random_int()) + w.randomnumber = _random_int() + w.save() + return {'id': w.id, 'randomNumber': w.randomnumber} + worlds = tuple(map(caller, range(queries))) + + return HttpResponse(uj_dumps(worlds), content_type="application/json") diff --git a/platform/python/django/instrumented_nylas/requirements-sentry.txt b/platform/python/django/instrumented_nylas/requirements-sentry.txt new file mode 100644 index 0000000..b668d61 --- /dev/null +++ b/platform/python/django/instrumented_nylas/requirements-sentry.txt @@ -0,0 +1 @@ +sentry-sdk==1.3.0 diff --git a/platform/python/django/instrumented_nylas/requirements.txt b/platform/python/django/instrumented_nylas/requirements.txt new file mode 100644 index 0000000..4993bf9 --- /dev/null +++ b/platform/python/django/instrumented_nylas/requirements.txt @@ -0,0 +1,8 @@ +Django==3.1.12 +greenlet==0.4.17 +gunicorn==20.0.4 +meinheld==1.0.2 +mysqlclient==1.4.6 +psycopg2==2.8.6 +pytz==2020.4 +ujson==4.0.1 \ No newline at end of file diff --git a/template/docker-compose.yml.tmpl b/template/docker-compose.yml.tmpl index b063ecc..3addebb 100644 --- a/template/docker-compose.yml.tmpl +++ b/template/docker-compose.yml.tmpl @@ -61,7 +61,7 @@ services: {{- if .NeedsRelay }} - "relay" environment: - SENTRY_DSN: "http://sentry@relay:5000/1" + SENTRY_DSN: "https://ce64aa6603f24add8e0b281678ff2d36@o1261078.ingest.sentry.io/6510008" OTEL_EXPORTER_ZIPKIN_ENDPOINT: "http://relay:5000/api/v2/spans" relay: container_name: "fakerelay-{{ .RunName }}-{{ .ID }}"