|
| 1 | +# BSD 3-Clause License |
| 2 | +# |
| 3 | +# Copyright (c) 2019, Elasticsearch BV |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# |
| 9 | +# * Redistributions of source code must retain the above copyright notice, this |
| 10 | +# list of conditions and the following disclaimer. |
| 11 | +# |
| 12 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +# this list of conditions and the following disclaimer in the documentation |
| 14 | +# and/or other materials provided with the distribution. |
| 15 | +# |
| 16 | +# * Neither the name of the copyright holder nor the names of its |
| 17 | +# contributors may be used to endorse or promote products derived from |
| 18 | +# this software without specific prior written permission. |
| 19 | +# |
| 20 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 24 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 26 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 27 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 28 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + |
| 31 | +from opentracing.span import Span as OTSpanBase |
| 32 | +from opentracing.span import SpanContext as OTSpanContextBase |
| 33 | + |
| 34 | +from elasticapm import traces |
| 35 | +from elasticapm.utils import get_url_dict |
| 36 | +from elasticapm.utils.logging import get_logger |
| 37 | + |
| 38 | +try: |
| 39 | + # opentracing-python 2.1+ |
| 40 | + from opentracing import logs as ot_logs |
| 41 | + from opentracing import tags |
| 42 | +except ImportError: |
| 43 | + # opentracing-python <2.1 |
| 44 | + from opentracing.ext import tags |
| 45 | + |
| 46 | + ot_logs = None |
| 47 | + |
| 48 | + |
| 49 | +logger = get_logger("elasticapm.contrib.opentracing") |
| 50 | + |
| 51 | + |
| 52 | +class OTSpan(OTSpanBase): |
| 53 | + def __init__(self, tracer, context, elastic_apm_ref) -> None: |
| 54 | + super(OTSpan, self).__init__(tracer, context) |
| 55 | + self.elastic_apm_ref = elastic_apm_ref |
| 56 | + self.is_transaction = isinstance(elastic_apm_ref, traces.Transaction) |
| 57 | + self.is_dropped = isinstance(elastic_apm_ref, traces.DroppedSpan) |
| 58 | + if not context.span: |
| 59 | + context.span = self |
| 60 | + |
| 61 | + def log_kv(self, key_values, timestamp=None): |
| 62 | + exc_type, exc_val, exc_tb = None, None, None |
| 63 | + if "python.exception.type" in key_values: |
| 64 | + exc_type = key_values["python.exception.type"] |
| 65 | + exc_val = key_values.get("python.exception.val") |
| 66 | + exc_tb = key_values.get("python.exception.tb") |
| 67 | + elif ot_logs and key_values.get(ot_logs.EVENT) == tags.ERROR: |
| 68 | + exc_type = key_values[ot_logs.ERROR_KIND] |
| 69 | + exc_val = key_values.get(ot_logs.ERROR_OBJECT) |
| 70 | + exc_tb = key_values.get(ot_logs.STACK) |
| 71 | + else: |
| 72 | + logger.debug("Can't handle non-exception type opentracing logs") |
| 73 | + if exc_type: |
| 74 | + agent = self.tracer._agent |
| 75 | + agent.capture_exception(exc_info=(exc_type, exc_val, exc_tb)) |
| 76 | + return self |
| 77 | + |
| 78 | + def set_operation_name(self, operation_name): |
| 79 | + self.elastic_apm_ref.name = operation_name |
| 80 | + return self |
| 81 | + |
| 82 | + def set_tag(self, key, value): |
| 83 | + if self.is_transaction: |
| 84 | + if key == "type": |
| 85 | + self.elastic_apm_ref.transaction_type = value |
| 86 | + elif key == "result": |
| 87 | + self.elastic_apm_ref.result = value |
| 88 | + elif key == tags.HTTP_STATUS_CODE: |
| 89 | + self.elastic_apm_ref.result = "HTTP {}xx".format(str(value)[0]) |
| 90 | + traces.set_context({"status_code": value}, "response") |
| 91 | + elif key == "user.id": |
| 92 | + traces.set_user_context(user_id=value) |
| 93 | + elif key == "user.username": |
| 94 | + traces.set_user_context(username=value) |
| 95 | + elif key == "user.email": |
| 96 | + traces.set_user_context(email=value) |
| 97 | + elif key == tags.HTTP_URL: |
| 98 | + traces.set_context({"url": get_url_dict(value)}, "request") |
| 99 | + elif key == tags.HTTP_METHOD: |
| 100 | + traces.set_context({"method": value}, "request") |
| 101 | + elif key == tags.COMPONENT: |
| 102 | + traces.set_context({"framework": {"name": value}}, "service") |
| 103 | + else: |
| 104 | + self.elastic_apm_ref.label(**{key: value}) |
| 105 | + elif not self.is_dropped: |
| 106 | + if key.startswith("db."): |
| 107 | + span_context = self.elastic_apm_ref.context or {} |
| 108 | + if "db" not in span_context: |
| 109 | + span_context["db"] = {} |
| 110 | + if key == tags.DATABASE_STATEMENT: |
| 111 | + span_context["db"]["statement"] = value |
| 112 | + elif key == tags.DATABASE_USER: |
| 113 | + span_context["db"]["user"] = value |
| 114 | + elif key == tags.DATABASE_TYPE: |
| 115 | + span_context["db"]["type"] = value |
| 116 | + self.elastic_apm_ref.type = "db." + value |
| 117 | + else: |
| 118 | + self.elastic_apm_ref.label(**{key: value}) |
| 119 | + self.elastic_apm_ref.context = span_context |
| 120 | + elif key == tags.SPAN_KIND: |
| 121 | + self.elastic_apm_ref.type = value |
| 122 | + else: |
| 123 | + self.elastic_apm_ref.label(**{key: value}) |
| 124 | + return self |
| 125 | + |
| 126 | + def finish(self, finish_time=None) -> None: |
| 127 | + if self.is_transaction: |
| 128 | + self.tracer._agent.end_transaction() |
| 129 | + elif not self.is_dropped: |
| 130 | + self.elastic_apm_ref.transaction.end_span() |
| 131 | + |
| 132 | + |
| 133 | +class OTSpanContext(OTSpanContextBase): |
| 134 | + def __init__(self, trace_parent, span=None) -> None: |
| 135 | + self.trace_parent = trace_parent |
| 136 | + self.span = span |
0 commit comments