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
Feature: add metrics in metasrv #5208
Merged
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fe2e3af
Feature: add metrics in metasrv
lichuang 90c9918
Merge remote-tracking branch 'upstream/main' into metasrv_add_metrics
lichuang f7e4863
Feature: fix clippy warnings
lichuang 83d92c8
Merge branch 'main' into metasrv_add_metrics
mergify[bot] 9d7d0a9
Merge branch 'main' into metasrv_add_metrics
mergify[bot] c975289
Merge branch 'main' into metasrv_add_metrics
mergify[bot] 79c1ae4
Merge branch 'main' into metasrv_add_metrics
mergify[bot] fc3a30e
Merge branch 'main' into metasrv_add_metrics
mergify[bot] 0d7a1d2
Merge branch 'main' into metasrv_add_metrics
mergify[bot] e8857c6
fix test case error
lichuang 3d02256
Merge remote-tracking branch 'upstream/main' into metasrv_add_metrics
lichuang 45a5364
Merge branch 'metasrv_add_metrics' of github.com:lichuang/databend in…
lichuang ce1ba1e
fix compile error
lichuang a843b9e
Merge branch 'main' into metasrv_add_metrics
mergify[bot] 61ac4ed
Merge branch 'main' into metasrv_add_metrics
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 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 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 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 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 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 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,78 @@ | ||
// Copyright 2021 Datafuse Labs. | ||
// | ||
// Licensed 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. | ||
|
||
use std::sync::Once; | ||
|
||
use common_metrics::Gauge; | ||
use common_metrics::MetricOption; | ||
use once_cell::sync::OnceCell; | ||
|
||
pub const META_NAMESPACE: &str = "metasrv"; | ||
pub const SERVER_SUBSYSTEM: &str = "server"; | ||
|
||
pub struct MetaMetrics { | ||
has_leader: Option<Gauge>, | ||
} | ||
|
||
static INSTANCE: OnceCell<MetaMetrics> = OnceCell::new(); | ||
|
||
impl Default for MetaMetrics { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl MetaMetrics { | ||
pub fn new() -> MetaMetrics { | ||
MetaMetrics { has_leader: None } | ||
} | ||
|
||
pub fn instance() -> &'static MetaMetrics { | ||
INSTANCE.get().expect("metametrics is not initialized") | ||
} | ||
|
||
/// Init all meta metrics. | ||
pub fn register(self: &mut MetaMetrics) { | ||
// add has_leader metric | ||
let has_leader = MetricOption::new( | ||
"has_leader".to_string(), | ||
META_NAMESPACE.to_string(), | ||
SERVER_SUBSYSTEM.to_string(), | ||
"Whether or not a leader exists.".to_string(), | ||
); | ||
self.has_leader = Some(common_metrics::register_gauge(has_leader)); | ||
} | ||
|
||
pub fn has_leader(&self, has_leader: bool) { | ||
let a = self.has_leader.as_ref().unwrap(); | ||
a.set(if has_leader { 1.0 } else { 0.0 }); | ||
} | ||
} | ||
|
||
pub fn init_meta_metrics_recorder() { | ||
static START: Once = Once::new(); | ||
START.call_once(init_meta_recorder) | ||
} | ||
|
||
/// Init meta metrics recorder. | ||
fn init_meta_recorder() { | ||
let mut meta_metrics = MetaMetrics::new(); | ||
meta_metrics.register(); | ||
|
||
let _ = INSTANCE.set(meta_metrics); | ||
} | ||
|
||
pub fn set_meta_metrics_has_leader(has_leader: bool) { | ||
MetaMetrics::instance().has_leader(has_leader); | ||
} |
This file contains 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 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
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.
I think this can be derived.
But it's not a big deal and should not be a blocker for this PR.