|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import traceback |
| 17 | + |
| 18 | +from opentelemetry._logs import LogRecord |
| 19 | +from opentelemetry.attributes import BoundedAttributes |
| 20 | +from opentelemetry.semconv.attributes import exception_attributes |
| 21 | +from opentelemetry.util.types import AnyValue, _ExtendedAttributes |
| 22 | + |
| 23 | + |
| 24 | +def _get_exception_attributes( |
| 25 | + exception: BaseException, |
| 26 | +) -> dict[str, AnyValue]: |
| 27 | + stacktrace = "".join( |
| 28 | + traceback.format_exception( |
| 29 | + type(exception), value=exception, tb=exception.__traceback__ |
| 30 | + ) |
| 31 | + ) |
| 32 | + module = type(exception).__module__ |
| 33 | + qualname = type(exception).__qualname__ |
| 34 | + exception_type = ( |
| 35 | + f"{module}.{qualname}" if module and module != "builtins" else qualname |
| 36 | + ) |
| 37 | + return { |
| 38 | + exception_attributes.EXCEPTION_TYPE: exception_type, |
| 39 | + exception_attributes.EXCEPTION_MESSAGE: str(exception), |
| 40 | + exception_attributes.EXCEPTION_STACKTRACE: stacktrace, |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +def _get_attributes_with_exception( |
| 45 | + attributes: _ExtendedAttributes | None, |
| 46 | + exception: BaseException | None, |
| 47 | +) -> _ExtendedAttributes | None: |
| 48 | + if exception is None: |
| 49 | + return attributes |
| 50 | + |
| 51 | + exception_attributes_map = _get_exception_attributes(exception) |
| 52 | + if attributes is None: |
| 53 | + attributes_map: _ExtendedAttributes = {} |
| 54 | + else: |
| 55 | + attributes_map = attributes |
| 56 | + |
| 57 | + if isinstance(attributes_map, BoundedAttributes): |
| 58 | + bounded_attributes = attributes_map |
| 59 | + merged = BoundedAttributes( |
| 60 | + maxlen=bounded_attributes.maxlen, |
| 61 | + attributes=bounded_attributes, |
| 62 | + immutable=False, |
| 63 | + max_value_len=bounded_attributes.max_value_len, |
| 64 | + extended_attributes=bounded_attributes._extended_attributes, # pylint: disable=protected-access |
| 65 | + ) |
| 66 | + merged.dropped = bounded_attributes.dropped |
| 67 | + for key, value in exception_attributes_map.items(): |
| 68 | + if key not in merged: |
| 69 | + merged[key] = value |
| 70 | + return merged |
| 71 | + |
| 72 | + return exception_attributes_map | dict(attributes_map.items()) |
| 73 | + |
| 74 | + |
| 75 | +def _copy_log_record( |
| 76 | + record: LogRecord, |
| 77 | + attributes: _ExtendedAttributes | None, |
| 78 | +) -> LogRecord: |
| 79 | + copied_record = LogRecord( |
| 80 | + timestamp=record.timestamp, |
| 81 | + observed_timestamp=record.observed_timestamp, |
| 82 | + context=record.context, |
| 83 | + severity_text=record.severity_text, |
| 84 | + severity_number=record.severity_number, |
| 85 | + body=record.body, |
| 86 | + attributes=attributes, |
| 87 | + event_name=record.event_name, |
| 88 | + exception=getattr(record, "exception", None), |
| 89 | + ) |
| 90 | + copied_record.trace_id = record.trace_id |
| 91 | + copied_record.span_id = record.span_id |
| 92 | + copied_record.trace_flags = record.trace_flags |
| 93 | + return copied_record |
| 94 | + |
| 95 | + |
| 96 | +def _copy_log_record_with_exception(record: LogRecord) -> LogRecord: |
| 97 | + return _copy_log_record( |
| 98 | + record, |
| 99 | + _get_attributes_with_exception(record.attributes, record.exception), |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +def _set_log_record_exception_attributes(record: LogRecord) -> None: |
| 104 | + record.attributes = _get_attributes_with_exception( |
| 105 | + record.attributes, |
| 106 | + record.exception, |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +def _create_log_record_with_exception( |
| 111 | + *, |
| 112 | + timestamp: int | None = None, |
| 113 | + observed_timestamp: int | None = None, |
| 114 | + context=None, |
| 115 | + severity_number=None, |
| 116 | + severity_text: str | None = None, |
| 117 | + body: AnyValue | None = None, |
| 118 | + attributes: _ExtendedAttributes | None = None, |
| 119 | + event_name: str | None = None, |
| 120 | + exception: BaseException | None = None, |
| 121 | +) -> LogRecord: |
| 122 | + return LogRecord( |
| 123 | + timestamp=timestamp, |
| 124 | + observed_timestamp=observed_timestamp, |
| 125 | + context=context, |
| 126 | + severity_number=severity_number, |
| 127 | + severity_text=severity_text, |
| 128 | + body=body, |
| 129 | + attributes=_get_attributes_with_exception(attributes, exception), |
| 130 | + event_name=event_name, |
| 131 | + exception=exception, |
| 132 | + ) |
0 commit comments