Skip to content

Commit 14dd76b

Browse files
authored
Merge pull request #12 from instana/better_error_logging
Improved error capture and reporting
2 parents e8af8e8 + 28775c2 commit 14dd76b

File tree

8 files changed

+39
-11
lines changed

8 files changed

+39
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ To enable runtime monitoring (without request tracing), set the following enviro
4646

4747
`export AUTOWRAPT_BOOTSTRAP=runtime`
4848

49-
## UWSGI
49+
## uWSGI
5050

51-
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).
51+
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).
5252

5353
## Usage
5454

instana/django.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def __call__(self, request):
3131

3232
response = self.get_response(request)
3333

34+
if 500 <= response.status_code <= 511:
35+
span.set_tag("error", True)
36+
ec = span.tags.get('ec', 0)
37+
span.set_tag("ec", ec+1)
38+
3439
span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
3540
ot.global_tracer.inject(span.context, ot.Format.HTTP_HEADERS, response)
3641
span.finish()

instana/django19.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import opentracing as ot
33
from instana import tracer, options
44
import opentracing.ext.tags as ext
5+
import os
56

67

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

3233
def process_response(self, request, response):
3334
if self.span:
35+
if 500 <= response.status_code <= 511:
36+
self.span.set_tag("error", True)
37+
ec = self.span.tags.get('ec', 0)
38+
self.span.set_tag("ec", ec+1)
39+
3440
self.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
3541
ot.global_tracer.inject(self.span.context, ot.Format.HTTP_HEADERS, response)
3642
self.span.finish()
@@ -41,6 +47,12 @@ def process_response(self, request, response):
4147

4248
def hook(module):
4349
""" Hook method to install the Instana middleware into Django """
50+
if "INSTANA_DEV" in os.environ:
51+
print("==============================================================")
52+
print("Instana: Running django19 hook")
53+
print("==============================================================")
54+
55+
4456
if DJ19_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE_CLASSES:
4557
return
4658

instana/options.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import os
23

34

45
class Options(object):
@@ -8,4 +9,7 @@ class Options(object):
89
log_level = logging.WARN
910

1011
def __init__(self, **kwds):
12+
if "INSTANA_DEV" in os.environ:
13+
self.log_level = logging.DEBUG
14+
1115
self.__dict__.update(kwds)

instana/recorder.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ def build_registered_span(self, span):
9393
ts=int(round(span.start_time * 1000)),
9494
d=int(round(span.duration * 1000)),
9595
f=self.sensor.agent.from_,
96+
ec=self.get_tag(span, "ec"),
97+
error=self.get_tag(span, "error"),
9698
data=data)
9799

98100
def build_sdk_span(self, span):

instana/runtime.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ def hook(module):
1111
print("==========================================================")
1212
print("Instana: Running runtime hook")
1313
print("==========================================================")
14-
level = logging.DEBUG
15-
else:
16-
level = logging.WARN
1714

18-
opts = options.Options(log_level=level)
15+
opts = options.Options()
1916
ot.global_tracer = tracer.InstanaTracer(opts)

instana/span.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class InstanaSpan(object):
88
n = None
99
f = None
1010
ec = 0
11+
error = False
1112
data = None
1213

1314
def __init__(self, **kwds):

instana/wsgi.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import opentracing as ot
22
from instana import tracer, options
3-
import opentracing.ext.tags as ext
3+
import opentracing.ext.tags as tags
44
import logging
55

66

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

1010
def __init__(self, app):
1111
self.app = app
12-
opts = options.Options(log_level=logging.DEBUG)
12+
opts = options.Options()
1313
ot.global_tracer = tracer.InstanaTracer(opts)
1414
self
1515

@@ -20,7 +20,14 @@ def new_start_response(status, headers, exc_info=None):
2020
"""Modified start response with additional headers."""
2121
ot.global_tracer.inject(span.context, ot.Format.HTTP_HEADERS, headers)
2222
res = start_response(status, headers, exc_info)
23-
span.set_tag(ext.HTTP_STATUS_CODE, status.split(' ')[0])
23+
24+
sc = status.split(' ')[0]
25+
if 500 <= int(sc) <= 511:
26+
span.set_tag("error", True)
27+
ec = span.tags.get('ec', 0)
28+
span.set_tag("ec", ec+1)
29+
30+
span.set_tag(tags.HTTP_STATUS_CODE, sc)
2431
span.finish()
2532
return res
2633

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

33-
span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
40+
span.set_tag(tags.HTTP_URL, env['PATH_INFO'])
3441
span.set_tag("http.params", env['QUERY_STRING'])
35-
span.set_tag(ext.HTTP_METHOD, env['REQUEST_METHOD'])
42+
span.set_tag(tags.HTTP_METHOD, env['REQUEST_METHOD'])
3643
span.set_tag("http.host", env['HTTP_HOST'])
3744

3845
return self.app(environ, new_start_response)

0 commit comments

Comments
 (0)