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

Add common properties argument to WSGIApplication wrapper #88

Merged
merged 3 commits into from
May 31, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Changelog

## 0.11.5
- Add common properties argument to WSGIApplication initialization.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't shipped 11.4 yet. So it should be reported under previous title. I'll merge and fix this


## 0.11.4
- Schemas for all data types and context objects updated to the latest version.


## 0.11.3

Changelog started from this version.
- Changelog started from this version.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,32 @@ For any other Python web framework that is [WSGI compliant](https://www.python.o
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:
```
from flask import Flask
from applicationinsights.requests import WSGIApplication

# instantiate the Flask application and wrap its WSGI application
app = Flask(__name__)

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

app.wsgi_app = WSGIApplication('<YOUR INSTRUMENTATION KEY GOES HERE>', app.wsgi_app, common_properties=common_properties)

# define a simple route
@app.route('/')
def hello_world():
return 'Hello World!'

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


## Publishing new version to pypi.python.org

Expand Down
5 changes: 3 additions & 2 deletions applicationinsights/requests/WSGIApplication.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def hello(request):
app = config.make_wsgi_app()

# Enable Application Insights middleware
app = WSGIApplication('<YOUR INSTRUMENTATION KEY GOES HERE>', app)
app = WSGIApplication('<YOUR INSTRUMENTATION KEY GOES HERE>', app, common_properties={'service': 'hello_world_service'})

serve(app, host='0.0.0.0')
"""
Expand All @@ -49,6 +49,7 @@ def __init__(self, instrumentation_key, wsgi_application, *args, **kwargs):
self.client = applicationinsights.TelemetryClient(instrumentation_key, telemetry_channel)
self.client.context.device.type = "PC"
self._wsgi_application = wsgi_application
self._common_properties = kwargs.pop('common_properties', {})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SergeyKanzhelev How would you like the 'common_properties' parameter to be documented in the init documentation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it will be great if you can add a note into README.md. I see that DESCRIPTION.rst is out of sync with readme. @OsvaldoRosado @jjjordanmsft do you know the history here? From looks of it I'd say readme should just point to DESCRIPTION.rst instead of duplicating the context


def flush(self):
"""Flushes the queued up telemetry to the service.
Expand Down Expand Up @@ -104,4 +105,4 @@ def status_interceptor(status_string, headers_array, exc_info=None):
end_time = datetime.datetime.utcnow()
duration = int((end_time - start_time).total_seconds() * 1000)

self.client.track_request(name, url, success, start_time.isoformat() + 'Z', duration, response_code, http_method)
self.client.track_request(name, url, success, start_time.isoformat() + 'Z', duration, response_code, http_method, self._common_properties)