Skip to content

Commit 6bedf12

Browse files
authored
Merge pull request #3 from instana/post_import_hooks
Auto instrumentation using post import hooks
2 parents efa6a17 + 7929352 commit 6bedf12

File tree

7 files changed

+37
-32
lines changed

7 files changed

+37
-32
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: python
22
python:
33
- "2.7"
4-
- "3.6"
54
install: "pip install -r requirements.txt"
6-
script: nosetests
5+
script: nosetests -v

README.md

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,17 @@ Any and all feedback is welcome. Happy Python visibility.
1616

1717
## Installation
1818

19-
The instana package is [hosted on pypi](https://pypi.python.org/pypi/instana) and can be installed with:
19+
There are two steps required to install the the Instana package for your applications:
2020

21-
$ pip install instana
21+
1. `pip install instana` into the virtual-env or container ([hosted on pypi](https://pypi.python.org/pypi/instana))
22+
23+
2. Enable instrumentation for the frameworks in use by setting an environment variable:
24+
`AUTOWRAPT_BOOTSTRAP=instana.django`
2225

2326
## Usage
2427

2528
The instana package will automatically collect key metrics from your Python processes. Just install and go.
2629

27-
## Django Middleware
28-
29-
For the initial BETA, Django instrumentation must be manually inserted into the Django middleware list. This will be automated before post BETA release.
30-
31-
in `myapp/settings.py`:
32-
```python
33-
import instana.middleware
34-
35-
MIDDLEWARE = [
36-
'instana.middleware.InstanaMiddleware', # Add Instana at the start of the list
37-
'django.middleware.security.SecurityMiddleware',
38-
'django.contrib.sessions.middleware.SessionMiddleware',
39-
'django.middleware.common.CommonMiddleware',
40-
'django.middleware.csrf.CsrfViewMiddleware',
41-
'django.contrib.auth.middleware.AuthenticationMiddleware',
42-
'django.contrib.messages.middleware.MessageMiddleware',
43-
'django.middleware.clickjacking.XFrameOptionsMiddleware',
44-
]
45-
```
46-
4730
## Tracing
4831

4932
This Python package supports [OpenTracing](http://opentracing.io/).

instana/middleware.py renamed to instana/django.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
from __future__ import print_function
12
import opentracing as ot
23
from instana import tracer
34
import opentracing.ext.tags as ext
45

6+
DJ_INSTANA_MIDDLEWARE = 'instana.django.InstanaMiddleware'
7+
58

69
class InstanaMiddleware(object):
10+
""" Django Middleware to provide request tracing for Instana """
711
def __init__(self, get_response):
812
self.get_response = get_response
913
ot.global_tracer = tracer.InstanaTracer()
@@ -28,3 +32,18 @@ def __call__(self, request):
2832
ot.global_tracer.inject(span.context, ot.Format.HTTP_HEADERS, response)
2933
span.finish()
3034
return response
35+
36+
37+
def hook(module):
38+
""" Hook method to install the Instana middleware into Django """
39+
if DJ_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE:
40+
return
41+
42+
if type(module.settings.MIDDLEWARE) is tuple:
43+
module.settings.MIDDLEWARE = (
44+
DJ_INSTANA_MIDDLEWARE,) + module.settings.MIDDLEWARE
45+
elif type(module.settings.MIDDLEWARE) is list:
46+
module.settings.MIDDLEWARE = [
47+
DJ_INSTANA_MIDDLEWARE] + module.settings.MIDDLEWARE
48+
else:
49+
print("Instana: Couldn't add InstanaMiddleware to Django")

instana/fsm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def announce_sensor(self, e):
111111
return True
112112

113113
def schedule_retry(self, fun, e, name):
114-
l.error("Scheduling: " + name)
114+
l.debug("Scheduling: " + name)
115115
self.timer = t.Timer(self.RETRY_PERIOD, fun, [e])
116116
self.timer.daemon = True
117117
self.timer.name = name

instana/recorder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import instana.span as sd
77
import Queue
88
import time
9+
import os
910

1011

1112
class InstanaRecorder(SpanRecorder):
@@ -60,7 +61,7 @@ def record_span(self, span):
6061
Convert the passed BasicSpan into an InstanaSpan and
6162
add it to the span queue
6263
"""
63-
if self.sensor.agent.can_send():
64+
if self.sensor.agent.can_send() or os.environ["INSTANA_TEST"]:
6465
instana_span = None
6566

6667
if span.operation_name in self.registered_spans:

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ fysom>=2.1.2
33
opentracing>=1.2.1
44
basictracer>=2.2.0
55
psutil>=5.1.3
6+
autowrapt>=1.0

setup.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup, find_packages
22

33
setup(name='instana',
4-
version='0.0.1.2',
4+
version='0.4.0',
55
download_url='https://github.com/instana/python-sensor',
66
url='https://www.instana.com/',
77
license='MIT',
@@ -11,11 +11,13 @@
1111
packages=find_packages(exclude=['tests', 'examples']),
1212
long_description="The instana package provides Python metrics and traces for Instana.",
1313
zip_safe=False,
14-
setup_requires=['nose>=1.0',
15-
'fysom>=2.1.2',
16-
'opentracing>=1.2.1,<1.3',
17-
'basictracer>=2.2.0',
18-
'psutil>=5.1.3'],
14+
setup_requires=['nose>=1.0'],
15+
install_requires=['autowrapt>=1.0',
16+
'fysom>=2.1.2',
17+
'opentracing>=1.2.1,<1.3',
18+
'basictracer>=2.2.0',
19+
'psutil>=5.1.3'],
20+
entry_points={'instana.django': ['django.core.handlers.base = instana.django:hook']},
1921
test_suite='nose.collector',
2022
keywords=['performance', 'opentracing', 'metrics', 'monitoring'],
2123
classifiers=[

0 commit comments

Comments
 (0)