Skip to content

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

Open
MabudAlam wants to merge 1 commit 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 1 commit into
tinyfish-io:mainfrom
MabudAlam:add-traces

Conversation

@MabudAlam
Copy link
Copy Markdown

@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

coderabbitai Bot commented May 23, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

This PR integrates the Mastra workflow runtime engine into the backend with persistent storage and observability. The Mastra instance is now configured with a PostgreSQL backend store and observability exporter. The /populate route handler is refactored to invoke workflows through the Mastra runtime via mastra.getWorkflow() and flushed observability on both success and error paths. Required npm packages and Docker Compose environment variables and volume mounts are added to enable database connectivity.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: wiring Mastra tracing to PostgreSQL storage, which is reflected across all modified files including the Mastra config, index.ts updates, and docker-compose changes.
Description check ✅ Passed The description is directly related to the changeset, explaining what was added (Mastra trace export and PostgreSQL storage) and why it is helpful (inspection and persistence of trace data).
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

@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

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

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.

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.

1 participant