Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(langchain): auto-instrument with langgraph #12208

Merged
merged 25 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions ddtrace/contrib/internal/langchain/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ def traced_llm_generate(langchain, pin, func, instance, args, kwargs):
api_key=_extract_api_key(instance),
)
completions = None

integration.record_instance(instance, span)

try:
if integration.is_pc_sampled_span(span):
for idx, prompt in enumerate(prompts):
Expand Down Expand Up @@ -231,6 +234,9 @@ async def traced_llm_agenerate(langchain, pin, func, instance, args, kwargs):
model=model,
api_key=_extract_api_key(instance),
)

integration.record_instance(instance, span)

completions = None
try:
if integration.is_pc_sampled_span(span):
Expand Down Expand Up @@ -282,6 +288,9 @@ def traced_chat_model_generate(langchain, pin, func, instance, args, kwargs):
model=_extract_model_name(instance),
api_key=_extract_api_key(instance),
)

integration.record_instance(instance, span)

chat_completions = None
try:
for message_set_idx, message_set in enumerate(chat_messages):
Expand Down Expand Up @@ -372,6 +381,9 @@ async def traced_chat_model_agenerate(langchain, pin, func, instance, args, kwar
model=_extract_model_name(instance),
api_key=_extract_api_key(instance),
)

integration.record_instance(instance, span)

chat_completions = None
try:
for message_set_idx, message_set in enumerate(chat_messages):
Expand Down Expand Up @@ -469,6 +481,9 @@ def traced_embedding(langchain, pin, func, instance, args, kwargs):
model=_extract_model_name(instance),
api_key=_extract_api_key(instance),
)

integration.record_instance(instance, span)

embeddings = None
try:
if isinstance(input_texts, str):
Expand Down Expand Up @@ -520,6 +535,9 @@ def traced_lcel_runnable_sequence(langchain, pin, func, instance, args, kwargs):
)
inputs = None
final_output = None

integration.record_instance(instance, span)

try:
try:
inputs = get_argument_value(args, kwargs, 0, "input")
Expand Down Expand Up @@ -564,6 +582,9 @@ async def traced_lcel_runnable_sequence_async(langchain, pin, func, instance, ar
)
inputs = None
final_output = None

integration.record_instance(instance, span)

try:
try:
inputs = get_argument_value(args, kwargs, 0, "input")
Expand Down Expand Up @@ -608,6 +629,9 @@ def traced_similarity_search(langchain, pin, func, instance, args, kwargs):
provider=provider,
api_key=_extract_api_key(instance),
)

integration.record_instance(instance, span)

documents = []
try:
if integration.is_pc_sampled_span(span):
Expand Down Expand Up @@ -655,6 +679,7 @@ def traced_chain_stream(langchain, pin, func, instance, args, kwargs):
integration: LangChainIntegration = langchain._datadog_integration

def _on_span_started(span: Span):
integration.record_instance(instance, span)
sabrenner marked this conversation as resolved.
Show resolved Hide resolved
inputs = get_argument_value(args, kwargs, 0, "input")
if not integration.is_pc_sampled_span(span):
return
Expand Down Expand Up @@ -712,6 +737,7 @@ def traced_chat_stream(langchain, pin, func, instance, args, kwargs):
model = _extract_model_name(instance)

def _on_span_started(span: Span):
integration.record_instance(instance, span)
if not integration.is_pc_sampled_span(span):
return
chat_messages = get_argument_value(args, kwargs, 0, "input")
Expand Down Expand Up @@ -771,6 +797,7 @@ def traced_llm_stream(langchain, pin, func, instance, args, kwargs):
model = _extract_model_name(instance)

def _on_span_start(span: Span):
integration.record_instance(instance, span)
if not integration.is_pc_sampled_span(span):
return
inp = get_argument_value(args, kwargs, 0, "input")
Expand Down Expand Up @@ -818,6 +845,8 @@ def traced_base_tool_invoke(langchain, pin, func, instance, args, kwargs):
submit_to_llmobs=True,
)

integration.record_instance(instance, span)

tool_output = None
tool_info = {}
try:
Expand Down Expand Up @@ -869,6 +898,8 @@ async def traced_base_tool_ainvoke(langchain, pin, func, instance, args, kwargs)
submit_to_llmobs=True,
)

integration.record_instance(instance, span)

tool_output = None
tool_info = {}
try:
Expand Down
8 changes: 8 additions & 0 deletions ddtrace/contrib/internal/langgraph/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def traced_pregel_stream(langgraph, pin, func, instance, args, kwargs):
result = func(*args, **kwargs)
except Exception:
span.set_exc_info(*sys.exc_info())
integration.llmobs_set_tags(span, args=args, kwargs={**kwargs, "name": name}, response=None, operation="graph")
span.finish()
raise

Expand All @@ -139,6 +140,9 @@ def _stream():
break
except Exception:
span.set_exc_info(*sys.exc_info())
integration.llmobs_set_tags(
sabrenner marked this conversation as resolved.
Show resolved Hide resolved
span, args=args, kwargs={**kwargs, "name": name}, response=None, operation="graph"
)
span.finish()
raise

Expand All @@ -160,6 +164,7 @@ def traced_pregel_astream(langgraph, pin, func, instance, args, kwargs):
result = func(*args, **kwargs)
except Exception:
span.set_exc_info(*sys.exc_info())
integration.llmobs_set_tags(span, args=args, kwargs={**kwargs, "name": name}, response=None, operation="graph")
span.finish()
raise

Expand All @@ -178,6 +183,9 @@ async def _astream():
break
except Exception:
span.set_exc_info(*sys.exc_info())
integration.llmobs_set_tags(
span, args=args, kwargs={**kwargs, "name": name}, response=None, operation="graph"
)
span.finish()
raise

Expand Down
6 changes: 6 additions & 0 deletions ddtrace/llmobs/_integrations/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def __init__(self, integration_config: IntegrationConfig) -> None:
self.start_log_writer()
self._llmobs_pc_sampler = RateSampler(sample_rate=config._llmobs_sample_rate)

@property
def span_linking_enabled(self) -> bool:
return asbool(os.getenv("_DD_LLMOBS_AUTO_SPAN_LINKING_ENABLED", "false")) or asbool(
os.getenv("_DD_TRACE_LANGGRAPH_ENABLED", "false")
)

@property
def metrics_enabled(self) -> bool:
"""
Expand Down
Loading
Loading