Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
quzard committed Mar 8, 2024
1 parent 90323cb commit 3bfa633
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 42 deletions.
16 changes: 15 additions & 1 deletion core/models/PipelineEventGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "models/PipelineEventGroup.h"

#include <processor/ProcessorParseContainerLogNative.h>

#include <sstream>

#include "logger/Logger.h"
Expand Down Expand Up @@ -170,6 +172,9 @@ const std::string EVENT_GROUP_META_CONTAINER_IP = "container.ip";
const std::string EVENT_GROUP_META_CONTAINER_IMAGE_NAME = "container.image.name";
const std::string EVENT_GROUP_META_CONTAINER_IMAGE_ID = "container.image.id";

const std::string EVENT_GROUP_META_CONTAINERD_TEXT = "containerd_text";
const std::string EVENT_GROUP_META_DOCKER_JSON_FILE = "docker_json-file";

const std::string& EventGroupMetaKeyToString(EventGroupMetaKey key) {
switch (key) {
case EventGroupMetaKey::AGENT_TAG:
Expand Down Expand Up @@ -198,6 +203,15 @@ const std::string& EventGroupMetaKeyToString(EventGroupMetaKey key) {
}
}

const std::string EventGroupMetaValueToString(std::string value) {
if (value == ProcessorParseContainerLogNative::CONTAINERD_TEXT) {
return EVENT_GROUP_META_CONTAINERD_TEXT;
} else if (value == ProcessorParseContainerLogNative::DOCKER_JSON_FILE) {
return EVENT_GROUP_META_DOCKER_JSON_FILE;
}
return value;
}

EventGroupMetaKey StringToEventGroupMetaKey(const std::string& key) {
static std::unordered_map<std::string, EventGroupMetaKey> sStringToEnum
= {{EVENT_GROUP_META_AGENT_TAG, EventGroupMetaKey::AGENT_TAG},
Expand All @@ -221,7 +235,7 @@ Json::Value PipelineEventGroup::ToJson() const {
if (!mMetadata.empty()) {
Json::Value metadata;
for (const auto& meta : mMetadata) {
metadata[EventGroupMetaKeyToString(meta.first)] = meta.second.to_string();
metadata[EventGroupMetaKeyToString(meta.first)] = EventGroupMetaValueToString(meta.second.to_string());
}
root["metadata"] = metadata;
}
Expand Down
32 changes: 20 additions & 12 deletions core/processor/ProcessorParseContainerLogNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

namespace logtail {

const std::string ProcessorParseContainerLogNative::CONTAINERD_TEXT = "1";
const std::string ProcessorParseContainerLogNative::DOCKER_JSON_FILE = "2";

const std::string ProcessorParseContainerLogNative::sName = "processor_parse_container_log_native";
const char ProcessorParseContainerLogNative::CONTIANERD_DELIMITER = ' '; // 分隔符
const char ProcessorParseContainerLogNative::CONTIANERD_FULL_TAG = 'F'; // 容器全标签
Expand Down Expand Up @@ -133,9 +136,9 @@ bool ProcessorParseContainerLogNative::ProcessEvent(StringView containerType, Pi

std::string errorMsg;
bool shouldKeepEvent = true;
if (containerType == "containerd_text") {
if (containerType == CONTAINERD_TEXT) {
shouldKeepEvent = ParseContainerdTextLogLine(sourceEvent, errorMsg);
} else if (containerType == "docker_json-file") {
} else if (containerType == DOCKER_JSON_FILE) {
shouldKeepEvent = ParseDockerJsonLogLine(sourceEvent, errorMsg);
}
if (!errorMsg.empty()) {
Expand Down Expand Up @@ -250,22 +253,20 @@ bool ProcessorParseContainerLogNative::ParseDockerJsonLogLine(LogEvent& sourceEv
return true;
}

auto findMemberAndGetString = [&](const std::string& memberName) {
auto it = doc.FindMember(memberName.c_str());
return it != doc.MemberEnd() ? StringView(it->value.GetString()) : StringView();
};

StringView timeValue = findMemberAndGetString(DOCKER_JSON_TIME);
if (timeValue.empty()) {
// time
auto it = doc.FindMember(DOCKER_JSON_TIME.c_str());
if (it == doc.MemberEnd() || !it->value.IsString()) {
std::ostringstream errorMsgStream;
errorMsgStream << "time field cannot be found in log line."
<< "\tfirst 1KB log:" << buffer.substr(0, 1024).to_string();
errorMsg = errorMsgStream.str();
return true;
}
StringView timeValue = StringView(it->value.GetString());

auto it = doc.FindMember(DOCKER_JSON_LOG.c_str());
if (it == doc.MemberEnd()) {
// content
it = doc.FindMember(DOCKER_JSON_LOG.c_str());
if (it == doc.MemberEnd() || !it->value.IsString()) {
std::ostringstream errorMsgStream;
errorMsgStream << "content field cannot be found in log line."
<< "\tfirst 1KB log:" << buffer.substr(0, 1024).to_string();
Expand All @@ -274,7 +275,14 @@ bool ProcessorParseContainerLogNative::ParseDockerJsonLogLine(LogEvent& sourceEv
}
StringView content = StringView(it->value.GetString());

StringView sourceValue = findMemberAndGetString(DOCKER_JSON_STREAM_TYPE);
// source
it = doc.FindMember(DOCKER_JSON_STREAM_TYPE.c_str());
StringView sourceValue;
if (it == doc.MemberEnd() || !it->value.IsString()) {
sourceValue = StringView();
} else {
sourceValue = StringView(it->value.GetString());
}
if (sourceValue.empty() || (sourceValue != "stdout" && sourceValue != "stderr")) {
std::ostringstream errorMsgStream;
errorMsgStream << "source field cannot be found in log line."
Expand Down
5 changes: 4 additions & 1 deletion core/processor/ProcessorParseContainerLogNative.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class ProcessorParseContainerLogNative : public Processor {
public:
static const std::string sName;

static const std::string CONTAINERD_TEXT;
static const std::string DOCKER_JSON_FILE;

// needed by LastMatchedLine
static const char CONTIANERD_DELIMITER; // 分隔符
static const char CONTIANERD_FULL_TAG; // 容器全标签
Expand Down Expand Up @@ -60,7 +63,7 @@ class ProcessorParseContainerLogNative : public Processor {
bool ParseDockerJsonLogLine(LogEvent& sourceEvent, std::string& errorMsg);

CounterPtr mProcParseInSizeBytes; // 成功且保留的日志中,解析字段的INBYTES
CounterPtr mProcParseOutSizeBytes; // 成功且保留的日志中,解析出来字段的OUTBYTES和
CounterPtr mProcParseOutSizeBytes; // 成功且保留的日志中,解析出来字段的OUTBYTES
CounterPtr mProcParseErrorTotal; // 解析失败条数
CounterPtr mProcParseSuccessTotal; // 成功解析条数
// CounterPtr mProcParseSuccessSizeBytes; // 成功bytes
Expand Down
Loading

0 comments on commit 3bfa633

Please sign in to comment.