Skip to content

This PR wires Mastra tracing to persistent PostgreSQL storage for local development #73

Open
MabudAlam wants to merge 2 commits into
tinyfish-io:mainfrom
MabudAlam:add-traces
Open

This PR wires Mastra tracing to persistent PostgreSQL storage for local development #73
MabudAlam wants to merge 2 commits into
tinyfish-io:mainfrom
MabudAlam:add-traces

Conversation

@MabudAlam
Copy link
Copy Markdown
Contributor

@MabudAlam MabudAlam commented May 23, 2026

Issue : #71

What functionality was added

  • Enabled Mastra trace export (MastraStorageExporter) in backend workflows.
  • Switched Mastra storage to PostgreSQL (PostgresStore) so trace data is persisted.

Why this is helpful

  • You can inspect trace/span data for each run and identify failures faster.
  • Trace data survives service restarts because it’s stored in Postgres.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 23, 2026

Review Change Stack

Caution

Review failed

Failed to post review comments

📝 Walkthrough

Walkthrough

Adds Mastra runtime integration: new runtime dependencies, MASTRA_DATABASE_URL in docker-compose, a PostgresStore using that URL, and an Observability instance wired into the exported Mastra singleton. The backend index imports mastra and calls mastra.observability.getDefaultInstance()?.flush() in the /populate route error path.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant Backend
  participant MastraRuntime
  participant PostgresDB
  participant MastraObservability
  Client->>Backend: POST /populate
  Backend->>MastraRuntime: invoke populate workflow
  MastraRuntime->>PostgresDB: read/write workflow state
  MastraRuntime->>MastraObservability: emit spans/metrics
  alt error path
    Backend->>MastraObservability: getDefaultInstance()?.flush()
    Backend->>Client: return error response
  else success
    Backend->>Client: return success response
  end
Loading

Possibly related PRs

  • tinyfish-io/bigset#83: Also modifies backend/src/mastra/index.ts to wire updateWorkflow into the Mastra registry and touches populate/update workflow integration.

Suggested reviewers

  • simantak-dabhade
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The PR title accurately describes the main change: wiring Mastra tracing to persistent PostgreSQL storage for local development, which aligns with the changeset modifications.
Description check ✅ Passed The PR description is directly related to the changeset, explaining what functionality was added and why it is helpful with clear, relevant details.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Copy link
Copy Markdown
Contributor

@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

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@backend/src/mastra/index.ts`:
- Around line 12-15: The code uses a non-null assertion for
process.env.MASTRA_DATABASE_URL when constructing PostgresStore (storage), which
can pass undefined and produce cryptic runtime errors; replace that with an
explicit validation: read and validate MASTRA_DATABASE_URL (either in env.ts
alongside other vars or at the top of this module), throw a clear startup Error
if it's missing, and then pass the validated string into new PostgresStore({ id:
'mastra-pg-storage', connectionString: validatedMastraDatabaseUrl }); ensure you
remove the trailing "!" and reference the validated variable when creating
storage.

In `@docker-compose.dev.yml`:
- Around line 58-61: The mastra service is missing a dependency on the db
service even though MASTRA_DATABASE_URL points at PostgreSQL; update the mastra
service's depends_on block to include db with condition: service_healthy (in
addition to the existing convex dependency) so Docker Compose waits for the db
to be healthy before starting mastra.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 837453a3-38ae-4990-b738-49527951570d

📥 Commits

Reviewing files that changed from the base of the PR and between 9f8d5cd and ff069f3.

📒 Files selected for processing (4)
  • backend/package.json
  • backend/src/index.ts
  • backend/src/mastra/index.ts
  • docker-compose.dev.yml

Comment on lines +12 to +15
const storage = new PostgresStore({
id: 'mastra-pg-storage',
connectionString: process.env.MASTRA_DATABASE_URL!,
})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Missing validation for MASTRA_DATABASE_URL environment variable.

The non-null assertion (!) on process.env.MASTRA_DATABASE_URL will pass undefined to PostgresStore if the variable is missing, likely causing a cryptic connection error at runtime rather than a clear startup failure.

Consider validating this in env.ts alongside other required variables, or add a guard here:

Proposed fix
+const mastraDbUrl = process.env.MASTRA_DATABASE_URL;
+if (!mastraDbUrl) {
+  throw new Error('MASTRA_DATABASE_URL environment variable is required');
+}
+
 const storage = new PostgresStore({
   id: 'mastra-pg-storage',
-  connectionString: process.env.MASTRA_DATABASE_URL!,
+  connectionString: mastraDbUrl,
 })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const storage = new PostgresStore({
id: 'mastra-pg-storage',
connectionString: process.env.MASTRA_DATABASE_URL!,
})
const mastraDbUrl = process.env.MASTRA_DATABASE_URL;
if (!mastraDbUrl) {
throw new Error('MASTRA_DATABASE_URL environment variable is required');
}
const storage = new PostgresStore({
id: 'mastra-pg-storage',
connectionString: mastraDbUrl,
})
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/src/mastra/index.ts` around lines 12 - 15, The code uses a non-null
assertion for process.env.MASTRA_DATABASE_URL when constructing PostgresStore
(storage), which can pass undefined and produce cryptic runtime errors; replace
that with an explicit validation: read and validate MASTRA_DATABASE_URL (either
in env.ts alongside other vars or at the top of this module), throw a clear
startup Error if it's missing, and then pass the validated string into new
PostgresStore({ id: 'mastra-pg-storage', connectionString:
validatedMastraDatabaseUrl }); ensure you remove the trailing "!" and reference
the validated variable when creating storage.

Comment thread docker-compose.dev.yml
Comment on lines +58 to 61
MASTRA_DATABASE_URL: postgresql://bigset:bigset@db:5432/bigset_internal
depends_on:
convex:
condition: service_healthy
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

mastra service should depend on db for PostgreSQL connectivity.

The mastra service uses MASTRA_DATABASE_URL pointing to the db service, but only declares a dependency on convex. If mastra starts before db is healthy, the PostgreSQL connection will fail.

Proposed fix
     depends_on:
+      db:
+        condition: service_healthy
       convex:
         condition: service_healthy
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
MASTRA_DATABASE_URL: postgresql://bigset:bigset@db:5432/bigset_internal
depends_on:
convex:
condition: service_healthy
MASTRA_DATABASE_URL: postgresql://bigset:bigset@db:5432/bigset_internal
depends_on:
db:
condition: service_healthy
convex:
condition: service_healthy
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docker-compose.dev.yml` around lines 58 - 61, The mastra service is missing a
dependency on the db service even though MASTRA_DATABASE_URL points at
PostgreSQL; update the mastra service's depends_on block to include db with
condition: service_healthy (in addition to the existing convex dependency) so
Docker Compose waits for the db to be healthy before starting mastra.

Copy link
Copy Markdown
Collaborator

@giaphutran12 giaphutran12 left a comment

Choose a reason for hiding this comment

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

Holding for launch safety. This branch is stale and merge-conflicting with latest main, and only CodeRabbit is reported. Please update it onto current main and rerun the normal checks before review.

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.

2 participants