def end()

in instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/wrappers.py [0:0]


    def end(self, exc=None):
        if self.ended:
            return

        self.ended = True
        if exc is not None:
            self.span.set_status(StatusCode.ERROR, str(exc))
            self.span.set_attribute(ERROR_TYPE, exc.__class__.__qualname__)
            self.span.end()
            error_attributes = {**self.span_attributes, ERROR_TYPE: exc.__class__.__qualname__}
            _record_operation_duration_metric(self.operation_duration_metric, error_attributes, self.start_time)
            return

        response_attributes = _get_attributes_from_response(
            self.response_id, self.model, self.choices, self.usage, self.service_tier
        )
        if self.span.is_recording():
            for k, v in response_attributes.items():
                self.span.set_attribute(k, v)

        metrics_attributes = {**self.span_attributes, **response_attributes}
        _record_operation_duration_metric(self.operation_duration_metric, metrics_attributes, self.start_time)
        if self.usage:
            _record_token_usage_metrics(self.token_usage_metric, metrics_attributes, self.usage)

        _send_log_events_from_stream_choices(
            self.event_logger,
            choices=self.choices,
            span=self.span,
            attributes=self.event_attributes,
            capture_message_content=self.capture_message_content,
        )

        self.span.end()