|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import json |
| 4 | +import wrapt |
| 5 | +import inspect |
| 6 | + |
| 7 | +from ..log import logger |
| 8 | +from ..singletons import tracer |
| 9 | + |
| 10 | + |
| 11 | +try: |
| 12 | + import boto3 |
| 13 | + from boto3.s3 import inject |
| 14 | + |
| 15 | + def lambda_inject_context(payload, scope): |
| 16 | + """ |
| 17 | + When boto3 lambda client 'Invoke' is called, we want to inject the tracing context. |
| 18 | + boto3/botocore has specific requirements: |
| 19 | + https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.invoke |
| 20 | + """ |
| 21 | + try: |
| 22 | + invoke_payload = payload.get('Payload', {}) |
| 23 | + |
| 24 | + if not isinstance(invoke_payload, dict): |
| 25 | + invoke_payload = json.loads(invoke_payload) |
| 26 | + |
| 27 | + tracer.inject(scope.span.context, 'http_headers', invoke_payload) |
| 28 | + payload['Payload'] = json.dumps(invoke_payload) |
| 29 | + except Exception: |
| 30 | + logger.debug("non-fatal lambda_inject_context: ", exc_info=True) |
| 31 | + |
| 32 | + |
| 33 | + @wrapt.patch_function_wrapper('botocore.client', 'BaseClient._make_api_call') |
| 34 | + def make_api_call_with_instana(wrapped, instance, arg_list, kwargs): |
| 35 | + # pylint: disable=protected-access |
| 36 | + parent_span = tracer.active_span |
| 37 | + |
| 38 | + # If we're not tracing, just return |
| 39 | + if parent_span is None: |
| 40 | + return wrapped(*arg_list, **kwargs) |
| 41 | + |
| 42 | + with tracer.start_active_span("boto3", child_of=parent_span) as scope: |
| 43 | + try: |
| 44 | + operation = arg_list[0] |
| 45 | + payload = arg_list[1] |
| 46 | + |
| 47 | + scope.span.set_tag('op', operation) |
| 48 | + scope.span.set_tag('ep', instance._endpoint.host) |
| 49 | + scope.span.set_tag('reg', instance._client_config.region_name) |
| 50 | + |
| 51 | + scope.span.set_tag('http.url', instance._endpoint.host + ':443/' + arg_list[0]) |
| 52 | + scope.span.set_tag('http.method', 'POST') |
| 53 | + |
| 54 | + # Don't collect payload for SecretsManager |
| 55 | + if not hasattr(instance, 'get_secret_value'): |
| 56 | + scope.span.set_tag('payload', payload) |
| 57 | + |
| 58 | + # Inject context when invoking lambdas |
| 59 | + if 'lambda' in instance._endpoint.host and operation == 'Invoke': |
| 60 | + lambda_inject_context(payload, scope) |
| 61 | + |
| 62 | + |
| 63 | + except Exception as exc: |
| 64 | + logger.debug("make_api_call_with_instana: collect error", exc_info=True) |
| 65 | + |
| 66 | + try: |
| 67 | + result = wrapped(*arg_list, **kwargs) |
| 68 | + |
| 69 | + if isinstance(result, dict): |
| 70 | + http_dict = result.get('ResponseMetadata') |
| 71 | + if isinstance(http_dict, dict): |
| 72 | + status = http_dict.get('HTTPStatusCode') |
| 73 | + if status is not None: |
| 74 | + scope.span.set_tag('http.status_code', status) |
| 75 | + |
| 76 | + return result |
| 77 | + except Exception as exc: |
| 78 | + scope.span.mark_as_errored({'error': exc}) |
| 79 | + raise |
| 80 | + |
| 81 | + def s3_inject_method_with_instana(wrapped, instance, arg_list, kwargs): |
| 82 | + fas = inspect.getfullargspec(wrapped) |
| 83 | + fas_args = fas.args |
| 84 | + fas_args.remove('self') |
| 85 | + |
| 86 | + # pylint: disable=protected-access |
| 87 | + parent_span = tracer.active_span |
| 88 | + |
| 89 | + # If we're not tracing, just return |
| 90 | + if parent_span is None: |
| 91 | + return wrapped(*arg_list, **kwargs) |
| 92 | + |
| 93 | + with tracer.start_active_span("boto3", child_of=parent_span) as scope: |
| 94 | + try: |
| 95 | + operation = wrapped.__name__ |
| 96 | + scope.span.set_tag('op', operation) |
| 97 | + scope.span.set_tag('ep', instance._endpoint.host) |
| 98 | + scope.span.set_tag('reg', instance._client_config.region_name) |
| 99 | + |
| 100 | + scope.span.set_tag('http.url', instance._endpoint.host + ':443/' + operation) |
| 101 | + scope.span.set_tag('http.method', 'POST') |
| 102 | + |
| 103 | + index = 1 |
| 104 | + payload = {} |
| 105 | + arg_length = len(arg_list) |
| 106 | + |
| 107 | + for arg_name in fas_args: |
| 108 | + payload[arg_name] = arg_list[index-1] |
| 109 | + |
| 110 | + index += 1 |
| 111 | + if index > arg_length: |
| 112 | + break |
| 113 | + |
| 114 | + scope.span.set_tag('payload', payload) |
| 115 | + except Exception as exc: |
| 116 | + logger.debug("s3_inject_method_with_instana: collect error", exc_info=True) |
| 117 | + |
| 118 | + try: |
| 119 | + return wrapped(*arg_list, **kwargs) |
| 120 | + except Exception as exc: |
| 121 | + scope.span.mark_as_errored({'error': exc}) |
| 122 | + raise |
| 123 | + |
| 124 | + for method in ['upload_file', 'upload_fileobj', 'download_file', 'download_fileobj']: |
| 125 | + wrapt.wrap_function_wrapper('boto3.s3.inject', method, s3_inject_method_with_instana) |
| 126 | + |
| 127 | + logger.debug("Instrumenting boto3") |
| 128 | +except ImportError: |
| 129 | + pass |
0 commit comments