Skip to content

Conversation

@everettbu
Copy link

@everettbu everettbu commented Jul 26, 2025

Test 3

Summary by CodeRabbit

  • New Features

    • Introduced middleware to enhance plugin client requests with contextual logging, adding plugin and user metadata to logs for improved traceability.
  • Refactor

    • Updated logging interfaces to use modern type aliases for variadic parameters.
    • Added support for creating loggers from context, improving contextual logging capabilities.
    • Renamed and streamlined the metrics middleware for clarity and consistency.
  • Chores

    • Updated middleware initialization to include the new contextual logger and revised metrics middleware.

…are (#76186)

* Plugins: Chore: Renamed instrumentation middleware to metrics middleware

* Removed repeated logger attributes in middleware and contextual logger

* renamed loggerParams to logParams

* PR review suggestion

* Add contextual logger middleware

* Removed unused params from logRequest

* Removed unwanted changes

* Safer FromContext method

* Removed traceID from logParams
@coderabbitai
Copy link

coderabbitai bot commented Jul 26, 2025

Walkthrough

The changes introduce a new contextual logger middleware for plugin client requests, update logging interfaces to support context-based loggers, and refactor metrics middleware naming and usage. Middleware composition is updated to include the new contextual logger, and logging methods are modernized to use any instead of interface{} for variadic parameters.

Changes

File(s) Change Summary
pkg/plugins/log/ifaces.go Updated Logger and PrettyLogger interfaces: variadic params use ...any; added FromContext(ctx) to Logger.
pkg/plugins/log/fake.go, pkg/plugins/log/logger.go Added FromContext method to logger types for context-based logger retrieval.
pkg/services/pluginsintegration/clientmiddleware/contextual_logger_middleware.go Introduced ContextualLoggerMiddleware to inject plugin metadata into request context for logging.
pkg/services/pluginsintegration/clientmiddleware/logger_middleware.go Simplified logging: removed plugin metadata/context enrichment, updated to use FromContext; refactored method sigs.
pkg/services/pluginsintegration/clientmiddleware/metrics_middleware.go Renamed InstrumentationMiddleware to MetricsMiddleware; removed logger context enrichment.
pkg/services/pluginsintegration/clientmiddleware/metrics_middleware_test.go Updated tests to use newMetricsMiddleware instead of newInstrumentationMiddleware.
pkg/services/pluginsintegration/pluginsintegration.go Updated middleware stack: replaced metrics middleware, added contextual logger middleware.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ContextualLoggerMiddleware
    participant MetricsMiddleware
    participant LoggerMiddleware
    participant Plugin

    Client->>ContextualLoggerMiddleware: QueryData/CallResource/CheckHealth/CollectMetrics
    ContextualLoggerMiddleware->>ContextualLoggerMiddleware: Add plugin metadata to context
    ContextualLoggerMiddleware->>MetricsMiddleware: Forward request with enriched context
    MetricsMiddleware->>LoggerMiddleware: Forward request
    LoggerMiddleware->>Plugin: Forward request
    Plugin-->>LoggerMiddleware: Response
    LoggerMiddleware-->>MetricsMiddleware: Response
    MetricsMiddleware-->>ContextualLoggerMiddleware: Response
    ContextualLoggerMiddleware-->>Client: Response
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~18 minutes

Poem

A rabbit hopped through logging lanes,
Context and metrics—now it gains!
With middlewares stacked high and neat,
Each plugin call is now a treat.
Logs know the context, metrics shine,
In Grafana’s code—oh, how divine!
🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.2.2)

Error: can't load config: unsupported version of the configuration: "" See https://golangci-lint.run/product/migration-guide for migration instructions
The command is terminated due to an error: can't load config: unsupported version of the configuration: "" See https://golangci-lint.run/product/migration-guide for migration instructions

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5dac673 and 00393c2.

📒 Files selected for processing (8)
  • pkg/plugins/log/fake.go (2 hunks)
  • pkg/plugins/log/ifaces.go (1 hunks)
  • pkg/plugins/log/logger.go (2 hunks)
  • pkg/services/pluginsintegration/clientmiddleware/contextual_logger_middleware.go (1 hunks)
  • pkg/services/pluginsintegration/clientmiddleware/logger_middleware.go (7 hunks)
  • pkg/services/pluginsintegration/clientmiddleware/metrics_middleware.go (9 hunks)
  • pkg/services/pluginsintegration/clientmiddleware/metrics_middleware_test.go (1 hunks)
  • pkg/services/pluginsintegration/pluginsintegration.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (7)
pkg/services/pluginsintegration/pluginsintegration.go (2)
pkg/services/pluginsintegration/clientmiddleware/metrics_middleware.go (1)
  • NewMetricsMiddleware (76-82)
pkg/services/pluginsintegration/clientmiddleware/contextual_logger_middleware.go (1)
  • NewContextualLoggerMiddleware (14-20)
pkg/plugins/log/fake.go (2)
pkg/services/contexthandler/contexthandler.go (1)
  • FromContext (46-51)
pkg/plugins/log/ifaces.go (1)
  • Logger (6-24)
pkg/plugins/log/logger.go (2)
pkg/plugins/log/ifaces.go (1)
  • Logger (6-24)
pkg/infra/log/log.go (1)
  • ConcreteLogger (153-156)
pkg/services/pluginsintegration/clientmiddleware/logger_middleware.go (1)
pkg/services/contexthandler/contexthandler.go (1)
  • FromContext (46-51)
pkg/services/pluginsintegration/clientmiddleware/metrics_middleware.go (2)
pkg/plugins/ifaces.go (2)
  • ClientMiddleware (142-145)
  • ClientMiddlewareFunc (150-150)
pkg/plugins/errors.go (1)
  • ErrPluginNotRegistered (9-9)
pkg/services/pluginsintegration/clientmiddleware/contextual_logger_middleware.go (3)
pkg/plugins/ifaces.go (2)
  • ClientMiddleware (142-145)
  • ClientMiddlewareFunc (150-150)
packages/grafana-data/src/types/datasource.ts (1)
  • DataSourceInstanceSettings (646-679)
pkg/infra/log/log.go (1)
  • WithContextualAttributes (285-291)
pkg/plugins/log/ifaces.go (2)
pkg/plugins/log/logger.go (1)
  • New (9-13)
pkg/services/contexthandler/contexthandler.go (1)
  • FromContext (46-51)
🔇 Additional comments (23)
pkg/plugins/log/fake.go (2)

3-3: LGTM: Import addition is necessary.

The context import is required for the new FromContext method signature.


46-48: LGTM: Correct interface implementation for testing.

The FromContext method correctly implements the Logger interface. For a test logger, returning a fresh instance via NewTestLogger() is appropriate since test scenarios typically don't require context-specific behavior.

pkg/services/pluginsintegration/clientmiddleware/metrics_middleware_test.go (1)

78-78: LGTM: Correct update to renamed middleware constructor.

The change from newInstrumentationMiddleware to newMetricsMiddleware correctly aligns with the middleware renaming described in the PR objectives.

pkg/services/pluginsintegration/pluginsintegration.go (1)

159-160: LGTM: Correct middleware refactoring and ordering.

The changes properly implement the separation of concerns:

  1. NewInstrumentationMiddlewareNewMetricsMiddleware aligns with the PR objective
  2. NewContextualLoggerMiddleware() is correctly positioned after metrics middleware, allowing contextual information to be injected before downstream middleware that might use logging
pkg/plugins/log/logger.go (2)

4-4: LGTM: Import addition is necessary.

The context import is required for the new FromContext method signature.


48-56: LGTM: Robust implementation with proper fallback.

The FromContext method is well-implemented:

  • Delegates to the underlying logger's FromContext method
  • Uses safe type assertion with proper fallback to d.New() if the assertion fails
  • Correctly wraps the result in grafanaInfraLogWrapper to maintain the wrapper pattern

This approach ensures resilience if the underlying logger's FromContext returns unexpected types.

pkg/services/pluginsintegration/clientmiddleware/logger_middleware.go (6)

9-9: LGTM: Unused import removal.

The tracing package import is correctly removed since tracing-related contextual logging was moved to the contextual logger middleware.


36-60: LGTM: Effective separation of logging concerns.

The logRequest method simplification correctly implements the separation of concerns:

  • Removes plugin-specific context parameters that are now handled by ContextualLoggerMiddleware
  • Uses m.logger.FromContext(ctx).Info to leverage contextual information already injected into the context
  • Maintains the core logging functionality (status, duration, error handling) without duplication

This design is cleaner and follows the single responsibility principle.


68-71: LGTM: Consistent with simplified method signature.

The QueryData method correctly calls the simplified logRequest without the removed parameters.


81-84: LGTM: Consistent with simplified method signature.

The CallResource method correctly calls the simplified logRequest without the removed parameters.


95-98: LGTM: Consistent with simplified method signature.

The CheckHealth method correctly calls the simplified logRequest without the removed parameters.


109-112: LGTM: Consistent with simplified method signature.

The CollectMetrics method correctly calls the simplified logRequest without the removed parameters.

pkg/plugins/log/ifaces.go (4)

3-3: LGTM: Context package import added appropriately.

The import is necessary for the new FromContext method signature.


8-8: LGTM: Interface modernization with any type alias.

Replacing ...interface{} with ...any follows Go 1.18+ best practices and improves code readability.

Also applies to: 11-11, 14-14, 17-17, 20-20


22-23: LGTM: Context-based logger creation method added.

The new FromContext method enables creating contextual loggers from context, which supports the new contextual logging middleware pattern.


29-40: LGTM: PrettyLogger interface consistently updated.

All variadic parameters properly updated to use any instead of interface{}, maintaining consistency across the logging interfaces.

pkg/services/pluginsintegration/clientmiddleware/metrics_middleware.go (3)

16-16: LGTM: Consistent renaming to MetricsMiddleware.

The type name and comments accurately reflect the middleware's responsibility for metrics collection.

Also applies to: 24-26


32-32: LGTM: Constructor function properly renamed and updated.

The function name NewMetricsMiddleware and internal constructor newMetricsMiddleware maintain consistency with the type rename.

Also applies to: 64-64, 75-77


85-85: LGTM: Method receivers consistently updated.

All method receivers properly updated to use *MetricsMiddleware type, maintaining functional consistency while reflecting the rename.

Also applies to: 94-94, 104-104, 144-198

pkg/services/pluginsintegration/clientmiddleware/contextual_logger_middleware.go (4)

14-20: LGTM: Clean middleware constructor implementation.

The constructor follows standard middleware patterns and properly implements the ClientMiddleware interface.


27-37: LGTM: Well-designed context enrichment function.

The instrumentContext function systematically adds relevant contextual information (endpoint, plugin ID, datasource details, user info) to the logging context. The conditional checks for nil pointers are appropriate safety measures.


59-69: LGTM: Streaming methods appropriately implemented.

Streaming methods correctly delegate to the next middleware without adding contextual logging, which is appropriate since these are typically long-running operations where the initial context enrichment may not be as valuable.


39-57: Endpoint constants are already defined

I’ve confirmed that endpointQueryData, endpointCallResource, endpointCheckHealth, and endpointCollectMetrics are declared in
• pkg/services/pluginsintegration/clientmiddleware/utils.go

No changes needed.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch plugins/rename-instrumentation-middleware-to-metrics-middleware

🪧 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.
    • Explain this complex logic.
    • 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. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

  • 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.

@everettbu
Copy link
Author

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Jul 28, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@github-actions
Copy link
Contributor

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 2 weeks if no further activity occurs. Please feel free to give a status update or ping for review. Thank you for your contributions!

@github-actions github-actions bot added the stale label Aug 28, 2025
@github-actions
Copy link
Contributor

This pull request has been automatically closed because it has not had any further activity in the last 2 weeks. Thank you for your contributions!

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