Skip to content

Commit

Permalink
fix: boost::lexical_cast throw error when pass empty string (#1718)
Browse files Browse the repository at this point in the history
  • Loading branch information
catdogpandas authored Aug 30, 2024
1 parent 46d63b9 commit 7e98ac4
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 17 deletions.
1 change: 0 additions & 1 deletion core/models/PipelineEventGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ enum class EventGroupMetaKey {
PROMETHEUS_SAMPLES_SCRAPED,
PROMETHEUS_SCRAPE_TIMESTAMP_MILLISEC,
PROMETHEUS_INSTANCE,
PROMETHEUS_SERIES_ADDED,
PROMETHEUS_UP_STATE,

SOURCE_ID
Expand Down
10 changes: 3 additions & 7 deletions core/processor/inner/ProcessorPromRelabelMetricNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ void ProcessorPromRelabelMetricNative::AddAutoMetrics(PipelineEventGroup& metric
return;
}

StringView scrapeTimestampMilliSecStr = metricGroup.GetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_TIMESTAMP_MILLISEC);
StringView scrapeTimestampMilliSecStr
= metricGroup.GetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_TIMESTAMP_MILLISEC);
auto timestampMilliSec = StringTo<uint64_t>(scrapeTimestampMilliSecStr.to_string());
auto timestamp = timestampMilliSec / 1000;
auto nanoSec = timestampMilliSec % 1000 * 1000000;
Expand Down Expand Up @@ -172,15 +173,10 @@ void ProcessorPromRelabelMetricNative::AddAutoMetrics(PipelineEventGroup& metric

AddMetric(metricGroup, prometheus::SCRAPE_SAMPLES_SCRAPED, samplesScraped, timestamp, nanoSec, instance);

auto seriesAdded
= StringTo<uint64_t>(metricGroup.GetMetadata(EventGroupMetaKey::PROMETHEUS_SERIES_ADDED).to_string());

AddMetric(metricGroup, prometheus::SCRAPE_SERIES_ADDED, seriesAdded, timestamp, nanoSec, instance);

AddMetric(metricGroup, prometheus::SCRAPE_TIMEOUT_SECONDS, mScrapeTimeoutSeconds, timestamp, nanoSec, instance);

// up metric must be the last one
bool upState = metricGroup.GetMetadata(EventGroupMetaKey::PROMETHEUS_UP_STATE).to_string() == "1";
bool upState = StringTo<bool>(metricGroup.GetMetadata(EventGroupMetaKey::PROMETHEUS_UP_STATE).to_string());

AddMetric(metricGroup, prometheus::UP, 1.0 * upState, timestamp, nanoSec, instance);
}
Expand Down
1 change: 0 additions & 1 deletion core/prometheus/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ const char* const SCRAPE_DURATION_SECONDS = "scrape_duration_seconds";
const char* const SCRAPE_RESPONSE_SIZE_BYTES = "scrape_response_size_bytes";
const char* const SCRAPE_SAMPLES_LIMIT = "scrape_samples_limit";
const char* const SCRAPE_SAMPLES_POST_METRIC_RELABELING = "scrape_samples_post_metric_relabeling";
const char* const SCRAPE_SERIES_ADDED = "scrape_series_added";
const char* const SCRAPE_SAMPLES_SCRAPED = "scrape_samples_scraped";
const char* const SCRAPE_TIMEOUT_SECONDS = "scrape_timeout_seconds";
const char* const UP = "up";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,23 +199,21 @@ test_metric8{k1="v1", k3="v2", } 9.9410452992e+10 1715829785083
eventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_TIMESTAMP_MILLISEC, ToString(1715829785083));
eventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SAMPLES_SCRAPED, ToString(8));
eventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_DURATION, ToString(1.5));
eventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SERIES_ADDED, ToString(8));
eventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_RESPONSE_SIZE, ToString(2325));
eventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_UP_STATE, ToString(1));
eventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_UP_STATE, ToString(true));
eventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_INSTANCE, string("localhost:8080"));
processor.AddAutoMetrics(eventGroup);

APSARA_TEST_EQUAL((size_t)16, eventGroup.GetEvents().size());
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(8, eventGroup.GetEvents().at(13).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
APSARA_TEST_EQUAL(15, eventGroup.GetEvents().at(14).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
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(15, eventGroup.GetEvents().at(13).Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue);
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"));
}

UNIT_TEST_CASE(ProcessorPromRelabelMetricNativeUnittest, TestInit)
Expand Down

0 comments on commit 7e98ac4

Please sign in to comment.