Skip to content

Commit

Permalink
fix time parsing problem (#552)
Browse files Browse the repository at this point in the history
* fix time parsing problem
  • Loading branch information
henryzhx8 authored Jan 10, 2023
1 parent 59667ee commit c90d573
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 47 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ your changes, such as:
- [public] [both] [updated] add a new feature

## [Unreleased]

- [public] [both] [fixed] ignore time zone adjustment in config when using system time as log time
36 changes: 28 additions & 8 deletions core/config_manager/ConfigManagerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,8 @@ void ConfigManagerBase::LoadSingleUserConfig(const std::string& logName, const J
config->mUnAcceptDirPattern = GetStringVector(value["dir_pattern_black_list"]);
}

// TODO: the following codes (line 673 - line 724) seem to be the same as the codes in line 816 - line
// 874. Remove the following codes in the future.
if (value.isMember("merge_type") && value["merge_type"].isString()) {
string mergeType = value["merge_type"].asString();

Expand Down Expand Up @@ -828,17 +830,35 @@ void ConfigManagerBase::LoadSingleUserConfig(const std::string& logName, const J
string logTZ = value["log_tz"].asString();
int logTZSecond = 0;
bool adjustFlag = value["tz_adjust"].asBool();
if (adjustFlag && !ParseTimeZoneOffsetSecond(logTZ, logTZSecond)) {
LOG_ERROR(sLogger, ("invalid log time zone set", logTZ));
config->mTimeZoneAdjust = false;
if (adjustFlag) {
if (config->mTimeFormat.empty()) {
LOG_WARNING(sLogger,
("choose to adjust log time zone, but time format is not specified",
"use system time as log time instead")("project", config->mProjectName)(
"logstore", config->mCategory)("config", config->mConfigName));
config->mTimeZoneAdjust = false;
} else if ((logType == DELIMITER_LOG || logType == JSON_LOG) && config->mTimeKey.empty()) {
LOG_WARNING(sLogger,
("choose to adjust log time zone, but time key is not specified",
"use system time as log time instead")("project", config->mProjectName)(
"logstore", config->mCategory)("config", config->mConfigName));
config->mTimeZoneAdjust = false;
} else if (!ParseTimeZoneOffsetSecond(logTZ, logTZSecond)) {
LOG_WARNING(sLogger,
("invalid log time zone specified, will parse log time without time zone adjusted",
logTZ)("project", config->mProjectName)("logstore", config->mCategory)(
"config", config->mConfigName));
config->mTimeZoneAdjust = false;
} else {
config->mTimeZoneAdjust = adjustFlag;
config->mLogTimeZoneOffsetSecond = logTZSecond;
LOG_INFO(sLogger,
("set log time zone", logTZ)("project", config->mProjectName)(
"logstore", config->mCategory)("config", config->mConfigName));
}
} else {
config->mTimeZoneAdjust = adjustFlag;
config->mLogTimeZoneOffsetSecond = logTZSecond;
if (adjustFlag) {
LOG_INFO(sLogger,
("set log timezone adjust, project", config->mProjectName)(
"logstore", config->mCategory)("time zone", logTZ)("offset seconds", logTZSecond));
}
}
}

Expand Down
18 changes: 7 additions & 11 deletions core/parser/LogParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ bool LogParser::RegexLogLineParser(const char* buffer,
return true;
} else if (!discardUnmatch) {
LogParser::AddUnmatchLog(buffer, logGroup, logGroupSize);
return true;
}
return false;
}
Expand Down Expand Up @@ -448,12 +447,10 @@ bool LogParser::RegexLogLineParser(const char* buffer,
parseSuccess = false;
}
if (!parseSuccess) {
if (discardUnmatch)
return false;
else {
if (!discardUnmatch) {
AddUnmatchLog(buffer, logGroup, logGroupSize);
return true;
}
return false;
}

Log* logPtr = logGroup.add_logs();
Expand Down Expand Up @@ -524,7 +521,7 @@ bool LogParser::ParseLogTime(const char* buffer,
}

if (logTime <= 0
|| (BOOL_FLAG(ilogtail_discard_old_data) && (time(NULL) - logTime) > INT32_FLAG(ilogtail_discard_interval))) {
|| (BOOL_FLAG(ilogtail_discard_old_data) && (time(NULL) - logTime + tzOffsetSecond) > INT32_FLAG(ilogtail_discard_interval))) {
if (AppConfig::GetInstance()->IsLogParseAlarmValid()) {
if (LogtailAlarm::GetInstance()->IsLowLevelAlarmValid()) {
LOG_WARNING(sLogger,
Expand Down Expand Up @@ -694,14 +691,13 @@ bool LogParser::ApsaraEasyReadLogLineParser(const char* buffer,
PARSE_TIME_FAIL_ALARM, bufOut + " $ " + ToString(logTime), projectName, category, region);
error = PARSE_LOG_TIMEFORMAT_ERROR;

if (discardUnmatch)
return false;
else {
if (!discardUnmatch)
{
AddUnmatchLog(buffer, logGroup, logGroupSize);
return true;
}
return false;
}
if (BOOL_FLAG(ilogtail_discard_old_data) && (time(NULL) - logTime) > INT32_FLAG(ilogtail_discard_interval)) {
if (BOOL_FLAG(ilogtail_discard_old_data) && (time(NULL) - logTime + tzOffsetSecond) > INT32_FLAG(ilogtail_discard_interval)) {
if (AppConfig::GetInstance()->IsLogParseAlarmValid()) {
string bufOut(buffer);
if (bufOut.size() > (size_t)(1024)) {
Expand Down
7 changes: 4 additions & 3 deletions core/processor/LogProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,9 @@ void* LogProcess::ProcessLoop(int32_t threadNo) {
int32_t successLogSize = 0;
int32_t parseStartTime = (int32_t)time(NULL);
for (uint32_t i = 0; i < lines; i++) {
if (!logFileReader->ParseLogLine(
buffer + logIndex[i], logGroup, error, lastLogLineTime, lastLogTimeStr, logGroupSize)) {
bool successful = logFileReader->ParseLogLine(
buffer + logIndex[i], logGroup, error, lastLogLineTime, lastLogTimeStr, logGroupSize);
if (!successful) {
++parseFailures;
if (error == PARSE_LOG_REGEX_ERROR)
++regexMatchFailures;
Expand All @@ -433,7 +434,7 @@ void* LogProcess::ProcessLoop(int32_t threadNo) {
LogParser::AddLog(
logPtr, config->mAdvancedConfig.mRawLogTag, buffer + logIndex[i], logGroupSize);
}
if (config->mTimeZoneAdjust) {
if (successful && config->mTimeZoneAdjust) {
LogParser::AdjustLogTime(
logPtr, config->mLogTimeZoneOffsetSecond, localTimeZoneOffsetSecond);
}
Expand Down
1 change: 0 additions & 1 deletion core/reader/DelimiterLogFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ bool DelimiterLogFileReader::ParseLogLine(const char* buffer,
return true;
} else if (!mDiscardUnmatch) {
LogParser::AddUnmatchLog(buffer, logGroup, logGroupSize);
return true;
}
return false;
}
Expand Down
1 change: 0 additions & 1 deletion core/reader/JsonLogFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ bool JsonLogFileReader::ParseLogLine(const char* buffer,
return true;
} else if (!mDiscardUnmatch) {
LogParser::AddUnmatchLog(buffer, logGroup, logGroupSize);
return true;
}
return false;
}
Expand Down
Loading

0 comments on commit c90d573

Please sign in to comment.