|  | 
| 11 | 11 | ) | 
| 12 | 12 | 
 | 
| 13 | 13 | from opentelemetry import context, trace | 
| 14 |  | -from opentelemetry.trace import StatusCode, Tracer, Span | 
|  | 14 | +from opentelemetry.trace import StatusCode, Tracer, Span, Link, get_tracer | 
| 15 | 15 | 
 | 
| 16 | 16 | from guardrails.settings import settings | 
| 17 | 17 | from guardrails.classes.generic.stack import Stack | 
| @@ -145,18 +145,18 @@ def trace_stream_guard( | 
| 145 | 145 |             res = next(result)  # type: ignore | 
| 146 | 146 |             # FIXME: This should only be called once; | 
| 147 | 147 |             # Accumulate the validated output and call at the end | 
| 148 |  | -            # if not guard_span.is_recording(): | 
| 149 |  | -            #     # Assuming you have a tracer instance | 
| 150 |  | -            #     tracer = get_tracer(__name__) | 
| 151 |  | -            #     # Create a new span and link it to the previous span | 
| 152 |  | -            #     with tracer.start_as_current_span( | 
| 153 |  | -            #         "new_guard_span",  # type: ignore | 
| 154 |  | -            #         links=[Link(guard_span.get_span_context())], | 
| 155 |  | -            #     ) as new_span: | 
| 156 |  | -            #         guard_span = new_span | 
| 157 |  | -            #         add_guard_attributes(guard_span, history, res) | 
| 158 |  | -            #         add_user_attributes(guard_span) | 
| 159 |  | -            yield res | 
|  | 148 | +            if not guard_span.is_recording(): | 
|  | 149 | +                # Assuming you have a tracer instance | 
|  | 150 | +                tracer = get_tracer(__name__) | 
|  | 151 | +                # Create a new span and link it to the previous span | 
|  | 152 | +                with tracer.start_as_current_span( | 
|  | 153 | +                    "stream_guard_span",  # type: ignore | 
|  | 154 | +                    links=[Link(guard_span.get_span_context())], | 
|  | 155 | +                ) as new_span: | 
|  | 156 | +                    guard_span = new_span | 
|  | 157 | +                    add_guard_attributes(guard_span, history, res) | 
|  | 158 | +                    add_user_attributes(guard_span) | 
|  | 159 | +                    yield res | 
| 160 | 160 |         except StopIteration: | 
| 161 | 161 |             next_exists = False | 
| 162 | 162 | 
 | 
| @@ -209,19 +209,19 @@ async def trace_async_stream_guard( | 
| 209 | 209 |     while next_exists: | 
| 210 | 210 |         try: | 
| 211 | 211 |             res = await anext(result)  # type: ignore | 
| 212 |  | -            # if not guard_span.is_recording(): | 
| 213 |  | -            # Assuming you have a tracer instance | 
| 214 |  | -            # tracer = get_tracer(__name__) | 
| 215 |  | -            # # Create a new span and link it to the previous span | 
| 216 |  | -            # with tracer.start_as_current_span( | 
| 217 |  | -            #     "new_guard_span",  # type: ignore | 
| 218 |  | -            #     links=[Link(guard_span.get_span_context())], | 
| 219 |  | -            # ) as new_span: | 
| 220 |  | -            #     guard_span = new_span | 
| 221 |  | - | 
| 222 |  | -            #     add_guard_attributes(guard_span, history, res) | 
| 223 |  | -            #     add_user_attributes(guard_span) | 
| 224 |  | -            yield res | 
|  | 212 | +            if not guard_span.is_recording(): | 
|  | 213 | +                # Assuming you have a tracer instance | 
|  | 214 | +                tracer = get_tracer(__name__) | 
|  | 215 | +                # Create a new span and link it to the previous span | 
|  | 216 | +                with tracer.start_as_current_span( | 
|  | 217 | +                    "async_stream_span",  # type: ignore | 
|  | 218 | +                    links=[Link(guard_span.get_span_context())], | 
|  | 219 | +                ) as new_span: | 
|  | 220 | +                    guard_span = new_span | 
|  | 221 | + | 
|  | 222 | +                    add_guard_attributes(guard_span, history, res) | 
|  | 223 | +                    add_user_attributes(guard_span) | 
|  | 224 | +                    yield res | 
| 225 | 225 |         except StopIteration: | 
| 226 | 226 |             next_exists = False | 
| 227 | 227 |         except StopAsyncIteration: | 
|  | 
0 commit comments