Skip to content

Telemetry insights#2255

Merged
glennjacobs merged 16 commits into1.xfrom
lun-315-telemetry
Aug 14, 2025
Merged

Telemetry insights#2255
glennjacobs merged 16 commits into1.xfrom
lun-315-telemetry

Conversation

@alecritson
Copy link
Collaborator

@alecritson alecritson commented Jul 24, 2025

The idea behind this PR is for Lunar to send anonymous usage insights to a predetermined endpoint. This will help us see how Lunar is being used and at what capacity.

It is completely anonoymous and you will be able to easily opt out of sending any stats by calling Telemetry::optOut() in your service provider. We will also only do the call once per day and will be on a deffered request so that it does not interfere with the performance of your storefront.

Summary by CodeRabbit

  • New Features
    • Introduced anonymous usage insights enabled by default; data is non-identifying, sent at most once per day, and can be opted out.
  • Documentation
    • Added clear "Telemetry insights" and upgrade notes with opt-out instructions.
  • Behavior
    • Telemetry is triggered automatically at app shutdown (when not running in console).
  • Tests
    • Added comprehensive tests covering opt-out, run frequency, and payload contents.

@alecritson alecritson requested a review from glennjacobs July 24, 2025 10:45
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 24, 2025

Warning

Rate limit exceeded

@glennjacobs has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 17 minutes and 23 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between d927396 and 922c44e.

📒 Files selected for processing (3)
  • docs/core/upgrading.md (1 hunks)
  • packages/core/src/Base/TelemetryInsights.php (1 hunks)
  • packages/core/src/Base/TelemetryService.php (1 hunks)

Walkthrough

Adds a telemetry subsystem: interfaces, implementations, a service and facade, service provider bindings and termination hook, docs describing anonymous telemetry and opt-out, and tests covering telemetry behavior and payloads.

Changes

Cohort / File(s) Change Summary
Docs
docs/core/installation.md, docs/core/upgrading.md
Added documentation describing anonymous telemetry collection, opt-out instructions, and upgrade notes.
Telemetry interfaces & contracts
packages/core/src/Base/TelemetryServiceInterface.php, packages/core/src/Base/ProvidesTelemetryInsights.php
Introduced TelemetryServiceInterface and ProvidesTelemetryInsights interface declarations.
Telemetry implementations
packages/core/src/Base/TelemetryService.php, packages/core/src/Base/TelemetryInsights.php
Added TelemetryService (opt-out, caching, payload construction, HTTP submission) and TelemetryInsights (gathers environment and ecommerce metrics).
Facade & provider
packages/core/src/Facades/Telemetry.php, packages/core/src/LunarServiceProvider.php
Added Telemetry facade; registered telemetry singletons and wired termination callback to invoke telemetry on app termination (non-console).
Tests
tests/core/Feature/Base/TelemetryServiceTest.php
Added tests for opt-out, daily-run behavior, HTTP submission, and payload content assertions.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch lun-315-telemetry

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@vercel
Copy link

vercel bot commented Jul 24, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Project Deployment Preview Comments Updated (UTC)
lunar-docs Ready Preview Comment Aug 14, 2025 1:46pm

Copy link
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

🧹 Nitpick comments (1)
packages/core/src/Base/TelemetryInsights.php (1)

57-60: Recent order count with potential performance consideration.

The 24-hour window provides meaningful telemetry data. Consider ensuring the placed_at column is indexed for optimal performance on large order tables.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 60ac49f and 4ced7e5.

📒 Files selected for processing (9)
  • docs/core/installation.md (1 hunks)
  • docs/core/upgrading.md (1 hunks)
  • packages/core/src/Base/ProvidesTelemetryInsights.php (1 hunks)
  • packages/core/src/Base/TelemetryInsights.php (1 hunks)
  • packages/core/src/Base/TelemetryService.php (1 hunks)
  • packages/core/src/Base/TelemetryServiceInterface.php (1 hunks)
  • packages/core/src/Facades/Telemetry.php (1 hunks)
  • packages/core/src/LunarServiceProvider.php (3 hunks)
  • tests/core/Feature/Base/TelemetryServiceTest.php (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/core/src/Base/TelemetryServiceInterface.php (1)
packages/core/src/Base/TelemetryService.php (5)
  • optOut (20-23)
  • getInsightsUrl (25-28)
  • getCacheKey (30-33)
  • shouldRun (35-44)
  • run (64-78)
packages/core/src/Base/ProvidesTelemetryInsights.php (1)
packages/core/src/Base/TelemetryInsights.php (12)
  • domainHash (15-20)
  • environment (22-25)
  • laravelVersion (27-30)
  • lunarVersion (32-35)
  • dbDriver (37-40)
  • phpVersion (42-45)
  • productCount (47-50)
  • productVariantCount (52-55)
  • orderCount (57-60)
  • orderTotal (62-65)
  • currencies (67-72)
  • languages (74-79)
⏰ 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). (20)
  • GitHub Check: admin - PHP 8.4 - L12.* ↑ E
  • GitHub Check: admin - PHP 8.4 - L12.* ↑
  • GitHub Check: shipping - PHP 8.4 - L12.* ↑
  • GitHub Check: stripe - PHP 8.4 - L12.* ↑ E
  • GitHub Check: stripe - PHP 8.4 - L12.* ↑
  • GitHub Check: search - PHP 8.4 - L11.* ↑
  • GitHub Check: stripe - PHP 8.4 - L11.* ↑ E
  • GitHub Check: core - PHP 8.4 - L12.* ↑ E
  • GitHub Check: core - PHP 8.4 - L11.* ↑
  • GitHub Check: core - PHP 8.4 - L11.* ↑ E
  • GitHub Check: admin - PHP 8.4 - L11.* ↑ E
  • GitHub Check: search - PHP 8.3 - L12.* ↑ E
  • GitHub Check: admin - PHP 8.3 - L12.* ↑
  • GitHub Check: admin - PHP 8.3 - L12.* ↑ E
  • GitHub Check: core - PHP 8.3 - L12.* ↑ E
  • GitHub Check: core - PHP 8.3 - L12.* ↑
  • GitHub Check: admin - PHP 8.3 - L11.* ↑ E
  • GitHub Check: core - PHP 8.3 - L11.* ↑ E
  • GitHub Check: admin - PHP 8.3 - L11.* ↑
  • GitHub Check: core - PHP 8.3 - L11.* ↑
🔇 Additional comments (29)
docs/core/upgrading.md (1)

21-31: Clear and informative telemetry documentation.

The documentation effectively communicates the purpose, anonymity, and opt-out mechanism for the new telemetry feature. The explanation is transparent about what data is collected and provides a simple opt-out method.

docs/core/installation.md (1)

97-105: Good telemetry documentation with clear opt-out instructions.

The telemetry section provides clear information about data collection and an easy opt-out mechanism, maintaining transparency with users.

packages/core/src/Facades/Telemetry.php (1)

1-23: Well-implemented Laravel facade following best practices.

The facade correctly extends Laravel's base Facade class, returns the proper service interface from getFacadeAccessor(), and includes comprehensive PHPDoc annotations for IDE support. The implementation follows Laravel conventions perfectly.

packages/core/src/LunarServiceProvider.php (3)

35-35: Proper import statements for telemetry classes.

The necessary telemetry classes and facade are correctly imported to support the new functionality.

Also applies to: 41-43, 59-59


195-201: Well-structured singleton bindings for telemetry services.

The telemetry services are properly registered as singletons in the service container, following Laravel's dependency injection patterns. This ensures consistent instances throughout the application lifecycle.


203-205: Smart use of application termination callback for telemetry.

Using the terminating() callback is an excellent approach as it ensures telemetry data is sent at the end of the application lifecycle without impacting runtime performance. This aligns with the PR objective of minimizing performance overhead.

packages/core/src/Base/TelemetryServiceInterface.php (1)

5-16: Clean and well-designed telemetry service interface.

The interface provides a clear contract with appropriately named methods that cover all necessary telemetry operations: opt-out functionality, URL configuration, caching control, execution logic, and the main telemetry runner. The method signatures are intuitive and the interface maintains a good separation of concerns.

tests/core/Feature/Base/TelemetryServiceTest.php (4)

9-15: Well-structured opt-out test.

The test correctly verifies the telemetry opt-out functionality by checking the state before and after calling optOut().


17-27: Effective daily run limitation test.

The test properly validates that telemetry runs only once per day by manipulating cache timestamps and verifying the shouldRun() behavior in both scenarios.


29-47: Good basic HTTP request test.

The test effectively verifies that telemetry sends HTTP requests to the correct endpoint using proper mocking techniques.


49-134: Comprehensive payload validation test.

This test thoroughly validates all fields in the telemetry payload. The test structure is excellent and covers all data points collected by the telemetry system.

The comma-separated format for currencies and languages (lines 131-132) correctly matches the implementation in TelemetryService.php where join(',') is used on the collections.

packages/core/src/Base/ProvidesTelemetryInsights.php (1)

7-38: Well-designed telemetry insights interface.

The interface provides a comprehensive contract for telemetry data collection with:

  • Clear method names that indicate their purpose
  • Appropriate return types for each data point
  • Proper PHPDoc annotations for generic collection types
  • Good separation of concerns between environment data, counts, and collections

The interface design supports the anonymous telemetry requirements effectively.

packages/core/src/Base/TelemetryInsights.php (11)

15-20: Appropriate domain identification method.

Using MD5 hash of the app URL is suitable for anonymous domain identification in telemetry data. The implementation correctly uses Laravel's config helper.


22-25: Standard Laravel environment detection.

Correctly uses Laravel's built-in environment detection method.


27-30: Correct Laravel version retrieval.

Uses the standard Laravel method to get framework version information.


32-35: Proper package version detection.

Correctly uses Composer's InstalledVersions API to retrieve the Lunar core package version.


37-40: Database connection identification.

The method returns the default database connection name, which effectively indicates the database type being used for telemetry purposes.


42-45: Standard PHP version detection.

Correctly uses the built-in phpversion() function to retrieve PHP runtime version.


47-50: Simple product count query.

Uses standard Eloquent count method to retrieve total product count for telemetry.


52-55: Simple product variant count query.

Uses standard Eloquent count method to retrieve total product variant count for telemetry.


62-65: Recent order total calculation.

Consistent with orderCount() method using the same 24-hour window. Same indexing consideration applies for optimal performance.


67-72: Efficient currency code collection.

Correctly filters for enabled currencies and maps to their codes using Laravel's collection methods.


74-79: Complete language code collection.

Retrieves all configured languages and maps to their codes, which is appropriate for telemetry data collection.

packages/core/src/Base/TelemetryService.php (6)

11-18: Clean constructor with proper dependency injection.

The constructor design is appropriate with:

  • Clear default state for telemetry execution
  • Proper dependency injection using constructor property promotion
  • Protected visibility for internal state management

20-23: Simple and effective opt-out mechanism.

The method correctly disables telemetry by setting the internal flag to false.


25-28: Hardcoded telemetry endpoint URL.

The HTTPS URL is appropriate for the telemetry service. The hardcoded approach is acceptable for this specific use case.


30-33: Clear cache key naming.

The cache key is descriptive and follows Laravel naming conventions for cache keys.


35-44: Correct daily execution logic.

The method properly implements the daily execution limit by:

  • Checking opt-out status first
  • Using cached timestamps to track last execution
  • Leveraging Carbon's isToday() method for accurate date comparison

46-62: Comprehensive telemetry payload construction.

The method effectively builds the telemetry payload by:

  • Including all relevant system and application metrics
  • Properly formatting collections as comma-separated strings
  • Using clear, descriptive keys for the API payload

Cache::forget($this->getCacheKey());

Cache::remember($this->getCacheKey(), 86400, function (): ?Carbon {
$response = Http::withHeader('Accept', 'application/json')
Copy link
Contributor

Choose a reason for hiding this comment

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

The timeout for this will be the default Laravel setting of 30 secs.
Do we want to lessen this? And do we want to add any retries?

@glennjacobs glennjacobs merged commit c328fb7 into 1.x Aug 14, 2025
48 checks passed
@glennjacobs glennjacobs deleted the lun-315-telemetry branch August 14, 2025 13:53
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