Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[93] fix: Connected Grafana Api to the response-time-interceptor #150

Open
wants to merge 24 commits into
base: dev
Choose a base branch
from

Conversation

DarrenDsouza7273
Copy link

@DarrenDsouza7273 DarrenDsouza7273 commented Jul 4, 2024

Referene Issue : #76

Dashboards.-.Grafana.-.Google.Chrome.2024-07-04.12-07-19.mp4

Summary by CodeRabbit

  • New Features

    • Introduced configuration for Grafana API token and base URL.
    • Added axios and uuid packages as dependencies.
    • Included ConfigModule for environment configuration management.
    • Enhanced dashboard interaction with Grafana APIs.
  • Improvements

    • Updated the method of setting the content type in the metrics endpoint.
    • Refactored response time metrics collection to include error handling and additional metadata.
  • Bug Fixes

    • Corrected schematics collection reference in nest-cli.json.
  • Tests

    • Added unit tests for FastifyFileInterceptor focusing on file uploads and error scenarios.
  • Documentation

    • Updated example environment file with Grafana configuration variables.

Copy link

coderabbitai bot commented Jul 4, 2024

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This release introduces important updates across multiple files, focusing on enhancing functionality and code maintainability. Key changes include improvements in Prometheus metrics handling, updates to the response-time tracking interceptor for better Grafana integration, and addition of new utility functions for generating Grafana dashboard panels. Minor formatting updates and new unit tests for file upload interceptors are also included, along with updated configuration management using NestJS ConfigModule and new dependencies in package.json.

Changes

Files Change Summary
packages/.../prometheus.controller.ts Updated method to use response.header instead of response.headers for setting content type.
packages/.../file-upload.interceptor.ts, file-upload.interceptor.spec.ts Minor formatting changes and new unit tests for FastifyFileInterceptor.
packages/.../response-time.interceptor.ts, packages/.../utils.ts Enhanced Grafana integration, new utility functions for dashboard management, and updated error handling.
sample/02-monitoring/env-example, nest-cli.json, package.json Added env-example, updated Grafana API token and URL configurations, dependency updates.
sample/02-monitoring/src/app.controller.ts Added @UseInterceptors decorator to AppController class.
sample/02-monitoring/src/app.module.ts Included ConfigModule and initialized it in imports array.
sample/02-monitoring/src/main.ts Introduced ConfigService to fetch configuration for Grafana integration in ResponseTimeInterceptor.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant AppController
    participant ResponseTimeInterceptor
    participant GrafanaAPI
    participant PrometheusController

    Client->>AppController: Send request
    AppController->>ResponseTimeInterceptor: Intercept request
    ResponseTimeInterceptor->>GrafanaAPI: Fetch or update dashboard
    GrafanaAPI-->>ResponseTimeInterceptor: Return dashboard data
    ResponseTimeInterceptor->>AppController: Proceed with request
    AppController->>PrometheusController: Fetch metrics
    PrometheusController->>Client: Return metrics with content-type header
Loading

Poem

In code we trust, a change of light,
From headers to header, metrics right.
Grafana's dash, a smoother play,
Configured paths now guide the way.
Fastify and tests spring to life,
In harmony, beyond all strife.
CodeRabbit's cheer, in each commit tight!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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

Copy link

gitguardian bot commented Jul 4, 2024

⚠️ GitGuardian has uncovered 8 secrets following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

Since your pull request originates from a forked repository, GitGuardian is not able to associate the secrets uncovered with secret incidents on your GitGuardian dashboard.
Skipping this check run and merging your pull request will create secret incidents on your GitGuardian dashboard.

🔎 Detected hardcoded secrets in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
- - Grafana Service Account Token 3b0e08e sample/02-monitoring/src/response.interceptor.ts View secret
- - Grafana Service Account Token 3b0e08e sample/02-monitoring/src/response.interceptor.ts View secret
- - Grafana Service Account Token f4ec971 sample/02-monitoring/src/response.interceptor.ts View secret
- - Grafana Service Account Token f4ec971 sample/02-monitoring/src/response.interceptor.ts View secret
- - Grafana Service Account Token 125e67d sample/02-monitoring/src/response.interceptor.ts View secret
- - Grafana Token 9154a6d sample/02-monitoring/README.md View secret
- - Grafana Token 3fa0a9c sample/02-monitoring/README.md View secret
- - Grafana Token 9154a6d sample/02-monitoring/README.md View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secrets safely. Learn here the best practices.
  3. Revoke and rotate these secrets.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

Copy link

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f8221bb and 1ce4457.

Files ignored due to path filters (1)
  • sample/02-monitoring/yarn.lock is excluded by !**/yarn.lock, !**/*.lock
Files selected for processing (9)
  • packages/common/src/controllers/prometheus.controller.ts (1 hunks)
  • packages/common/src/interceptors/response-time.interceptor.ts (3 hunks)
  • packages/common/src/interceptors/utils.ts (2 hunks)
  • sample/02-monitoring/env-example (1 hunks)
  • sample/02-monitoring/nest-cli.json (1 hunks)
  • sample/02-monitoring/package.json (2 hunks)
  • sample/02-monitoring/src/app.controller.ts (1 hunks)
  • sample/02-monitoring/src/app.module.ts (1 hunks)
  • sample/02-monitoring/src/main.ts (1 hunks)
Files skipped from review due to trivial changes (2)
  • sample/02-monitoring/env-example
  • sample/02-monitoring/nest-cli.json
Additional context used
Biome
packages/common/src/controllers/prometheus.controller.ts

[error] 8-8: Decorators are not valid here.

Decorators are only valid on class declarations, class expressions, and class methods.
You can enable parameter decorators by setting the unsafeParameterDecoratorsEnabled option to true in your configuration file.

(parse)

Additional comments not posted (16)
packages/common/src/controllers/prometheus.controller.ts (1)

9-9: LGTM!

The change from response.headers to response.header is correct for setting a single header in Fastify.

sample/02-monitoring/src/app.module.ts (1)

8-8: LGTM!

The addition of ConfigModule.forRoot() is correct for loading environment variables.

sample/02-monitoring/src/main.ts (1)

8-16: LGTM!

The changes correctly fetch configuration values and use them to set up the ResponseTimeInterceptor.

sample/02-monitoring/package.json (4)

27-27: Approved: Addition of axios dependency.

The addition of axios is appropriate for making HTTP requests.


30-30: Approved: Addition of uuid dependency.

The addition of uuid is appropriate for generating unique identifiers.


34-35: Approved: Addition of @samagra-x/schematics and @samagra-x/stencil-cli devDependencies.

The addition of @samagra-x/schematics and @samagra-x/stencil-cli is appropriate for managing project schematics and CLI tools.


45-45: Approved: Addition of husky devDependency.

The addition of husky is appropriate for managing Git hooks.

packages/common/src/interceptors/response-time.interceptor.ts (5)

5-6: Approved: Addition of axios import.

The addition of axios is necessary for making HTTP requests to the Grafana API.


7-12: Approved: Addition of utility function imports.

The addition of generateBaseJSON, generateRow, getDashboardByUID, and getDashboardJSON imports is necessary for the updated functionality of the ResponseTimeInterceptor class.


22-33: Approved: Updates to the constructor.

The constructor updates are necessary for initializing the ResponseTimeInterceptor with the required parameters for interacting with the Grafana API.


18-20: Approved: Addition of private members.

The addition of dashboardUid, grafanaBaseURL, and apiToken as private members is necessary for storing the Grafana API configuration and dashboard UID.


Line range hint 35-111:
Approved: Updates to the init method.

The updates to the init method are necessary for setting up the Grafana dashboard and adding the histogram panel.

packages/common/src/interceptors/utils.ts (4)

1-2: Approved: Addition of axios import.

The addition of axios is necessary for making HTTP requests to the Grafana API.


3-82: Approved: Updates to generateBaseJSON function.

The updates to the generateBaseJSON function are necessary for generating the base JSON structure for the Grafana dashboard with the given histogram title.


Line range hint 86-113:
Approved: Updates to generateRow function.

The updates to the generateRow function are necessary for generating a row with multiple panels for the Grafana dashboard.


609-650: Approved: Addition of new utility functions.

The addition of generateAverageResponseTimePanel, getDashboardByUID, and getDashboardJSON utility functions is necessary for generating specific panels and retrieving dashboard JSON from the Grafana API.

Comment on lines 6 to 7
@UseInterceptors(
)
Copy link

Choose a reason for hiding this comment

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

Empty @UseInterceptors decorator.

The @UseInterceptors decorator is empty, which might be an oversight. Ensure that the intended interceptor is specified.

-@UseInterceptors(
-)
+@UseInterceptors(ResponseTimeInterceptor)
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
@UseInterceptors(
)
@UseInterceptors(ResponseTimeInterceptor)

@@ -1,11 +1,12 @@
import { Controller, Get, Res } from '@nestjs/common';
import { FastifyReply } from 'fastify';
import { register } from 'prom-client';

@Controller()
export class PrometheusController {
@Get('metrics')
async metrics(@Res() response: FastifyReply) {
Copy link

Choose a reason for hiding this comment

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

Static Analysis: Decorators are not valid here.

Decorators are only valid on class declarations, class expressions, and class methods. You can enable parameter decorators by setting the unsafeParameterDecoratorsEnabled option to true in your configuration file.

-  async metrics(@Res() response: FastifyReply) {
+  async metrics(response: FastifyReply) {
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
async metrics(@Res() response: FastifyReply) {
async metrics(response: FastifyReply) {
Tools
Biome

[error] 8-8: Decorators are not valid here.

Decorators are only valid on class declarations, class expressions, and class methods.
You can enable parameter decorators by setting the unsafeParameterDecoratorsEnabled option to true in your configuration file.

(parse)

@DarrenDsouza7273
Copy link
Author

main.ts.-.new.-.Visual.Studio.Code.2024-07-04.23-50-45.mp4
Dashboards.-.Grafana.-.Google.Chrome.2024-07-04.23-51-20.mp4

@techsavvyash techsavvyash linked an issue Jul 6, 2024 that may be closed by this pull request
@DarrenDsouza7273 DarrenDsouza7273 changed the base branch from main to dev July 6, 2024 11:12
@DarrenDsouza7273
Copy link
Author

DarrenDsouza7273 commented Jul 6, 2024

  • Global level inteceptor
  • Controller level interceptor (Metrics arent updating)
  • e2e tests
  • Readme Updated

Copy link

@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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1ce4457 and 3b0e08e.

Files ignored due to path filters (2)
  • packages/user-service/yarn.lock is excluded by !**/yarn.lock, !**/*.lock
  • sample/02-monitoring/yarn.lock is excluded by !**/yarn.lock, !**/*.lock
Files selected for processing (11)
  • packages/common/src/controllers/prometheus.controller.ts (1 hunks)
  • packages/common/src/interceptors/file-upload.interceptor.ts (1 hunks)
  • packages/common/src/interceptors/response-time.interceptor.ts (3 hunks)
  • packages/common/src/interceptors/utils.ts (2 hunks)
  • packages/common/test/file-upload.interceptor.spec.ts (1 hunks)
  • sample/02-monitoring/env-example (1 hunks)
  • sample/02-monitoring/nest-cli.json (1 hunks)
  • sample/02-monitoring/package.json (2 hunks)
  • sample/02-monitoring/src/app.controller.ts (1 hunks)
  • sample/02-monitoring/src/app.module.ts (1 hunks)
  • sample/02-monitoring/src/main.ts (1 hunks)
Files skipped from review due to trivial changes (2)
  • packages/common/src/interceptors/file-upload.interceptor.ts
  • sample/02-monitoring/nest-cli.json
Files skipped from review as they are similar to previous changes (6)
  • packages/common/src/interceptors/response-time.interceptor.ts
  • packages/common/src/interceptors/utils.ts
  • sample/02-monitoring/env-example
  • sample/02-monitoring/package.json
  • sample/02-monitoring/src/app.controller.ts
  • sample/02-monitoring/src/main.ts
Additional comments not posted (11)
packages/common/src/controllers/prometheus.controller.ts (2)

9-9: Change approved.

The change from response.headers to response.header is correct and aligns with the FastifyReply API.


8-8: Static Analysis: Decorators are not valid here.

Decorators are only valid on class declarations, class expressions, and class methods. You can enable parameter decorators by setting the unsafeParameterDecoratorsEnabled option to true in your configuration file.

-  async metrics(@Res() response: FastifyReply) {
+  async metrics(response: FastifyReply) {
sample/02-monitoring/src/app.module.ts (2)

5-5: Change approved.

The import statement for ConfigModule is correct and necessary.


8-8: Change approved.

The inclusion of ConfigModule.forRoot() in the imports array follows best practices for configuration management in NestJS.

packages/common/test/file-upload.interceptor.spec.ts (7)

1-3: Change approved.

The import statements are correct and necessary for the tests.


5-11: Change approved.

The describe block and setup for FastifyFileInterceptor are correct and follow best practices.


13-15: Change approved.

The test case correctly checks if the interceptor is defined.


17-26: Change approved.

The test case correctly checks if the interceptor handles file upload.


30-44: Change approved.

The test case correctly checks if the interceptor handles errors.


47-59: Change approved.

The test case correctly checks if the interceptor handles cases where the file is not present or null.


63-73: Change approved.

The helper functions createMockContext and createMockNextHandler are correct and follow best practices.

@Savio629
Copy link
Collaborator

Savio629 commented Jul 27, 2024

image

I have commented one of the test which isn't passing, can you point what can be the issue here?

@Savio629
Copy link
Collaborator

Savio629 commented Aug 5, 2024

Parent issue: #93

@Savio629 Savio629 changed the title fix: Connected Grafana Api to the response-time-interceptor [93] fix: Connected Grafana Api to the response-time-interceptor Aug 6, 2024
@techsavvyash
Copy link
Member

@Savio629 CI is failing on this.

@techsavvyash
Copy link
Member

  1. Have we fixed the issue of each multiple dashboards? It should just be multiple panels in a single dashboard.

@Savio629
Copy link
Collaborator

Savio629 commented Aug 10, 2024

@Savio629 CI is failing on this
Fixed one of the failing ci.

test / test-sample (01-monitoring) (pull_request) is failing because package is not updated of @samagra-x/stencil with this pr changes

GitGuardian Security Checks is failing as key was push in this pr

Have we fixed the issue of each multiple dashboards? It should just be multiple panels in a single dashboard.

Yes

@techsavvyash

@Savio629
Copy link
Collaborator

image

By tomorrow, will test it properly and update you

@Savio629
Copy link
Collaborator

Savio629 commented Sep 24, 2024

updated test & the interceptor
image

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.

[stencil] Update ResponseTimeInterceptor
3 participants