Skip to content

fix: show posthog errors only on debug logs and don't treat them as important#2489

Merged
alepane21 merged 14 commits intomainfrom
ale/eng-8779-posthog-errors-shouldnt-be-logged-as-error
Feb 11, 2026
Merged

fix: show posthog errors only on debug logs and don't treat them as important#2489
alepane21 merged 14 commits intomainfrom
ale/eng-8779-posthog-errors-shouldnt-be-logged-as-error

Conversation

@alepane21
Copy link
Copy Markdown
Contributor

@alepane21 alepane21 commented Feb 4, 2026

PostHog is considered important, when really it is not. With this PR I'm changing this so that:

  • if at start the PostHog service is not available, we just skip starting PostHog and log it with an Info level
  • messages from PostHog component are logged with debug level

Summary by CodeRabbit

  • Bug Fixes

    • Usage-tracking setup no longer blocks application startup; initialization proceeds even if tracking cannot be configured.
    • Uptime and tracking are started asynchronously and safely no-op when disabled or unavailable, avoiding startup blocking.
  • Refactor

    • Reduced log severity for non-fatal tracking issues to cut log noise.
    • Transient tracking errors are logged instead of surfacing as startup failures.

Checklist

@github-actions github-actions Bot added the router label Feb 4, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 4, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Start no longer fails for usage-tracker initialization errors. configureUsageTracking was changed to return no error, sets r.usage to a NoOp by default, logs debug/info on disabled or failed setup, and starts a real tracker asynchronously when created. Tracking routines lower log severity and wrap some init errors.

Changes

Cohort / File(s) Summary
Router startup / usage wiring
router/core/router.go
Start no longer returns on usage-tracker init errors. configureUsageTracking(ctx) signature removed its error return; it initializes r.usage to a NoOp, logs debug/info for disabled or failure paths, and assigns & starts a real tracker in a goroutine when created.
Usage tracker implementation & logging
router/internal/track/track.go
Initialization and token-parsing errors are wrapped with fmt.Errorf and returned from NewUsageTracker instead of direct log.Error. Several tracking functions and the uptime loop lower log severity from Error to Debug; no exported signatures changed.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: demoting PostHog error logging from error to debug level and removing it as a startup blocker.

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


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

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 4, 2026

Router image scan passed

✅ No security vulnerabilities found in image:

ghcr.io/wundergraph/cosmo/router:sha-3ed64088b2b32f26e8cc63ed93e81ef8a158e37d

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 4, 2026

Codecov Report

❌ Patch coverage is 26.31579% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 61.72%. Comparing base (010e8e2) to head (bd63325).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
router/internal/track/track.go 0.00% 8 Missing ⚠️
router/core/router.go 45.45% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2489      +/-   ##
==========================================
+ Coverage   56.05%   61.72%   +5.67%     
==========================================
  Files         215      231      +16     
  Lines       23548    24154     +606     
==========================================
+ Hits        13199    14909    +1710     
+ Misses       9107     8002    -1105     
- Partials     1242     1243       +1     
Files with missing lines Coverage Δ
router/core/router.go 72.92% <45.45%> (+1.11%) ⬆️
router/internal/track/track.go 0.00% <0.00%> (ø)

... and 94 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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: 1

Caution

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

⚠️ Outside diff range comments (1)
router/core/router.go (1)

1400-1424: ⚠️ Potential issue | 🟠 Major

Potential nil-pointer panic in Shutdown if usage tracking fails.

If track.NewUsageTracker returns an error at Line 1418, configureUsageTracking returns the error at Line 1420. The caller (Start at Line 1206-1208) logs the error but continues. However, r.usage remains nil because it was never assigned after the failure. Later, Shutdown calls r.usage.Close() at Line 1577, which will panic on a nil receiver.

Consider setting r.usage to a no-op tracker when initialization fails:

🐛 Proposed fix
 	r.usage, err = track.NewUsageTracker(r.logger, cfg)
 	if err != nil {
+		r.usage = &UsageTrackerNoOp{}
 		return err
 	}
🤖 Fix all issues with AI agents
In `@router/internal/track/track.go`:
- Around line 56-63: The PostHog endpoint string in the posthog.Config is
misspelled ("https://eu.i.posthoga.com"); update the Endpoint value used when
creating cfg (the variable used with posthog.NewWithConfig) to the correct EU
Cloud URL "https://eu.i.posthog.com" so tracker.client is initialized against
the right host (refer to tracker.posthogLogger(), cfg, and posthog.NewWithConfig
in the file to locate the change).

Comment thread router/internal/track/track.go
@alepane21
Copy link
Copy Markdown
Contributor Author

Actionable comments posted: 1

Caution

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

⚠️ Outside diff range comments (1)

router/core/router.go (1)> 1400-1424: ⚠️ Potential issue | 🟠 Major

Potential nil-pointer panic in Shutdown if usage tracking fails.
If track.NewUsageTracker returns an error at Line 1418, configureUsageTracking returns the error at Line 1420. The caller (Start at Line 1206-1208) logs the error but continues. However, r.usage remains nil because it was never assigned after the failure. Later, Shutdown calls r.usage.Close() at Line 1577, which will panic on a nil receiver.
Consider setting r.usage to a no-op tracker when initialization fails:

🐛 Proposed fix

 	r.usage, err = track.NewUsageTracker(r.logger, cfg)
 	if err != nil {
+		r.usage = &UsageTrackerNoOp{}
 		return err
 	}

🤖 Fix all issues with AI agents

@coderabbitai But Close isn't already checking about nil receiver?

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 5, 2026

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

@alepane21, great question! Let me check the implementation of the Close() method to see if it handles nil receivers.


🏁 Script executed:

#!/bin/bash
# Find the usage tracker type and its Close method implementation
rg -n "type.*UsageTracker" --type=go -A 5

# Find Close method implementations for usage tracker
rg -n "func.*Close\(\)" --type=go -B 2 -A 10 router/pkg/track/

Length of output: 1068


🏁 Script executed:

#!/bin/bash
# Find the Close method implementation for UsageTracker
rg -n "func.*\*UsageTracker.*Close" --type=go -A 10 router/internal/track/

# Also search for Close method in the entire track.go file
cat router/internal/track/track.go | grep -n "Close" -A 10 -B 2

Length of output: 1019


@alepane21, you're absolutely right! I apologize for the incorrect review comment.

Looking at the Close() method implementation in router/internal/track/track.go (lines 197-203), it does indeed check for a nil receiver:

func (u *UsageTracker) Close() {
	if u == nil || u.client == nil {
		return
	}
	_ = u.trackRouterUptime(uptimeOptions{closed: true})
	_ = u.client.Close()
}

The method safely handles the case where u is nil by returning early. This means calling r.usage.Close() in Shutdown is safe even if r.usage is nil due to initialization failure. There's no nil-pointer panic risk, and no need for the UsageTrackerNoOp workaround I suggested.

Thank you for catching this! 🙏


🧠 Learnings used
Learnt from: SkArchon
Repo: wundergraph/cosmo PR: 2172
File: router/core/retry_builder.go:13-15
Timestamp: 2025-09-01T13:26:16.284Z
Learning: In router/core/retry_builder.go, the BuildRetryFunction's manager parameter is guaranteed to be non-nil by the calling code, so defensive nil checks are not needed according to the team's preference.

Learnt from: asoorm
Repo: wundergraph/cosmo PR: 2438
File: router-tests/go.mod:15-15
Timestamp: 2026-01-05T11:53:58.709Z
Learning: For mcp-go dependency in wundergraph/cosmo, patch releases may not have formal release notes published. Tag diffs should be reviewed to verify patch-level changes contain no API surface changes or breaking behavior before adopting new patch versions.

@alepane21 alepane21 marked this pull request as ready for review February 5, 2026 16:32
Comment thread router/core/router.go Outdated
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.

Caution

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

⚠️ Outside diff range comments (1)
router/core/router.go (1)

1397-1418: ⚠️ Potential issue | 🟡 Minor

Align tracker-init failure log level with the stated Info-level requirement.

The PR objectives mention logging an Info message when PostHog is unavailable at start. Right now track.NewUsageTracker failures are logged at Debug, which can hide the “tracking disabled” signal in production. Consider upgrading this specific log to Info (or adding a targeted Info log for PostHog unavailability).

Proposed adjustment
-    r.logger.Debug("failed to start usage tracking", zap.Error(err))
+    r.logger.Info("usage tracking disabled; failed to start usage tracker", zap.Error(err))

@alepane21 alepane21 requested a review from StarpTech February 9, 2026 11:20
Copy link
Copy Markdown
Contributor

@StarpTech StarpTech left a comment

Choose a reason for hiding this comment

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

LGTM

@alepane21 alepane21 merged commit 4b780fe into main Feb 11, 2026
29 checks passed
@alepane21 alepane21 deleted the ale/eng-8779-posthog-errors-shouldnt-be-logged-as-error branch February 11, 2026 10:55
maxbol pushed a commit to maxbol/cosmo that referenced this pull request Feb 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants