Convert a Session to a Trace with spans
(self)
| 202 | project_id_secondary: Optional[dict] = None |
| 203 | |
| 204 | async def to_trace(self) -> Trace: |
| 205 | """Convert a Session to a Trace with spans""" |
| 206 | span = Span( |
| 207 | name="session", |
| 208 | trace_id=str(self.id), |
| 209 | span_id=str(self.id), # TODO this is the same as trace_id |
| 210 | parent_span_id=None, # parent span has no parent |
| 211 | kind=SpanKind.SERVER, |
| 212 | start_time=datetime_to_ns(self.init_timestamp), |
| 213 | end_time=datetime_to_ns(self.end_timestamp), |
| 214 | project_id=str(self.project_id), |
| 215 | service_name=DEFAULT_SERVICE_NAME, |
| 216 | scope_name=f"{DEFAULT_SCOPE_NAME}.session", |
| 217 | scope_version=DEFAULT_VERSION, |
| 218 | resource_attributes={ |
| 219 | "host.name": str(self.host_env) if self.host_env else "unknown", |
| 220 | "service.name": DEFAULT_SERVICE_NAME, |
| 221 | }, |
| 222 | span_attributes={ |
| 223 | "session.id": str(self.id), |
| 224 | "session.end_state": self.end_state or "", |
| 225 | "session.end_state_reason": self.end_state_reason or "", |
| 226 | }, |
| 227 | ) |
| 228 | |
| 229 | try: |
| 230 | if self.tags: |
| 231 | if not isinstance(self.tags, list): |
| 232 | self.tags = [self.tags] |
| 233 | for i, value in enumerate(self.tags): |
| 234 | span.span_attributes[f"session.tag.{i}"] = str(value) |
| 235 | except Exception: |
| 236 | pass |
| 237 | |
| 238 | trace = Trace(id=self.id) |
| 239 | trace.spans.append(span) |
| 240 | return trace |
| 241 | |
| 242 | |
| 243 | class Agent(BaseModel): |
no test coverage detected