Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved error capture and reporting #12

Merged
merged 7 commits into from
Aug 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ To enable runtime monitoring (without request tracing), set the following enviro

`export AUTOWRAPT_BOOTSTRAP=runtime`

## UWSGI
## uWSGI

This Python instrumentation spawns a lightweight background thread to periodically collect and report process metrics. By default, the GIL and threading is disabled under `uwsgi`. If you wish to instrument your application running under UWSGI, make sure that you enable threads by passing `--enable-thread` (or `enable-threads = true` in ini style). More details in the [uwsgi documentation](https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html#a-note-on-python-threads).
This Python instrumentation spawns a lightweight background thread to periodically collect and report process metrics. By default, the GIL and threading is disabled under uWSGI. If you wish to instrument your application running under uWSGI, make sure that you enable threads by passing `--enable-thread` (or `enable-threads = true` in ini style). More details in the [uWSGI documentation](https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html#a-note-on-python-threads).

## Usage

Expand Down
5 changes: 5 additions & 0 deletions instana/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def __call__(self, request):

response = self.get_response(request)

if 500 <= response.status_code <= 511:
span.set_tag("error", True)
ec = span.tags.get('ec', 0)
span.set_tag("ec", ec+1)

span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
ot.global_tracer.inject(span.context, ot.Format.HTTP_HEADERS, response)
span.finish()
Expand Down
12 changes: 12 additions & 0 deletions instana/django19.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import opentracing as ot
from instana import tracer, options
import opentracing.ext.tags as ext
import os


DJ19_INSTANA_MIDDLEWARE = 'instana.django19.InstanaMiddleware19'
Expand Down Expand Up @@ -31,6 +32,11 @@ def process_request(self, request):

def process_response(self, request, response):
if self.span:
if 500 <= response.status_code <= 511:
self.span.set_tag("error", True)
ec = self.span.tags.get('ec', 0)
self.span.set_tag("ec", ec+1)

self.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
ot.global_tracer.inject(self.span.context, ot.Format.HTTP_HEADERS, response)
self.span.finish()
Expand All @@ -41,6 +47,12 @@ def process_response(self, request, response):

def hook(module):
""" Hook method to install the Instana middleware into Django """
if "INSTANA_DEV" in os.environ:
print("==============================================================")
print("Instana: Running django19 hook")
print("==============================================================")


if DJ19_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE_CLASSES:
return

Expand Down
4 changes: 4 additions & 0 deletions instana/options.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os


class Options(object):
Expand All @@ -8,4 +9,7 @@ class Options(object):
log_level = logging.WARN

def __init__(self, **kwds):
if "INSTANA_DEV" in os.environ:
self.log_level = logging.DEBUG

self.__dict__.update(kwds)
2 changes: 2 additions & 0 deletions instana/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def build_registered_span(self, span):
ts=int(round(span.start_time * 1000)),
d=int(round(span.duration * 1000)),
f=self.sensor.agent.from_,
ec=self.get_tag(span, "ec"),
error=self.get_tag(span, "error"),
data=data)

def build_sdk_span(self, span):
Expand Down
5 changes: 1 addition & 4 deletions instana/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ def hook(module):
print("==========================================================")
print("Instana: Running runtime hook")
print("==========================================================")
level = logging.DEBUG
else:
level = logging.WARN

opts = options.Options(log_level=level)
opts = options.Options()
ot.global_tracer = tracer.InstanaTracer(opts)
1 change: 1 addition & 0 deletions instana/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class InstanaSpan(object):
n = None
f = None
ec = 0
error = False
data = None

def __init__(self, **kwds):
Expand Down
17 changes: 12 additions & 5 deletions instana/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import opentracing as ot
from instana import tracer, options
import opentracing.ext.tags as ext
import opentracing.ext.tags as tags
import logging


Expand All @@ -9,7 +9,7 @@ class iWSGIMiddleware(object):

def __init__(self, app):
self.app = app
opts = options.Options(log_level=logging.DEBUG)
opts = options.Options()
ot.global_tracer = tracer.InstanaTracer(opts)
self

Expand All @@ -20,7 +20,14 @@ def new_start_response(status, headers, exc_info=None):
"""Modified start response with additional headers."""
ot.global_tracer.inject(span.context, ot.Format.HTTP_HEADERS, headers)
res = start_response(status, headers, exc_info)
span.set_tag(ext.HTTP_STATUS_CODE, status.split(' ')[0])

sc = status.split(' ')[0]
if 500 <= int(sc) <= 511:
span.set_tag("error", True)
ec = span.tags.get('ec', 0)
span.set_tag("ec", ec+1)

span.set_tag(tags.HTTP_STATUS_CODE, sc)
span.finish()
return res

Expand All @@ -30,9 +37,9 @@ def new_start_response(status, headers, exc_info=None):
else:
span = ot.global_tracer.start_span("wsgi")

span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
span.set_tag(tags.HTTP_URL, env['PATH_INFO'])
span.set_tag("http.params", env['QUERY_STRING'])
span.set_tag(ext.HTTP_METHOD, env['REQUEST_METHOD'])
span.set_tag(tags.HTTP_METHOD, env['REQUEST_METHOD'])
span.set_tag("http.host", env['HTTP_HOST'])

return self.app(environ, new_start_response)
Expand Down