Skip to content

Conversation

@akitaSummer
Copy link
Contributor

@akitaSummer akitaSummer commented Jan 14, 2026

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
Description of change

Summary by CodeRabbit

  • New Features

    • Unified tracing wrapper for graph operations to ensure consistent tracing and runId propagation.
    • Context-aware tracer setup to improve observability when tracing is enabled.
  • Tests

    • Added/updated tests to verify trace ID propagation and logging behavior.
  • Chores

    • Enabled tracer export in test configuration to support tracing tests.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 14, 2026

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Walkthrough

A centralized wrapper (wrapGraphMethod) was added to CompiledStateGraphObject to apply tracing and ensure runId for graph.invoke/graph.stream. getTracer() was added to obtain the tracer from Egg context. Plugin test fixture and a unit test were updated to enable and assert tracing.

Changes

Cohort / File(s) Summary
Graph tracing wrapper
plugin/langchain/lib/graph/CompiledStateGraphObject.ts
Added wrapGraphMethod(originalMethod, input, config) to centralize tracing/runId handling for graph.invoke and graph.stream; added getTracer() to fetch tracer from Egg context; binds wrapped methods on init.
Test fixture - plugin config
plugin/langchain/test/fixtures/apps/langchain/config/plugin.js
Added exports.tracer to enable egg-tracer in test fixture.
Unit test update
plugin/langchain/test/llm.test.ts
Stubs Tracer.prototype.traceId and updates assertions to expect traceId in logs.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Wrapper as wrapGraphMethod
    participant EggCtx as Egg Context
    participant Tracer as LangGraphTracer
    participant Original as Original graph.invoke/stream

    Client->>Wrapper: invoke/stream(input, config)
    activate Wrapper
    Wrapper->>EggCtx: get tracer instance
    EggCtx-->>Wrapper: tracer (if available)
    alt trace-log enabled
        Wrapper->>Tracer: configure tracer & ensure runId in config
    end
    Wrapper->>Original: call originalMethod(input, config)
    activate Original
    Original-->>Wrapper: result
    deactivate Original
    Wrapper-->>Client: result
    deactivate Wrapper
Loading

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly Related PRs

Suggested Reviewers

  • killagu

Poem

🐰 Hop, hop—one wrapper binds the flow,

Tracing trails where runIds go.
No more scattered checks to chase,
One cozy hop unites the trace.
🥕✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: add trace to log' accurately describes the main change: adding tracing capability to log output through the LangGraphTracer integration in CompiledStateGraphObject.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @akitaSummer, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the observability of LangChain graph executions within the Egg.js framework. It introduces a standardized and context-aware mechanism for integrating tracing into the CompiledStateGraphObject's invoke and stream methods. By centralizing the tracing logic and leveraging the application's trace context, this change makes it easier to monitor, debug, and understand the flow of complex LangChain processes.

Highlights

  • Tracing Integration Refactor: The graph.invoke and graph.stream methods within CompiledStateGraphObject have been refactored to use a new wrapGraphMethod. This centralizes the logic for applying tracing callbacks and ensures consistent tracing behavior across graph executions.
  • Context-Aware Tracing: The EGG_CONTEXT is now imported and utilized to retrieve the current trace ID, allowing LangChain graph executions to be properly linked and correlated with the broader application trace.
  • New Tracing Wrapper Method: A new asynchronous method, wrapGraphMethod, has been introduced. It handles the conditional application of LangGraphTracer callbacks and sets the runId for graph invocations based on the active Egg.js trace context.
  • Tracer Plugin Enabled: The egg-tracer plugin has been explicitly enabled in the test fixture configuration, providing the necessary infrastructure for the new tracing functionality.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces tracing for LangChain graphs by wrapping invoke and stream methods. The refactoring to a common wrapGraphMethod is a good improvement for code clarity and maintainability. However, there are a couple of areas for improvement:

  • The implementation of wrapGraphMethod can be made more efficient by caching the LangGraphTracer instance.
  • There's a potential runtime error in getTracer due to unsafe context access.

My review includes specific suggestions to address these points for better performance and robustness. The change to enable the tracer plugin in the test configuration is correct.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
plugin/langchain/lib/graph/CompiledStateGraphObject.ts (1)

40-51: Unused tracer setup in init() — consider removing.

Lines 42-44 fetch a tracer and set its name, but this tracer instance is never used afterward. The wrapGraphMethod creates its own tracer instance when tracing is enabled. This appears to be leftover code from the previous implementation.

🔧 Suggested fix
    const originalInvoke = graph.invoke;
    const originalStream = graph.stream;
-    const langGraphTraceObj = await EggContainerFactory.getOrCreateEggObjectFromName('langGraphTracer');
-    const tracer = langGraphTraceObj.obj as LangGraphTracer;
-    tracer.setName(this.graphName);


    graph.invoke = (input: any, config?: any) =>
      this.wrapGraphMethod(originalInvoke.bind(graph), input, config);

    graph.stream = (input: any, config?: any) =>
      this.wrapGraphMethod(originalStream.bind(graph), input, config);
🤖 Fix all issues with AI agents
In `@plugin/langchain/lib/graph/CompiledStateGraphObject.ts`:
- Around line 147-150: getTracer uses a non-null assertion on
ContextHandler.getContext() and assumes ctx and ctx.tracer exist, which can
throw at runtime; update getTracer to defensively fetch the context (call
ContextHandler.getContext() without "!"), check that the returned context and
its EGG_CONTEXT entry exist, and either return undefined (or a safe default) or
throw a descriptive error if missing, then access and return ctx.tracer only
after those checks; reference the getTracer method, ContextHandler.getContext,
EGG_CONTEXT and ctx.tracer when making the changes.
- Around line 120-145: wrapGraphMethod currently assigns finalConfig = config ||
{} which lets you mutate the caller's config; change it to make a shallow copy
(e.g., const finalConfig = { ...(config || {}) }) so modifications to runId,
callbacks, etc. don't affect the original object, and when you prepend tracer to
finalConfig.callbacks ensure you create a new array (e.g., finalConfig.callbacks
= [tracer, ...(finalConfig.callbacks || [])]) so the original callbacks array is
not mutated; update the logic in wrapGraphMethod (referencing finalConfig,
originalMethod, and callbacks/runId) accordingly.
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between da12489 and 2769c3b.

📒 Files selected for processing (2)
  • plugin/langchain/lib/graph/CompiledStateGraphObject.ts
  • plugin/langchain/test/fixtures/apps/langchain/config/plugin.js
🧰 Additional context used
📓 Path-based instructions (3)
**/test/fixtures/**

📄 CodeRabbit inference engine (CLAUDE.md)

Store test fixtures in 'test/fixtures/' directories within packages

Files:

  • plugin/langchain/test/fixtures/apps/langchain/config/plugin.js
plugin/**/*.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

Framework internal code in plugin packages should import types from '@eggjs/tegg-types' and decorators from '@eggjs/core-decorator'

Files:

  • plugin/langchain/lib/graph/CompiledStateGraphObject.ts
plugin/*/lib/**/*.ts

📄 CodeRabbit inference engine (CLAUDE.md)

Plugin services and lifecycle hooks should be implemented in files within the 'lib/' directory

Files:

  • plugin/langchain/lib/graph/CompiledStateGraphObject.ts
🧠 Learnings (7)
📓 Common learnings
Learnt from: CR
Repo: eggjs/tegg PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-01-10T12:35:11.421Z
Learning: Egg-specific subclasses extending core wrappers should be implemented in plugin packages and add context tracking and tegg-specific behavior
📚 Learning: 2026-01-10T12:35:11.421Z
Learnt from: CR
Repo: eggjs/tegg PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-01-10T12:35:11.421Z
Learning: Use 'eggjs/tegg/helper' for internal APIs like ModuleConfigUtil, LoaderFactory, and EggObjectLifeCycleContext in advanced usage scenarios

Applied to files:

  • plugin/langchain/test/fixtures/apps/langchain/config/plugin.js
  • plugin/langchain/lib/graph/CompiledStateGraphObject.ts
📚 Learning: 2026-01-10T12:35:11.421Z
Learnt from: CR
Repo: eggjs/tegg PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-01-10T12:35:11.421Z
Learning: Create subpath exports in 'eggjs/tegg' for optional features (aop, orm, dal, schedule, transaction, ajv, helper, standalone)

Applied to files:

  • plugin/langchain/test/fixtures/apps/langchain/config/plugin.js
📚 Learning: 2026-01-10T12:35:11.421Z
Learnt from: CR
Repo: eggjs/tegg PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-01-10T12:35:11.421Z
Learning: Applies to core/tegg/index.ts : Re-export core decorator, runtime, and type functionality from the main 'eggjs/tegg' package index.ts

Applied to files:

  • plugin/langchain/test/fixtures/apps/langchain/config/plugin.js
  • plugin/langchain/lib/graph/CompiledStateGraphObject.ts
📚 Learning: 2026-01-10T12:35:11.421Z
Learnt from: CR
Repo: eggjs/tegg PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-01-10T12:35:11.421Z
Learning: Always import decorators, enums, and public APIs from 'eggjs/tegg' in application code

Applied to files:

  • plugin/langchain/test/fixtures/apps/langchain/config/plugin.js
  • plugin/langchain/lib/graph/CompiledStateGraphObject.ts
📚 Learning: 2026-01-10T12:35:11.421Z
Learnt from: CR
Repo: eggjs/tegg PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-01-10T12:35:11.421Z
Learning: Applies to core/**/*.{ts,tsx} : Framework internal code in core packages should import types from 'eggjs/tegg-types' and decorators from 'eggjs/core-decorator'

Applied to files:

  • plugin/langchain/lib/graph/CompiledStateGraphObject.ts
📚 Learning: 2026-01-10T12:35:11.421Z
Learnt from: CR
Repo: eggjs/tegg PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-01-10T12:35:11.421Z
Learning: Applies to plugin/**/*.{ts,tsx} : Framework internal code in plugin packages should import types from 'eggjs/tegg-types' and decorators from 'eggjs/core-decorator'

Applied to files:

  • plugin/langchain/lib/graph/CompiledStateGraphObject.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Runner-macos (20)
  • GitHub Check: Runner-macos (16)
  • GitHub Check: Runner-ubuntu (18)
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Runner-ubuntu (20)
  • GitHub Check: Runner-ubuntu (16)
  • GitHub Check: Analyze (typescript)
  • GitHub Check: Runner-macos (18)
🔇 Additional comments (2)
plugin/langchain/test/fixtures/apps/langchain/config/plugin.js (1)

31-35: LGTM!

The tracer plugin configuration follows the established pattern of other exports in this file and correctly enables egg-tracer for testing the new tracing functionality. As per coding guidelines, this test fixture is appropriately placed in the test/fixtures/ directory.

plugin/langchain/lib/graph/CompiledStateGraphObject.ts (1)

14-14: LGTM!

Import of EGG_CONTEXT from @eggjs/egg-module-common is appropriate for accessing the Egg context in the new getTracer() method.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@plugin/langchain/test/llm.test.ts`:
- Line 4: Replace the fragile internal import of Tracer by changing the import
statement that currently reads import Tracer from 'egg-tracer/lib/tracer' to
import Tracer from 'egg-tracer' so the test uses the public API (update the
import for the Tracer symbol in llm.test.ts).
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2769c3b and 6aa47e1.

📒 Files selected for processing (1)
  • plugin/langchain/test/llm.test.ts
🧰 Additional context used
📓 Path-based instructions (3)
plugin/**/*.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

Framework internal code in plugin packages should import types from '@eggjs/tegg-types' and decorators from '@eggjs/core-decorator'

Files:

  • plugin/langchain/test/llm.test.ts
**/test/**/*.{test,spec}.{ts,js}

📄 CodeRabbit inference engine (CLAUDE.md)

Test files should be located in 'test/' directories within each package and use Mocha

Files:

  • plugin/langchain/test/llm.test.ts
plugin/**/*.test.{ts,js}

📄 CodeRabbit inference engine (CLAUDE.md)

Use 'egg-mock' with 'app.mockModuleContextScope()' for testing plugin modules in isolation

Files:

  • plugin/langchain/test/llm.test.ts
🧠 Learnings (2)
📚 Learning: 2026-01-10T12:35:11.421Z
Learnt from: CR
Repo: eggjs/tegg PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-01-10T12:35:11.421Z
Learning: Applies to plugin/**/*.test.{ts,js} : Use 'egg-mock' with 'app.mockModuleContextScope()' for testing plugin modules in isolation

Applied to files:

  • plugin/langchain/test/llm.test.ts
📚 Learning: 2026-01-10T12:35:11.421Z
Learnt from: CR
Repo: eggjs/tegg PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-01-10T12:35:11.421Z
Learning: Create subpath exports in 'eggjs/tegg' for optional features (aop, orm, dal, schedule, transaction, ajv, helper, standalone)

Applied to files:

  • plugin/langchain/test/llm.test.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Runner-ubuntu (16)
  • GitHub Check: Runner-macos (20)
  • GitHub Check: Runner-macos (18)
  • GitHub Check: Runner-macos (16)
  • GitHub Check: Runner-ubuntu (20)
  • GitHub Check: Runner-ubuntu (18)
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Analyze (typescript)
🔇 Additional comments (1)
plugin/langchain/test/llm.test.ts (1)

74-83: LGTM!

The test correctly:

  • Captures logs with app.mockLog()
  • Stubs Tracer.prototype.traceId to provide a deterministic trace ID
  • Asserts the traceId appears in the log output

The mock is properly cleaned up by the existing mm.restore() in afterEach. This effectively validates the new tracing integration.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

Copy link
Contributor

@killagu killagu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@akitaSummer akitaSummer merged commit bcfb895 into master Jan 14, 2026
12 checks passed
@akitaSummer akitaSummer deleted the feat/langchain-tracer branch January 14, 2026 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants