in core/unittest/processor/ProcessorSplitMultilineLogStringNativeUnittest.cpp [2002:2269]
void ProcessorSplitMultilineLogKeepUnmatchUnittest::TestLogSplitWithBegin() {
// make config
Json::Value config;
config["StartPattern"] = LOG_BEGIN_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 + start
// input: 1 event, 2 lines
// output: 2 events, 2 lines
{
auto sourceBuffer = std::make_shared<SourceBuffer>();
PipelineEventGroup eventGroup(sourceBuffer);
std::stringstream inJson;
inJson << R"({
"events" :
[
{
"contents" :
{
"content" : ")"
<< LOG_UNMATCH << R"(\n)" << LOG_BEGIN_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_UNMATCH << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
},
{
"contents" :
{
"content" : ")"
<< LOG_BEGIN_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: 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: start + start
// input: 1 event, 2 lines
// output: 2 events, 2 lines
{
auto sourceBuffer = std::make_shared<SourceBuffer>();
PipelineEventGroup eventGroup(sourceBuffer);
std::stringstream inJson;
inJson << R"({
"events" :
[
{
"contents" :
{
"content" : ")"
<< LOG_BEGIN_STRING << R"(\n)" << LOG_BEGIN_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_BEGIN_STRING << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
},
{
"contents" :
{
"content" : ")"
<< LOG_BEGIN_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: start + 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_BEGIN_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_BEGIN_STRING << R"(\n)" << 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: start + unmatch + \n
// 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_BEGIN_STRING << R"(\n)" << LOG_UNMATCH << R"(\n"
},
"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_BEGIN_STRING << R"(\n)" << LOG_UNMATCH << R"(\n"
},
"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(1 + 0 + 2 + 1 + 1, ProcessorSplitMultilineLogStringNative.mMatchedEventsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(1 + 0 + 2 + 2 + 2, ProcessorSplitMultilineLogStringNative.mMatchedLinesTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(1 + 1 + 0 + 0 + 0, ProcessorSplitMultilineLogStringNative.mUnmatchedLinesTotal->GetValue());
}