|
2 | 2 | from importlib_metadata import version as v |
3 | 3 | from langtrace_python_sdk.constants import LANGTRACE_SDK_NAME |
4 | 4 | from langtrace_python_sdk.utils import set_span_attribute |
| 5 | +from langtrace_python_sdk.utils.llm import ( |
| 6 | + get_extra_attributes, |
| 7 | + get_langtrace_attributes, |
| 8 | + get_span_name, |
| 9 | + set_span_attributes, |
| 10 | +) |
5 | 11 | from langtrace_python_sdk.utils.silently_fail import silently_fail |
6 | 12 | from langtrace_python_sdk.constants.instrumentation.common import ( |
7 | 13 | LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, |
@@ -39,25 +45,29 @@ def traced_method(wrapped, instance, args, kwargs): |
39 | 45 | ), |
40 | 46 | } |
41 | 47 | span_attributes["dspy.optimizer.module.prog"] = json.dumps(prog) |
42 | | - if hasattr(instance, 'metric'): |
43 | | - span_attributes["dspy.optimizer.metric"] = getattr(instance, 'metric').__name__ |
| 48 | + if hasattr(instance, "metric"): |
| 49 | + span_attributes["dspy.optimizer.metric"] = getattr( |
| 50 | + instance, "metric" |
| 51 | + ).__name__ |
44 | 52 | if kwargs.get("trainset") and len(kwargs.get("trainset")) > 0: |
45 | 53 | span_attributes["dspy.optimizer.trainset"] = str(kwargs.get("trainset")) |
46 | 54 | config = {} |
47 | | - if hasattr(instance, 'metric_threshold'): |
48 | | - config["metric_threshold"] = getattr(instance, 'metric_threshold') |
49 | | - if hasattr(instance, 'teacher_settings'): |
50 | | - config["teacher_settings"] = getattr(instance, 'teacher_settings') |
51 | | - if hasattr(instance, 'max_bootstrapped_demos'): |
52 | | - config["max_bootstrapped_demos"] = getattr(instance, 'max_bootstrapped_demos') |
53 | | - if hasattr(instance, 'max_labeled_demos'): |
54 | | - config["max_labeled_demos"] = getattr(instance, 'max_labeled_demos') |
55 | | - if hasattr(instance, 'max_rounds'): |
56 | | - config["max_rounds"] = getattr(instance, 'max_rounds') |
57 | | - if hasattr(instance, 'max_steps'): |
58 | | - config["max_errors"] = getattr(instance, 'max_errors') |
59 | | - if hasattr(instance, 'error_count'): |
60 | | - config["error_count"] = getattr(instance, 'error_count') |
| 55 | + if hasattr(instance, "metric_threshold"): |
| 56 | + config["metric_threshold"] = getattr(instance, "metric_threshold") |
| 57 | + if hasattr(instance, "teacher_settings"): |
| 58 | + config["teacher_settings"] = getattr(instance, "teacher_settings") |
| 59 | + if hasattr(instance, "max_bootstrapped_demos"): |
| 60 | + config["max_bootstrapped_demos"] = getattr( |
| 61 | + instance, "max_bootstrapped_demos" |
| 62 | + ) |
| 63 | + if hasattr(instance, "max_labeled_demos"): |
| 64 | + config["max_labeled_demos"] = getattr(instance, "max_labeled_demos") |
| 65 | + if hasattr(instance, "max_rounds"): |
| 66 | + config["max_rounds"] = getattr(instance, "max_rounds") |
| 67 | + if hasattr(instance, "max_steps"): |
| 68 | + config["max_errors"] = getattr(instance, "max_errors") |
| 69 | + if hasattr(instance, "error_count"): |
| 70 | + config["error_count"] = getattr(instance, "error_count") |
61 | 71 | if config and len(config) > 0: |
62 | 72 | span_attributes["dspy.optimizer.config"] = json.dumps(config) |
63 | 73 |
|
@@ -96,37 +106,36 @@ def patch_signature(operation_name, version, tracer): |
96 | 106 | def traced_method(wrapped, instance, args, kwargs): |
97 | 107 |
|
98 | 108 | service_provider = SERVICE_PROVIDERS["DSPY"] |
99 | | - extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY) |
100 | 109 | span_attributes = { |
101 | | - "langtrace.sdk.name": "langtrace-python-sdk", |
102 | | - "langtrace.service.name": service_provider, |
103 | | - "langtrace.service.type": "framework", |
104 | | - "langtrace.service.version": version, |
105 | | - "langtrace.version": v(LANGTRACE_SDK_NAME), |
106 | | - **(extra_attributes if extra_attributes is not None else {}), |
| 110 | + **get_langtrace_attributes( |
| 111 | + service_provider=service_provider, |
| 112 | + version=version, |
| 113 | + vendor_type="framework", |
| 114 | + ), |
| 115 | + **get_extra_attributes(), |
107 | 116 | } |
108 | 117 |
|
109 | | - # passed operation name |
110 | | - opname = operation_name |
111 | | - if extra_attributes is not None and "langtrace.span.name" in extra_attributes: |
112 | | - # append the operation name to the span name |
113 | | - opname = f"{operation_name}-{extra_attributes['langtrace.span.name']}" |
114 | | - |
115 | 118 | if instance.__class__.__name__: |
116 | 119 | span_attributes["dspy.signature.name"] = instance.__class__.__name__ |
117 | | - span_attributes["dspy.signature"] = str(instance) |
| 120 | + span_attributes["dspy.signature"] = str(instance.signature) |
118 | 121 |
|
119 | 122 | if kwargs and len(kwargs) > 0: |
120 | 123 | span_attributes["dspy.signature.args"] = str(kwargs) |
121 | 124 |
|
122 | 125 | attributes = FrameworkSpanAttributes(**span_attributes) |
123 | | - with tracer.start_as_current_span(opname, kind=SpanKind.CLIENT) as span: |
124 | | - _set_input_attributes(span, kwargs, attributes) |
| 126 | + with tracer.start_as_current_span( |
| 127 | + get_span_name(operation_name=operation_name), kind=SpanKind.CLIENT |
| 128 | + ) as span: |
| 129 | + set_span_attributes(span, attributes) |
125 | 130 |
|
126 | 131 | try: |
127 | 132 | result = wrapped(*args, **kwargs) |
128 | 133 | if result: |
129 | | - set_span_attribute(span, "dspy.signature.result", str(result)) |
| 134 | + set_span_attribute( |
| 135 | + span, |
| 136 | + "dspy.signature.result", |
| 137 | + json.dumps(result.toDict()), |
| 138 | + ) |
130 | 139 | span.set_status(Status(StatusCode.OK)) |
131 | 140 |
|
132 | 141 | span.end() |
@@ -168,27 +177,41 @@ def traced_method(wrapped, instance, args, kwargs): |
168 | 177 | if hasattr(instance, "devset"): |
169 | 178 | span_attributes["dspy.evaluate.devset"] = str(getattr(instance, "devset")) |
170 | 179 | if hasattr(instance, "trainset"): |
171 | | - span_attributes["dspy.evaluate.display"] = str(getattr(instance, "trainset")) |
| 180 | + span_attributes["dspy.evaluate.display"] = str( |
| 181 | + getattr(instance, "trainset") |
| 182 | + ) |
172 | 183 | if hasattr(instance, "num_threads"): |
173 | | - span_attributes["dspy.evaluate.num_threads"] = str(getattr(instance, "num_threads")) |
| 184 | + span_attributes["dspy.evaluate.num_threads"] = str( |
| 185 | + getattr(instance, "num_threads") |
| 186 | + ) |
174 | 187 | if hasattr(instance, "return_outputs"): |
175 | 188 | span_attributes["dspy.evaluate.return_outputs"] = str( |
176 | 189 | getattr(instance, "return_outputs") |
177 | 190 | ) |
178 | 191 | if hasattr(instance, "display_table"): |
179 | | - span_attributes["dspy.evaluate.display_table"] = str(getattr(instance, "display_table")) |
| 192 | + span_attributes["dspy.evaluate.display_table"] = str( |
| 193 | + getattr(instance, "display_table") |
| 194 | + ) |
180 | 195 | if hasattr(instance, "display_progress"): |
181 | 196 | span_attributes["dspy.evaluate.display_progress"] = str( |
182 | 197 | getattr(instance, "display_progress") |
183 | 198 | ) |
184 | 199 | if hasattr(instance, "metric"): |
185 | | - span_attributes["dspy.evaluate.metric"] = getattr(instance, "metric").__name__ |
| 200 | + span_attributes["dspy.evaluate.metric"] = getattr( |
| 201 | + instance, "metric" |
| 202 | + ).__name__ |
186 | 203 | if hasattr(instance, "error_count"): |
187 | | - span_attributes["dspy.evaluate.error_count"] = str(getattr(instance, "error_count")) |
| 204 | + span_attributes["dspy.evaluate.error_count"] = str( |
| 205 | + getattr(instance, "error_count") |
| 206 | + ) |
188 | 207 | if hasattr(instance, "error_lock"): |
189 | | - span_attributes["dspy.evaluate.error_lock"] = str(getattr(instance, "error_lock")) |
| 208 | + span_attributes["dspy.evaluate.error_lock"] = str( |
| 209 | + getattr(instance, "error_lock") |
| 210 | + ) |
190 | 211 | if hasattr(instance, "max_errors"): |
191 | | - span_attributes["dspy.evaluate.max_errors"] = str(getattr(instance, "max_errors")) |
| 212 | + span_attributes["dspy.evaluate.max_errors"] = str( |
| 213 | + getattr(instance, "max_errors") |
| 214 | + ) |
192 | 215 | if args and len(args) > 0: |
193 | 216 | span_attributes["dspy.evaluate.args"] = str(args) |
194 | 217 |
|
|
0 commit comments