szaszm commented on code in PR #1532:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1532#discussion_r1221798519


##########
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:
   The approach we took in similar situations in the past is to use 
[direct-list-initialization](https://en.cppreference.com/w/cpp/language/list_initialization)
 with `int64_t`, and print that with the `PRId64` macro from `<cinttypes>`. The 
standard specifies common duration unit aliases to be at least 64 bits signed 
integer (or less in less precise cases). With `int64_t{duration.count()}` and 
`PRId64`, we can guarantee correct printing on all platforms where `int64_t` 
can represent all possible values of the underlying signed integer type, and 
where this isn't the case, we fail with a compilation error, due to narrowing 
conversion.



-- 
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]

Reply via email to