Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pb is illegal when length of metricEvent timestamp is less than 10 #2038

Merged
5 changes: 5 additions & 0 deletions core/prometheus/component/StreamScraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ size_t StreamScraper::MetricWriteCallback(char* buffer, size_t size, size_t nmem

if (begin < sizes) {
body->mCache.append(buffer + begin, sizes - begin);
// limit the last line cache size to 8K bytes
if (body->mCache.size() > 8192) {
LOG_WARNING(sLogger, ("stream scraper", "cache is too large, drop it."));
body->mCache.clear();
}
}
body->mRawSize += sizes;
body->mCurrStreamSize += sizes;
Expand Down
5 changes: 5 additions & 0 deletions core/prometheus/labels/TextParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ void TextParser::HandleTimestamp(MetricEvent& metricEvent) {
time_t timestamp = (int64_t)milliTimestamp / 1000;
auto ns = ((int64_t)milliTimestamp % 1000) * 1000000;
if (mHonorTimestamps) {
// limit length of timestamp to 10 digits
if (timestamp < 1000000000) {
HandleError("invalid timestamp");
return;
}
metricEvent.SetTimestamp(timestamp, ns);
} else {
metricEvent.SetTimestamp(mDefaultTimestamp, mDefaultNanoTimestamp);
Expand Down
9 changes: 6 additions & 3 deletions core/prometheus/schedulers/ScrapeScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void ScrapeScheduler::OnMetricResult(HttpResponse& response, uint64_t) {

const auto& networkStatus = response.GetNetworkStatus();
string scrapeState;
mUpState = false;
if (networkStatus.mCode != NetworkCode::Ok) {
// not 0 means curl error
scrapeState = prom::NetworkCodeToState(networkStatus.mCode);
Expand All @@ -81,20 +82,22 @@ void ScrapeScheduler::OnMetricResult(HttpResponse& response, uint64_t) {
} else {
// 0 means success
scrapeState = prom::NetworkCodeToState(NetworkCode::Ok);
mUpState = true;
}

mScrapeDurationSeconds = scrapeDurationMilliSeconds * sRate;
mUpState = response.GetStatusCode() == 200;
if (response.GetStatusCode() != 200) {
LOG_WARNING(sLogger,
("scrape failed, status code",
response.GetStatusCode())("target", mHash)("curl msg", response.GetNetworkStatus().mMessage));
}

auto mScrapeDurationSeconds = scrapeDurationMilliSeconds * sRate;
auto mUpState = response.GetStatusCode() == 200;
streamScraper->mStreamIndex++;
streamScraper->FlushCache();

if (mUpState) {
catdogpandas marked this conversation as resolved.
Show resolved Hide resolved
streamScraper->FlushCache();
}
streamScraper->SetAutoMetricMeta(mScrapeDurationSeconds, mUpState, scrapeState);
streamScraper->SendMetrics();
streamScraper->Reset();
Expand Down
26 changes: 13 additions & 13 deletions core/unittest/prometheus/TextParserUnittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ void TextParserUnittest::TestParseSuccess() {
APSARA_TEST_TRUE(
IsDoubleEqual(res.GetEvents().back().Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue, 123.0));

rawData = "foobar 123.456 789\n";
rawData = "foobar 123.456 1000000000\n";
res = parser.Parse(rawData, 0, 0);
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetName().to_string(), "foobar");
APSARA_TEST_TRUE(
IsDoubleEqual(res.GetEvents().back().Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue, 123.456));
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetTimestamp(), 789);
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetTimestamp(), 1000000000);

rawData = R"(
# TYPE cassandra_token_ownership_ratio gauge
Expand Down Expand Up @@ -275,27 +275,27 @@ cassandra_token_ownership_ratio 78.9)";
IsDoubleEqual(res.GetEvents().back().Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue, -1.2));

// Empty tags
rawData = R"(foo {bar="baz",aa="",x="y"} 1 2)";
rawData = R"(foo {bar="baz",aa="",x="y"} 1 1000000000)";
res = parser.Parse(rawData, 0, 0);
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetName().to_string(), "foo");
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetTag("bar").to_string(), "baz");
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetTag("aa").to_string(), "");
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetTag("x").to_string(), "y");
APSARA_TEST_TRUE(
IsDoubleEqual(res.GetEvents().back().Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue, 1.0));
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetTimestamp(), 2);
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetTimestamp(), 1000000000);

// Multi lines with invalid line
rawData = "\t foo\t { } 0.3\t 2\naaa\n barbaz 0.34 43\n";
rawData = "\t foo\t { } 0.3\t 1000000000\naaa\n barbaz 0.34 1000000000\n";
res = parser.Parse(rawData, 0, 0);
APSARA_TEST_EQUAL(res.GetEvents().size(), 2UL);
APSARA_TEST_EQUAL(res.GetEvents()[0].Cast<MetricEvent>().GetName().to_string(), "foo");
APSARA_TEST_TRUE(IsDoubleEqual(res.GetEvents()[0].Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue, 0.3));
APSARA_TEST_EQUAL(res.GetEvents()[0].Cast<MetricEvent>().GetTimestamp(), 2);
APSARA_TEST_EQUAL(res.GetEvents()[0].Cast<MetricEvent>().GetTimestamp(), 1000000000);
APSARA_TEST_EQUAL(res.GetEvents()[1].Cast<MetricEvent>().GetName().to_string(), "barbaz");
APSARA_TEST_TRUE(
IsDoubleEqual(res.GetEvents()[1].Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue, 0.34));
APSARA_TEST_EQUAL(res.GetEvents()[1].Cast<MetricEvent>().GetTimestamp(), 43);
APSARA_TEST_EQUAL(res.GetEvents()[1].Cast<MetricEvent>().GetTimestamp(), 1000000000);

// Spaces around tags
rawData = R"(vm_accounting { name="vminsertRows", accountID = "1" , projectID= "1" } 277779100)";
Expand All @@ -308,20 +308,20 @@ cassandra_token_ownership_ratio 78.9)";
IsDoubleEqual(res.GetEvents().back().Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue, 277779100.0));

// Exemplars
rawData = "abc 123 456 # foobar";
rawData = "abc 123 1000000000 # foobar";
res = parser.Parse(rawData, 0, 0);
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetName().to_string(), "abc");
APSARA_TEST_TRUE(
IsDoubleEqual(res.GetEvents().back().Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue, 123.0));
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetTimestamp(), 456);
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetTimestamp(), 1000000000);

// float timestamp
rawData = "abc 123 456.789";
rawData = "abc 123 1000000000.789";
res = parser.Parse(rawData, 0, 0);
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetName().to_string(), "abc");
APSARA_TEST_TRUE(
IsDoubleEqual(res.GetEvents().back().Cast<MetricEvent>().GetValue<UntypedSingleValue>()->mValue, 123.0));
APSARA_TEST_TRUE(IsDoubleEqual(res.GetEvents().back().Cast<MetricEvent>().GetTimestamp(), 456));
APSARA_TEST_TRUE(IsDoubleEqual(res.GetEvents().back().Cast<MetricEvent>().GetTimestamp(), 1000000000));
APSARA_TEST_TRUE(
IsDoubleEqual(res.GetEvents().back().Cast<MetricEvent>().GetTimestampNanosecond().value(), 789000000));
}
Expand All @@ -347,9 +347,9 @@ void TextParserUnittest::TestHonorTimestamps() {
// true
parser.mHonorTimestamps = true;
// has timestamp
rawData = "abc 123 456";
rawData = "abc 123 1000000000";
res = parser.Parse(rawData, 789, 111);
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetTimestamp(), 456);
APSARA_TEST_EQUAL(res.GetEvents().back().Cast<MetricEvent>().GetTimestamp(), 1000000000);
APSARA_TEST_TRUE(IsDoubleEqual(res.GetEvents().back().Cast<MetricEvent>().GetTimestampNanosecond().value(), 0));

// no timestamp
Expand Down
Loading