|
41 | 41 | request_to_events,
|
42 | 42 | response_to_events,
|
43 | 43 | )
|
| 44 | +from opentelemetry.semconv._incubating.attributes import ( |
| 45 | + gen_ai_attributes as GenAI, |
| 46 | +) |
44 | 47 | from opentelemetry.trace import SpanKind, Tracer
|
45 | 48 | from opentelemetry.util.genai.types import ContentCapturingMode
|
46 | 49 |
|
@@ -147,32 +150,35 @@ def _with_new_instrumentation(
|
147 | 150 | ):
|
148 | 151 | params = _extract_params(*args, **kwargs)
|
149 | 152 | api_endpoint: str = instance.api_endpoint # type: ignore[reportUnknownMemberType]
|
150 |
| - span_attributes = { |
151 |
| - **get_genai_request_attributes(False, params), |
152 |
| - **get_server_attributes(api_endpoint), |
153 |
| - } |
154 |
| - |
155 |
| - span_name = get_span_name(span_attributes) |
156 |
| - |
| 153 | + request_attributes = get_genai_request_attributes(True, params) |
| 154 | + server_attributes = get_server_attributes(api_endpoint) |
157 | 155 | with self.tracer.start_as_current_span(
|
158 |
| - name=span_name, |
| 156 | + name=f"{GenAI.GenAiOperationNameValues.CHAT.value} {request_attributes.get(GenAI.GEN_AI_REQUEST_MODEL, '')}", |
159 | 157 | kind=SpanKind.CLIENT,
|
160 |
| - attributes=span_attributes, |
161 | 158 | ) as span:
|
162 | 159 |
|
163 | 160 | def handle_response(
|
164 | 161 | response: prediction_service.GenerateContentResponse
|
165 | 162 | | prediction_service_v1beta1.GenerateContentResponse
|
166 | 163 | | None,
|
167 | 164 | ) -> None:
|
| 165 | + response_attributes = ( |
| 166 | + {} |
| 167 | + if not response |
| 168 | + else get_genai_response_attributes(response) |
| 169 | + ) |
168 | 170 | if span.is_recording() and response:
|
169 | 171 | # When streaming, this is called multiple times so attributes would be
|
170 | 172 | # overwritten. In practice, it looks the API only returns the interesting
|
171 | 173 | # attributes on the last streamed response. However, I couldn't find
|
172 | 174 | # documentation for this and setting attributes shouldn't be too expensive.
|
173 | 175 | span.set_attributes(
|
174 |
| - get_genai_response_attributes(response) |
| 176 | + **response_attributes, |
| 177 | + **server_attributes, |
| 178 | + **request_attributes, |
175 | 179 | )
|
| 180 | + # event = Event(name="gen_ai.client.inference.operation.details") |
| 181 | + |
176 | 182 | self.event_logger.emit(
|
177 | 183 | create_operation_details_event(
|
178 | 184 | api_endpoint=api_endpoint,
|
|
0 commit comments