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/10 14:20:54 UTC

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

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


##########
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:
   Updated in 0da9263bee800563b495e40aba966a32fd1b1cb0



##########
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:
   Updated in 0da9263bee800563b495e40aba966a32fd1b1cb0



##########
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:
   Updated in 0da9263bee800563b495e40aba966a32fd1b1cb0



##########
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:
   We should discuss it with @fgerlits too as it was his comment to extract it to a generic utility. I thought it was not that specific to this use case and could be reused later as it can calculate the average of any numerical or time values. I think the `Averager` together with the `sample_size` constructor argument should be descriptive enough to use it, but I'm open for discussing this on a team level.



##########
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 added a `reserve` call in 0da9263bee800563b495e40aba966a32fd1b1cb0, but we cannot really initialize the members beforehand because we need to know the number of actual elements in the vector when calculating the average, which is retrieved with the `size()` member.



##########
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:
   Updated in 0da9263bee800563b495e40aba966a32fd1b1cb0



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