File: README.rst

package info (click to toggle)
python-applicationinsights 0.11.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 876 kB
  • sloc: python: 5,948; makefile: 154; sh: 77
file content (429 lines) | stat: -rw-r--r-- 15,943 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
Application Insights for Python
===============================

.. image:: https://travis-ci.org/Microsoft/ApplicationInsights-Python.svg?branch=master
    :target: https://travis-ci.org/Microsoft/ApplicationInsights-Python

.. image:: https://badge.fury.io/py/applicationinsights.svg
    :target: http://badge.fury.io/py/applicationinsights


This project extends the Application Insights API surface to support Python.
`Application Insights
<http://azure.microsoft.com/services/application-insights/>`_ is a service that
allows developers to keep their application available, performing and
succeeding. This Python module will allow you to send telemetry of various kinds
(event, trace, exception, etc.) to the Application Insights service where they
can be visualized in the Azure Portal. A link to the Application Insights API
documentation can be found `here
<https://microsoft.github.io/ApplicationInsights-Python/>`_.

This project is not officially supported and not recommended for high load
production use. The project is open source and welcomes contributions. Please
refer to
`CONTRIBUTING.md <https://github.com/Microsoft/ApplicationInsights-Python/blob/develop/CONTRIBUTING.md>`_
for details.

Requirements
------------

Python >=2.7 and Python >=3.4 are currently supported by this module.

Installation
------------

To install the latest release you can use `pip <http://www.pip-installer.org/>`_.

::

    $ pip install applicationinsights

Documentation
-------------

Please see https://microsoft.github.io/ApplicationInsights-Python/ for full documentation.

Usage
-----

Once installed, you can send telemetry to Application Insights. Here are a few samples.

    **Note**: before you can send data to you will need an instrumentation key. Please see the `Getting an Application Insights Instrumentation Key <https://github.com/Microsoft/AppInsights-Home/wiki#getting-an-application-insights-instrumentation-key>`_ section for more information.

**Sending a simple event telemetry item**

.. code:: python

    from applicationinsights import TelemetryClient
    tc = TelemetryClient('<YOUR INSTRUMENTATION KEY GOES HERE>')
    tc.track_event('Test event')
    tc.flush()

**Sending an event telemetry item with custom properties and measurements**

.. code:: python

    from applicationinsights import TelemetryClient
    tc = TelemetryClient('<YOUR INSTRUMENTATION KEY GOES HERE>')
    tc.track_event('Test event', { 'foo': 'bar' }, { 'baz': 42 })
    tc.flush()

**Sending a trace telemetry item with custom properties**

.. code:: python

    from applicationinsights import TelemetryClient
    tc = TelemetryClient('<YOUR INSTRUMENTATION KEY GOES HERE>')
    tc.track_trace('Test trace', { 'foo': 'bar' })
    tc.flush()

**Sending a metric telemetry item**

.. code:: python

    from applicationinsights import TelemetryClient
    tc = TelemetryClient('<YOUR INSTRUMENTATION KEY GOES HERE>')
    tc.track_metric('My Metric', 42)
    tc.flush()

**Sending an exception telemetry item with custom properties and measurements**

.. code:: python

    import sys
    from applicationinsights import TelemetryClient
    tc = TelemetryClient('<YOUR INSTRUMENTATION KEY GOES HERE>')
    try:
        raise Exception('blah')
    except:
        tc.track_exception()

    try:
        raise Exception("blah")
    except:
        tc.track_exception(*sys.exc_info(), properties={ 'foo': 'bar' }, measurements={ 'x': 42 })
    tc.flush()

**Configuring context for a telemetry client instance**

.. code:: python

    from applicationinsights import TelemetryClient
    tc = TelemetryClient('<YOUR INSTRUMENTATION KEY GOES HERE>')
    tc.context.application.ver = '1.2.3'
    tc.context.device.id = 'My current device'
    tc.context.device.oem_name = 'Asus'
    tc.context.device.model = 'X31A'
    tc.context.device.type = "Other"
    tc.context.user.id = 'santa@northpole.net'
    tc.context.properties['my_property'] = 'my_value'
    tc.track_trace('My trace with context')
    tc.flush()

**Establishing correlation between telemetry objects**

context field called operation_id can be set to associate telemetry items.
Since operation_id is being set as a property of telemetry client, the client shouldn't be reused in parallel threads as it might lead to concurrency issues.

.. code:: python
  
    tc = TelemetryClient(instrumentation_key=instrumentation_key)
    tc.context.operation.id = <operation_id>
    tc.track_trace('Test trace')
    tc.flush()

**Configuring channel related properties**

.. code:: python

    from applicationinsights import TelemetryClient
    tc = TelemetryClient('<YOUR INSTRUMENTATION KEY GOES HERE>')
    # flush telemetry every 30 seconds (assuming we don't hit max_queue_item_count first)
    tc.channel.sender.send_interval_in_milliseconds = 30 * 1000
    # flush telemetry if we have 10 or more telemetry items in our queue
    tc.channel.queue.max_queue_length = 10

**Configuring TelemetryProcessor**

.. code:: python

    from applicationinsights import TelemetryClient
    def process(data, context):
       data.properties["NEW_PROP"] = "MYPROP"  # Add property
       context.user.id = "MYID"   # Change ID
       return True # Not filtered
    tc = TelemetryClient('<YOUR INSTRUMENTATION KEY GOES HERE>')
    tc.add_telemetry_processor(process)

**Basic logging configuration (first option)**

.. code:: python

    import logging
    from applicationinsights.logging import enable

    # set up logging
    enable('<YOUR INSTRUMENTATION KEY GOES HERE>')

    # log something (this will be sent to the Application Insights service as a trace)
    logging.info('This is a message')

    # logging shutdown will cause a flush of all un-sent telemetry items
    logging.shutdown()

**Basic logging configuration (second option)**

.. code:: python

    import logging
    from applicationinsights.logging import LoggingHandler

    # set up logging
    handler = LoggingHandler('<YOUR INSTRUMENTATION KEY GOES HERE>')
    logging.basicConfig(handlers=[ handler ], format='%(levelname)s: %(message)s', level=logging.DEBUG)

    # log something (this will be sent to the Application Insights service as a trace)
    logging.debug('This is a message')

    try:
        raise Exception('Some exception')
    except:
        # this will send an exception to the Application Insights service
        logging.exception('Code went boom!')

    # logging shutdown will cause a flush of all un-sent telemetry items
    # alternatively flush manually via handler.flush()
    logging.shutdown()

**Advanced logging configuration**

.. code:: python

    import logging
    from applicationinsights import channel
    from applicationinsights.logging import LoggingHandler

    # set up channel with context
    telemetry_channel = channel.TelemetryChannel()
    telemetry_channel.context.application.ver = '1.2.3'
    telemetry_channel.context.properties['my_property'] = 'my_value'

    # set up logging
    handler = LoggingHandler('<YOUR INSTRUMENTATION KEY GOES HERE>', telemetry_channel=telemetry_channel)
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
    my_logger = logging.getLogger('simple_logger')
    my_logger.setLevel(logging.DEBUG)
    my_logger.addHandler(handler)

    # log something (this will be sent to the Application Insights service as a trace)
    my_logger.debug('This is a message')

    # logging shutdown will cause a flush of all un-sent telemetry items
    # alternatively flush manually via handler.flush()
    logging.shutdown()

**Logging unhandled exceptions**

.. code:: python

    from applicationinsights.exceptions import enable

    # set up exception capture
    enable('<YOUR INSTRUMENTATION KEY GOES HERE>')

    # raise an exception (this will be sent to the Application Insights service as an exception telemetry object)
    raise Exception('Boom!')

    # exceptions will cause a flush of all un-sent telemetry items

**Logging unhandled exceptions with context**

.. code:: python

    from applicationinsights import channel
    from applicationinsights.exceptions import enable

    # set up channel with context
    telemetry_channel = channel.TelemetryChannel()
    telemetry_channel.context.application.ver = '1.2.3'
    telemetry_channel.context.properties['my_property'] = 'my_value'

    # set up exception capture
    enable('<YOUR INSTRUMENTATION KEY GOES HERE>', telemetry_channel=telemetry_channel)

    # raise an exception (this will be sent to the Application Insights service as an exception telemetry object)
    raise Exception('Boom!')

    # exceptions will cause a flush of all un-sent telemetry items

**Integrating with Flask**

.. code:: python

    from flask import Flask
    from applicationinsights.flask.ext import AppInsights
    
    # instantiate the Flask application
    app = Flask(__name__)
    app.config['APPINSIGHTS_INSTRUMENTATIONKEY'] = '<YOUR INSTRUMENTATION KEY GOES HERE>'

    # log requests, traces and exceptions to the Application Insights service
    appinsights = AppInsights(app)

    # define a simple route
    @app.route('/')
    def hello_world():
        # the following message will be sent to the Flask log as well as Application Insights
        app.logger.info('Hello World route was called')

        return 'Hello World!'

    # run the application
    if __name__ == '__main__':
        app.run()

**Integrating with Django**

Place the following in your `settings.py` file:

.. code:: python

    # If on Django < 1.10
    MIDDLEWARE_CLASSES = [
        # ... or whatever is below for you ...
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        # ... or whatever is above for you ...
        'applicationinsights.django.ApplicationInsightsMiddleware',   # Add this middleware to the end
    ]

    # If on Django >= 1.10
    MIDDLEWARE = [
        # ... or whatever is below for you ...
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        # ... or whatever is above for you ...
        'applicationinsights.django.ApplicationInsightsMiddleware',   # Add this middleware to the end
    ]

    APPLICATION_INSIGHTS = {
        # (required) Your Application Insights instrumentation key
        'ikey': "00000000-0000-0000-0000-000000000000",
        
        # (optional) By default, request names are logged as the request method
        # and relative path of the URL.  To log the fully-qualified view names
        # instead, set this to True.  Defaults to False.
        'use_view_name': True,
        
        # (optional) To log arguments passed into the views as custom properties,
        # set this to True.  Defaults to False.
        'record_view_arguments': True,
        
        # (optional) Exceptions are logged by default, to disable, set this to False.
        'log_exceptions': False,
        
        # (optional) Events are submitted to Application Insights asynchronously.
        # send_interval specifies how often the queue is checked for items to submit.
        # send_time specifies how long the sender waits for new input before recycling
        # the background thread.
        'send_interval': 1.0, # Check every second
        'send_time': 3.0, # Wait up to 3 seconds for an event
        
        # (optional, uncommon) If you must send to an endpoint other than the
        # default endpoint, specify it here:
        'endpoint': "https://dc.services.visualstudio.com/v2/track",
    }

This will log all requests and exceptions to the instrumentation key
specified in the `APPLICATION_INSIGHTS` setting.  In addition, an
`appinsights` property will be placed on each incoming `request` object in
your views.  This will have the following properties:

* `client`: This is an instance of the `applicationinsights.TelemetryClient`
  type, which will submit telemetry to the same instrumentation key, and
  will parent each telemetry item to the current request.
* `request`: This is the `applicationinsights.channel.contracts.RequestData`
  instance for the current request.  You can modify properties on this
  object during the handling of the current request.  It will be submitted
  when the request has finished.
* `context`: This is the `applicationinsights.channel.TelemetryContext`
  object for the current ApplicationInsights sender.

You can also hook up logging to Django.  For example, to log all builtin
Django warnings and errors, use the following logging configuration in
`settings.py`:

.. code:: python

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'handlers': {
            # The application insights handler is here
            'appinsights': {
                'class': 'applicationinsights.django.LoggingHandler',
                'level': 'WARNING'
            }
        },
        'loggers': {
            'django': {
                'handlers': ['appinsights'],
                'level': 'WARNING',
                'propagate': True,
            }
        }
    }

See Django's `logging documentation <https://docs.djangoproject.com/en/1.11/topics/logging/>`_
for more information.

**Integrating with other web frameworks**

For any other Python web framework that is `WSGI compliant <https://www.python.org/dev/peps/pep-0333/>`_,
the `WSGIApplication <https://github.com/Microsoft/ApplicationInsights-Python/blob/master/applicationinsights/requests/WSGIApplication.py>`_
can be used as a middleware to log requests to Application Insights.

Add common properties to WSGIApplication request events by passing in a dictionary to the WSGIApplication constructor:

.. code:: python

    from wsgiref.simple_server import make_server
    from pyramid.config import Configurator
    from pyramid.response import Response
    from applicationinsights.requests import WSGIApplication

    # define a simple pyramid route
    def hello_world(request):
        return Response('Hello World!')

    # construct dictionary which contains properties to be included with every request event
    common_properties = {
        "service": "hello_world_flask_app",
        "environment": "production"
    }

    if __name__ == '__main__':
        # create a simple pyramid app
        with Configurator() as config:
            config.add_route('hello', '/')
            config.add_view(hello_world, route_name='hello')
            app = config.make_wsgi_app()

            # wrap the app in the application insights request logging middleware
            app = WSGIApplication('<YOUR INSTRUMENTATION KEY GOES HERE>', app, common_properties=common_properties)

        # run the app
        server = make_server('0.0.0.0', 6543, app)
        server.serve_forever()