def test_http_put_body_masked(): try: ConfigProvider.set( config_names.THUNDRA_TRACE_INTEGRATIONS_HTTP_BODY_MASK, 'true') url = 'https://jsonplaceholder.typicode.com/users/3' parsed_url = urlparse(url) path = parsed_url.path normalized_path = "/users" query = parsed_url.query host = parsed_url.netloc requests.put(url, data={"message": "test"}) tracer = ThundraTracer.get_instance() http_span = tracer.get_spans()[1] assert http_span.operation_name == host + normalized_path assert http_span.domain_name == constants.DomainNames['API'] assert http_span.class_name == constants.ClassNames['HTTP'] assert http_span.get_tag(constants.SpanTags['OPERATION_TYPE']) == 'PUT' assert http_span.get_tag(constants.HttpTags['HTTP_METHOD']) == 'PUT' assert http_span.get_tag(constants.HttpTags['HTTP_URL']) == host + path assert http_span.get_tag(constants.HttpTags['HTTP_HOST']) == host assert http_span.get_tag(constants.HttpTags['HTTP_PATH']) == path assert http_span.get_tag(constants.HttpTags['QUERY_PARAMS']) == query assert http_span.get_tag(constants.HttpTags['BODY']) is None except Exception: raise finally: tracer.clear()
def test_http_4xx_error_with_min_status_500(mock_actual_call, monkeypatch): ConfigProvider.set( config_names.THUNDRA_TRACE_INTEGRATIONS_HTTP_ERROR_STATUS_CODE_MIN, '500') mock_actual_call.return_value = requests.Response() mock_actual_call.return_value.status_code = 404 mock_actual_call.return_value.reason = "Not Found" url = 'http://adummyurlthatnotexists.xyz/' parsed_url = urlparse(url) path = parsed_url.path query = parsed_url.query host = parsed_url.netloc requests.get(url) tracer = ThundraTracer.get_instance() http_span = tracer.get_spans()[1] assert http_span.operation_name == host + path assert http_span.domain_name == constants.DomainNames['API'] assert http_span.class_name == constants.ClassNames['HTTP'] assert http_span.get_tag(constants.SpanTags['OPERATION_TYPE']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_METHOD']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_URL']) == host + path assert http_span.get_tag(constants.HttpTags['HTTP_HOST']) == host assert http_span.get_tag(constants.HttpTags['HTTP_PATH']) == path assert http_span.get_tag(constants.HttpTags['QUERY_PARAMS']) == query assert http_span.get_tag('error') is None assert http_span.get_tag('error.kind') is None assert http_span.get_tag('error.message') is None
def test_http_5xx_error(mock_actual_call): mock_actual_call.return_value = requests.Response() mock_actual_call.return_value.status_code = 500 mock_actual_call.return_value.reason = "Internal Server Error" url = 'http://adummyurlthatnotexists.xyz/' parsed_url = urlparse(url) path = parsed_url.path query = parsed_url.query host = parsed_url.netloc requests.get(url) tracer = ThundraTracer.get_instance() http_span = tracer.get_spans()[1] assert http_span.operation_name == host + path assert http_span.domain_name == constants.DomainNames['API'] assert http_span.class_name == constants.ClassNames['HTTP'] assert http_span.get_tag(constants.SpanTags['OPERATION_TYPE']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_METHOD']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_URL']) == host + path assert http_span.get_tag(constants.HttpTags['HTTP_HOST']) == host assert http_span.get_tag(constants.HttpTags['HTTP_PATH']) == path assert http_span.get_tag(constants.HttpTags['QUERY_PARAMS']) == query assert http_span.get_tag('error') is True assert http_span.get_tag('error.kind') == 'HttpError' assert http_span.get_tag('error.message') == 'Internal Server Error'
def test_apigw_call_v2(mock_actual_call): mock_actual_call.return_value = requests.Response() mock_actual_call.return_value.headers = { "apigw-requestid": "test_id", "x-thundra-resource-name": "test" } try: url = 'https://1a23bcdefg.execute-api.us-west-2.amazonaws.com/dev/test' parsed_url = urlparse(url) path = parsed_url.path query = parsed_url.query host = parsed_url.netloc requests.get(url) tracer = ThundraTracer.get_instance() http_span = tracer.get_spans()[1] assert http_span.operation_name == "test" assert http_span.domain_name == constants.DomainNames['API'] assert http_span.class_name == constants.ClassNames['APIGATEWAY'] assert http_span.get_tag(constants.SpanTags['OPERATION_TYPE']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_METHOD']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_URL']) == host + path assert http_span.get_tag(constants.HttpTags['HTTP_HOST']) == host assert http_span.get_tag(constants.HttpTags['HTTP_PATH']) == path assert http_span.get_tag(constants.HttpTags['QUERY_PARAMS']) == query except Exception: raise
def test_http_path_depth(): ConfigProvider.set(config_names.THUNDRA_TRACE_INTEGRATIONS_HTTP_URL_DEPTH, '2') try: url = 'https://jsonplaceholder.typicode.com/asd/qwe/xyz' parsed_url = urlparse(url) normalized_path = "/asd/qwe" path = parsed_url.path query = parsed_url.query host = parsed_url.netloc requests.get(url) tracer = ThundraTracer.get_instance() http_span = tracer.get_spans()[1] assert http_span.operation_name == host + normalized_path assert http_span.domain_name == constants.DomainNames['API'] assert http_span.class_name == constants.ClassNames['HTTP'] assert http_span.get_tag(constants.SpanTags['OPERATION_TYPE']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_METHOD']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_URL']) == host + path assert http_span.get_tag(constants.HttpTags['HTTP_HOST']) == host assert http_span.get_tag(constants.HttpTags['HTTP_PATH']) == path assert http_span.get_tag(constants.HttpTags['QUERY_PARAMS']) == query except Exception: raise
def test_erroneous_http_call(): try: url = 'http://adummyurlthatnotexists.xyz/' parsed_url = urlparse(url) path = parsed_url.path query = parsed_url.query host = parsed_url.netloc try: requests.get(url) except Exception: pass tracer = ThundraTracer.get_instance() http_span = tracer.get_spans()[1] assert http_span.operation_name == host + path assert http_span.domain_name == constants.DomainNames['API'] assert http_span.class_name == constants.ClassNames['HTTP'] assert http_span.get_tag(constants.SpanTags['OPERATION_TYPE']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_METHOD']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_URL']) == host + path assert http_span.get_tag(constants.HttpTags['HTTP_HOST']) == host assert http_span.get_tag(constants.HttpTags['HTTP_PATH']) == path assert http_span.get_tag(constants.HttpTags['QUERY_PARAMS']) == query assert http_span.get_tag('error') == True except Exception: raise finally: tracer.clear()
def test_successful_http_call_with_query_params(): try: url = "https://jsonplaceholder.typicode.com/users/1?test=test" parsed_url = urlparse(url) path = parsed_url.path normalized_path = "/users" query = parsed_url.query host = parsed_url.netloc requests.get(url) tracer = ThundraTracer.get_instance() http_span = tracer.get_spans()[1] assert http_span.operation_name == host + normalized_path assert http_span.domain_name == constants.DomainNames['API'] assert http_span.class_name == constants.ClassNames['HTTP'] assert http_span.get_tag(constants.SpanTags['OPERATION_TYPE']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_METHOD']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_URL']) == host + path assert http_span.get_tag(constants.HttpTags['HTTP_HOST']) == host assert http_span.get_tag(constants.HttpTags['HTTP_PATH']) == path assert http_span.get_tag(constants.HttpTags['QUERY_PARAMS']) == query except Exception: raise
def is_excluded_url(url): host = urlparse(url).netloc for method in EXCLUDE_EXCEPTION_URLS: for exclude_exception_url in EXCLUDE_EXCEPTION_URLS[method]: if method(host, exclude_exception_url): return False for method in EXCLUDED_URLS: for excluded_url in EXCLUDED_URLS[method]: if method(host, excluded_url): return True return False
def parse_http_url(url, url_path_depth): url_dict = {'path': '', 'query': '', 'host': '', 'url': url} try: parsed_url = urlparse(url) url_dict['path'] = parsed_url.path url_dict['query'] = parsed_url.query url_dict['host'] = parsed_url.netloc normalized_path = get_normalized_path(parsed_url.path, url_path_depth) url_dict['operation_name'] = parsed_url.hostname + normalized_path url_dict['url'] = parsed_url.hostname + parsed_url.path except Exception: pass return url_dict
def test_http_call_with_session(): try: url = 'https://httpbin.org/cookies/set/sessioncookie/123456789' parsed_url = urlparse(url) query = parsed_url.query host = parsed_url.netloc s = requests.Session() s.get(url) tracer = ThundraTracer.get_instance() http_span = tracer.get_spans()[1] assert http_span.domain_name == constants.DomainNames['API'] assert http_span.class_name == constants.ClassNames['HTTP'] assert http_span.get_tag(constants.SpanTags['OPERATION_TYPE']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_METHOD']) == 'GET' assert http_span.get_tag(constants.HttpTags['HTTP_HOST']) == host assert http_span.get_tag(constants.HttpTags['QUERY_PARAMS']) == query except Exception: raise