Skip to content

Commit

Permalink
Expose REST API to fetch runtime metrics in prometheus format
Browse files Browse the repository at this point in the history
Co-authored-by:jaystarshot <jay.narale@uber.com>
  • Loading branch information
karteekmurthys committed May 15, 2024
1 parent e484062 commit 1d56b00
Show file tree
Hide file tree
Showing 17 changed files with 620 additions and 15 deletions.
4 changes: 3 additions & 1 deletion presto-native-execution/etc/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ discovery.uri=http://127.0.0.1:58215
presto.version=testversion
http-server.http.port=7777
shutdown-onset-sec=1
register-test-functions=true
register-test-functions=true
runtime-metrics-collection-enabled=true
runtime-metrics-reporter=prometheus
1 change: 1 addition & 0 deletions presto-native-execution/presto_cpp/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ target_link_libraries(
presto_common
presto_exception
presto_http
presto_runtime_metrics
presto_operators
velox_aggregates
velox_caching
Expand Down
6 changes: 0 additions & 6 deletions presto-native-execution/presto_cpp/main/PrestoMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <glog/logging.h>
#include "presto_cpp/main/PrestoServer.h"
#include "presto_cpp/main/common/Utils.h"
#include "velox/common/base/StatsReporter.h"

DEFINE_string(etc_dir, ".", "etc directory for presto configuration");

Expand All @@ -30,8 +29,3 @@ int main(int argc, char* argv[]) {
presto.run();
PRESTO_SHUTDOWN_LOG(INFO) << "Exiting main()";
}

// Initialize singleton for the reporter.
folly::Singleton<facebook::velox::BaseStatsReporter> reporter([]() {
return new facebook::velox::DummyStatsReporter();
});
19 changes: 15 additions & 4 deletions presto-native-execution/presto_cpp/main/PrestoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "presto_cpp/main/common/ConfigReader.h"
#include "presto_cpp/main/common/Counters.h"
#include "presto_cpp/main/common/Utils.h"
#include "presto_cpp/main/common/runtime-metrics/PrestoRuntimeMetricsReporter.h"
#include "presto_cpp/main/http/filters/AccessLogFilter.h"
#include "presto_cpp/main/http/filters/HttpEndpointLatencyFilter.h"
#include "presto_cpp/main/http/filters/InternalAuthenticationFilter.h"
Expand All @@ -36,7 +37,6 @@
#include "presto_cpp/main/operators/PartitionAndSerialize.h"
#include "presto_cpp/main/operators/ShuffleRead.h"
#include "presto_cpp/main/operators/UnsafeRowExchangeSource.h"
#include "presto_cpp/main/types/PrestoToVeloxConnector.h"
#include "presto_cpp/main/types/PrestoToVeloxQueryPlan.h"
#include "velox/common/base/Counters.h"
#include "velox/common/base/StatsReporter.h"
Expand Down Expand Up @@ -335,6 +335,18 @@ void PrestoServer::run() {
proxygen::ResponseHandler* downstream) {
server->reportServerInfo(downstream);
});
if (systemConfig->enableRuntimeMetricsCollection()) {
httpServer_->registerGet(
"/v1/info/metrics",
[](proxygen::HTTPMessage* /*message*/,
const std::vector<std::unique_ptr<folly::IOBuf>>& /*body*/,
proxygen::ResponseHandler* downstream) {
auto reporter = std::dynamic_pointer_cast<
facebook::presto::prometheus::PrometheusReporter>(
folly::Singleton<facebook::velox::BaseStatsReporter>::try_get());
http::sendOkResponse(downstream, reporter->getSerializedMetrics());
});
}
httpServer_->registerGet(
"/v1/info/state",
[server = this](
Expand Down Expand Up @@ -1104,8 +1116,7 @@ std::string PrestoServer::getBaseSpillDirectory() const {
}

void PrestoServer::enableRuntimeMetricReporting() {
// This flag must be set to register the counters.
facebook::velox::BaseStatsReporter::registered = true;
registerRuntimeMetricsReporters();
registerStatsCounters();
}

Expand Down Expand Up @@ -1134,7 +1145,6 @@ void PrestoServer::populateMemAndCPUInfo() {
poolInfo.queryMemoryAllocations.insert(
{queryId, {protocol::MemoryAllocation{"total", bytes}}});
++numContexts;
poolInfo.reservedBytes += bytes;
});
RECORD_METRIC_VALUE(kCounterNumQueryContexts, numContexts);
cpuMon_.update();
Expand Down Expand Up @@ -1173,6 +1183,7 @@ void PrestoServer::reportServerInfo(proxygen::ResponseHandler* downstream) {
http::sendOkResponse(downstream, json(serverInfo));
}

void PrestoServer::reportWorkerMetrics(proxygen::ResponseHandler* downstream) {}
void PrestoServer::reportNodeStatus(proxygen::ResponseHandler* downstream) {
http::sendOkResponse(downstream, json(fetchNodeStatus()));
}
Expand Down
2 changes: 2 additions & 0 deletions presto-native-execution/presto_cpp/main/PrestoServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ class PrestoServer {

void reportNodeStatus(proxygen::ResponseHandler* downstream);

void reportWorkerMetrics(proxygen::ResponseHandler* downstream);

protocol::NodeStatus fetchNodeStatus();

void populateMemAndCPUInfo();
Expand Down
6 changes: 3 additions & 3 deletions presto-native-execution/presto_cpp/main/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ set_property(TARGET presto_exception PROPERTY JOB_POOL_LINK
target_link_libraries(presto_common velox_config velox_core velox_exception)
set_property(TARGET presto_common PROPERTY JOB_POOL_LINK presto_link_job_pool)

if(PRESTO_ENABLE_TESTING)
add_subdirectory(tests)
endif()
add_subdirectory(runtime-metrics)

add_subdirectory(tests)
6 changes: 5 additions & 1 deletion presto-native-execution/presto_cpp/main/common/Configs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ SystemConfig::SystemConfig() {
STR_PROP(kCacheVeloxTtlThreshold, "2d"),
STR_PROP(kCacheVeloxTtlCheckInterval, "1h"),
BOOL_PROP(kEnableRuntimeMetricsCollection, false),
};
STR_PROP(kRuntimeMetricReporter, "dummy")};
}

SystemConfig* SystemConfig::instance() {
Expand Down Expand Up @@ -627,6 +627,10 @@ bool SystemConfig::enableRuntimeMetricsCollection() const {
return optionalProperty<bool>(kEnableRuntimeMetricsCollection).value();
}

const std::string& SystemConfig::getRuntimeMetricReporter() const {
return optionalProperty<std::string>(kRuntimeMetricReporter).value();
}

NodeConfig::NodeConfig() {
registeredProps_ =
std::unordered_map<std::string, folly::Optional<std::string>>{
Expand Down
4 changes: 4 additions & 0 deletions presto-native-execution/presto_cpp/main/common/Configs.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ class SystemConfig : public ConfigBase {
static constexpr std::string_view kEnableRuntimeMetricsCollection{
"runtime-metrics-collection-enabled"};

static constexpr std::string_view kRuntimeMetricReporter{
"runtime-metrics-reporter"};
/// Specifies the memory arbitrator kind. If it is empty, then there is no
/// memory arbitration.
static constexpr std::string_view kMemoryArbitratorKind{
Expand Down Expand Up @@ -710,6 +712,8 @@ class SystemConfig : public ConfigBase {
std::chrono::duration<double> cacheVeloxTtlCheckInterval() const;

bool enableRuntimeMetricsCollection() const;

const std::string& getRuntimeMetricReporter() const;
};

/// Provides access to node properties defined in node.properties file.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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_runtime_metrics PrestoRuntimeMetricsReporter.cpp)

target_link_libraries(presto_runtime_metrics prometheus_reporter)

set_property(TARGET presto_runtime_metrics PROPERTY JOB_POOL_LINK
presto_link_job_pool)

add_subdirectory(prometheus-reporter)
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Created by Karteek Murthy Samba Murthy on 5/14/24.
//

#include "presto_cpp/main/common/runtime-metrics/PrestoRuntimeMetricsReporter.h"

namespace facebook::presto {
namespace {

std::unordered_map<
std::string,
std::unique_ptr<facebook::velox::BaseStatsReporter>>&
metricsReporters() {
static std::unordered_map<
std::string,
std::unique_ptr<facebook::velox::BaseStatsReporter>>
metricsReporters;
return metricsReporters;
}

void initLabelsForPrometheusReporter(prometheus::Labels& labels) {
facebook::velox::BaseStatsReporter::registered = true;
auto nodeConfig = facebook::presto::NodeConfig::instance();
const std::string cluster = nodeConfig->nodeEnvironment();
const char* hostName = std::getenv("HOSTNAME");
const std::string worker = !hostName ? "" : hostName;
labels.emplace("cluster", cluster);
labels.emplace("worker", worker);
}
} // namespace

void registerRuntimeMetricsReporters() {
prometheus::Labels labels;
initLabelsForPrometheusReporter(labels);
auto prometheusReporter =
std::make_unique<prometheus::PrometheusReporter>(labels);
metricsReporters().insert(
{kPrometheusReporterName.data(), std::move(prometheusReporter)});
metricsReporters().insert(
{kDummyReporterName.data(),
std::make_unique<facebook::velox::DummyStatsReporter>()});
}

// Initialize singleton for the reporter
folly::Singleton<facebook::velox::BaseStatsReporter> reporter([]() {
auto systemConfig = facebook::presto::SystemConfig::instance();
auto reporterName = systemConfig->getRuntimeMetricReporter();
return metricsReporters()[reporterName].get();
});
}; // namespace facebook::presto.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 "presto_cpp/main/common/Configs.h"
#include "presto_cpp/main/common/runtime-metrics/prometheus-reporter/PrometheusReporter.h"

namespace facebook::presto {

constexpr std::string_view kPrometheusReporterName = "prometheus";
constexpr std::string_view kDummyReporterName = "dummy";

void registerRuntimeMetricsReporters();
}; // namespace facebook::presto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
find_package(prometheus-cpp CONFIG REQUIRED)

add_library(prometheus_reporter PrometheusReporter.cpp)

target_link_libraries(prometheus_reporter presto_common prometheus-cpp::core)

set_property(TARGET prometheus_reporter PROPERTY JOB_POOL_LINK
presto_link_job_pool)
add_subdirectory(tests)
Loading

0 comments on commit 1d56b00

Please sign in to comment.