Commit cb1a3ce
committed
Introduce to_dict
Introduce `to_dict` to the objects included in the existing
JSON serialization process for `ReadableSpan`, `MetricsData`,
`LogRecord`, and `Resource` objects. This includes adding
`to_dict` to objects that are included within the serialized
data structures of these objects. In places where `repr()`
serialization was used, it has been replaced by a
JSON-compatible serialization instead. Inconsistencies between
null and empty string values were preserved, but in cases
where attributes are optional, an empty dictionary is provided
as well to be more consistent with cases where attributes are
not optional and an empty dictionary represents no attributes
were specified on the containing object.
These changes also included:
1. Dictionary typing was included for all the `to_dict`
methods for clarity in subsequent usage.
2. `DataT` and `DataPointT` were did not include the
exponential histogram types in point.py, and so those
were added with new `to_json` and `to_dict` methods as
well for consistency. It appears that the exponential
types were added later and including them in the types
might have been overlooked. Please let me know if that
is a misunderstanding on my part.
3. OrderedDict was removed in a number of places
associated with the existing `to_json` functionality
given its redundancy for Python 3.7+ compatibility.
I was assuming this was legacy code for previous
compatibility, but please let me know if that's not
the case as well.
4. `to_dict` was added to objects like `SpanContext`,
`Link`, and `Event` that were previously being
serialized by static methods within the `ReadableSpan`
class and accessing private/protected members.
This simplified the serialization in the `ReadableSpan`
class and those methods were removed. However, once
again, let me know if there was a larger purpose to
those I could not find.
Finally, I used `to_dict` as the method names here to be
consistent with other related usages. For example,
`dataclasses.asdict()`. But, mostly because that was by
far the most popular usage within the larger community:
328k files found on GitHub that define `to_dict` functions,
which include some of the most popular Python libraries
to date:
https://github.com/search?q=%22def+to_dict%28%22+language%3APython&type=code&p=1&l=Python
versus
3.3k files found on GitHub that define `to_dictionary`
functions:
https://github.com/search?q=%22def+to_dictionary%28%22+language%3APython&type=code&l=Python
However, if there is a preference for this library to use
`to_dictionary` instead let me know and I will adjust.
Fixes #33641 parent 37de27a commit cb1a3ce
File tree
10 files changed
+427
-207
lines changed- opentelemetry-api/src/opentelemetry/trace
- opentelemetry-sdk
- src/opentelemetry/sdk
- _logs/_internal
- metrics/_internal
- resources
- trace
- util
- tests
- logs
- trace
10 files changed
+427
-207
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
106 | 107 | | |
107 | 108 | | |
108 | 109 | | |
| |||
130 | 131 | | |
131 | 132 | | |
132 | 133 | | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
133 | 141 | | |
134 | 142 | | |
135 | 143 | | |
| |||
152 | 160 | | |
153 | 161 | | |
154 | 162 | | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
155 | 169 | | |
156 | 170 | | |
157 | 171 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
403 | 403 | | |
404 | 404 | | |
405 | 405 | | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
406 | 414 | | |
407 | 415 | | |
408 | 416 | | |
| |||
477 | 485 | | |
478 | 486 | | |
479 | 487 | | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
480 | 495 | | |
481 | 496 | | |
482 | 497 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
35 | 42 | | |
36 | 43 | | |
37 | 44 | | |
| |||
80 | 87 | | |
81 | 88 | | |
82 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
Lines changed: 38 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
40 | | - | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| |||
147 | 148 | | |
148 | 149 | | |
149 | 150 | | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
150 | 166 | | |
151 | 167 | | |
152 | 168 | | |
| |||
194 | 210 | | |
195 | 211 | | |
196 | 212 | | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
197 | 233 | | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
| 234 | + | |
221 | 235 | | |
222 | 236 | | |
223 | 237 | | |
| |||
0 commit comments