You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/10/06 12:45:06 UTC

[GitHub] [nifi-minifi-cpp] szaszm commented on a diff in pull request #1400: MINIFICPP-1848 Create a generic solution for processor metrics

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


##########
extensions/standard-processors/processors/GetFile.cpp:
##########
@@ -248,8 +246,9 @@ bool GetFile::fileMatchesRequestCriteria(std::string fullName, std::string name,
     return false;
   }
 
-  metrics_->input_bytes_ += file_size;
-  metrics_->accepted_files_++;
+  auto getfile_metrics = static_cast<GetFileMetrics*>(metrics_.get());

Review Comment:
   Let's use `auto*` (or even better, `auto* const`, if you like using const pointers to mutable objects, like me), to make it clear that this is a pointer.
   ```suggestion
     auto* const getfile_metrics = static_cast<GetFileMetrics*>(metrics_.get());
   ```



##########
libminifi/src/core/ProcessSession.cpp:
##########
@@ -779,6 +779,11 @@ ProcessSession::RouteResult ProcessSession::routeFlowFile(const std::shared_ptr<
       }
     }
   }
+  if (metrics_) {
+    metrics_->transferred_bytes += record->getSize();
+    ++metrics_->transferred_flow_files;
+    metrics_->incrementRelationshipTransferCount(relationship.getName());
+  }

Review Comment:
   Consider doing this on commit instead, so that we only count actions that have actually happened.



##########
libminifi/include/utils/Averager.h:
##########
@@ -0,0 +1,90 @@
+/**
+ * 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.
+ */
+#pragma once
+
+#include <vector>
+#include <mutex>
+
+namespace org::apache::nifi::minifi::utils {
+
+template<typename T>
+concept Summable = requires(T x) { x + x; };  // NOLINT(readability/braces)
+
+template<typename T>
+concept DividableByInteger = requires(T x, uint32_t divisor) { x / divisor; };  // NOLINT(readability/braces)
+
+template<typename ValueType>
+requires Summable<ValueType> && DividableByInteger<ValueType>
+class Averager {
+ public:
+  explicit Averager(uint32_t sample_size) : SAMPLE_SIZE_(sample_size) {
+  }
+
+  ValueType getAverage() const;
+  ValueType getLastValue() const;
+  void addValue(ValueType runtime);
+
+ private:
+  const uint32_t SAMPLE_SIZE_;
+  mutable std::mutex average_value_mutex_;
+  uint32_t next_average_index_ = 0;
+  std::vector<ValueType> values_;
+};
+
+template<typename ValueType>
+requires Summable<ValueType> && DividableByInteger<ValueType>
+ValueType Averager<ValueType>::getAverage() const {
+  if (values_.empty()) {
+    return {};
+  }
+  ValueType sum{};
+  std::lock_guard<std::mutex> lock(average_value_mutex_);
+  for (const auto& value : values_) {
+    sum += value;
+  }
+  return sum / values_.size();
+}
+
+template<typename ValueType>
+requires Summable<ValueType> && DividableByInteger<ValueType>
+void Averager<ValueType>::addValue(ValueType runtime) {
+  std::lock_guard<std::mutex> lock(average_value_mutex_);
+  if (values_.size() < SAMPLE_SIZE_) {
+    values_.push_back(runtime);
+  } else {

Review Comment:
   I suggest restructuring in a way that limits the number of allocations to at most one, and removes one of the branches.



##########
libminifi/src/core/ProcessorMetrics.cpp:
##########
@@ -0,0 +1,126 @@
+/**
+ * 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/ProcessorMetrics.h"
+
+#include "core/Processor.h"
+#include "utils/gsl.h"
+
+using namespace std::literals::chrono_literals;
+
+namespace org::apache::nifi::minifi::core {
+
+ProcessorMetrics::ProcessorMetrics(const Processor& source_processor)
+    : source_processor_(source_processor),
+      on_trigger_runtime_averager_(STORED_ON_TRIGGER_RUNTIME_COUNT) {
+}
+
+std::string ProcessorMetrics::getName() const {
+  return source_processor_.getProcessorType() + "Metrics";
+}
+
+std::unordered_map<std::string, std::string> ProcessorMetrics::getCommonLabels() const {
+  return {{"metric_class", getName()}, {"processor_name", source_processor_.getName()}, {"processor_uuid", source_processor_.getUUIDStr()}};
+}
+
+std::vector<state::response::SerializedResponseNode> ProcessorMetrics::serialize() {
+  std::vector<state::response::SerializedResponseNode> resp;
+
+  state::response::SerializedResponseNode root_node;
+  root_node.name = source_processor_.getUUIDStr();
+
+  state::response::SerializedResponseNode iter;

Review Comment:
   Prefer using a simpler, single initializer syntax wherever possible, like here: https://github.com/apache/nifi-minifi-cpp/blob/main/libminifi/include/core/state/nodes/AgentInformation.h#L619



##########
METRICS.md:
##########
@@ -103,34 +112,6 @@ RepositoryMetrics is a system level metric that reports metrics for the register
 |--------------------------|-----------------------------------------------------------------|
 | repository_name          | Name of the reported repository                                 |
 
-### GetFileMetrics
-
-Processor level metric that reports metrics for the GetFile processor if defined in the flow configuration
-
-| Metric name           | Labels                         | Description                                    |
-|-----------------------|--------------------------------|------------------------------------------------|
-| onTrigger_invocations | processor_name, processor_uuid | Number of times the processor was triggered    |

Review Comment:
   This onTrigger_invocations metric seems to be missing from the new structure. Is this intentional? Why?
   
   edit: It's still referenced in code, but not in the docs. Maybe we just need it back in the docs.



##########
libminifi/include/utils/Averager.h:
##########
@@ -0,0 +1,90 @@
+/**
+ * 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.
+ */
+#pragma once
+
+#include <vector>
+#include <mutex>
+
+namespace org::apache::nifi::minifi::utils {
+
+template<typename T>
+concept Summable = requires(T x) { x + x; };  // NOLINT(readability/braces)
+
+template<typename T>
+concept DividableByInteger = requires(T x, uint32_t divisor) { x / divisor; };  // NOLINT(readability/braces)
+
+template<typename ValueType>
+requires Summable<ValueType> && DividableByInteger<ValueType>
+class Averager {

Review Comment:
   This class is too specific to be considered a general utility. I would rewrite it as a ring buffer, and make "average" a separate function somewhere else. It would also improve readability, because nobody knows at first glance what an "averager" is, but many people know ring buffers and know what the average of a collection means.
   
   If you don't want to rewrite it, please move it close to the usage, into an anonymous namespace.



-- 
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: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org