Skip to content

track number of clickhouse insert operations #236

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

Merged
merged 1 commit into from
Jul 8, 2025

Conversation

iuwqyir
Copy link
Collaborator

@iuwqyir iuwqyir commented Jul 8, 2025

TL;DR

Added a new ClickHouse insert operations metric and fixed row count tracking.

What changed?

  • Added a new ClickHouseMainStorageInsertOperations counter to track the total number of insert operations into ClickHouse main storage
  • Modified the InsertBlockData method to increment the row count metric within the batch processing loop instead of once at the end
  • Now incrementing the new operations counter for each batch of inserts

How to test?

  1. Run the application and verify the new clickhouse_main_storage_insert_operations metric is being reported in Prometheus
  2. Confirm that clickhouse_main_storage_rows_inserted_total now accurately reflects the actual number of rows inserted in batches

Why make this change?

This change provides better visibility into ClickHouse database operations by tracking not just the total rows inserted but also the number of distinct insert operations. The previous implementation only counted rows at the end of the function, which could be inaccurate if the function processed data in batches. The new approach ensures metrics are incremented correctly for each batch processed.

Summary by CodeRabbit

  • New Features

    • Added a new metric to track the total number of insert operations into ClickHouse main storage.
  • Improvements

    • Enhanced metrics reporting for ClickHouse inserts by updating row and operation counts per batch, providing more granular monitoring.

Copy link

coderabbitai bot commented Jul 8, 2025

Walkthrough

A new Prometheus counter metric was introduced to track the number of insert operations into ClickHouse main storage. The metrics update logic in the InsertBlockData method was changed to increment both the row count and the new operation count per batch, rather than only once after all batches.

Changes

File(s) Change Summary
internal/metrics/metrics.go Added ClickHouseMainStorageInsertOperations Prometheus counter metric declaration.
internal/storage/clickhouse.go Updated InsertBlockData to increment row and insert operation metrics per batch insert.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ClickHouseConnector
    participant Metrics

    Client->>ClickHouseConnector: InsertBlockData(data)
    loop For each batch in data
        ClickHouseConnector->>ClickHouseConnector: Insert batch into ClickHouse
        ClickHouseConnector->>Metrics: Increment RowsInserted by batch size
        ClickHouseConnector->>Metrics: Increment InsertOperations by 1
    end
    ClickHouseConnector-->>Client: Return result
Loading

Possibly related PRs

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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 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
Collaborator Author

iuwqyir commented Jul 8, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@iuwqyir iuwqyir marked this pull request as ready for review July 8, 2025 21:47
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

🧹 Nitpick comments (1)
internal/storage/clickhouse.go (1)

1361-1362: LGTM: Proper per-batch metric tracking implementation.

The metrics are correctly updated after successful batch operations:

  • ClickHouseMainStorageRowsInserted tracks the exact number of rows in each batch (end - i)
  • ClickHouseMainStorageInsertOperations increments once per batch operation
  • Updates occur after successful batch.Send(), ensuring accurate tracking

The mathematical logic is sound - summing batch sizes will equal the total rows processed.

Consider standardizing the metric update approach across all insert methods. Currently:

  • InsertBlockData updates metrics per batch (new approach)
  • insertTransactions, insertLogs, insertTraces update metrics once at the end (lines 275, 338, 410)

For consistency, you might want to either:

  1. Update other methods to use per-batch tracking for better granularity
  2. Or document why InsertBlockData requires different tracking granularity
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 184181a and fc826ed.

📒 Files selected for processing (2)
  • internal/metrics/metrics.go (1 hunks)
  • internal/storage/clickhouse.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
internal/storage/clickhouse.go (1)
internal/metrics/metrics.go (2)
  • ClickHouseMainStorageRowsInserted (163-166)
  • ClickHouseMainStorageInsertOperations (158-161)
🔇 Additional comments (1)
internal/metrics/metrics.go (1)

158-161: LGTM: Well-defined metric for tracking insert operations.

The new Prometheus counter metric is properly defined with appropriate naming conventions and clear help text. It complements the existing row count metric by providing visibility into the number of distinct insert operations.

@iuwqyir iuwqyir merged commit 04f775a into main Jul 8, 2025
6 of 8 checks passed
@iuwqyir iuwqyir deleted the 07-09-track_number_of_clickhouse_insert_operations branch July 8, 2025 21:57
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