Skip to content
This repository has been archived by the owner on Jun 11, 2018. It is now read-only.

Commit

Permalink
Merge pull request #123 from opbeat/feature/platform-info
Browse files Browse the repository at this point in the history
add some information about the python/django/flask version to the request header
  • Loading branch information
beniwohli authored Aug 24, 2016
2 parents 5243bd3 + 7428486 commit dff49a4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
18 changes: 16 additions & 2 deletions opbeat/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import datetime
import logging
import os
import platform
import sys
import time
import uuid
Expand Down Expand Up @@ -119,7 +120,7 @@ def __init__(self, organization_id=None, app_id=None, secret_token=None,
string_max_length=None, list_max_length=None, processors=None,
filter_exception_types=None, servers=None, api_path=None,
async=None, async_mode=None, traces_send_freq_secs=None,
transactions_ignore_patterns=None,
transactions_ignore_patterns=None, framework_version='',
**kwargs):
# configure loggers first
cls = self.__class__
Expand Down Expand Up @@ -203,6 +204,8 @@ def __init__(self, organization_id=None, app_id=None, secret_token=None,
else:
self.processors = processors

self._framework_version = framework_version

self.module_cache = ModuleProxyCache()

self.instrumentation_store = RequestsStore(
Expand Down Expand Up @@ -498,7 +501,8 @@ def send_encoded(self, message, secret_token, auth_header=None,
headers = {
'Authorization': auth_header,
'Content-Type': 'application/octet-stream',
'User-Agent': 'opbeat-python/%s' % opbeat.VERSION
'User-Agent': 'opbeat-python/%s' % opbeat.VERSION,
'X-Opbeat-Platform': self.get_platform_info()
}

self.send_remote(url=url, data=message, headers=headers)
Expand Down Expand Up @@ -632,6 +636,16 @@ def _traces_collect(self):
data['servers'] = [server + api_path for server in self.servers]
self.send(**data)

def get_platform_info(self):
platform_bits = {'lang': 'python/' + platform.python_version()}
implementation = platform.python_implementation()
if implementation == 'PyPy':
implementation += '/' + '.'.join(map(str, sys.pypy_version_info[:3]))
platform_bits['platform'] = implementation
if self._framework_version:
platform_bits['framework'] = self._framework_version
return ' '.join('%s=%s' % item for item in platform_bits.items())


class DummyClient(Client):
"""Sends messages into an empty void"""
Expand Down
2 changes: 2 additions & 0 deletions opbeat/contrib/django/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import logging

import django
from django.db import DatabaseError
from django.http import HttpRequest
from django.template import TemplateSyntaxError
Expand Down Expand Up @@ -58,6 +59,7 @@ def __init__(self, **kwargs):
self.instrument_django_middleware = instrument_django_middleware
else:
self.instrument_django_middleware = defaults.INSTRUMENT_DJANGO_MIDDLEWARE
kwargs['framework_version'] = 'django/' + django.get_version()
super(DjangoClient, self).__init__(**kwargs)

def get_user_info(self, request):
Expand Down
8 changes: 7 additions & 1 deletion opbeat/contrib/flask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import os
import warnings

import flask
from flask import request, signals

import opbeat.instrumentation.control
Expand Down Expand Up @@ -82,6 +83,10 @@ def make_client(client_cls, app, organization_id=None, app_id=None, secret_token
os.environ.get('OPBEAT_SECRET_TOKEN') or # environment
os.environ.get('SECRET_TOKEN') # deprecated fallback
)
if hasattr(flask, '__version__'):
framework_version = 'flask/' + flask.__version__
else:
framework_version = 'flask/<0.7'

return client_cls(
organization_id=organization_id,
Expand All @@ -100,7 +105,8 @@ def make_client(client_cls, app, organization_id=None, app_id=None, secret_token
traces_freq_send=opbeat_config.get('TRACES_FREQ_SEND'),
processors=opbeat_config.get('PROCESSORS'),
async_mode=opbeat_config.get('ASYNC_MODE'),
transactions_ignore_patterns=opbeat_config.get('TRANSACTIONS_IGNORE_PATTERNS')
transactions_ignore_patterns=opbeat_config.get('TRANSACTIONS_IGNORE_PATTERNS'),
framework_version=framework_version,
)


Expand Down
19 changes: 16 additions & 3 deletions tests/client/client_tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import platform
import time

import mock
Expand Down Expand Up @@ -61,6 +62,17 @@ class ClientTest(TestCase):
def setUp(self):
self.client = get_tempstoreclient()

def test_platform_info(self):
platform_info = self.client.get_platform_info()
self.assertIn(
'lang=python/' + platform.python_version(),
platform_info,
)
self.assertIn(
'platform=' + platform.python_implementation(),
platform_info,
)

def test_config_by_environment(self):
with mock.patch.dict('os.environ', {
'OPBEAT_ORGANIZATION_ID': 'org',
Expand Down Expand Up @@ -209,7 +221,8 @@ def test_send(self, time, send_remote):
headers={
'Content-Type': 'application/octet-stream',
'Authorization': 'Bearer %s' % (access_token),
'User-Agent': 'opbeat-python/%s' % opbeat.VERSION
'User-Agent': 'opbeat-python/%s' % opbeat.VERSION,
'X-Opbeat-Platform': self.client.get_platform_info(),
},
)

Expand Down Expand Up @@ -249,8 +262,8 @@ def test_send_with_auth_header(self, time, send_remote):
headers={
'Content-Type': 'application/octet-stream',
'Authorization': 'foo',
'User-Agent': 'opbeat-python/%s' % opbeat.VERSION

'User-Agent': 'opbeat-python/%s' % opbeat.VERSION,
'X-Opbeat-Platform': self.client.get_platform_info(),
},
)

Expand Down
5 changes: 5 additions & 0 deletions tests/contrib/django/django_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,11 @@ def test_request_metrics_404_resolve_error(self):
''
)

def test_get_platform_info(self):
client = get_client()
platform_info = client.get_platform_info()
self.assertIn(django.get_version(), platform_info)


class DjangoClientNoTempTest(TestCase):
def setUp(self):
Expand Down
4 changes: 4 additions & 0 deletions tests/contrib/flask/flask_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,7 @@ def test_instrumentation_404(self):
assert traces[0]['signature'] == 'transaction'
assert traces[0]['transaction'] == expected_transaction
assert traces[0]['kind'] == 'transaction'

def test_framework_version(self):
opbeat = Opbeat(app=self.app)
self.assertIn('framework=flask', opbeat.client.get_platform_info())

0 comments on commit dff49a4

Please sign in to comment.