Skip to content

Commit f6a2f16

Browse files
committed
WIP
1 parent 1061d96 commit f6a2f16

File tree

12 files changed

+650
-70
lines changed

12 files changed

+650
-70
lines changed

opentelemetry-api/src/opentelemetry/attributes/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from collections.abc import MutableMapping
2020
from typing import Optional, Sequence, Union
2121

22+
from opentelemetry.opentelemetry import OpenTelemetry
2223
from opentelemetry.util import types
2324

2425
# bytes are accepted as a user supplied value for attributes but
@@ -126,7 +127,7 @@ def _clean_attribute_value(
126127
return value
127128

128129

129-
class BoundedAttributes(MutableMapping):
130+
class BoundedAttributes(OpenTelemetry, MutableMapping):
130131
"""An ordered dict with a fixed max capacity.
131132
132133
Oldest elements are dropped when the dict is full and a new element is
@@ -140,6 +141,12 @@ def __init__(
140141
immutable: bool = True,
141142
max_value_len: Optional[int] = None,
142143
):
144+
super().__init__(
145+
maxlen=maxlen,
146+
attributes=attributes,
147+
immutable=immutable,
148+
max_value_len=max_value_len,
149+
)
143150
if maxlen is not None:
144151
if not isinstance(maxlen, int) or maxlen < 0:
145152
raise ValueError(
@@ -155,11 +162,6 @@ def __init__(
155162
self[key] = value
156163
self._immutable = immutable
157164

158-
def __repr__(self):
159-
return (
160-
f"{type(self).__name__}({dict(self._dict)}, maxlen={self.maxlen})"
161-
)
162-
163165
def __getitem__(self, key):
164166
return self._dict[key]
165167

opentelemetry-api/src/opentelemetry/metrics/_internal/observation.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414

1515
from typing import Union
1616

17+
from opentelemetry.opentelemetry import OpenTelemetry
1718
from opentelemetry.util.types import Attributes
1819

1920

20-
class Observation:
21+
class Observation(OpenTelemetry):
2122
"""A measurement observed in an asynchronous instrument
2223
2324
Return/yield instances of this class from asynchronous instrument callbacks.
@@ -47,6 +48,3 @@ def __eq__(self, other: object) -> bool:
4748
and self.value == other.value
4849
and self.attributes == other.attributes
4950
)
50-
51-
def __repr__(self) -> str:
52-
return f"Observation(value={self.value}, attributes={self.attributes})"
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
15+
from inspect import signature
16+
17+
18+
class OpenTelemetry:
19+
def __init__(self, *args, **kwargs) -> None:
20+
21+
args = list(args)
22+
23+
object.__setattr__(self, "_repr", [])
24+
25+
parameters = signature(self.__init__).parameters.values()
26+
27+
for index, parameter in enumerate(parameters):
28+
if (
29+
parameter.kind is parameter.POSITIONAL_ONLY
30+
or parameter.kind is parameter.POSITIONAL_OR_KEYWORD
31+
):
32+
if args:
33+
self._repr.append(repr(args.pop(0)))
34+
else:
35+
break
36+
elif parameter.kind is parameter.VAR_POSITIONAL:
37+
for _ in range(len(args)):
38+
self._repr.append(repr(args.pop(0)))
39+
40+
for index, parameter in enumerate(parameters):
41+
if parameter.kind is parameter.KEYWORD_ONLY:
42+
if args:
43+
value = args.pop(0)
44+
45+
if parameter.default != value:
46+
self._repr.append(f"{parameter.name}={repr(value)}")
47+
else:
48+
break
49+
50+
for parameter in parameters:
51+
if (
52+
parameter.kind is parameter.KEYWORD_ONLY
53+
or parameter.kind is parameter.POSITIONAL_OR_KEYWORD
54+
) and parameter.name in kwargs.keys():
55+
value = kwargs.pop(parameter.name)
56+
57+
if parameter.default != value:
58+
self._repr.append(f"{parameter.name}={repr(value)}")
59+
60+
elif parameter.kind is parameter.VAR_KEYWORD:
61+
for key, value in kwargs.items():
62+
self._repr.append(f"{key}={repr(value)}")
63+
64+
self._repr = f"{self.__class__.__name__}({', '.join(self._repr)})"
65+
66+
def __repr__(self) -> str:
67+
return self._repr

opentelemetry-api/src/opentelemetry/trace/span.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import typing
66
from collections import OrderedDict
77

8+
from opentelemetry.opentelemetry import OpenTelemetry
89
from opentelemetry.trace.status import Status, StatusCode
910
from opentelemetry.util import types
1011

@@ -54,7 +55,7 @@ def _is_valid_pair(key: str, value: str) -> bool:
5455
)
5556

5657

57-
class Span(abc.ABC):
58+
class Span(OpenTelemetry, abc.ABC):
5859
"""A span represents a single operation within a trace."""
5960

6061
@abc.abstractmethod
@@ -201,7 +202,7 @@ def sampled(self) -> bool:
201202
DEFAULT_TRACE_OPTIONS = TraceFlags.get_default()
202203

203204

204-
class TraceState(typing.Mapping[str, str]):
205+
class TraceState(OpenTelemetry, typing.Mapping[str, str]):
205206
"""A list of key-value pairs representing vendor-specific trace info.
206207
207208
Keys and values are strings of up to 256 printable US-ASCII characters.
@@ -218,6 +219,7 @@ def __init__(
218219
typing.Sequence[typing.Tuple[str, str]]
219220
] = None,
220221
) -> None:
222+
super().__init__(entries=entries)
221223
self._dict = OrderedDict() # type: OrderedDict[str, str]
222224
if entries is None:
223225
return
@@ -251,13 +253,6 @@ def __iter__(self) -> typing.Iterator[str]:
251253
def __len__(self) -> int:
252254
return len(self._dict)
253255

254-
def __repr__(self) -> str:
255-
pairs = [
256-
f"{{key={key}, value={value}}}"
257-
for key, value in self._dict.items()
258-
]
259-
return str(pairs)
260-
261256
def add(self, key: str, value: str) -> "TraceState":
262257
"""Adds a key-value pair to tracestate. The provided pair should
263258
adhere to w3c tracestate identifiers format.
@@ -404,7 +399,8 @@ def values(self) -> typing.ValuesView[str]:
404399

405400

406401
class SpanContext(
407-
typing.Tuple[int, int, bool, "TraceFlags", "TraceState", bool]
402+
OpenTelemetry,
403+
typing.Tuple[int, int, bool, "TraceFlags", "TraceState", bool],
408404
):
409405
"""The state of a Span to propagate between processes.
410406
@@ -437,10 +433,19 @@ def __new__(
437433
and INVALID_SPAN_ID < span_id <= _SPAN_ID_MAX_VALUE
438434
)
439435

440-
return tuple.__new__(
436+
span_context = tuple.__new__(
441437
cls,
442438
(trace_id, span_id, is_remote, trace_flags, trace_state, is_valid),
443439
)
440+
cls.__init__(
441+
span_context,
442+
trace_id,
443+
span_id,
444+
is_remote,
445+
trace_flags=trace_flags,
446+
trace_state=trace_state,
447+
)
448+
return span_context
444449

445450
def __getnewargs__(
446451
self,
@@ -487,9 +492,6 @@ def __delattr__(self, *args: str) -> None:
487492
"Immutable type, ignoring call to set attribute", stack_info=True
488493
)
489494

490-
def __repr__(self) -> str:
491-
return f"{type(self).__name__}(trace_id=0x{format_trace_id(self.trace_id)}, span_id=0x{format_span_id(self.span_id)}, trace_flags=0x{self.trace_flags:02x}, trace_state={self.trace_state!r}, is_remote={self.is_remote})"
492-
493495

494496
class NonRecordingSpan(Span):
495497
"""The Span that is used when no Span implementation is available.
@@ -544,9 +546,6 @@ def record_exception(
544546
) -> None:
545547
pass
546548

547-
def __repr__(self) -> str:
548-
return f"NonRecordingSpan({self._context!r})"
549-
550549

551550
INVALID_SPAN_ID = 0x0000000000000000
552551
INVALID_TRACE_ID = 0x00000000000000000000000000000000

0 commit comments

Comments
 (0)