-
Notifications
You must be signed in to change notification settings - Fork 45
[metrics] Add client metrics and prometheus example #617
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
Open
charlesdong1991
wants to merge
2
commits into
apache:main
Choose a base branch
from
charlesdong1991:metrics-doc-update-and-prometheus-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+354
−9
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Exposes Fluss client metrics on a Prometheus scrape endpoint. | ||
| //! | ||
| //! Run a local cluster, then: | ||
| //! ```shell | ||
| //! cargo run -p fluss-examples --example example-prometheus-metrics | ||
| //! curl http://localhost:9000/metrics | ||
| //! ``` | ||
| //! The endpoint exposes `fluss_client_writer_*`, `fluss_client_scanner_*`, and | ||
| //! `fluss_client_requests_*` series produced by the workload below. | ||
|
|
||
| #[cfg(not(target_env = "msvc"))] | ||
| #[global_allocator] | ||
| static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; | ||
|
|
||
| use clap::Parser; | ||
| use fluss::client::FlussConnection; | ||
| use fluss::config::Config; | ||
| use fluss::error::Result; | ||
| use fluss::metadata::{DataTypes, Schema, TableDescriptor, TablePath}; | ||
| use fluss::row::{DataGetters, GenericRow}; | ||
| use metrics_exporter_prometheus::PrometheusBuilder; | ||
| use std::time::Duration; | ||
|
|
||
| #[tokio::main] | ||
| pub async fn main() -> Result<()> { | ||
| // Install the global Prometheus recorder BEFORE creating any connection, | ||
| // writer, or scanner: the client caches metric handles on first use and | ||
| // binds them to whichever recorder is installed at that moment. | ||
| // | ||
| // `build()` (rather than `install()`) hands back a `PrometheusHandle` so the | ||
| // example can read its own metrics back and self-verify; the returned | ||
| // exporter future runs the HTTP scrape endpoint. | ||
| let (recorder, exporter) = PrometheusBuilder::new() | ||
| .with_http_listener(([0, 0, 0, 0], 9000)) | ||
| .build() | ||
| .expect("failed to build Prometheus recorder"); | ||
| let metrics_handle = recorder.handle(); | ||
| metrics::set_global_recorder(recorder).expect("failed to install global recorder"); | ||
| tokio::spawn(exporter); | ||
| println!("Metrics exposed on http://localhost:9000/metrics"); | ||
|
|
||
| let mut config = Config::parse(); | ||
| config.bootstrap_servers = "127.0.0.1:9123".to_string(); | ||
|
|
||
| let conn = FlussConnection::new(config).await?; | ||
| let admin = conn.get_admin()?; | ||
|
|
||
| let table_path = TablePath::new("fluss", "rust_prometheus_metrics"); | ||
| let table_descriptor = TableDescriptor::builder() | ||
| .schema( | ||
| Schema::builder() | ||
| .column("id", DataTypes::int()) | ||
| .column("message", DataTypes::string()) | ||
| .build()?, | ||
| ) | ||
| .build()?; | ||
| admin | ||
| .create_table(&table_path, &table_descriptor, true) | ||
| .await?; | ||
|
|
||
| let table = conn.get_table(&table_path).await?; | ||
| let append_writer = table.new_append()?.create_writer()?; | ||
| let log_scanner = table.new_scan().create_log_scanner()?; | ||
| log_scanner.subscribe(0, 0).await?; | ||
|
|
||
| // Continuously write and read so the metrics keep updating and can be | ||
| // observed over multiple Prometheus scrapes. | ||
| let rows_per_iter = 100; | ||
| let mut id = 0i32; | ||
| let mut verified = false; | ||
| loop { | ||
| for _ in 0..rows_per_iter { | ||
| let mut row = GenericRow::new(2); | ||
| row.set_field(0, id); | ||
| row.set_field(1, "metrics demo"); | ||
| append_writer.append(&row)?; | ||
| id += 1; | ||
| } | ||
| append_writer.flush().await?; | ||
|
|
||
| let scan_records = log_scanner.poll(Duration::from_secs(1)).await?; | ||
| let mut count = 0; | ||
| for record in scan_records { | ||
| let row = record.row(); | ||
| let _ = (row.get_int(0)?, row.get_string(1)?, record.offset()); | ||
| count += 1; | ||
| } | ||
| println!( | ||
| "appended {rows_per_iter} rows, polled {count} rows; scrape /metrics to see counters" | ||
| ); | ||
|
|
||
| // After the first flush every appended row has been acknowledged, so the | ||
| // writer counter must have advanced by at least the rows we sent (retries | ||
| // can push it higher). This proves the recorder is wired up correctly. | ||
| if !verified { | ||
| let rendered = metrics_handle.render(); | ||
| let sent = counter_value(&rendered, "fluss_client_writer_records_send_total"); | ||
| assert!( | ||
| sent.is_some_and(|v| v >= rows_per_iter as f64), | ||
| "expected fluss_client_writer_records_send_total >= {rows_per_iter}, got {sent:?}\n{rendered}" | ||
| ); | ||
| println!("self-check OK: records_send_total = {}", sent.unwrap()); | ||
| verified = true; | ||
| } | ||
|
|
||
| tokio::time::sleep(Duration::from_secs(1)).await; | ||
| } | ||
| } | ||
|
|
||
| /// Parse the value of an unlabeled counter/gauge line from rendered Prometheus | ||
| /// exposition text (lines shaped `metric_name <value>`). | ||
| fn counter_value(rendered: &str, name: &str) -> Option<f64> { | ||
| let prefix = format!("{name} "); | ||
| rendered | ||
| .lines() | ||
| .find(|line| line.starts_with(&prefix)) | ||
| .and_then(|line| line.rsplit(' ').next()) | ||
| .and_then(|value| value.parse().ok()) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to break out of the loop once verification is done? As is, the example would run indefinitely (is that your intention?)