lordgamez commented on code in PR #1532: URL: https://github.com/apache/nifi-minifi-cpp/pull/1532#discussion_r1222652869
########## libminifi/src/core/state/LogMetricsPublisher.cpp: ########## @@ -0,0 +1,129 @@ +/** + * + * 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. + */ +#include "core/state/LogMetricsPublisher.h" + +#include "core/Resource.h" +#include "properties/Configuration.h" +#include "core/TypedValues.h" + +namespace org::apache::nifi::minifi::state { + +LogMetricsPublisher::~LogMetricsPublisher() { + if (metrics_logger_thread_) { + metrics_logger_thread_->stopAndJoin(); + } +} + +void LogMetricsPublisher::initialize(const std::shared_ptr<Configure>& configuration, const std::shared_ptr<state::response::ResponseNodeLoader>& response_node_loader) { + state::MetricsPublisher::initialize(configuration, response_node_loader); + readLoggingInterval(); + readLogLevel(); +} + +void LogMetricsPublisher::logMetrics() { + do { + response::SerializedResponseNode parent_node; + parent_node.name = "LogMetrics"; + { + std::lock_guard<std::mutex> lock(response_nodes_mutex_); + for (const auto& response_node : response_nodes_) { + response::SerializedResponseNode metric_response_node; + metric_response_node.name = response_node->getName(); + for (const auto& serialized_node : response_node->serialize()) { + metric_response_node.children.push_back(serialized_node); + } + parent_node.children.push_back(metric_response_node); + } + } + utils::LogUtils::logWithLevel(logger_, log_level_, parent_node.to_pretty_string().c_str()); + } while (!utils::StoppableThread::waitForStopRequest(logging_interval_)); +} + +void LogMetricsPublisher::readLoggingInterval() { + gsl_Expects(configuration_); + if (auto logging_interval_str = configuration_->get(Configure::nifi_metrics_publisher_log_metrics_logging_interval)) { + if (auto logging_interval = minifi::core::TimePeriodValue::fromString(logging_interval_str.value())) { + logging_interval_ = logging_interval->getMilliseconds(); + logger_->log_info("Metric logging interval is set to %lld milliseconds", logging_interval_.count()); Review Comment: Thanks, updated in 70c67450a591c251b152759bab6b2bb8c1422bb5 ########## libminifi/src/core/state/LogMetricsPublisher.cpp: ########## @@ -0,0 +1,129 @@ +/** + * + * 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. + */ +#include "core/state/LogMetricsPublisher.h" + +#include "core/Resource.h" +#include "properties/Configuration.h" +#include "core/TypedValues.h" + +namespace org::apache::nifi::minifi::state { + +LogMetricsPublisher::~LogMetricsPublisher() { + if (metrics_logger_thread_) { + metrics_logger_thread_->stopAndJoin(); + } +} + +void LogMetricsPublisher::initialize(const std::shared_ptr<Configure>& configuration, const std::shared_ptr<state::response::ResponseNodeLoader>& response_node_loader) { + state::MetricsPublisher::initialize(configuration, response_node_loader); + readLoggingInterval(); + readLogLevel(); +} + +void LogMetricsPublisher::logMetrics() { + do { + response::SerializedResponseNode parent_node; + parent_node.name = "LogMetrics"; + { + std::lock_guard<std::mutex> lock(response_nodes_mutex_); + for (const auto& response_node : response_nodes_) { + response::SerializedResponseNode metric_response_node; + metric_response_node.name = response_node->getName(); + for (const auto& serialized_node : response_node->serialize()) { + metric_response_node.children.push_back(serialized_node); + } + parent_node.children.push_back(metric_response_node); + } + } + utils::LogUtils::logWithLevel(logger_, log_level_, parent_node.to_pretty_string().c_str()); + } while (!utils::StoppableThread::waitForStopRequest(logging_interval_)); +} + +void LogMetricsPublisher::readLoggingInterval() { + gsl_Expects(configuration_); + if (auto logging_interval_str = configuration_->get(Configure::nifi_metrics_publisher_log_metrics_logging_interval)) { + if (auto logging_interval = minifi::core::TimePeriodValue::fromString(logging_interval_str.value())) { + logging_interval_ = logging_interval->getMilliseconds(); + logger_->log_info("Metric logging interval is set to %lld milliseconds", logging_interval_.count()); + return; + } else { + logger_->log_error("Configured logging interval '%s' is invalid!", logging_interval_str.value()); + } + } + + throw Exception(GENERAL_EXCEPTION, "Metrics logging interval not configured for log metrics publisher!"); +} + +void LogMetricsPublisher::readLogLevel() { + gsl_Expects(configuration_); + if (auto log_level_str = configuration_->get(Configure::nifi_metrics_publisher_log_metrics_log_level)) { + auto full_caps_log_level_str = utils::StringUtils::toUpper(*log_level_str); + log_level_ = utils::LogUtils::LogLevelOption::parse(full_caps_log_level_str.c_str(), utils::LogUtils::LogLevelOption::LOGGING_INFO); Review Comment: Thanks, I did not know about this, updated in 70c67450a591c251b152759bab6b2bb8c1422bb5 ########## libminifi/src/core/state/LogMetricsPublisher.cpp: ########## @@ -0,0 +1,129 @@ +/** + * + * 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. + */ +#include "core/state/LogMetricsPublisher.h" + +#include "core/Resource.h" +#include "properties/Configuration.h" +#include "core/TypedValues.h" + +namespace org::apache::nifi::minifi::state { + +LogMetricsPublisher::~LogMetricsPublisher() { + if (metrics_logger_thread_) { + metrics_logger_thread_->stopAndJoin(); + } +} + +void LogMetricsPublisher::initialize(const std::shared_ptr<Configure>& configuration, const std::shared_ptr<state::response::ResponseNodeLoader>& response_node_loader) { + state::MetricsPublisher::initialize(configuration, response_node_loader); + readLoggingInterval(); + readLogLevel(); +} + +void LogMetricsPublisher::logMetrics() { + do { + response::SerializedResponseNode parent_node; + parent_node.name = "LogMetrics"; + { + std::lock_guard<std::mutex> lock(response_nodes_mutex_); + for (const auto& response_node : response_nodes_) { + response::SerializedResponseNode metric_response_node; + metric_response_node.name = response_node->getName(); + for (const auto& serialized_node : response_node->serialize()) { + metric_response_node.children.push_back(serialized_node); + } + parent_node.children.push_back(metric_response_node); + } + } + utils::LogUtils::logWithLevel(logger_, log_level_, parent_node.to_pretty_string().c_str()); + } while (!utils::StoppableThread::waitForStopRequest(logging_interval_)); +} + +void LogMetricsPublisher::readLoggingInterval() { + gsl_Expects(configuration_); + if (auto logging_interval_str = configuration_->get(Configure::nifi_metrics_publisher_log_metrics_logging_interval)) { + if (auto logging_interval = minifi::core::TimePeriodValue::fromString(logging_interval_str.value())) { + logging_interval_ = logging_interval->getMilliseconds(); + logger_->log_info("Metric logging interval is set to %lld milliseconds", logging_interval_.count()); + return; + } else { + logger_->log_error("Configured logging interval '%s' is invalid!", logging_interval_str.value()); + } + } + + throw Exception(GENERAL_EXCEPTION, "Metrics logging interval not configured for log metrics publisher!"); +} + +void LogMetricsPublisher::readLogLevel() { + gsl_Expects(configuration_); + if (auto log_level_str = configuration_->get(Configure::nifi_metrics_publisher_log_metrics_log_level)) { + auto full_caps_log_level_str = utils::StringUtils::toUpper(*log_level_str); + log_level_ = utils::LogUtils::LogLevelOption::parse(full_caps_log_level_str.c_str(), utils::LogUtils::LogLevelOption::LOGGING_INFO); + logger_->log_info("Metric log level is set to %s", log_level_.toString()); + return; + } + + logger_->log_info("Metric log level is set to INFO by default"); +} + +void LogMetricsPublisher::clearMetricNodes() { + { + std::lock_guard<std::mutex> lock(response_nodes_mutex_); + logger_->log_debug("Clearing all metric nodes."); + response_nodes_.clear(); + } + if (metrics_logger_thread_) { + metrics_logger_thread_->stopAndJoin(); + metrics_logger_thread_.reset(); + } +} + +void LogMetricsPublisher::loadMetricNodes() { + gsl_Expects(response_node_loader_ && configuration_); + auto metric_classes_str = configuration_->get(minifi::Configuration::nifi_metrics_publisher_log_metrics_publisher_metrics); + if (!metric_classes_str || metric_classes_str->empty()) { + metric_classes_str = configuration_->get(minifi::Configuration::nifi_metrics_publisher_metrics); + } + bool response_nodes_empty = true; + if (metric_classes_str && !metric_classes_str->empty()) { + auto metric_classes = utils::StringUtils::split(*metric_classes_str, ","); + std::lock_guard<std::mutex> lock(response_nodes_mutex_); + for (const std::string& clazz : metric_classes) { + auto loaded_response_nodes = response_node_loader_->loadResponseNodes(clazz); + if (loaded_response_nodes.empty()) { + logger_->log_warn("Metric class '%s' could not be loaded.", clazz); + continue; + } + response_nodes_.insert(response_nodes_.end(), loaded_response_nodes.begin(), loaded_response_nodes.end()); + } + response_nodes_empty = response_nodes_.empty(); + } + if (response_nodes_empty) { Review Comment: I think it was due to the `response_nodes_` being changed under the lock and didn't want to access it outside of it, but on second thought it's not needed as the lock is only used for synchronizing the logger thread and the changes to the `response_nodes_`, so it does not make any difference. Updated in 70c67450a591c251b152759bab6b2bb8c1422bb5 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
