diff --git a/core/models/MetricEvent.cpp b/core/models/MetricEvent.cpp index d1ae8e139d..74af28acbc 100644 --- a/core/models/MetricEvent.cpp +++ b/core/models/MetricEvent.cpp @@ -16,6 +16,8 @@ #include "models/MetricEvent.h" +#include + using namespace std; namespace logtail { @@ -48,7 +50,7 @@ void MetricEvent::SetNameNoCopy(StringView name) { } StringView MetricEvent::GetTag(StringView key) const { - auto it = mTags.mInner.find(key); + auto it = std::find_if(mTags.mInner.begin(), mTags.mInner.end(), [&key](const auto& p) { return p.first == key; }); if (it != mTags.mInner.end()) { return it->second; } @@ -56,7 +58,8 @@ StringView MetricEvent::GetTag(StringView key) const { } bool MetricEvent::HasTag(StringView key) const { - return mTags.mInner.find(key) != mTags.mInner.end(); + auto it = std::find_if(mTags.mInner.begin(), mTags.mInner.end(), [&key](const auto& p) { return p.first == key; }); + return it != mTags.mInner.end(); } void MetricEvent::SetTag(StringView key, StringView val) { diff --git a/core/models/MetricEvent.h b/core/models/MetricEvent.h index da9c47934d..42f0a52552 100644 --- a/core/models/MetricEvent.h +++ b/core/models/MetricEvent.h @@ -16,9 +16,14 @@ #pragma once +#include + #include #include +#include +#include +#include "StringView.h" #include "common/memory/SourceBuffer.h" #include "models/MetricValue.h" #include "models/PipelineEvent.h" @@ -71,20 +76,25 @@ class MetricEvent : public PipelineEvent { mValue = UntypedMultiDoubleValues{multiDoubleValues.mValues, this}; } - StringView GetTag(StringView key) const; - bool HasTag(StringView key) const; + [[nodiscard]] StringView GetTag(StringView key) const; + [[nodiscard]] bool HasTag(StringView key) const; void SetTag(StringView key, StringView val); void SetTag(const std::string& key, const std::string& val); void SetTagNoCopy(const StringBuffer& key, const StringBuffer& val); void SetTagNoCopy(StringView key, StringView val); + void DelTag(StringView key); + void EraseIf(const std::function)>& condition) { mTags.EraseIf(condition); } + void SortTags() { std::sort(mTags.mInner.begin(), mTags.mInner.end()); }; + + std::vector>::const_iterator TagsBegin() const { return mTags.mInner.begin(); } + std::vector>::const_iterator TagsEnd() const { return mTags.mInner.end(); } - std::map::const_iterator TagsBegin() const { return mTags.mInner.begin(); } - std::map::const_iterator TagsEnd() const { return mTags.mInner.end(); } size_t TagsSize() const { return mTags.mInner.size(); } size_t DataSize() const override; + #ifdef APSARA_UNIT_TEST_MAIN Json::Value ToJson(bool enableEventMeta = false) const override; bool FromJson(const Json::Value&) override; @@ -95,7 +105,7 @@ class MetricEvent : public PipelineEvent { StringView mName; MetricValue mValue; - SizedMap mTags; + SizedVectorTags mTags; }; } // namespace logtail diff --git a/core/models/SizedContainer.h b/core/models/SizedContainer.h index 9593f76a9e..20bb858cbc 100644 --- a/core/models/SizedContainer.h +++ b/core/models/SizedContainer.h @@ -59,4 +59,51 @@ class SizedMap { size_t mAllocatedSize = 0; }; +class SizedVectorTags { +public: + void Insert(StringView key, StringView val) { + auto iter = std::find_if(mInner.begin(), mInner.end(), [key](const auto& item) { return item.first == key; }); + if (iter != mInner.end()) { + mAllocatedSize += val.size() - iter->second.size(); + iter->second = val; + } else { + mAllocatedSize += key.size() + val.size(); + mInner.emplace_back(key, val); + } + } + + void Erase(StringView key) { + auto iter = std::find_if(mInner.begin(), mInner.end(), [key](const auto& item) { return item.first == key; }); + if (iter != mInner.end()) { + mAllocatedSize -= (iter->first.size() + iter->second.size()); + mInner.erase(iter); + } + } + + void EraseIf(const std::function)>& condition) { + size_t index = 0; + mAllocatedSize = 0; + for (const auto& item : mInner) { + if (condition(item)) { + continue; + } + mInner[index++] = item; + mAllocatedSize += item.first.size() + item.second.size(); + } + mInner.resize(index); + } + + size_t DataSize() const { return sizeof(decltype(mInner)) + mAllocatedSize; } + + void Clear() { + mInner.clear(); + mAllocatedSize = 0; + } + + std::vector> mInner; + +private: + size_t mAllocatedSize = 0; +}; + } // namespace logtail diff --git a/core/pipeline/serializer/SLSSerializer.cpp b/core/pipeline/serializer/SLSSerializer.cpp index 504a1a2b77..7f3e30ccaf 100644 --- a/core/pipeline/serializer/SLSSerializer.cpp +++ b/core/pipeline/serializer/SLSSerializer.cpp @@ -235,12 +235,13 @@ bool SLSEventGroupSerializer::Serialize(BatchedEvents&& group, string& res, stri break; case PipelineEvent::Type::METRIC: for (size_t i = 0; i < group.mEvents.size(); ++i) { - const auto& e = group.mEvents[i].Cast(); + auto& e = group.mEvents[i].Cast(); if (e.Is()) { continue; } serializer.StartToAddLog(logSZ[i]); serializer.AddLogTime(e.GetTimestamp()); + e.SortTags(); serializer.AddLogContentMetricLabel(e, metricEventContentCache[i].second); serializer.AddLogContentMetricTimeNano(e); serializer.AddLogContent(METRIC_RESERVED_KEY_VALUE, metricEventContentCache[i].first); diff --git a/core/plugin/processor/inner/ProcessorPromRelabelMetricNative.cpp b/core/plugin/processor/inner/ProcessorPromRelabelMetricNative.cpp index b93fb801e5..1cb8206c98 100644 --- a/core/plugin/processor/inner/ProcessorPromRelabelMetricNative.cpp +++ b/core/plugin/processor/inner/ProcessorPromRelabelMetricNative.cpp @@ -19,7 +19,7 @@ #include "json/json.h" -#include "common/Flags.h" +#include "StringView.h" #include "common/StringTools.h" #include "models/MetricEvent.h" #include "models/PipelineEventGroup.h" @@ -28,8 +28,6 @@ using namespace std; -DECLARE_FLAG_STRING(_pod_name_); - namespace logtail { const string ProcessorPromRelabelMetricNative::sName = "processor_prom_relabel_metric_native"; @@ -42,21 +40,18 @@ bool ProcessorPromRelabelMetricNative::Init(const Json::Value& config) { return false; } - mLoongCollectorScraper = STRING_FLAG(_pod_name_); - return true; } void ProcessorPromRelabelMetricNative::Process(PipelineEventGroup& metricGroup) { // if mMetricRelabelConfigs is empty and honor_labels is true, skip it auto targetTags = metricGroup.GetTags(); - auto toDelete = GetToDeleteTargetLabels(targetTags); if (!mScrapeConfigPtr->mMetricRelabelConfigs.Empty() || !targetTags.empty()) { EventsContainer& events = metricGroup.MutableEvents(); size_t wIdx = 0; for (size_t rIdx = 0; rIdx < events.size(); ++rIdx) { - if (ProcessEvent(events[rIdx], targetTags, toDelete)) { + if (ProcessEvent(events[rIdx], targetTags)) { if (wIdx != rIdx) { events[wIdx] = std::move(events[rIdx]); } @@ -66,18 +61,12 @@ void ProcessorPromRelabelMetricNative::Process(PipelineEventGroup& metricGroup) events.resize(wIdx); } - // delete mTags when key starts with __ - for (const auto& k : toDelete) { - metricGroup.DelTag(k); - } - if (metricGroup.HasMetadata(EventGroupMetaKey::PROMETHEUS_STREAM_TOTAL)) { auto autoMetric = prom::AutoMetric(); UpdateAutoMetrics(metricGroup, autoMetric); AddAutoMetrics(metricGroup, autoMetric); } - // delete all tags for (const auto& [k, v] : targetTags) { metricGroup.DelTag(k); @@ -88,75 +77,59 @@ bool ProcessorPromRelabelMetricNative::IsSupportedEvent(const PipelineEventPtr& return e.Is(); } -bool ProcessorPromRelabelMetricNative::ProcessEvent(PipelineEventPtr& e, - const GroupTags& targetTags, - const vector& toDelete) { +bool ProcessorPromRelabelMetricNative::ProcessEvent(PipelineEventPtr& e, const GroupTags& targetTags) { if (!IsSupportedEvent(e)) { return false; } auto& sourceEvent = e.Cast(); for (const auto& [k, v] : targetTags) { - if (sourceEvent.HasTag(k)) { - if (!mScrapeConfigPtr->mHonorLabels) { - // metric event labels is secondary - // if confiliction, then rename it exported_ - auto key = prometheus::EXPORTED_PREFIX + k.to_string(); - auto b = sourceEvent.GetSourceBuffer()->CopyString(key); - sourceEvent.SetTagNoCopy(StringView(b.data, b.size), sourceEvent.GetTag(k)); - sourceEvent.SetTagNoCopy(k, v); - } - } else { - sourceEvent.SetTagNoCopy(k, v); - } + AppendLabel(k, v, sourceEvent, mScrapeConfigPtr->mHonorLabels); } - vector toDeleteInRelabel; if (!mScrapeConfigPtr->mMetricRelabelConfigs.Empty() - && !mScrapeConfigPtr->mMetricRelabelConfigs.Process(sourceEvent, toDeleteInRelabel)) { + && !mScrapeConfigPtr->mMetricRelabelConfigs.Process(sourceEvent)) { return false; } // set metricEvent name sourceEvent.SetNameNoCopy(sourceEvent.GetTag(prometheus::NAME)); - for (const auto& k : toDelete) { - sourceEvent.DelTag(k); - } - for (const auto& k : toDeleteInRelabel) { - sourceEvent.DelTag(k); - } + sourceEvent.EraseIf([](const std::pair& tag) -> bool { + if (tag.first.starts_with("__") && tag.first != prometheus::NAME) { + return true; + } + return false; + }); for (const auto& [k, v] : mScrapeConfigPtr->mExternalLabels) { - if (sourceEvent.HasTag(k)) { - if (!mScrapeConfigPtr->mHonorLabels) { - // metric event labels is secondary - // if confiliction, then rename it exported_ - auto key = prometheus::EXPORTED_PREFIX + k; - auto b = sourceEvent.GetSourceBuffer()->CopyString(key); - sourceEvent.SetTagNoCopy(StringView(b.data, b.size), sourceEvent.GetTag(k)); - sourceEvent.SetTagNoCopy(k, v); - } - } else { - sourceEvent.SetTagNoCopy(k, v); - } + // the lifetime of mExternalLabels is longer than the sourceEvent + AppendLabel(k, v, sourceEvent, mScrapeConfigPtr->mHonorLabels); } - // set metricEvent name - sourceEvent.SetTagNoCopy(prometheus::NAME, sourceEvent.GetName()); - return true; } -vector ProcessorPromRelabelMetricNative::GetToDeleteTargetLabels(const GroupTags& targetTags) const { - // delete tag which starts with __ - vector toDelete; - for (const auto& [k, v] : targetTags) { - if (k.starts_with("__")) { - toDelete.push_back(k); +void ProcessorPromRelabelMetricNative::AppendLabel(StringView k, StringView v, MetricEvent& e, bool honorLabels) const { + auto tagValue = e.GetTag(k); + if (!tagValue.empty()) { + if (!honorLabels) { + // metric event labels is secondary + // if confiliction, then rename it exported_ + auto exportedKey = prometheus::EXPORTED_PREFIX + k.to_string(); + auto exportedTagValue = e.GetTag(exportedKey); + if (!exportedTagValue.empty()) { + auto exportedExportedKey = prometheus::EXPORTED_PREFIX + exportedKey; + auto sb = e.GetSourceBuffer()->CopyString(exportedExportedKey); + e.SetTagNoCopy(StringView(sb.data, sb.size), exportedTagValue); + } + auto sb = e.GetSourceBuffer()->CopyString(exportedKey); + e.SetTagNoCopy(StringView(sb.data, sb.size), tagValue); + e.SetTagNoCopy(k, v); } + } else { + e.SetTagNoCopy(k, v); } - return toDelete; -} +}; void ProcessorPromRelabelMetricNative::UpdateAutoMetrics(const PipelineEventGroup& eGroup, prom::AutoMetric& autoMetric) const { @@ -247,7 +220,7 @@ void ProcessorPromRelabelMetricNative::AddMetric(PipelineEventGroup& metricGroup metricEvent->SetTimestamp(timestamp, nanoSec); metricEvent->SetTag(prometheus::NAME, name); for (const auto& [k, v] : targetTags) { - metricEvent->SetTag(k, v); + metricEvent->SetTagNoCopy(k, v); } } diff --git a/core/plugin/processor/inner/ProcessorPromRelabelMetricNative.h b/core/plugin/processor/inner/ProcessorPromRelabelMetricNative.h index 1624532e8c..5f52a0f8d1 100644 --- a/core/plugin/processor/inner/ProcessorPromRelabelMetricNative.h +++ b/core/plugin/processor/inner/ProcessorPromRelabelMetricNative.h @@ -50,8 +50,7 @@ class ProcessorPromRelabelMetricNative : public Processor { bool IsSupportedEvent(const PipelineEventPtr& e) const override; private: - bool ProcessEvent(PipelineEventPtr& e, const GroupTags& targetTags, const std::vector& toDelete); - std::vector GetToDeleteTargetLabels(const GroupTags& targetTags) const; + bool ProcessEvent(PipelineEventPtr& e, const GroupTags& targetTags); void AddAutoMetrics(PipelineEventGroup& eGroup, const prom::AutoMetric& autoMetric) const; void UpdateAutoMetrics(const PipelineEventGroup& eGroup, prom::AutoMetric& autoMetric) const; @@ -61,9 +60,9 @@ class ProcessorPromRelabelMetricNative : public Processor { time_t timestamp, uint32_t nanoSec, const GroupTags& targetTags) const; + void AppendLabel(StringView k, StringView v, MetricEvent& e, bool honorLabels) const; std::unique_ptr mScrapeConfigPtr; - std::string mLoongCollectorScraper; #ifdef APSARA_UNIT_TEST_MAIN friend class ProcessorPromRelabelMetricNativeUnittest; diff --git a/core/prometheus/labels/Relabel.cpp b/core/prometheus/labels/Relabel.cpp index 45d5944a29..661072d029 100644 --- a/core/prometheus/labels/Relabel.cpp +++ b/core/prometheus/labels/Relabel.cpp @@ -137,14 +137,13 @@ bool RelabelConfig::Init(const Json::Value& config) { return true; } -bool RelabelConfig::Process(Labels& l, vector& toDelete) const { +bool RelabelConfig::Process(Labels& l) const { vector values; values.reserve(mSourceLabels.size()); for (const auto& item : mSourceLabels) { values.push_back(l.Get(item)); } string val = boost::algorithm::join(values, mSeparator); - CollectLabelsToDelete(mTargetLabel, toDelete); switch (mAction) { case Action::DROP: { if (boost::regex_match(val, mRegex)) { @@ -183,7 +182,6 @@ bool RelabelConfig::Process(Labels& l, vector& toDelete) const { break; } l.Set(target, string(res)); - CollectLabelsToDelete(target, toDelete); break; } case Action::LOWERCASE: { @@ -212,7 +210,6 @@ bool RelabelConfig::Process(Labels& l, vector& toDelete) const { string res = boost::regex_replace(key, mRegex, mReplacement, boost::match_default | boost::format_all); l.Set(res, value); - CollectLabelsToDelete(res, toDelete); } }); break; @@ -255,12 +252,6 @@ bool RelabelConfig::Process(Labels& l, vector& toDelete) const { return true; } -void RelabelConfig::CollectLabelsToDelete(const string& labelName, vector& toDelete) const { - if (StartWith(labelName, "__")) { - toDelete.push_back(labelName); - } -} - bool RelabelConfigList::Init(const Json::Value& relabelConfigs) { if (!relabelConfigs.isArray()) { return false; @@ -276,19 +267,19 @@ bool RelabelConfigList::Init(const Json::Value& relabelConfigs) { return true; } -bool RelabelConfigList::Process(Labels& l, vector& toDelete) const { +bool RelabelConfigList::Process(Labels& l) const { for (const auto& cfg : mRelabelConfigs) { - if (!cfg.Process(l, toDelete)) { + if (!cfg.Process(l)) { return false; } } return true; } -bool RelabelConfigList::Process(MetricEvent& event, vector& toDelete) const { +bool RelabelConfigList::Process(MetricEvent& event) const { Labels labels; labels.Reset(&event); - return Process(labels, toDelete); + return Process(labels); } bool RelabelConfigList::Empty() const { diff --git a/core/prometheus/labels/Relabel.h b/core/prometheus/labels/Relabel.h index edb06e9828..b712904937 100644 --- a/core/prometheus/labels/Relabel.h +++ b/core/prometheus/labels/Relabel.h @@ -47,7 +47,7 @@ class RelabelConfig { public: RelabelConfig(); bool Init(const Json::Value&); - bool Process(Labels&, std::vector& toDelete) const; + bool Process(Labels&) const; // A list of labels from which values are taken and concatenated // with the configured separator in order. @@ -69,14 +69,13 @@ class RelabelConfig { std::set mMatchList; private: - void CollectLabelsToDelete(const std::string& labelName, std::vector& toDelete) const; }; class RelabelConfigList { public: bool Init(const Json::Value& relabelConfigs); - bool Process(MetricEvent&, std::vector& toDelete) const; - bool Process(Labels&, std::vector& toDelete) const; + bool Process(MetricEvent&) const; + bool Process(Labels&) const; [[nodiscard]] bool Empty() const; @@ -84,7 +83,7 @@ class RelabelConfigList { std::vector mRelabelConfigs; #ifdef APSARA_UNIT_TEST_MAIN - friend class RelabelConfigListTest; + friend class RelabelConfigUnittest; friend class InputPrometheusUnittest; friend class ScrapeConfigUnittest; #endif diff --git a/core/prometheus/schedulers/ScrapeConfig.cpp b/core/prometheus/schedulers/ScrapeConfig.cpp index 33d8a06942..4497db1f95 100644 --- a/core/prometheus/schedulers/ScrapeConfig.cpp +++ b/core/prometheus/schedulers/ScrapeConfig.cpp @@ -409,14 +409,23 @@ bool ScrapeConfig::InitExternalLabels(const Json::Value& externalLabels) { LOG_ERROR(sLogger, ("external_labels config error", "")); return false; } + set dups; for (auto& key : externalLabels.getMemberNames()) { if (externalLabels[key].isString()) { + if (dups.count(key)) { + LOG_ERROR(sLogger, ("duplicated key in external_labels", key)); + return false; + } + dups.insert(key); mExternalLabels.emplace_back(key, externalLabels[key].asString()); } else { LOG_ERROR(sLogger, ("external_labels config error", "")); return false; } } + std::sort(mExternalLabels.begin(), mExternalLabels.end(), [](const auto& lhs, const auto& rhs) { + return lhs.first < rhs.first; + }); return true; } diff --git a/core/prometheus/schedulers/TargetSubscriberScheduler.cpp b/core/prometheus/schedulers/TargetSubscriberScheduler.cpp index f3f7647adc..fb308d2ea0 100644 --- a/core/prometheus/schedulers/TargetSubscriberScheduler.cpp +++ b/core/prometheus/schedulers/TargetSubscriberScheduler.cpp @@ -195,8 +195,7 @@ TargetSubscriberScheduler::BuildScrapeSchedulerSet(std::vector& targetGr for (const auto& labels : targetGroups) { // Relabel Config Labels resultLabel = labels; - vector toDelete; - if (!mScrapeConfigPtr->mRelabelConfigs.Process(resultLabel, toDelete)) { + if (!mScrapeConfigPtr->mRelabelConfigs.Process(resultLabel)) { continue; } resultLabel.RemoveMetaLabels(); diff --git a/core/unittest/processor/ProcessorPromRelabelMetricNativeUnittest.cpp b/core/unittest/processor/ProcessorPromRelabelMetricNativeUnittest.cpp index 06a0668aa8..5730ccf3a4 100644 --- a/core/unittest/processor/ProcessorPromRelabelMetricNativeUnittest.cpp +++ b/core/unittest/processor/ProcessorPromRelabelMetricNativeUnittest.cpp @@ -278,16 +278,15 @@ test_metric8{k1="v1", k3="v2", } 9.9410452992e+10 1715829785083 eventGroup.SetTag(string("k3"), string("v3")); APSARA_TEST_EQUAL((size_t)8, eventGroup.GetEvents().size()); auto targetTags = eventGroup.GetTags(); - auto toDelete = processor.GetToDeleteTargetLabels(targetTags); // honor_labels is true - processor.ProcessEvent(eventGroup.MutableEvents()[0], targetTags, toDelete); + processor.ProcessEvent(eventGroup.MutableEvents()[0], targetTags); APSARA_TEST_EQUAL("v3", eventGroup.GetEvents().at(0).Cast().GetTag(string("k3"))); - processor.ProcessEvent(eventGroup.MutableEvents()[6], targetTags, toDelete); + processor.ProcessEvent(eventGroup.MutableEvents()[6], targetTags); APSARA_TEST_EQUAL("2", eventGroup.GetEvents().at(6).Cast().GetTag(string("k3")).to_string()); // honor_labels is false processor.mScrapeConfigPtr->mHonorLabels = false; - processor.ProcessEvent(eventGroup.MutableEvents()[7], targetTags, toDelete); + processor.ProcessEvent(eventGroup.MutableEvents()[7], targetTags); APSARA_TEST_EQUAL("v3", eventGroup.GetEvents().at(7).Cast().GetTag(string("k3")).to_string()); APSARA_TEST_EQUAL("v2", eventGroup.GetEvents().at(7).Cast().GetTag(string("exported_k3")).to_string()); } diff --git a/core/unittest/prometheus/RelabelUnittest.cpp b/core/unittest/prometheus/RelabelUnittest.cpp index f6fdd2edfc..aa7c37cd0b 100644 --- a/core/unittest/prometheus/RelabelUnittest.cpp +++ b/core/unittest/prometheus/RelabelUnittest.cpp @@ -14,10 +14,10 @@ * limitations under the License. */ -#include +#include -#include "boost/regex.hpp" -#include "json/json.h" +#include +#include #include "common/JsonUtil.h" #include "prometheus/labels/Relabel.h" @@ -137,8 +137,7 @@ void RelabelConfigUnittest::TestReplace() { APSARA_TEST_TRUE(configList.Init(configJson)); Labels result = labels; - vector toDelete; - configList.Process(result, toDelete); + configList.Process(result); APSARA_TEST_EQUAL((size_t)3, result.Size()); APSARA_TEST_EQUAL("172.17.0.3:9100", result.Get("__address__")); @@ -168,8 +167,7 @@ void RelabelConfigUnittest::TestKeep() { configList = RelabelConfigList(); APSARA_TEST_TRUE(configList.Init(configJson)); auto result = labels; - vector toDelete; - configList.Process(result, toDelete); + configList.Process(result); APSARA_TEST_EQUAL((size_t)2, result.Size()); APSARA_TEST_EQUAL("172.17.0.3", result.Get("__meta_kubernetes_pod_ip")); } @@ -197,8 +195,7 @@ void RelabelConfigUnittest::TestDrop() { configList = RelabelConfigList(); APSARA_TEST_TRUE(configList.Init(configJson)); auto result = labels; - vector toDelete; - APSARA_TEST_FALSE(configList.Process(result, toDelete)); + APSARA_TEST_FALSE(configList.Process(result)); } void RelabelConfigUnittest::TestDropMetric() { @@ -222,8 +219,8 @@ void RelabelConfigUnittest::TestDropMetric() { configList = RelabelConfigList(); APSARA_TEST_TRUE(configList.Init(configJson)); auto result = labels; - vector toDelete; - APSARA_TEST_FALSE(configList.Process(result, toDelete)); + + APSARA_TEST_FALSE(configList.Process(result)); } void RelabelConfigUnittest::TestDropEqual() { @@ -251,8 +248,8 @@ void RelabelConfigUnittest::TestDropEqual() { configList = RelabelConfigList(); APSARA_TEST_TRUE(configList.Init(configJson)); auto result = dropEqualLabels; - vector toDelete; - APSARA_TEST_FALSE(configList.Process(result, toDelete)); + + APSARA_TEST_FALSE(configList.Process(result)); } void RelabelConfigUnittest::TestKeepEqual() { @@ -280,8 +277,8 @@ void RelabelConfigUnittest::TestKeepEqual() { configList = RelabelConfigList(); APSARA_TEST_TRUE(configList.Init(configJson)); auto result = keepEqualLabels; - vector toDelete; - configList.Process(result, toDelete); + + configList.Process(result); APSARA_TEST_EQUAL((size_t)3, result.Size()); APSARA_TEST_EQUAL("172.17.0.3", result.Get("__meta_kubernetes_pod_ip")); } @@ -311,8 +308,8 @@ void RelabelConfigUnittest::TestLowerCase() { configList = RelabelConfigList(); APSARA_TEST_TRUE(configList.Init(configJson)); auto result = lowercaseLabels; - vector toDelete; - configList.Process(result, toDelete); + + configList.Process(result); APSARA_TEST_EQUAL((size_t)3, result.Size()); APSARA_TEST_EQUAL("node-exporter", result.Get("__meta_kubernetes_pod_label_app")); } @@ -342,8 +339,8 @@ void RelabelConfigUnittest::TestUpperCase() { configList = RelabelConfigList(); APSARA_TEST_TRUE(configList.Init(configJson)); auto result = uppercaseLabels; - vector toDelete; - configList.Process(result, toDelete); + + configList.Process(result); APSARA_TEST_EQUAL((size_t)3, result.Size()); APSARA_TEST_EQUAL("NODE-EXPORTER", result.Get("__meta_kubernetes_pod_label_app")); } @@ -375,8 +372,8 @@ void RelabelConfigUnittest::TestHashMod() { configList = RelabelConfigList(); APSARA_TEST_TRUE(configList.Init(configJson)); auto result = hashmodLabels; - vector toDelete; - configList.Process(result, toDelete); + + configList.Process(result); APSARA_TEST_EQUAL((size_t)4, result.Size()); APSARA_TEST_TRUE(!result.Get("hash_val").empty()); } @@ -401,8 +398,8 @@ void RelabelConfigUnittest::TestLabelMap() { configList = RelabelConfigList(); APSARA_TEST_TRUE(configList.Init(configJson)); auto result = labels; - vector toDelete; - configList.Process(result, toDelete); + + configList.Process(result); APSARA_TEST_EQUAL((size_t)3, result.Size()); APSARA_TEST_EQUAL("node-exporter", result.Get("k8s_app")); } @@ -429,8 +426,8 @@ void RelabelConfigUnittest::TestLabelDrop() { configList = RelabelConfigList(); APSARA_TEST_TRUE(configList.Init(configJson)); auto result = labelDropLabels; - vector toDelete; - configList.Process(result, toDelete); + + configList.Process(result); APSARA_TEST_EQUAL((size_t)1, result.Size()); APSARA_TEST_EQUAL("172.17.0.3", result.Get("pod_ip")); } @@ -457,8 +454,8 @@ void RelabelConfigUnittest::TestLabelKeep() { configList = RelabelConfigList(); APSARA_TEST_TRUE(configList.Init(configJson)); auto result = labelKeepLabels; - vector toDelete; - configList.Process(result, toDelete); + + configList.Process(result); APSARA_TEST_EQUAL((size_t)2, result.Size()); APSARA_TEST_EQUAL("172.17.0.3", result.Get("__meta_kubernetes_pod_ip")); APSARA_TEST_EQUAL("node-exporter", result.Get("__meta_kubernetes_pod_label_app")); @@ -503,8 +500,8 @@ void RelabelConfigUnittest::TestMultiRelabel() { configList = RelabelConfigList(); APSARA_TEST_TRUE(configList.Init(configJson)); auto result = labels; - vector toDelete; - APSARA_TEST_TRUE(configList.Process(result, toDelete)); + + APSARA_TEST_TRUE(configList.Process(result)); APSARA_TEST_EQUAL((size_t)3, result.Size()); APSARA_TEST_EQUAL("172.17.0.3:9100", result.Get("__address__")); @@ -512,7 +509,7 @@ void RelabelConfigUnittest::TestMultiRelabel() { configList = RelabelConfigList(); APSARA_TEST_TRUE(configList.Init(configJson)); result = labels; - APSARA_TEST_TRUE(configList.Process(result, toDelete)); + APSARA_TEST_TRUE(configList.Process(result)); } UNIT_TEST_CASE(ActionConverterUnittest, TestStringToAction) diff --git a/core/unittest/prometheus/ScrapeConfigUnittest.cpp b/core/unittest/prometheus/ScrapeConfigUnittest.cpp index 7efa40f17c..5336b685d9 100644 --- a/core/unittest/prometheus/ScrapeConfigUnittest.cpp +++ b/core/unittest/prometheus/ScrapeConfigUnittest.cpp @@ -95,8 +95,8 @@ void ScrapeConfigUnittest::TestInit() { } ], "external_labels": { - "test_key1": "test_value1", - "test_key2": "test_value2" + "test_key2": "test_value2", + "test_key1": "test_value1" }, "params" : { "__param_query": [