otlp-json 0.9.2__py3-none-any.whl → 0.9.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
otlp_json/__init__.py CHANGED
@@ -7,6 +7,8 @@ from typing import Any, TYPE_CHECKING
7
7
  if TYPE_CHECKING:
8
8
  from typing_extensions import TypeAlias
9
9
 
10
+ from opentelemetry.trace import Link
11
+ from opentelemetry._logs import LogRecord
10
12
  from opentelemetry.sdk.trace import ReadableSpan, Event
11
13
  from opentelemetry.sdk.resources import Resource
12
14
  from opentelemetry.sdk.util.instrumentation import InstrumentationScope
@@ -66,26 +68,27 @@ def encode_spans(spans: Sequence[ReadableSpan]) -> bytes:
66
68
 
67
69
 
68
70
  def _resource(resource: Resource):
69
- rv = {"attributes": []}
70
- for k, v in resource.attributes.items():
71
+ # TODO: add schema_url once that lands in opentelemetry-sdk
72
+ return _attributes(resource)
73
+
74
+
75
+ def _attributes(
76
+ thing: Resource | InstrumentationScope | ReadableSpan | Event | Link | LogRecord,
77
+ ) -> dict[str, Any]:
78
+ rv = {"attributes": [], "dropped_attributes_count": 0}
79
+
80
+ assert thing.attributes is not None
81
+ for k, v in thing.attributes.items():
71
82
  try:
72
83
  rv["attributes"].append({"key": k, "value": _value(v)})
73
84
  except ValueError:
74
85
  pass
75
86
 
76
- # NOTE: blocks that contain droppedAttributesCount:
77
- # - Event
78
- # - Link
79
- # - InstrumentationScope
80
- # - LogRecord (out of scope for this library)
81
- # - Resource
82
- rv["dropped_attributes_count"] = len(resource.attributes) - len(rv["attributes"]) # type: ignore
87
+ rv["dropped_attributes_count"] = len(thing.attributes) - len(rv["attributes"]) # type: ignore
83
88
 
84
- if not rv["attributes"]:
85
- del rv["attributes"]
86
-
87
- if not rv["dropped_attributes_count"]:
88
- del rv["dropped_attributes_count"]
89
+ for k in ("attributes", "dropped_attributes_count"):
90
+ if not rv[k]:
91
+ del rv[k]
89
92
 
90
93
  return rv
91
94
 
@@ -99,8 +102,9 @@ def _homogeneous_array(value: list[_LEAF_VALUE]) -> list[_LEAF_VALUE]:
99
102
 
100
103
  def _value(value: _VALUE) -> dict[str, Any]:
101
104
  # Attribute value can be a primitive type, excluging None...
102
- # TODO: protobuf allows bytes, but I think OTLP doesn't.
103
- # TODO: protobuf allows k:v pairs, but I think OTLP doesn't.
105
+ # protobuf allows bytes, but I think OTLP spec does not?
106
+ # protobuf allows k:v pairs, but I think OTLP doesn't.
107
+ # TODO: read up the spec and validate the allowed type range.
104
108
  for klass, (key, post) in _VALUE_TYPES.items():
105
109
  if isinstance(value, klass):
106
110
  return {key: post(value)}
@@ -109,7 +113,10 @@ def _value(value: _VALUE) -> dict[str, Any]:
109
113
 
110
114
 
111
115
  def _scope(scope: InstrumentationScope):
112
- rv = {"name": scope.name}
116
+ rv = {
117
+ "name": scope.name,
118
+ **_attributes(scope),
119
+ }
113
120
  if scope.version:
114
121
  rv["version"] = scope.version
115
122
  return rv
@@ -123,29 +130,16 @@ def _span(span: ReadableSpan):
123
130
  "traceId": _trace_id(span.context.trace_id),
124
131
  "spanId": _span_id(span.context.span_id),
125
132
  "flags": 0x100 | ([0, 0x200][bool(span.parent and span.parent.is_remote)]),
126
- "startTimeUnixNano": str(span.start_time), # TODO: is it ever optional?
127
- "endTimeUnixNano": str(span.end_time), # -"-
133
+ "startTimeUnixNano": str(span.start_time),
134
+ "endTimeUnixNano": str(span.end_time), # can this be unset?
128
135
  "status": _status(span.status),
129
- "attributes": [],
136
+ **_attributes(span),
130
137
  }
131
138
 
132
139
  if span.parent:
133
140
  rv["parentSpanId"] = _span_id(span.parent.span_id)
134
141
 
135
- for k, v in span.attributes.items(): # type: ignore
136
- try:
137
- rv["attributes"].append({"key": k, "value": _value(v)})
138
- except ValueError:
139
- pass
140
-
141
- rv["dropped_attributes_count"] = len(span.attributes) - len(rv["attributes"]) # type: ignore
142
-
143
- if not rv["attributes"]:
144
- del rv["attributes"]
145
-
146
- if not rv["dropped_attributes_count"]:
147
- del rv["dropped_attributes_count"]
148
-
142
+ # TODO: is this field really nullable?
149
143
  if span.events:
150
144
  rv["events"] = [_event(e) for e in span.events]
151
145
 
@@ -165,7 +159,7 @@ def _span_id(span_id: int) -> str:
165
159
 
166
160
 
167
161
  def _status(status: Status) -> dict[str, Any]:
168
- # FIXME
162
+ # FIXME: need an example of bad status
169
163
  return {}
170
164
 
171
165
 
@@ -173,21 +167,7 @@ def _event(event: Event) -> dict[str, Any]:
173
167
  rv = {
174
168
  "name": event.name,
175
169
  "timeUnixNano": str(event.timestamp),
176
- "attributes": [],
170
+ **_attributes(event),
177
171
  }
178
-
179
- for k, v in event.attributes.items(): # type: ignore
180
- try:
181
- rv["attributes"].append({"key": k, "value": _value(v)})
182
- except ValueError:
183
- pass
184
-
185
- rv["dropped_attributes_count"] = len(event.attributes) - len(rv["attributes"]) # type: ignore
186
-
187
- if not rv["attributes"]:
188
- del rv["attributes"]
189
-
190
- if not rv["dropped_attributes_count"]:
191
- del rv["dropped_attributes_count"]
192
-
172
+ # TODO: any optional attributes?
193
173
  return rv
otlp_json/py.typed ADDED
File without changes
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: otlp-json
3
- Version: 0.9.2
3
+ Version: 0.9.4
4
4
  Summary: 🐍Lightweight OTEL span to JSON converter, no dependencies, pure Python🐍
5
5
  Project-URL: Repository, https://github.com/dimaqq/otlp-json
6
6
  Project-URL: Issues, https://github.com/dimaqq/otlp-json/issues
@@ -0,0 +1,5 @@
1
+ otlp_json/__init__.py,sha256=Ur95iX9xoeylZxq_ZKr5jqUNPvlp7ELcgxwU71SC42A,5364
2
+ otlp_json/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ otlp_json-0.9.4.dist-info/METADATA,sha256=swioA9pQ_LXYVL-J0WgTcKjkbdaC7xh70m2rQ4FSBSs,2810
4
+ otlp_json-0.9.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
5
+ otlp_json-0.9.4.dist-info/RECORD,,
@@ -1,4 +0,0 @@
1
- otlp_json/__init__.py,sha256=KFI4PCFiyTOw3wTUClS1hIeUyAMCCZp1F9F6K6Qi_IQ,5862
2
- otlp_json-0.9.2.dist-info/METADATA,sha256=1CqS5onFoYgi8n4ft4DuQnsHwV_kp4aHC5zoQVxLIZA,2810
3
- otlp_json-0.9.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
4
- otlp_json-0.9.2.dist-info/RECORD,,