in core/unittest/processor/ProcessorSplitMultilineLogStringNativeUnittest.cpp [2271:2529]
void ProcessorSplitMultilineLogKeepUnmatchUnittest::TestLogSplitWithContinueEnd() {
// make config
Json::Value config;
config["ContinuePattern"] = LOG_CONTINUE_REGEX;
config["EndPattern"] = LOG_END_REGEX;
config["UnmatchedContentTreatment"] = "single_line";
config["SplitType"] = "regex";
// ProcessorSplitMultilineLogStringNative
ProcessorSplitMultilineLogStringNative ProcessorSplitMultilineLogStringNative;
ProcessorSplitMultilineLogStringNative.SetContext(mContext);
ProcessorSplitMultilineLogStringNative.SetMetricsRecordRef(ProcessorSplitMultilineLogStringNative::sName, "1");
APSARA_TEST_TRUE_FATAL(ProcessorSplitMultilineLogStringNative.Init(config));
// case: unmatch
// input: 1 event, 1 line
// output: 1 event, 1 line
{
auto sourceBuffer = std::make_shared<SourceBuffer>();
PipelineEventGroup eventGroup(sourceBuffer);
std::stringstream inJson;
inJson << R"({
"events" :
[
{
"contents" :
{
"content" : ")"
<< LOG_UNMATCH << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
}
]
})";
eventGroup.FromJsonString(inJson.str());
// run test function
ProcessorSplitMultilineLogStringNative.Process(eventGroup);
// judge result
std::stringstream expectJson;
expectJson << R"({
"events" :
[
{
"contents" :
{
"content" : ")"
<< LOG_UNMATCH << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
}
]
})";
std::string outJson = eventGroup.ToJsonString();
APSARA_TEST_STREQ(CompactJson(expectJson.str()).c_str(), CompactJson(outJson).c_str());
}
// case: Continue + unmatch
// input: 1 event, 2 lines
// output: 1 event, 2 lines
{
auto sourceBuffer = std::make_shared<SourceBuffer>();
PipelineEventGroup eventGroup(sourceBuffer);
std::stringstream inJson;
inJson << R"({
"events" :
[
{
"contents" :
{
"content" : ")"
<< LOG_CONTINUE_STRING << R"(\n)" << LOG_UNMATCH << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
}
]
})";
eventGroup.FromJsonString(inJson.str());
// run test function
ProcessorSplitMultilineLogStringNative.Process(eventGroup);
// judge result
std::stringstream expectJson;
expectJson << R"({
"events" :
[
{
"contents" :
{
"content" : ")"
<< LOG_CONTINUE_STRING << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
},
{
"contents" :
{
"content" : ")"
<< LOG_UNMATCH << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
}
]
})";
std::string outJson = eventGroup.ToJsonString();
APSARA_TEST_STREQ(CompactJson(expectJson.str()).c_str(), CompactJson(outJson).c_str());
}
// case: Continue + Continue + end
// input: 1 event, 3 lines
// output: 1 event, 3 lines
{
auto sourceBuffer = std::make_shared<SourceBuffer>();
PipelineEventGroup eventGroup(sourceBuffer);
std::stringstream inJson;
inJson << R"({
"events" :
[
{
"contents" :
{
"content" : ")"
<< LOG_CONTINUE_STRING << R"(\n)" << LOG_CONTINUE_STRING << R"(\n)" << LOG_END_STRING << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
}
]
})";
eventGroup.FromJsonString(inJson.str());
// run test function
ProcessorSplitMultilineLogStringNative.Process(eventGroup);
// judge result
std::stringstream expectJson;
expectJson << R"({
"events" :
[
{
"contents" :
{
"content" : ")"
<< LOG_CONTINUE_STRING << R"(\n)" << LOG_CONTINUE_STRING << R"(\n)" << LOG_END_STRING << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
}
]
})";
std::string outJson = eventGroup.ToJsonString();
APSARA_TEST_STREQ(CompactJson(expectJson.str()).c_str(), CompactJson(outJson).c_str());
}
// case: continue
// input: 1 event, 1 line
// output: 1 event, 1 line
{
auto sourceBuffer = std::make_shared<SourceBuffer>();
PipelineEventGroup eventGroup(sourceBuffer);
std::stringstream inJson;
inJson << R"({
"events" :
[
{
"contents" :
{
"content" : ")"
<< LOG_CONTINUE_STRING << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
}
]
})";
eventGroup.FromJsonString(inJson.str());
// run test function
ProcessorSplitMultilineLogStringNative.Process(eventGroup);
// judge result
std::stringstream expectJson;
expectJson << R"({
"events" :
[
{
"contents" :
{
"content" : ")"
<< LOG_CONTINUE_STRING << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
}
]
})";
std::string outJson = eventGroup.ToJsonString();
APSARA_TEST_STREQ(CompactJson(expectJson.str()).c_str(), CompactJson(outJson).c_str());
}
// case: end
// input: 1 event, 1 line
// output: 1 event, 1 line
{
auto sourceBuffer = std::make_shared<SourceBuffer>();
PipelineEventGroup eventGroup(sourceBuffer);
std::stringstream inJson;
inJson << R"({
"events" :
[
{
"contents" :
{
"content" : ")"
<< LOG_END_STRING << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
}
]
})";
eventGroup.FromJsonString(inJson.str());
// run test function
ProcessorSplitMultilineLogStringNative.Process(eventGroup);
// judge result
std::stringstream expectJson;
expectJson << R"({
"events" :
[
{
"contents" :
{
"content" : ")"
<< LOG_END_STRING << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
}
]
})";
std::string outJson = eventGroup.ToJsonString();
APSARA_TEST_STREQ(CompactJson(expectJson.str()).c_str(), CompactJson(outJson).c_str());
}
// metric
APSARA_TEST_EQUAL_FATAL(0 + 0 + 1 + 0 + 1, ProcessorSplitMultilineLogStringNative.mMatchedEventsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(0 + 0 + 3 + 0 + 1, ProcessorSplitMultilineLogStringNative.mMatchedLinesTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(1 + 2 + 0 + 1 + 0, ProcessorSplitMultilineLogStringNative.mUnmatchedLinesTotal->GetValue());
}