def fully_featured_app(environ, start_response): status = '200 OK' path = environ.get('PATH_INFO') use_user_attrs = environ.get('record_attributes', 'TRUE') == 'TRUE' environ['wsgi.input'].read() environ['wsgi.input'].readline() environ['wsgi.input'].readlines() if use_user_attrs: for attr, val in _custom_parameters.items(): add_custom_parameter(attr, val) if 'db' in environ and int(environ['db']) > 0: connection = db.connect(":memory:") for i in range(int(environ['db']) - 1): connection.execute("create table test_db%d (a, b, c)" % i) if 'external' in environ: for i in range(int(environ['external'])): r = urlopen('http://www.python.org') r.read(10) if 'err_message' in environ: n_errors = int(environ.get('n_errors', 1)) for i in range(n_errors): try: # append number to stats engine to get unique errors, so they # don't immediately get filtered out. raise ValueError(environ['err_message'] + str(i)) except ValueError: if use_user_attrs: record_exception(params=_err_param) else: record_exception() text = '<html><head>%s</head><body><p>RESPONSE</p>%s</body></html>' output = (text % (get_browser_timing_header(), get_browser_timing_footer())).encode('UTF-8') response_headers = [('Content-type', 'text/html; charset=utf-8'), ('Content-Length', str(len(output)))] write = start_response(status, response_headers) write(b'') return [output]
def target_wsgi_application(environ, start_response): status = '200 OK' output = b'Hello World!' path = environ.get('PATH_INFO') if path == '/user_attribute': add_custom_parameter('test_key', 'test_value') response_headers = [('Content-Type', 'text/plain; charset=utf-8'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output]
def target_wsgi_application_param_on_close(environ, start_response): output = b'<html><body><p>RESPONSE</p></body></html>' response_headers = [('Content-Type', 'text/html; charset=utf-8'), ('Content-Length', str(len(output)))] start_response('200 OK', response_headers) try: yield output finally: add_custom_parameter('key', 'value')
async def target_asgi_application_param(scope, receive, send): output = b'<html><body><p>RESPONSE</p></body></html>' response_headers = [(b'content-type', b'text/html; charset=utf-8'), (b'content-length', str(len(output)).encode("utf-8"))] add_custom_parameter('key', 'value') await send({ "type": "http.response.start", "status": 200, "headers": response_headers }) await send({"type": "http.response.body", "body": output})
def test_custom_param_too_many(): for i in range(129): result = add_custom_parameter('key-%02d' % i, 'value') if i < 128: assert result else: assert not result # Last one fails
def normal_wsgi_application(environ, start_response): status = "200 OK" output = "<html><head>header</head><body><p>RESPONSE</p></body></html>" output = output.encode("UTF-8") add_custom_parameter(USER_ATTRS[0], "test_value") add_custom_parameter(USER_ATTRS[1], "test_value") response_headers = [("Content-Type", "text/html; charset=utf-8"), ("Content-Length", str(len(output)))] start_response(status, response_headers) try: raise ValueError("Transaction had bad value") except ValueError: notice_error(attributes={ERROR_PARAMS[0]: "param-value"}) return [output]
def normal_wsgi_application(environ, start_response): status = '200 OK' output = '<html><head>header</head><body><p>RESPONSE</p></body></html>' output = output.encode('UTF-8') add_custom_parameter(USER_ATTRS[0], 'test_value') add_custom_parameter(USER_ATTRS[1], 'test_value') response_headers = [('Content-Type', 'text/html; charset=utf-8'), ('Content-Length', str(len(output)))] start_response(status, response_headers) try: raise ValueError('Transaction had bad value') except ValueError: record_exception(params={ERROR_PARAMS[0]: 'param-value'}) return [output]
async def normal_asgi_application(scope, receive, send): output = b"<html><head>header</head><body><p>RESPONSE</p></body></html>" add_custom_parameter("puppies", "test_value") add_custom_parameter("sunshine", "test_value") response_headers = [ (b"content-type", b"text/html; charset=utf-8"), (b"content-length", str(len(output)).encode("utf-8")), ] try: raise ValueError("Transaction had bad value") except ValueError: record_exception(params={"ohnoes": "param-value"}) await send( {"type": "http.response.start", "status": 200, "headers": response_headers} ) await send({"type": "http.response.body", "body": output})
def target_wsgi_application(environ, start_response): status = '200 OK' txn_name = environ.get('txn_name') set_transaction_name(txn_name, group='') user_attrs = json.loads(environ.get('user_attrs')) for key, value in user_attrs.items(): add_custom_parameter(key, value) text = '<html><head>%s</head><body><p>RESPONSE</p></body></html>' output = (text % get_browser_timing_footer()).encode('UTF-8') response_headers = [('Content-Type', 'text/html; charset=utf-8'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output]
def test_custom_param_value_too_long(): result = add_custom_parameter('key', TOO_LONG) assert result
def test_other_transaction_error_parameters_hsm_enabled(): add_custom_parameter("key-1", "value-1") try: raise TestException("test message") except Exception: notice_error(attributes={"key-2": "value-2"})
def test_other_transaction_custom_parameters_hsm_enabled(): add_custom_parameter("key", "value")
def add_custom_parameter(key, value): transaction = current_transaction() if transaction: transaction.add_custom_parameter(key, value)
def test_other_transaction_error_parameters_hsm_disabled(): add_custom_parameter('key-1', 'value-1') try: raise TestException('test message') except Exception: notice_error(attributes={'key-2': 'value-2'})
def _function3(): add_custom_parameter('txn-key-1', 1) _function4() raise RuntimeError('This is a test error and can be ignored.')
def test_other_transaction_custom_parameters_hsm_enabled(): add_custom_parameter('key', 'value')
def test_custom_param_int_too_big(): result = add_custom_parameter('key', TOO_BIG) assert not result
def test_custom_param_name_not_string(): result = add_custom_parameter(1, 'value') assert not result
def test_other_transaction_error_parameters_hsm_enabled(): add_custom_parameter('key-1', 'value-1') try: raise TestException('test message') except Exception: record_exception(params={'key-2': 'value-2'})
def test_custom_param_key_too_long(): result = add_custom_parameter(TOO_LONG, 'value') assert not result
def test_custom_param_ok(): result = add_custom_parameter('key', 'value') assert result