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
Prev Previous commit
Next Next commit
update
catdogpandas committed Jan 14, 2025
commit 631c66367530b6e5b8a933ef7023d117bf41bfc3
3 changes: 2 additions & 1 deletion core/prometheus/labels/TextParser.cpp
Original file line number Diff line number Diff line change
@@ -313,7 +313,8 @@ void TextParser::HandleTimestamp(MetricEvent& metricEvent) {
time_t timestamp = (int64_t)milliTimestamp / 1000;
auto ns = ((int64_t)milliTimestamp % 1000) * 1000000;
if (mHonorTimestamps) {
if (timestamp < (1 << 28)) {
// 2008年 8月 8日 星期五 08时00分00秒 CST
if (timestamp < 1218153600) {
HandleError("invalid timestamp");
return;
}
26 changes: 13 additions & 13 deletions core/unittest/prometheus/TextParserUnittest.cpp
Original file line number Diff line number Diff line change
@@ -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 1218153600\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(), 1218153600);

rawData = R"(
# TYPE cassandra_token_ownership_ratio gauge
@@ -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 1218153600)";
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(), 1218153600);

// 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 1218153600\naaa\n barbaz 0.34 1218153600\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(), 1218153600);
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(), 1218153600);

// Spaces around tags
rawData = R"(vm_accounting { name="vminsertRows", accountID = "1" , projectID= "1" } 277779100)";
@@ -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 1218153600 # 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(), 1218153600);

// float timestamp
rawData = "abc 123 456.789";
rawData = "abc 123 1218153600.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(), 1218153600));
APSARA_TEST_TRUE(
IsDoubleEqual(res.GetEvents().back().Cast<MetricEvent>().GetTimestampNanosecond().value(), 789000000));
}
@@ -347,9 +347,9 @@ void TextParserUnittest::TestHonorTimestamps() {
// true
parser.mHonorTimestamps = true;
// has timestamp
rawData = "abc 123 456";
rawData = "abc 123 1218153600";
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(), 1218153600);
APSARA_TEST_TRUE(IsDoubleEqual(res.GetEvents().back().Cast<MetricEvent>().GetTimestampNanosecond().value(), 0));

// no timestamp