lordgamez commented on code in PR #1695: URL: https://github.com/apache/nifi-minifi-cpp/pull/1695#discussion_r1439541257
########## extensions/grafana-loki/PushGrafanaLokiREST.cpp: ########## @@ -0,0 +1,396 @@ +/** + * 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 "PushGrafanaLokiREST.h" + +#include <utility> +#include <fstream> +#include <filesystem> + +#include "core/ProcessContext.h" +#include "core/ProcessSession.h" +#include "core/Resource.h" +#include "utils/ProcessorConfigUtils.h" +#include "utils/StringUtils.h" +#include "rapidjson/document.h" +#include "rapidjson/stream.h" +#include "rapidjson/writer.h" + +namespace org::apache::nifi::minifi::extensions::grafana::loki { + +void PushGrafanaLokiREST::LogBatch::add(const std::shared_ptr<core::FlowFile>& flowfile) { + gsl_Expects(state_manager_); + if (log_line_batch_wait_ && batched_flowfiles_.empty()) { + start_push_time_ = std::chrono::steady_clock::now(); + std::unordered_map<std::string, std::string> state; + state["start_push_time"] = std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(start_push_time_.time_since_epoch()).count()); + logger_->log_debug("Saved start push time to state: {}", state["start_push_time"]); + state_manager_->set(state); + } + batched_flowfiles_.push_back(flowfile); +} + +void PushGrafanaLokiREST::LogBatch::restore(const std::shared_ptr<core::FlowFile>& flowfile) { + batched_flowfiles_.push_back(flowfile); +} + +std::vector<std::shared_ptr<core::FlowFile>> PushGrafanaLokiREST::LogBatch::flush() { + gsl_Expects(state_manager_); + start_push_time_ = {}; + auto result = batched_flowfiles_; + batched_flowfiles_.clear(); + if (log_line_batch_wait_) { + start_push_time_ = {}; + std::unordered_map<std::string, std::string> state; + logger_->log_debug("Reset start push time state"); + state["start_push_time"] = "0"; + state_manager_->set(state); + } + return result; +} + +bool PushGrafanaLokiREST::LogBatch::isReady() const { + return (log_line_batch_size_ && batched_flowfiles_.size() >= *log_line_batch_size_) || (log_line_batch_wait_ && std::chrono::steady_clock::now() - start_push_time_ >= *log_line_batch_wait_); +} + +void PushGrafanaLokiREST::LogBatch::setLogLineBatchSize(std::optional<uint64_t> log_line_batch_size) { + log_line_batch_size_ = log_line_batch_size; +} + +void PushGrafanaLokiREST::LogBatch::setLogLineBatchWait(std::optional<std::chrono::milliseconds> log_line_batch_wait) { + log_line_batch_wait_ = log_line_batch_wait; +} + +void PushGrafanaLokiREST::LogBatch::setStateManager(core::StateManager* state_manager) { + state_manager_ = state_manager; +} + +void PushGrafanaLokiREST::LogBatch::setStartPushTime(std::chrono::steady_clock::time_point start_push_time) { + start_push_time_ = start_push_time; +} + +const core::Relationship PushGrafanaLokiREST::Self("__self__", "Marks the FlowFile to be owned by this processor"); + +void PushGrafanaLokiREST::initialize() { + setSupportedProperties(Properties); + setSupportedRelationships(Relationships); +} + +namespace { +auto getSSLContextService(core::ProcessContext& context) { + if (auto ssl_context = context.getProperty(PushGrafanaLokiREST::SSLContextService)) { + return std::dynamic_pointer_cast<minifi::controllers::SSLContextService>(context.getControllerService(*ssl_context)); + } + return std::shared_ptr<minifi::controllers::SSLContextService>{}; +} + +std::string readLogLineFromFlowFile(const std::shared_ptr<core::FlowFile>& flow_file, core::ProcessSession& session) { + auto read_buffer_result = session.readBuffer(flow_file); + return {reinterpret_cast<const char*>(read_buffer_result.buffer.data()), read_buffer_result.buffer.size()}; +} +} // namespace + +void PushGrafanaLokiREST::setUpStateManager(core::ProcessContext& context) { + auto state_manager = context.getStateManager(); + if (state_manager == nullptr) { + throw Exception(PROCESSOR_EXCEPTION, "Failed to get StateManager"); + } + log_batch_.setStateManager(state_manager); + + std::unordered_map<std::string, std::string> state_map; + if (state_manager->get(state_map)) { + auto it = state_map.find("start_push_time"); + if (it != state_map.end()) { + logger_->log_info("Restored start push time from processor state: {}", it->second); + std::chrono::steady_clock::time_point start_push_time{std::chrono::milliseconds{std::stoll(it->second)}}; + log_batch_.setStartPushTime(start_push_time); + } + } +} + +void PushGrafanaLokiREST::setUpStreamLabels(core::ProcessContext& context) { + if (auto stream_labels_str = context.getProperty(StreamLabels)) { + auto stream_labels = utils::StringUtils::splitAndTrimRemovingEmpty(*stream_labels_str, ","); + if (stream_labels.empty()) { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Missing or invalid Stream Label Attributes"); + } + for (const auto& label : stream_labels) { + auto stream_labels = utils::StringUtils::splitAndTrimRemovingEmpty(label, "="); + if (stream_labels.size() != 2) { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Missing or invalid Stream Label Attributes"); + } + stream_label_attributes_[stream_labels[0]] = stream_labels[1]; + } + } else { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Missing or invalid Stream Label Attributes"); + } +} + +void PushGrafanaLokiREST::setupClientTimeouts(const core::ProcessContext& context) { + if (auto connection_timeout = context.getProperty<core::TimePeriodValue>(PushGrafanaLokiREST::ConnectTimeout)) { + client_.setConnectionTimeout(connection_timeout->getMilliseconds()); + } + + if (auto read_timeout = context.getProperty<core::TimePeriodValue>(PushGrafanaLokiREST::ReadTimeout)) { + client_.setReadTimeout(read_timeout->getMilliseconds()); + } +} + +void PushGrafanaLokiREST::setAuthorization(const core::ProcessContext& context) { + if (auto username = context.getProperty(PushGrafanaLokiREST::Username)) { + auto password = context.getProperty(PushGrafanaLokiREST::Password); + if (!password) { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Username is set, but Password property is not!"); + } + std::string auth = *username + ":" + *password; + auto base64_encoded_auth = utils::StringUtils::to_base64(auth); + client_.setRequestHeader("Authorization", "Basic " + base64_encoded_auth); + } else if (auto bearer_token_file = context.getProperty(PushGrafanaLokiREST::BearerTokenFile)) { + if (!std::filesystem::exists(*bearer_token_file) || !std::filesystem::is_regular_file(*bearer_token_file)) { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Bearer Token File is not a regular file!"); + } + std::ifstream file(*bearer_token_file); Review Comment: I'm not sure if it matters, but it's safer to open it in binary mode, updated in 083c28ee59f96352e89d5675d635ea3b91bd9942 ########## extensions/grafana-loki/PushGrafanaLokiREST.cpp: ########## @@ -0,0 +1,396 @@ +/** + * 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 "PushGrafanaLokiREST.h" + +#include <utility> +#include <fstream> +#include <filesystem> + +#include "core/ProcessContext.h" +#include "core/ProcessSession.h" +#include "core/Resource.h" +#include "utils/ProcessorConfigUtils.h" +#include "utils/StringUtils.h" +#include "rapidjson/document.h" +#include "rapidjson/stream.h" +#include "rapidjson/writer.h" + +namespace org::apache::nifi::minifi::extensions::grafana::loki { + +void PushGrafanaLokiREST::LogBatch::add(const std::shared_ptr<core::FlowFile>& flowfile) { + gsl_Expects(state_manager_); + if (log_line_batch_wait_ && batched_flowfiles_.empty()) { + start_push_time_ = std::chrono::steady_clock::now(); + std::unordered_map<std::string, std::string> state; + state["start_push_time"] = std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(start_push_time_.time_since_epoch()).count()); + logger_->log_debug("Saved start push time to state: {}", state["start_push_time"]); + state_manager_->set(state); + } + batched_flowfiles_.push_back(flowfile); +} + +void PushGrafanaLokiREST::LogBatch::restore(const std::shared_ptr<core::FlowFile>& flowfile) { + batched_flowfiles_.push_back(flowfile); +} + +std::vector<std::shared_ptr<core::FlowFile>> PushGrafanaLokiREST::LogBatch::flush() { + gsl_Expects(state_manager_); + start_push_time_ = {}; + auto result = batched_flowfiles_; + batched_flowfiles_.clear(); + if (log_line_batch_wait_) { + start_push_time_ = {}; + std::unordered_map<std::string, std::string> state; + logger_->log_debug("Reset start push time state"); + state["start_push_time"] = "0"; + state_manager_->set(state); + } + return result; +} + +bool PushGrafanaLokiREST::LogBatch::isReady() const { + return (log_line_batch_size_ && batched_flowfiles_.size() >= *log_line_batch_size_) || (log_line_batch_wait_ && std::chrono::steady_clock::now() - start_push_time_ >= *log_line_batch_wait_); +} + +void PushGrafanaLokiREST::LogBatch::setLogLineBatchSize(std::optional<uint64_t> log_line_batch_size) { + log_line_batch_size_ = log_line_batch_size; +} + +void PushGrafanaLokiREST::LogBatch::setLogLineBatchWait(std::optional<std::chrono::milliseconds> log_line_batch_wait) { + log_line_batch_wait_ = log_line_batch_wait; +} + +void PushGrafanaLokiREST::LogBatch::setStateManager(core::StateManager* state_manager) { + state_manager_ = state_manager; +} + +void PushGrafanaLokiREST::LogBatch::setStartPushTime(std::chrono::steady_clock::time_point start_push_time) { + start_push_time_ = start_push_time; +} + +const core::Relationship PushGrafanaLokiREST::Self("__self__", "Marks the FlowFile to be owned by this processor"); + +void PushGrafanaLokiREST::initialize() { + setSupportedProperties(Properties); + setSupportedRelationships(Relationships); +} + +namespace { +auto getSSLContextService(core::ProcessContext& context) { + if (auto ssl_context = context.getProperty(PushGrafanaLokiREST::SSLContextService)) { + return std::dynamic_pointer_cast<minifi::controllers::SSLContextService>(context.getControllerService(*ssl_context)); + } + return std::shared_ptr<minifi::controllers::SSLContextService>{}; +} + +std::string readLogLineFromFlowFile(const std::shared_ptr<core::FlowFile>& flow_file, core::ProcessSession& session) { + auto read_buffer_result = session.readBuffer(flow_file); + return {reinterpret_cast<const char*>(read_buffer_result.buffer.data()), read_buffer_result.buffer.size()}; +} +} // namespace + +void PushGrafanaLokiREST::setUpStateManager(core::ProcessContext& context) { + auto state_manager = context.getStateManager(); + if (state_manager == nullptr) { + throw Exception(PROCESSOR_EXCEPTION, "Failed to get StateManager"); + } + log_batch_.setStateManager(state_manager); + + std::unordered_map<std::string, std::string> state_map; + if (state_manager->get(state_map)) { + auto it = state_map.find("start_push_time"); + if (it != state_map.end()) { + logger_->log_info("Restored start push time from processor state: {}", it->second); + std::chrono::steady_clock::time_point start_push_time{std::chrono::milliseconds{std::stoll(it->second)}}; + log_batch_.setStartPushTime(start_push_time); + } + } +} + +void PushGrafanaLokiREST::setUpStreamLabels(core::ProcessContext& context) { + if (auto stream_labels_str = context.getProperty(StreamLabels)) { + auto stream_labels = utils::StringUtils::splitAndTrimRemovingEmpty(*stream_labels_str, ","); + if (stream_labels.empty()) { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Missing or invalid Stream Label Attributes"); + } + for (const auto& label : stream_labels) { + auto stream_labels = utils::StringUtils::splitAndTrimRemovingEmpty(label, "="); + if (stream_labels.size() != 2) { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Missing or invalid Stream Label Attributes"); + } + stream_label_attributes_[stream_labels[0]] = stream_labels[1]; + } + } else { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Missing or invalid Stream Label Attributes"); + } +} + +void PushGrafanaLokiREST::setupClientTimeouts(const core::ProcessContext& context) { + if (auto connection_timeout = context.getProperty<core::TimePeriodValue>(PushGrafanaLokiREST::ConnectTimeout)) { + client_.setConnectionTimeout(connection_timeout->getMilliseconds()); + } + + if (auto read_timeout = context.getProperty<core::TimePeriodValue>(PushGrafanaLokiREST::ReadTimeout)) { + client_.setReadTimeout(read_timeout->getMilliseconds()); + } +} + +void PushGrafanaLokiREST::setAuthorization(const core::ProcessContext& context) { + if (auto username = context.getProperty(PushGrafanaLokiREST::Username)) { + auto password = context.getProperty(PushGrafanaLokiREST::Password); + if (!password) { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Username is set, but Password property is not!"); + } + std::string auth = *username + ":" + *password; + auto base64_encoded_auth = utils::StringUtils::to_base64(auth); + client_.setRequestHeader("Authorization", "Basic " + base64_encoded_auth); + } else if (auto bearer_token_file = context.getProperty(PushGrafanaLokiREST::BearerTokenFile)) { + if (!std::filesystem::exists(*bearer_token_file) || !std::filesystem::is_regular_file(*bearer_token_file)) { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Bearer Token File is not a regular file!"); + } + std::ifstream file(*bearer_token_file); + std::stringstream buffer; + buffer << file.rdbuf(); + std::string bearer_token = utils::StringUtils::trim(buffer.str()); + client_.setRequestHeader("Authorization", "Bearer " + bearer_token); + } else { + client_.setRequestHeader("Authorization", std::nullopt); + } +} + +void PushGrafanaLokiREST::initializeHttpClient(core::ProcessContext& context) { + auto url = utils::getRequiredPropertyOrThrow<std::string>(context, Url.name); + if (url.empty()) { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Url property cannot be empty!"); + } + if (utils::StringUtils::endsWith(url, "/")) { + url += "loki/api/v1/push"; + } else { + url += "/loki/api/v1/push"; + } + logger_->log_debug("PushGrafanaLokiREST push url is set to: {}", url); + client_.initialize(utils::HttpRequestMethod::POST, url, getSSLContextService(context)); +} + +void PushGrafanaLokiREST::onSchedule(core::ProcessContext& context, core::ProcessSessionFactory&) { + setUpStateManager(context); + initializeHttpClient(context); + client_.setContentType("application/json"); + client_.setFollowRedirects(true); + + setUpStreamLabels(context); + + if (auto log_line_metadata_attributes = context.getProperty(LogLineMetadataAttributes)) { + log_line_metadata_attributes_ = utils::StringUtils::splitAndTrimRemovingEmpty(*log_line_metadata_attributes, ","); + } + + auto tenant_id = context.getProperty(TenantID); + if (tenant_id && !tenant_id->empty()) { + client_.setRequestHeader("X-Scope-OrgID", tenant_id); + } else { + client_.setRequestHeader("X-Scope-OrgID", std::nullopt); + } + auto log_line_batch_wait = context.getProperty<core::TimePeriodValue>(LogLineBatchWait); + auto log_line_batch_size = context.getProperty<uint64_t>(LogLineBatchSize); + if (log_line_batch_size && *log_line_batch_size < 1) { Review Comment: Updated in 083c28ee59f96352e89d5675d635ea3b91bd9942 -- 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]
