77if TYPE_CHECKING :
88 from typing_extensions import TypeAlias
99
10- from opentelemetry .sdk .trace import ReadableSpan
10+ from opentelemetry .sdk .trace import ReadableSpan , Event
1111 from opentelemetry .sdk .resources import Resource
1212 from opentelemetry .sdk .util .instrumentation import InstrumentationScope
1313 from opentelemetry .trace .status import Status
@@ -79,8 +79,13 @@ def _resource(resource: Resource):
7979 # - InstrumentationScope
8080 # - LogRecord (out of scope for this library)
8181 # - Resource
82- if dropped := len (resource .attributes ) - len (rv ["attributes" ]):
83- rv ["dropped_attribute_count" ] = dropped # type: ignore
82+ rv ["dropped_attributes_count" ] = len (resource .attributes ) - len (rv ["attributes" ]) # type: ignore
83+
84+ if not rv ["attributes" ]:
85+ del rv ["attributes" ]
86+
87+ if not rv ["dropped_attributes_count" ]:
88+ del rv ["dropped_attributes_count" ]
8489
8590 return rv
8691
@@ -117,26 +122,32 @@ def _span(span: ReadableSpan):
117122 "kind" : span .kind .value or 1 , # unspecified -> internal
118123 "traceId" : _trace_id (span .context .trace_id ),
119124 "spanId" : _span_id (span .context .span_id ),
120- "flags" : 0x100 | ([0 , 0x200 ][bool (span .parent )]),
125+ "flags" : 0x100 | ([0 , 0x200 ][bool (span .parent and span . parent . is_remote )]),
121126 "startTimeUnixNano" : str (span .start_time ), # TODO: is it ever optional?
122127 "endTimeUnixNano" : str (span .end_time ), # -"-
123128 "status" : _status (span .status ),
129+ "attributes" : [],
124130 }
125131
126132 if span .parent :
127133 rv ["parentSpanId" ] = _span_id (span .parent .span_id )
128134
129- if span .attributes :
130- rv ["attributes" ] = []
131-
132135 for k , v in span .attributes .items (): # type: ignore
133136 try :
134137 rv ["attributes" ].append ({"key" : k , "value" : _value (v )})
135138 except ValueError :
136139 pass
137140
138- if dropped := len (span .attributes ) - len (rv .get ("attributes" , ())): # type: ignore
139- rv ["dropped_attribute_count" ] = dropped # type: ignore
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+
149+ if span .events :
150+ rv ["events" ] = [_event (e ) for e in span .events ]
140151
141152 return rv
142153
@@ -156,3 +167,27 @@ def _span_id(span_id: int) -> str:
156167def _status (status : Status ) -> dict [str , Any ]:
157168 # FIXME
158169 return {}
170+
171+
172+ def _event (event : Event ) -> dict [str , Any ]:
173+ rv = {
174+ "name" : event .name ,
175+ "timeUnixNano" : str (event .timestamp ),
176+ "attributes" : [],
177+ }
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+
193+ return rv
0 commit comments