-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlanggraph_instrumentor.py
More file actions
687 lines (567 loc) · 24.4 KB
/
Copy pathlanggraph_instrumentor.py
File metadata and controls
687 lines (567 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
from __future__ import annotations
import functools
import inspect
import os
import uuid
from copy import deepcopy
from datetime import datetime, timezone
from typing import Any, Mapping, Optional, Sequence, Tuple, Dict
from langgraph.graph import StateGraph
from ..interfaces import NodeExecution
from ..digitalocean_tracker import DigitalOceanTracesTracker
from ..network_interceptor import (
get_network_interceptor,
is_inference_url,
is_kbaas_url,
)
WRAPPED_FLAG = "__do_wrapped__"
def _utc() -> datetime:
return datetime.now(timezone.utc)
def _mk_exec(name: str, inputs: Any) -> NodeExecution:
return NodeExecution(
node_id=str(uuid.uuid4()),
node_name=name,
framework="langgraph",
start_time=_utc(),
inputs=inputs,
)
def _ensure_meta(rec: NodeExecution) -> dict:
md = getattr(rec, "metadata", None)
if not isinstance(md, dict):
md = {}
try:
rec.metadata = md
except Exception:
pass
return md
_MAX_DEPTH = 3
_MAX_ITEMS = 100 # keep payloads bounded
def _freeze(obj: Any, depth: int = _MAX_DEPTH) -> Any:
"""Mutation-safe, JSON-ish snapshot for arbitrary Python objects."""
# if depth < 0:
# return "<max-depth>"
if obj is None or isinstance(obj, (str, int, float, bool)):
return obj
# dict-like
if isinstance(obj, Mapping):
out: Dict[str, Any] = {}
for i, (k, v) in enumerate(obj.items()):
if i >= _MAX_ITEMS:
out["<truncated>"] = True
break
out[str(k)] = _freeze(v, depth - 1)
return out
# sequences
if isinstance(obj, (list, tuple, set)):
seq = list(obj)
out = []
for i, v in enumerate(seq):
if i >= _MAX_ITEMS:
out.append("<truncated>")
break
out.append(_freeze(v, depth - 1))
return out
# pydantic
try:
from pydantic import BaseModel # type: ignore
if isinstance(obj, BaseModel):
return _freeze(obj.model_dump(), depth - 1)
except Exception:
pass
# dataclass
try:
import dataclasses
if dataclasses.is_dataclass(obj):
return _freeze(dataclasses.asdict(obj), depth - 1)
except Exception:
pass
# fallback
return repr(obj)
def _snapshot_args_kwargs(a: Tuple[Any, ...], kw: Dict[str, Any]) -> Any:
"""Deepcopy then freeze to avoid mutation surprises."""
try:
a_copy = deepcopy(a)
kw_copy = deepcopy(kw)
except Exception:
a_copy, kw_copy = a, kw # best-effort
# If there's exactly one arg and no kwargs, return just that arg
if len(a_copy) == 1 and not kw_copy:
return _freeze(a_copy[0])
# If there are kwargs but no args, return just the kwargs
if not a_copy and kw_copy:
return _freeze(kw_copy)
# If there are multiple args or both args and kwargs, return a dict
if a_copy and kw_copy:
return {"args": _freeze(a_copy), "kwargs": _freeze(kw_copy)}
elif len(a_copy) > 1:
return _freeze(a_copy)
# Fallback
return _freeze(a_copy)
def _diff(a: Any, b: Any, depth: int = 2) -> Any:
"""Small, generic diff for dicts/lists/tuples; returns None if identical."""
# if depth < 0:
# return "<max-depth>"
# dict diff
if isinstance(a, dict) and isinstance(b, dict):
keys = list(set(a.keys()) | set(b.keys()))
keys.sort(key=str)
out: Dict[str, Any] = {}
count = 0
for k in keys:
if count >= _MAX_ITEMS:
out["<truncated_keys>"] = True
break
av = a.get(k, "<missing>")
bv = b.get(k, "<missing>")
if av == bv:
continue
if isinstance(av, (dict, list, tuple)) and isinstance(
bv, (dict, list, tuple)
):
sub = _diff(av, bv, depth - 1)
out[k] = sub if sub is not None else {"before": av, "after": bv}
else:
out[k] = {"before": av, "after": bv}
count += 1
return out or None
# list/tuple diff
if isinstance(a, (list, tuple)) and isinstance(b, (list, tuple)):
length = max(len(a), len(b))
changed = False
out_list = []
for i in range(min(length, _MAX_ITEMS)):
av = a[i] if i < len(a) else "<missing>"
bv = b[i] if i < len(b) else "<missing>"
if av == bv:
out_list.append("<same>")
else:
if isinstance(av, (dict, list, tuple)) and isinstance(
bv, (dict, list, tuple)
):
sub = _diff(av, bv, depth - 1)
out_list.append(
sub if sub is not None else {"before": av, "after": bv}
)
else:
out_list.append({"before": av, "after": bv})
changed = True
if length > _MAX_ITEMS:
out_list.append("<truncated>")
return out_list if changed else None
return None if a == b else {"before": a, "after": b}
def _first_arg_after(a: Tuple[Any, ...]) -> Optional[Any]:
return a[0] if (a and isinstance(a[0], dict)) else None
def _first_arg_before(before_inputs: dict) -> Optional[Any]:
try:
args = before_inputs.get("args")
if isinstance(args, list) and args:
return args[0]
except Exception:
pass
return None
def _canonical_output(
before_inputs: dict, a: Tuple[Any, ...], kw: Dict[str, Any], ret: Any
) -> Any:
"""
Choose a single, compact output:
1) If ret is a mapping -> return snapshot(ret)
2) Else if first arg is a dict and appears changed -> snapshot(first arg)
3) Else -> snapshot(ret)
"""
if isinstance(ret, Mapping):
return _freeze(ret)
arg0_before = _first_arg_before(before_inputs)
arg0_after = _first_arg_after(a)
if isinstance(arg0_after, dict):
arg0_after_frozen = _freeze(arg0_after)
if not isinstance(arg0_before, dict) or arg0_before != arg0_after_frozen:
return arg0_after_frozen
return _freeze(ret)
def _snap():
intr = get_network_interceptor()
try:
tok = intr.snapshot_token()
except Exception:
tok = 0
return intr, tok
def _had_hits_since(intr, token) -> bool:
try:
return intr.hits_since(token) > 0
except Exception:
return False
def _get_captured_payloads_with_type(intr, token) -> tuple:
"""Get captured API request/response payloads and classify the call type.
Returns:
(request_payload, response_payload, is_llm, is_retriever)
"""
try:
captured = intr.get_captured_requests_since(token)
if captured:
# Use the first captured request (most common case)
call = captured[0]
url = call.url
is_llm = is_inference_url(url)
is_retriever = is_kbaas_url(url)
return call.request_payload, call.response_payload, is_llm, is_retriever
except Exception:
pass
return None, None, False, False
def _transform_kbaas_response(response: Optional[Dict[str, Any]]) -> Optional[list]:
"""Transform KBaaS response to standard retriever format.
Extracts results and converts 'text_content' to 'page_content'.
Returns a list of dicts as expected for retriever spans.
"""
if not isinstance(response, dict):
return response
results = response.get("results", [])
if not isinstance(results, list):
return response
transformed_results = []
for item in results:
if isinstance(item, dict) and "text_content" in item:
new_item = dict(item)
new_item["page_content"] = new_item.pop("text_content")
transformed_results.append(new_item)
else:
transformed_results.append(item)
# Return just the array of results
return transformed_results
class LangGraphInstrumentor:
"""Wraps LangGraph nodes with tracing."""
def __init__(self) -> None:
self._installed = False
self._tracker: Optional[DigitalOceanTracesTracker] = None
def install(self, tracker: DigitalOceanTracesTracker) -> None:
if self._installed:
return
self._tracker = tracker
original_add_node = StateGraph.add_node
t = tracker # close over
def _start(node_name: str, a: Tuple[Any, ...], kw: Dict[str, Any]):
inputs_snapshot = _snapshot_args_kwargs(a, kw)
rec = _mk_exec(node_name, inputs_snapshot)
intr, tok = _snap()
t.on_node_start(rec)
return rec, inputs_snapshot, intr, tok
def _finish_ok(
rec: NodeExecution,
inputs_snapshot: dict,
a: Tuple[Any, ...],
kw: Dict[str, Any],
ret: Any,
intr,
tok,
):
# NOTE: Async generators should be handled by the wrapper functions
# (_wrap_async_func, _wrap_sync_func, etc.) BEFORE calling _finish_ok.
# The wrappers collect streamed content and pass {"content": "..."} here.
# Check if this node made any tracked API calls (e.g., LLM inference or KBaaS retrieval)
if _had_hits_since(intr, tok):
# Get captured payloads and classify the call type
api_request, api_response, is_llm, is_retriever = (
_get_captured_payloads_with_type(intr, tok)
)
# Set metadata based on call type
meta = _ensure_meta(rec)
if is_llm:
meta["is_llm_call"] = True
elif is_retriever:
meta["is_retriever_call"] = True
else:
# Fallback: assume LLM call for backward compatibility
meta["is_llm_call"] = True
if api_request or api_response:
# Use actual API payloads instead of function args
if api_request:
rec.inputs = _freeze(api_request)
# Use actual API response as output
if api_response:
# Transform KBaaS response to standard retriever format
if is_retriever:
api_response = _transform_kbaas_response(api_response)
out_payload = _freeze(api_response)
else:
out_payload = _canonical_output(inputs_snapshot, a, kw, ret)
else:
out_payload = _canonical_output(inputs_snapshot, a, kw, ret)
else:
out_payload = _canonical_output(inputs_snapshot, a, kw, ret)
t.on_node_end(rec, out_payload)
def _finish_err(rec: NodeExecution, intr, tok, e: BaseException):
if _had_hits_since(intr, tok):
# Get captured payloads and classify the call type
api_request, _, is_llm, is_retriever = _get_captured_payloads_with_type(
intr, tok
)
# Set metadata based on call type
meta = _ensure_meta(rec)
if is_llm:
meta["is_llm_call"] = True
elif is_retriever:
meta["is_retriever_call"] = True
else:
# Fallback: assume LLM call for backward compatibility
meta["is_llm_call"] = True
if api_request:
rec.inputs = _freeze(api_request)
t.on_node_error(rec, e)
def _wrap_async_func(node_name: str, func):
@functools.wraps(func)
async def _wrapped(*a, **kw):
rec, snap, intr, tok = _start(node_name, a, kw)
try:
ret = await func(*a, **kw)
# If ret is an async generator, we need to wrap it to collect
# content and defer _finish_ok until the stream is consumed
if ret is not None and (
hasattr(ret, "__aiter__") or inspect.isasyncgen(ret)
):
async def _streaming_wrapper(gen):
import json
collected: list[str] = []
try:
async for chunk in gen:
# Convert chunk to string for collection
if isinstance(chunk, bytes):
chunk_str = chunk.decode(
"utf-8", errors="replace"
)
elif isinstance(chunk, dict):
chunk_str = json.dumps(chunk)
elif chunk is None:
continue
else:
chunk_str = str(chunk)
collected.append(chunk_str)
yield chunk
# Stream complete - finalize with collected content
_finish_ok(
rec,
snap,
a,
kw,
{"content": "".join(collected)},
intr,
tok,
)
except BaseException as e:
_finish_err(rec, intr, tok, e)
raise
return _streaming_wrapper(ret)
# Non-streaming: finalize immediately
_finish_ok(rec, snap, a, kw, ret, intr, tok)
return ret
except BaseException as e:
_finish_err(rec, intr, tok, e)
raise
setattr(_wrapped, WRAPPED_FLAG, True)
return _wrapped
def _wrap_sync_func(node_name: str, func):
@functools.wraps(func)
def _wrapped(*a, **kw):
rec, snap, intr, tok = _start(node_name, a, kw)
try:
ret = func(*a, **kw)
# If ret is an async generator, we need to wrap it to collect
# content and defer _finish_ok until the stream is consumed
if ret is not None and (
hasattr(ret, "__aiter__") or inspect.isasyncgen(ret)
):
async def _streaming_wrapper(gen):
import json
collected: list[str] = []
try:
async for chunk in gen:
# Convert chunk to string for collection
if isinstance(chunk, bytes):
chunk_str = chunk.decode(
"utf-8", errors="replace"
)
elif isinstance(chunk, dict):
chunk_str = json.dumps(chunk)
elif chunk is None:
continue
else:
chunk_str = str(chunk)
collected.append(chunk_str)
yield chunk
# Stream complete - finalize with collected content
_finish_ok(
rec,
snap,
a,
kw,
{"content": "".join(collected)},
intr,
tok,
)
except BaseException as e:
_finish_err(rec, intr, tok, e)
raise
return _streaming_wrapper(ret)
# Non-streaming: finalize immediately
_finish_ok(rec, snap, a, kw, ret, intr, tok)
return ret
except BaseException as e:
_finish_err(rec, intr, tok, e)
raise
setattr(_wrapped, WRAPPED_FLAG, True)
return _wrapped
def _wrap_async_gen(node_name: str, func):
@functools.wraps(func)
async def _wrapped(*a, **kw):
rec, snap, intr, tok = _start(node_name, a, kw)
try:
# Accumulate a compact, canonical final payload
# (string: concatenate; list: extend; else: last write wins)
acc: Dict[str, Any] = {}
async for chunk in func(*a, **kw):
# Merge into acc for the final on_node_end payload
for k, v in chunk.items():
if isinstance(v, str):
acc[k] = acc.get(k, "") + v
elif isinstance(v, bytes):
acc[k] = acc.get(k, b"") + v
elif isinstance(v, list):
acc.setdefault(k, []).extend(v)
else:
acc[k] = v
# Pass the live chunk downstream unchanged
yield chunk
# Finish the span with the aggregated mapping
_finish_ok(rec, snap, a, kw, acc, intr, tok)
except BaseException as e:
_finish_err(rec, intr, tok, e)
raise
setattr(_wrapped, WRAPPED_FLAG, True)
return _wrapped
def _wrap_runnable_ainvoke(node_name: str, runnable):
async def _wrapped(*a, **kw):
rec, snap, intr, tok = _start(node_name, a, kw)
try:
ret = await runnable.ainvoke(*a, **kw)
# If ret is an async generator, wrap it to collect content
if ret is not None and (
hasattr(ret, "__aiter__") or inspect.isasyncgen(ret)
):
async def _streaming_wrapper(gen):
import json
collected: list[str] = []
try:
async for chunk in gen:
if isinstance(chunk, bytes):
chunk_str = chunk.decode(
"utf-8", errors="replace"
)
elif isinstance(chunk, dict):
chunk_str = json.dumps(chunk)
elif chunk is None:
continue
else:
chunk_str = str(chunk)
collected.append(chunk_str)
yield chunk
_finish_ok(
rec,
snap,
a,
kw,
{"content": "".join(collected)},
intr,
tok,
)
except BaseException as e:
_finish_err(rec, intr, tok, e)
raise
return _streaming_wrapper(ret)
_finish_ok(rec, snap, a, kw, ret, intr, tok)
return ret
except BaseException as e:
_finish_err(rec, intr, tok, e)
raise
setattr(_wrapped, WRAPPED_FLAG, True)
return _wrapped
def _wrap_runnable_invoke(node_name: str, runnable):
def _wrapped(*a, **kw):
rec, snap, intr, tok = _start(node_name, a, kw)
try:
ret = runnable.invoke(*a, **kw)
# If ret is an async generator, wrap it to collect content
if ret is not None and (
hasattr(ret, "__aiter__") or inspect.isasyncgen(ret)
):
async def _streaming_wrapper(gen):
import json
collected: list[str] = []
try:
async for chunk in gen:
if isinstance(chunk, bytes):
chunk_str = chunk.decode(
"utf-8", errors="replace"
)
elif isinstance(chunk, dict):
chunk_str = json.dumps(chunk)
elif chunk is None:
continue
else:
chunk_str = str(chunk)
collected.append(chunk_str)
yield chunk
_finish_ok(
rec,
snap,
a,
kw,
{"content": "".join(collected)},
intr,
tok,
)
except BaseException as e:
_finish_err(rec, intr, tok, e)
raise
return _streaming_wrapper(ret)
_finish_ok(rec, snap, a, kw, ret, intr, tok)
return ret
except BaseException as e:
_finish_err(rec, intr, tok, e)
raise
setattr(_wrapped, WRAPPED_FLAG, True)
return _wrapped
def wrap_callable(node_name: str, func: Any):
if getattr(func, WRAPPED_FLAG, False):
return func
# Runnable-like objects
if hasattr(func, "ainvoke"):
return _wrap_runnable_ainvoke(node_name, func)
if hasattr(func, "invoke"):
return _wrap_runnable_invoke(node_name, func)
# Functions
if inspect.isasyncgenfunction(func):
return _wrap_async_gen(node_name, func)
if inspect.iscoroutinefunction(func):
return _wrap_async_func(node_name, func)
if inspect.isfunction(func):
return _wrap_sync_func(node_name, func)
# Unknown type -> leave untouched
return func
def wrapped_add_node(graph_self, *args, **kwargs):
# Handle both call signatures:
# 1. add_node(func) - single arg
# 2. add_node(name, func) - two args
if len(args) == 1:
func = args[0]
# Infer name from function
name = getattr(func, "__name__", str(func))
wrapped_func = wrap_callable(name, func)
return original_add_node(graph_self, wrapped_func, **kwargs)
elif len(args) >= 2:
name = args[0]
func = args[1]
wrapped_func = wrap_callable(name, func)
return original_add_node(
graph_self, name, wrapped_func, *args[2:], **kwargs
)
# Fallback for edge cases
return original_add_node(graph_self, *args, **kwargs)
StateGraph.add_node = wrapped_add_node
self._installed = True