Skip to content

Commit

Permalink
chore: update ScrapeScheduler ut
Browse files Browse the repository at this point in the history
  • Loading branch information
catdogpandas committed Dec 17, 2024
1 parent c42b8ff commit 97a78d7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
4 changes: 3 additions & 1 deletion core/prometheus/component/StreamScraper.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ class PromStreamScraper {
// auto metrics
uint64_t mScrapeTimestampMilliSec = 0;
#ifdef APSARA_UNIT_TEST_MAIN
mutable std::vector<std::unique_ptr<ProcessQueueItem>> mItem;
friend class ProcessorParsePrometheusMetricUnittest;
friend class ScrapeSchedulerUnittest;
mutable std::vector<std::shared_ptr<ProcessQueueItem>> mItem;
#endif
};
} // namespace logtail::prom
2 changes: 0 additions & 2 deletions core/prometheus/schedulers/ScrapeScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class ScrapeScheduler : public BaseScheduler {

// pipeline
QueueKey mQueueKey;
size_t mInputIndex;

// auto metrics
uint64_t mScrapeTimestampMilliSec = 0;
Expand All @@ -84,7 +83,6 @@ class ScrapeScheduler : public BaseScheduler {
#ifdef APSARA_UNIT_TEST_MAIN
friend class ProcessorParsePrometheusMetricUnittest;
friend class ScrapeSchedulerUnittest;
std::vector<std::shared_ptr<ProcessQueueItem>> mItem;
#endif
};

Expand Down
49 changes: 24 additions & 25 deletions core/unittest/prometheus/ScrapeSchedulerUnittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@ void ScrapeSchedulerUnittest::TestInitscrapeScheduler() {

void ScrapeSchedulerUnittest::TestProcess() {
EventPool eventPool{true};
HttpResponse httpResponse = HttpResponse(
new PromMetricResponseBody(&eventPool),
[](void* ptr) { delete static_cast<PromMetricResponseBody*>(ptr); },
PromMetricWriteCallback);

Labels labels;
labels.Set(prometheus::ADDRESS_LABEL_NAME, "localhost:8080");
labels.Set(prometheus::ADDRESS_LABEL_NAME, "localhost:8080");
ScrapeScheduler event(mScrapeConfig, "localhost", 8080, labels, 0, 0);
HttpResponse httpResponse
= HttpResponse(&event.mPromStreamScraper, [](void*) {}, prom::PromStreamScraper::MetricWriteCallback);
auto defaultLabels = MetricLabels();
event.InitSelfMonitor(defaultLabels);
APSARA_TEST_EQUAL(event.GetId(), "test_jobhttp://localhost:8080/metrics" + ToString(labels.Hash()));
Expand All @@ -83,18 +82,19 @@ void ScrapeSchedulerUnittest::TestProcess() {
httpResponse.SetStatusCode(503);
httpResponse.SetNetworkStatus(CURLE_OK);
event.OnMetricResult(httpResponse, 0);
APSARA_TEST_EQUAL(1UL, event.mItem.size());
event.mItem.clear();
APSARA_TEST_EQUAL(1UL, event.mPromStreamScraper.mItem.size());
event.mPromStreamScraper.mItem.clear();

httpResponse.GetBody<PromMetricResponseBody>()->mEventGroup = PipelineEventGroup(std::make_shared<SourceBuffer>());
httpResponse.SetStatusCode(503);
httpResponse.SetNetworkStatus(CURLE_COULDNT_CONNECT);
event.OnMetricResult(httpResponse, 0);
APSARA_TEST_EQUAL(event.mScrapeState, "ERR_CONN_FAILED");
APSARA_TEST_EQUAL(1UL, event.mItem.size());
event.mItem.clear();
APSARA_TEST_EQUAL(event.mPromStreamScraper.mItem[0]
->mEventGroup.GetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_STATE)
.to_string(),
"ERR_CONN_FAILED");
APSARA_TEST_EQUAL(1UL, event.mPromStreamScraper.mItem.size());
event.mPromStreamScraper.mItem.clear();

httpResponse.GetBody<PromMetricResponseBody>()->mEventGroup = PipelineEventGroup(std::make_shared<SourceBuffer>());
httpResponse.SetStatusCode(200);
httpResponse.SetNetworkStatus(CURLE_OK);
string body1 = "# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.\n"
Expand All @@ -118,23 +118,22 @@ void ScrapeSchedulerUnittest::TestProcess() {
"# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.\n"
"# TYPE go_memstats_alloc_bytes_total counter\n"
"go_memstats_alloc_bytes_total 1.5159292e+08";
PromMetricWriteCallback(
body1.data(), (size_t)1, (size_t)body1.length(), (void*)httpResponse.GetBody<PromMetricResponseBody>());
prom::PromStreamScraper::MetricWriteCallback(
body1.data(), (size_t)1, (size_t)body1.length(), (void*)httpResponse.GetBody<prom::PromStreamScraper>());
event.OnMetricResult(httpResponse, 0);
APSARA_TEST_EQUAL(1UL, event.mItem.size());
APSARA_TEST_EQUAL(11UL, event.mItem[0]->mEventGroup.GetEvents().size());
APSARA_TEST_EQUAL(1UL, event.mPromStreamScraper.mItem.size());
APSARA_TEST_EQUAL(11UL, event.mPromStreamScraper.mItem[0]->mEventGroup.GetEvents().size());
}

void ScrapeSchedulerUnittest::TestStreamMetricWriteCallback() {
EventPool eventPool{true};
HttpResponse httpResponse = HttpResponse(
new PromMetricResponseBody(&eventPool),
[](void* ptr) { delete static_cast<PromMetricResponseBody*>(ptr); },
PromMetricWriteCallback);

Labels labels;
labels.Set(prometheus::ADDRESS_LABEL_NAME, "localhost:8080");
labels.Set(prometheus::ADDRESS_LABEL_NAME, "localhost:8080");
ScrapeScheduler event(mScrapeConfig, "localhost", 8080, labels, 0, 0);
HttpResponse httpResponse
= HttpResponse(&event.mPromStreamScraper, [](void*) {}, prom::PromStreamScraper::MetricWriteCallback);
APSARA_TEST_EQUAL(event.GetId(), "test_jobhttp://localhost:8080/metrics" + ToString(labels.Hash()));

string body1 = "# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.\n"
Expand All @@ -160,9 +159,9 @@ void ScrapeSchedulerUnittest::TestStreamMetricWriteCallback() {
"# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.\n"
"# TYPE go_memstats_alloc_bytes_total counter\n"
"go_memstats_alloc_bytes_total 1.5159292e+08";
PromMetricWriteCallback(
body1.data(), (size_t)1, (size_t)body1.length(), (void*)httpResponse.GetBody<PromMetricResponseBody>());
auto& res = httpResponse.GetBody<PromMetricResponseBody>()->mEventGroup;
prom::PromStreamScraper::MetricWriteCallback(
body1.data(), (size_t)1, (size_t)body1.length(), (void*)httpResponse.GetBody<prom::PromStreamScraper>());
auto& res = httpResponse.GetBody<prom::PromStreamScraper>()->mEventGroup;
APSARA_TEST_EQUAL(7UL, res.GetEvents().size());
APSARA_TEST_EQUAL("go_gc_duration_seconds{quantile=\"0\"} 1.5531e-05",
res.GetEvents()[0].Cast<RawEvent>().GetContent());
Expand All @@ -177,9 +176,9 @@ void ScrapeSchedulerUnittest::TestStreamMetricWriteCallback() {
APSARA_TEST_EQUAL("go_gc_duration_seconds_sum 0.034885631", res.GetEvents()[5].Cast<RawEvent>().GetContent());
APSARA_TEST_EQUAL("go_gc_duration_seconds_count 850", res.GetEvents()[6].Cast<RawEvent>().GetContent());
// httpResponse.GetBody<MetricResponseBody>()->mEventGroup = PipelineEventGroup(std::make_shared<SourceBuffer>());
PromMetricWriteCallback(
body2.data(), (size_t)1, (size_t)body2.length(), (void*)httpResponse.GetBody<PromMetricResponseBody>());
httpResponse.GetBody<PromMetricResponseBody>()->FlushCache();
prom::PromStreamScraper::MetricWriteCallback(
body2.data(), (size_t)1, (size_t)body2.length(), (void*)httpResponse.GetBody<prom::PromStreamScraper>());
httpResponse.GetBody<prom::PromStreamScraper>()->FlushCache();
APSARA_TEST_EQUAL(11UL, res.GetEvents().size());

APSARA_TEST_EQUAL("go_goroutines 7", res.GetEvents()[7].Cast<RawEvent>().GetContent());
Expand Down

0 comments on commit 97a78d7

Please sign in to comment.