-
Notifications
You must be signed in to change notification settings - Fork 5.5k
[Native] Add prometheus metric exporter using prometheus c++ library #21753
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
Closed
Closed
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
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
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
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,19 @@ | ||
| # 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. | ||
|
|
||
| add_library(presto_metrics OBJECT PrometheusStatsReporter.cpp) | ||
|
|
||
| target_link_libraries(presto_metrics presto_common velox_config velox_core velox_exception prometheus-cpp::core prometheus-cpp::pull) | ||
|
|
||
| if(PRESTO_ENABLE_TESTING) | ||
| add_subdirectory(tests) | ||
| endif() |
170 changes: 170 additions & 0 deletions
170
presto-native-execution/presto_cpp/metrics/PrometheusStatsReporter.cpp
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,170 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #include "PrometheusStatsReporter.h" | ||
| #include "velox/common/base/StatsReporter.h" | ||
| #include "prometheus/client_metric.h" | ||
| #include "prometheus/counter.h" | ||
| #include "prometheus/summary.h" | ||
| #include "prometheus/gauge.h" | ||
|
|
||
|
|
||
| namespace facebook::presto { | ||
|
|
||
| void PrometheusStatsReporter::registerMetricExportType(folly::StringPiece key, facebook::velox::StatType statType) const { | ||
| registerMetricExportType(key.start(), statType); | ||
| } | ||
|
|
||
|
|
||
| void PrometheusStatsReporter::registerMetricExportType(const char* key, velox::StatType statType) const { | ||
| std::string keyStr = sanitizeMetricName(key); | ||
| statTypeMap.insert({keyStr, statType}); | ||
| LOG(INFO) << "Registered metric: " << keyStr << " with type: " << statTypeToString(statType); | ||
|
|
||
| switch (statType) { | ||
| case velox::StatType::AVG:{ | ||
| registerSummaryMetricForAverageStatType(keyStr); | ||
| break; | ||
| } | ||
| case velox::StatType::SUM: { | ||
| registerGaugeMetric(keyStr); | ||
| break; | ||
| } | ||
| case velox::StatType::COUNT: { | ||
| registerCounterMetric(keyStr); | ||
| break; | ||
| } | ||
| case velox::StatType::RATE: | ||
| // ToDo: RATE Type is not yet used anywhere | ||
| default: | ||
| LOG(WARNING) << "Unsupported stat type for key: " << keyStr; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| void PrometheusStatsReporter::addMetricValue(const char* key, size_t value) | ||
| const { | ||
| addMetricValue(std::string(key), value); | ||
| } | ||
|
|
||
| void PrometheusStatsReporter::addMetricValue(folly::StringPiece key, size_t value) | ||
| const { | ||
| addMetricValue(key.str(), value); | ||
| } | ||
|
|
||
| void PrometheusStatsReporter::addMetricValue(const std::string& key, size_t value) const { | ||
| std::string keyStr = sanitizeMetricName(key); | ||
| auto statTypeIt = statTypeMap.find(keyStr); | ||
| if (statTypeIt == statTypeMap.end()) { | ||
| LOG(WARNING) << "Metric not registered: " << keyStr; | ||
| return; | ||
| } | ||
|
|
||
| switch (statTypeIt->second) { | ||
| case velox::StatType::AVG: { | ||
| updateSummaryMetric(keyStr, value); | ||
| break; | ||
| } | ||
| case velox::StatType::SUM: { | ||
| updateGaugeMetric(keyStr, value); | ||
| break; | ||
| } | ||
| case velox::StatType::COUNT: { | ||
| updateCounterMetric(keyStr, value); | ||
| break; | ||
| } | ||
| case velox::StatType::RATE: | ||
| // ToDo: RATE Type is not yet used anywhere | ||
| default: | ||
| LOG(WARNING) << "Unsupported stat type for key: " << keyStr; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| void PrometheusStatsReporter::registerSummaryMetricForAverageStatType(const std::string& keyStr) const { | ||
| auto& summaryFamily = prometheus::BuildSummary() | ||
| .Name(keyStr) | ||
| .Help("Summary for " + keyStr) | ||
| .Register(*registry); | ||
| auto& summary = summaryFamily.Add({{"cluster", cluster_}, {"node", node_}, {"host", host_}}, | ||
| prometheus::Summary::Quantiles{{0.5, 0.05}}); | ||
| summaryMap.insert({keyStr, &summary}); | ||
| } | ||
|
|
||
| void PrometheusStatsReporter::registerGaugeMetric(const std::string& keyStr) const { | ||
| auto& gaugeFamily = prometheus::BuildGauge() | ||
| .Name(keyStr) | ||
| .Help("Gauge for " + keyStr) | ||
| .Register(*registry); | ||
| auto& gauge = gaugeFamily.Add({{"cluster", cluster_}, {"node", node_}, {"host", host_}}); | ||
| gaugeMap.insert({keyStr, &gauge}); | ||
| } | ||
|
|
||
| void PrometheusStatsReporter::registerCounterMetric(const std::string& keyStr) const { | ||
| auto& counterFamily = prometheus::BuildCounter() | ||
| .Name(keyStr) | ||
| .Help("Counter for " + keyStr) | ||
| .Register(*registry); | ||
| auto& counter = counterFamily.Add({{"cluster", cluster_}, {"node", node_}, {"host", host_}}); | ||
| counterMap.insert({keyStr, &counter}); | ||
| } | ||
|
|
||
| void PrometheusStatsReporter::updateSummaryMetric(const std::string& keyStr, size_t value) const { | ||
| auto summaryIt = summaryMap.find(keyStr); | ||
| if (summaryIt != summaryMap.end()) { | ||
| summaryIt->second->Observe(static_cast<double>(value)); | ||
| } else { | ||
| LOG(WARNING) << "Summary metric not found: " << keyStr; | ||
| } | ||
| } | ||
|
|
||
| void PrometheusStatsReporter::updateGaugeMetric(const std::string& keyStr, size_t value) const { | ||
| auto gaugeIt = gaugeMap.find(keyStr); | ||
| if (gaugeIt != gaugeMap.end()) { | ||
| gaugeIt->second->Increment(static_cast<double>(value)); | ||
| } else { | ||
| LOG(WARNING) << "Gauge metric not found: " << keyStr; | ||
| } | ||
| } | ||
|
|
||
| void PrometheusStatsReporter::updateCounterMetric(const std::string& keyStr, size_t value) const { | ||
| auto counterIt = counterMap.find(keyStr); | ||
| if (counterIt != counterMap.end()) { | ||
| counterIt->second->Increment(static_cast<double>(value)); | ||
| } else { | ||
| LOG(WARNING) << "Counter metric not found: " << keyStr; | ||
| } | ||
| } | ||
|
|
||
| std::string PrometheusStatsReporter::sanitizeMetricName( | ||
| const std::string& name) { | ||
| std::string sanitized = name; | ||
| std::replace(sanitized.begin(), sanitized.end(), '.', '_'); | ||
| return sanitized; | ||
| } | ||
| std::string PrometheusStatsReporter::statTypeToString( | ||
| velox::StatType statType) const { | ||
| switch (statType) { | ||
| case velox::StatType::AVG: return "AVG"; | ||
| case velox::StatType::SUM: return "SUM"; | ||
| case velox::StatType::RATE: return "RATE"; | ||
| case velox::StatType::COUNT: return "COUNT"; | ||
| default: return "Unknown StatType"; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } // namespace facebook::presto | ||
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.
0.5 quantile is not the average, instead it is the median value. This is as good as reporting instantaneous value.
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.
It has sum and count so avg can be computed as well