Skip to content

Commit

Permalink
chore: update ProcessorPromRelabel ut
Browse files Browse the repository at this point in the history
  • Loading branch information
catdogpandas committed Dec 17, 2024
1 parent 2edd176 commit c42b8ff
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
17 changes: 10 additions & 7 deletions core/plugin/processor/inner/ProcessorPromRelabelMetricNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ void ProcessorPromRelabelMetricNative::UpdateAutoMetrics(const PipelineEventGrou
= StringTo<uint64_t>(eGroup.GetMetadata(EventGroupMetaKey::PROMETHEUS_SAMPLES_SCRAPED).to_string());
}
autoMetric.mScrapeTimeoutSeconds = mScrapeConfigPtr->mScrapeTimeoutSeconds;

if (eGroup.HasMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_STATE)) {
autoMetric.mScrapeState = eGroup.GetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_STATE).to_string();
}

if (eGroup.HasMetadata(EventGroupMetaKey::PROMETHEUS_UP_STATE)) {
autoMetric.mUp = StringTo<bool>(eGroup.GetMetadata(EventGroupMetaKey::PROMETHEUS_UP_STATE).to_string());
}
Expand Down Expand Up @@ -206,15 +211,13 @@ void ProcessorPromRelabelMetricNative::AddAutoMetrics(PipelineEventGroup& eGroup
AddMetric(
eGroup, prometheus::SCRAPE_TIMEOUT_SECONDS, autoMetric.mScrapeTimeoutSeconds, timestamp, nanoSec, targetTags);

AddMetric(eGroup, prometheus::SCRAPE_STATE, 1.0 * autoMetric.mUp, timestamp, nanoSec, targetTags);
auto& last = eGroup.MutableEvents()[eGroup.GetEvents().size() - 1];
auto scrapeState = eGroup.GetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_STATE);
last.Cast<MetricEvent>().SetTag(METRIC_LABEL_KEY_STATUS, scrapeState);

// up metric must be the last one
AddMetric(eGroup, prometheus::UP, 1.0 * autoMetric.mUp, timestamp, nanoSec, targetTags);

if (eGroup.HasMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_STATE)) {
auto scrapeState = eGroup.GetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_STATE);
AddMetric(eGroup, prometheus::SCRAPE_STATE, 1.0 * autoMetric.mUp, timestamp, nanoSec, targetTags);
auto& last = eGroup.MutableEvents()[eGroup.GetEvents().size() - 1];
last.Cast<MetricEvent>().SetTag(METRIC_LABEL_KEY_STATUS, scrapeState);
}
}

void ProcessorPromRelabelMetricNative::AddMetric(PipelineEventGroup& metricGroup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace prom {
// uint64_t mPostRelabel;
uint64_t mScrapeSamplesScraped;
uint64_t mScrapeTimeoutSeconds;
std::string mScrapeState;
bool mUp;
};
} // namespace prom
Expand Down
2 changes: 1 addition & 1 deletion core/prometheus/component/StreamScraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void PromStreamScraper::SetTargetLabels(PipelineEventGroup& eGroup) const {
void PromStreamScraper::PushEventGroup(PipelineEventGroup&& eGroup) const {
auto item = make_unique<ProcessQueueItem>(std::move(eGroup), mInputIndex);
#ifdef APSARA_UNIT_TEST_MAIN
mItem.push_back(std::move(item));
mItem.emplace_back(std::move(item));
return;
#endif
while (true) {
Expand Down
6 changes: 5 additions & 1 deletion core/prometheus/component/StreamScraper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

#include <memory>
#include <string>
#include <vector>

#include "Labels.h"
#include "models/PipelineEventGroup.h"
#include "pipeline/queue/ProcessQueueItem.h"
#include "pipeline/queue/QueueKey.h"


Expand Down Expand Up @@ -49,6 +51,8 @@ class PromStreamScraper {

// auto metrics
uint64_t mScrapeTimestampMilliSec = 0;

#ifdef APSARA_UNIT_TEST_MAIN
mutable std::vector<std::unique_ptr<ProcessQueueItem>> mItem;
#endif
};
} // namespace logtail::prom
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ test_metric8{k1="v1", k3="v2", } 9.9410452992e+10 1715829785083
APSARA_TEST_EQUAL((size_t)8, eventGroup.GetEvents().size());

// without metadata
processor.AddAutoMetrics(eventGroup);
auto autoMetric = prom::AutoMetric();
processor.UpdateAutoMetrics(eventGroup, autoMetric);
processor.AddAutoMetrics(eventGroup, autoMetric);
APSARA_TEST_EQUAL((size_t)8, eventGroup.GetEvents().size());

// with metadata
Expand All @@ -207,22 +209,24 @@ test_metric8{k1="v1", k3="v2", } 9.9410452992e+10 1715829785083
eventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_STATE, string("OK"));
eventGroup.SetTag(string("instance"), "localhost:8080");
eventGroup.SetTag(string("job"), "test_job");
processor.AddAutoMetrics(eventGroup);
processor.UpdateAutoMetrics(eventGroup, autoMetric);
processor.AddAutoMetrics(eventGroup, autoMetric);

APSARA_TEST_EQUAL((size_t)16, eventGroup.GetEvents().size());
// SCRAPE_SAMPLES_POST_METRIC_RELABELING is removed
APSARA_TEST_EQUAL((size_t)15, eventGroup.GetEvents().size());
APSARA_TEST_EQUAL(1.5, eventGroup.GetEvents().at(8).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
APSARA_TEST_EQUAL(2325, eventGroup.GetEvents().at(9).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
APSARA_TEST_EQUAL(1000, eventGroup.GetEvents().at(10).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
APSARA_TEST_EQUAL(8, eventGroup.GetEvents().at(11).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
APSARA_TEST_EQUAL(8, eventGroup.GetEvents().at(12).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
APSARA_TEST_EQUAL(15, eventGroup.GetEvents().at(13).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
// APSARA_TEST_EQUAL(8, eventGroup.GetEvents().at(12).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
APSARA_TEST_EQUAL(15, eventGroup.GetEvents().at(12).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
// scrape_state
APSARA_TEST_EQUAL(1, eventGroup.GetEvents().at(14).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
APSARA_TEST_EQUAL("OK", eventGroup.GetEvents().at(14).Cast<MetricEvent>().GetTag("status"));
APSARA_TEST_EQUAL(1, eventGroup.GetEvents().at(13).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
APSARA_TEST_EQUAL("OK", eventGroup.GetEvents().at(13).Cast<MetricEvent>().GetTag("status"));
// up
APSARA_TEST_EQUAL(1, eventGroup.GetEvents().at(15).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
APSARA_TEST_EQUAL("localhost:8080", eventGroup.GetEvents().at(15).Cast<MetricEvent>().GetTag("instance"));
APSARA_TEST_EQUAL("test_job", eventGroup.GetEvents().at(15).Cast<MetricEvent>().GetTag("job"));
APSARA_TEST_EQUAL(1, eventGroup.GetEvents().at(14).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
APSARA_TEST_EQUAL("localhost:8080", eventGroup.GetEvents().at(14).Cast<MetricEvent>().GetTag("instance"));
APSARA_TEST_EQUAL("test_job", eventGroup.GetEvents().at(14).Cast<MetricEvent>().GetTag("job"));
}

void ProcessorPromRelabelMetricNativeUnittest::TestHonorLabels() {
Expand Down

0 comments on commit c42b8ff

Please sign in to comment.