def call(self, module, method, wrapped, instance, args, kwargs): if 'operation_name' in kwargs: operation_name = kwargs['operation_name'] else: operation_name = args[0] target_endpoint = instance._endpoint.host parsed_url = urlparse.urlparse(target_endpoint) service, region, _ = parsed_url.hostname.split('.', 2) signature = '{}:{}'.format(service, operation_name) extra_data = { 'service': service, 'region': region, 'operation': operation_name, } with capture_span(signature, 'ext.http.aws', extra_data, leaf=True): return wrapped(*args, **kwargs)
def test_urllib_standard_port(mock_getresponse, mock_request, instrument, elasticapm_client): # "code" is needed for Python 3, "status" for Python 2 mock_getresponse.return_value = mock.Mock(code=200, status=200) url = "http://example.com/" parsed_url = urlparse.urlparse(url) elasticapm_client.begin_transaction("transaction") expected_sig = "GET {0}".format(parsed_url.netloc) r = urlopen(url) elasticapm_client.end_transaction("MyView") transactions = elasticapm_client.events[TRANSACTION] spans = elasticapm_client.spans_for_transaction(transactions[0]) assert spans[0]["name"] == expected_sig assert spans[0]["type"] == "external" assert spans[0]["subtype"] == "http" assert spans[0]["context"]["http"]["url"] == url assert spans[0]["parent_id"] == transactions[0]["id"]
def call(self, module, method, wrapped, instance, args, kwargs): if "operation_name" in kwargs: operation_name = kwargs["operation_name"] else: operation_name = args[0] target_endpoint = instance._endpoint.host parsed_url = urlparse.urlparse(target_endpoint) if "." in parsed_url.hostname: service = parsed_url.hostname.split(".", 2)[0] else: service = parsed_url.hostname signature = "{}:{}".format(service, operation_name) with capture_span(signature, "aws", leaf=True, span_subtype=service, span_action=operation_name): return wrapped(*args, **kwargs)
def test_instance_headers_are_respected(instrument, elasticapm_client, waiting_httpserver, is_sampled, instance_headers, header_arg, header_kwarg): traceparent = TraceParent.from_string( "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-03", "foo=bar,baz=bazzinga") waiting_httpserver.serve_content("") url = waiting_httpserver.url + "/hello_world" parsed_url = urlparse.urlparse(url) transaction_object = elasticapm_client.begin_transaction( "transaction", trace_parent=traceparent) transaction_object.is_sampled = is_sampled pool = urllib3.HTTPConnectionPool( parsed_url.hostname, parsed_url.port, maxsize=1, block=True, headers={"instance": "true"} if instance_headers else None, ) if header_arg: args = ("GET", url, None, {"args": "true"}) else: args = ("GET", url) if header_kwarg: kwargs = {"headers": {"kwargs": "true"}} else: kwargs = {} r = pool.urlopen(*args, **kwargs) request_headers = waiting_httpserver.requests[0].headers # all combinations should have the "traceparent" header assert "traceparent" in request_headers, (instance_headers, header_arg, header_kwarg) if header_arg: assert "args" in request_headers if header_kwarg: assert "kwargs" in request_headers if instance_headers and not (header_arg or header_kwarg): assert "instance" in request_headers
def test_urllib3(instrument, elasticapm_client, waiting_httpserver): waiting_httpserver.serve_content("") url = waiting_httpserver.url + "/hello_world" parsed_url = urlparse.urlparse(url) elasticapm_client.begin_transaction("transaction") expected_sig = "GET {0}".format(parsed_url.netloc) with capture_span("test_name", "test_type"): pool = urllib3.PoolManager(timeout=0.1) url = "http://{0}/hello_world".format(parsed_url.netloc) r = pool.request("GET", url) elasticapm_client.end_transaction("MyView") transactions = elasticapm_client.events[TRANSACTION] spans = elasticapm_client.spans_for_transaction(transactions[0]) expected_signatures = {"test_name", expected_sig} assert {t["name"] for t in spans} == expected_signatures assert len(spans) == 2 assert spans[0]["name"] == expected_sig assert spans[0]["type"] == "external" assert spans[0]["subtype"] == "http" assert spans[0]["context"]["http"]["url"] == url assert spans[0]["context"]["http"]["status_code"] == 200 assert spans[0]["context"]["destination"]["service"] == { "name": "", "resource": "127.0.0.1:%d" % parsed_url.port, "type": "", } assert spans[0]["parent_id"] == spans[1]["id"] assert spans[0]["outcome"] == "success" assert spans[1]["name"] == "test_name" assert spans[1]["type"] == "test_type" assert spans[1]["parent_id"] == transactions[0]["id"]
def call(self, module, method, wrapped, instance, args, kwargs): if "operation_name" in kwargs: operation_name = kwargs["operation_name"] else: operation_name = args[0] target_endpoint = instance._endpoint.host parsed_url = urlparse.urlparse(target_endpoint) if "." in parsed_url.hostname: service, region = parsed_url.hostname.split(".", 2)[:2] else: service, region = parsed_url.hostname, None signature = "{}:{}".format(service, operation_name) extra_data = { "service": service, "region": region, "operation": operation_name } with capture_span(signature, "ext.http.aws", extra_data, leaf=True): return wrapped(*args, **kwargs)
def call(self, module, method, wrapped, instance, args, kwargs): if "operation_name" in kwargs: operation_name = kwargs["operation_name"] else: operation_name = args[0] service_model = instance.meta.service_model if hasattr(service_model, "service_id"): # added in boto3 1.7 service = service_model.service_id else: service = service_model.service_name.upper() service = endpoint_to_service_id.get(service, service) parsed_url = urlparse.urlparse(instance.meta.endpoint_url) context = { "destination": { "address": parsed_url.hostname, "port": parsed_url.port, "cloud": {"region": instance.meta.region_name}, } } handler_info = None handler = handlers.get(service, False) if handler: handler_info = handler(operation_name, service, instance, args, kwargs, context) if not handler_info: handler_info = handle_default(operation_name, service, instance, args, kwargs, context) with capture_span( handler_info.signature, span_type=handler_info.span_type, leaf=True, span_subtype=handler_info.span_subtype, span_action=handler_info.span_action, extra=handler_info.context, ): return wrapped(*args, **kwargs)
def handle_check(self, command, **options): # noqa: C901 """Check your settings for common misconfigurations""" passed = True client = DjangoClient(metrics_interval="0ms") def is_set(x): return x and x != "None" # check if org/app is set: if is_set(client.config.service_name): self.write("Service name is set, good job!", green) else: passed = False self.write("Configuration errors detected!", red, ending="\n\n") self.write(" * SERVICE_NAME not set! ", red, ending="\n") self.write(CONFIG_EXAMPLE) # secret token is optional but recommended if not is_set(client.config.secret_token): self.write(" * optional SECRET_TOKEN not set", yellow, ending="\n") self.write("") server_url = client.config.server_url if server_url: parsed_url = urlparse.urlparse(server_url) if parsed_url.scheme.lower() in ("http", "https"): # parse netloc, making sure people did not supply basic auth if "@" in parsed_url.netloc: credentials, _, path = parsed_url.netloc.rpartition("@") passed = False self.write("Configuration errors detected!", red, ending="\n\n") if ":" in credentials: self.write(" * SERVER_URL cannot contain authentication " "credentials", red, ending="\n") else: self.write( " * SERVER_URL contains an unexpected at-sign!" " This is usually used for basic authentication, " "but the colon is left out", red, ending="\n", ) else: self.write("SERVER_URL {0} looks fine".format(server_url), green) # secret token in the clear not recommended if is_set(client.config.secret_token) and parsed_url.scheme.lower() == "http": self.write(" * SECRET_TOKEN set but server not using https", yellow, ending="\n") else: self.write( " * SERVER_URL has scheme {0} and we require " "http or https!".format(parsed_url.scheme), red, ending="\n", ) passed = False else: self.write("Configuration errors detected!", red, ending="\n\n") self.write(" * SERVER_URL appears to be empty", red, ending="\n") passed = False self.write("") # check if we're disabled due to DEBUG: if settings.DEBUG: if getattr(settings, "ELASTIC_APM", {}).get("DEBUG"): self.write( "Note: even though you are running in DEBUG mode, we will " 'send data to the APM Server, because you set ELASTIC_APM["DEBUG"] to ' "True. You can disable ElasticAPM while in DEBUG mode like this" "\n\n", yellow, ) self.write( " ELASTIC_APM = {\n" ' "DEBUG": False,\n' " # your other ELASTIC_APM settings\n" " }" ) else: self.write( "Looks like you're running in DEBUG mode. ElasticAPM will NOT " "gather any data while DEBUG is set to True.\n\n", red, ) self.write( "If you want to test ElasticAPM while DEBUG is set to True, you" " can force ElasticAPM to gather data by setting" ' ELASTIC_APM["DEBUG"] to True, like this\n\n' " ELASTIC_APM = {\n" ' "DEBUG": True,\n' " # your other ELASTIC_APM settings\n" " }" ) passed = False else: self.write("DEBUG mode is disabled! Looking good!", green) self.write("") # check if middleware is set, and if it is at the first position middleware_attr = "MIDDLEWARE" if getattr(settings, "MIDDLEWARE", None) is not None else "MIDDLEWARE_CLASSES" middleware = list(getattr(settings, middleware_attr)) try: pos = middleware.index("mcod.apm.middleware.TracingMiddleware") if pos == 0: self.write("Tracing middleware is configured! Awesome!", green) else: self.write("Tracing middleware is configured, but not at the first position\n", yellow) self.write("ElasticAPM works best if you add it at the top of your %s setting" % middleware_attr) except ValueError: self.write("Tracing middleware not configured!", red) self.write( "\n" "Add it to your %(name)s setting like this:\n\n" " %(name)s = (\n" ' "mcod.apm.middleware.TracingMiddleware",\n' " # your other middleware classes\n" " )\n" % {"name": middleware_attr} ) self.write("") if passed: self.write("Looks like everything should be ready!", green) else: self.write("Please fix the above errors.", red) self.write("") client.close() return passed
os.environ["AWS_ACCESS_KEY_ID"] = "key" os.environ["AWS_SECRET_ACCESS_KEY"] = "secret" os.environ["AWS_DEFAULT_REGION"] = "us-east-1" LOCALSTACK_ENDPOINT = os.environ.get("AWS_URL", None) from boto3.dynamodb.types import TypeSerializer dynamodb_serializer = TypeSerializer() if not LOCALSTACK_ENDPOINT: pytestmark.append( pytest.mark.skip( "Skipping botocore tests, no AWS_URL environment variable set")) LOCALSTACK_ENDPOINT_URL = urlparse.urlparse(LOCALSTACK_ENDPOINT) @pytest.fixture() def dynamodb(): db = boto3.client("dynamodb", endpoint_url=LOCALSTACK_ENDPOINT) db.create_table( TableName="Movies", KeySchema=[ { "AttributeName": "year", "KeyType": "HASH" }, # Partition key { "AttributeName": "title", "KeyType": "RANGE"
def test_timeout(self, mock_urlopen): transport = HTTPTransport(urlparse.urlparse('http://localhost:9999')) mock_urlopen.side_effect = socket.timeout() with pytest.raises(TransportException) as exc_info: transport.send('x', {}) assert 'timeout' in str(exc_info.value)
def handle_check(self, command, **options): """Check your settings for common misconfigurations""" passed = True client = DjangoClient() # check if org/app and token are set: is_set = lambda x: x and x != 'None' values = [client.config.service_name, client.config.secret_token] if all(map(is_set, values)): self.write('Service name and secret token are set, good job!', green) else: passed = False self.write('Configuration errors detected!', red, ending='\n\n') if not is_set(client.config.service_name): self.write(" * SERVICE_NAME not set! ", red, ending='\n') if not is_set(client.config.secret_token): self.write(" * SECRET_TOKEN not set!", red, ending='\n') self.write(CONFIG_EXAMPLE) self.write('') server_url = client.config.server_url if server_url: parsed_url = urlparse.urlparse(server_url) if parsed_url.scheme.lower() in ( 'http', 'https', ): # parse netloc, making sure people did not supply basic auth if '@' in parsed_url.netloc: credentials, _, path = parsed_url.netloc.rpartition('@') passed = False self.write('Configuration errors detected!', red, ending='\n\n') if ':' in credentials: self.write( ' * SERVER_URL cannot contain authentication ' 'credentials', red, ending='\n') else: self.write( ' * SERVER_URL contains an unexpected at-sign!' ' This is usually used for basic authentication, ' 'but the colon is left out', red, ending='\n') else: self.write( 'SERVER_URL {0} looks fine'.format(server_url, ), green) else: self.write(' * SERVER_URL has scheme {0} and we require ' 'http or https!'.format(parsed_url.scheme, ), red, ending='\n') passed = False else: self.write('Configuration errors detected!', red, ending='\n\n') self.write(" * SERVER_URL appears to be empty", red, ending='\n') passed = False self.write('') # check if we're disabled due to DEBUG: if settings.DEBUG: if getattr(settings, 'ELASTIC_APM', {}).get('DEBUG'): self.write( 'Note: even though you are running in DEBUG mode, we will ' 'send data to the APM Server, because you set ELASTIC_APM["DEBUG"] to ' 'True. You can disable ElasticAPM while in DEBUG mode like this' '\n\n', yellow) self.write(' ELASTIC_APM = {\n' ' "DEBUG": False,\n' ' # your other ELASTIC_APM settings\n' ' }') else: self.write( 'Looks like you\'re running in DEBUG mode. ElasticAPM will NOT ' 'gather any data while DEBUG is set to True.\n\n', red, ) self.write( 'If you want to test ElasticAPM while DEBUG is set to True, you' ' can force ElasticAPM to gather data by setting' ' ELASTIC_APM["DEBUG"] to True, like this\n\n' ' ELASTIC_APM = {\n' ' "DEBUG": True,\n' ' # your other ELASTIC_APM settings\n' ' }') passed = False else: self.write('DEBUG mode is disabled! Looking good!', green) self.write('') # check if middleware is set, and if it is at the first position middleware_attr = 'MIDDLEWARE' if getattr( settings, 'MIDDLEWARE', None) is not None else 'MIDDLEWARE_CLASSES' middleware = list(getattr(settings, middleware_attr)) try: pos = middleware.index( 'elasticapm.contrib.django.middleware.TracingMiddleware') if pos == 0: self.write('Tracing middleware is configured! Awesome!', green) else: self.write( 'Tracing middleware is configured, but not at the first position\n', yellow) self.write( 'ElasticAPM works best if you add it at the top of your %s setting' % middleware_attr) except ValueError: self.write('Tracing middleware not configured!', red) self.write( '\n' 'Add it to your %(name)s setting like this:\n\n' ' %(name)s = (\n' ' "elasticapm.contrib.django.middleware.TracingMiddleware",\n' ' # your other middleware classes\n' ' )\n' % {'name': middleware_attr}) self.write('') if passed: self.write('Looks like everything should be ready!', green) else: self.write('Please fix the above errors.', red) self.write('') return passed