Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
henryzhx8 committed Oct 31, 2024
1 parent f13a372 commit a3f452d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
47 changes: 24 additions & 23 deletions core/pipeline/serializer/SLSSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ bool SLSEventGroupSerializer::Serialize(BatchedEvents&& group, string& res, stri

bool enableNs = mFlusher->GetContext().GetGlobalConfig().mEnableTimestampNanosecond;

size_t logGroupSZ = 0;
// caculate serialized logGroup size first, where some critical results can be cached
vector<size_t> logSZ(group.mEvents.size());
vector<pair<string, size_t>> metricEventContentSZ(group.mEvents.size());
vector<pair<string, size_t>> metricEventContentCache(group.mEvents.size());
size_t logGroupSZ = 0;
switch (eventType) {
case PipelineEvent::Type::LOG:
for (size_t i = 0; i < group.mEvents.size(); ++i) {
Expand All @@ -86,16 +87,16 @@ bool SLSEventGroupSerializer::Serialize(BatchedEvents&& group, string& res, stri
if (e.Is<std::monostate>()) {
continue;
} else if (e.Is<UntypedSingleValue>()) {
metricEventContentSZ[i].first = to_string(e.GetValue<UntypedSingleValue>()->mValue);
metricEventContentCache[i].first = to_string(e.GetValue<UntypedSingleValue>()->mValue);
}
metricEventContentSZ[i].second = GetMetricLabelSize(e);
metricEventContentCache[i].second = GetMetricLabelSize(e);

size_t contentSZ = 0;
contentSZ += GetLogContentSize(METRIC_RESERVED_KEY_NAME.size(), e.GetName().size());
contentSZ += GetLogContentSize(METRIC_RESERVED_KEY_VALUE.size(), metricEventContentSZ[i].first.size());
contentSZ += GetLogContentSize(METRIC_RESERVED_KEY_VALUE.size(), metricEventContentCache[i].first.size());
contentSZ
+= GetLogContentSize(METRIC_RESERVED_KEY_TIME_NANO.size(), e.GetTimestampNanosecond() ? 19U : 10U);
contentSZ += GetLogContentSize(METRIC_RESERVED_KEY_LABELS.size(), metricEventContentSZ[i].second);
contentSZ += GetLogContentSize(METRIC_RESERVED_KEY_LABELS.size(), metricEventContentCache[i].second);
logGroupSZ += GetLogSize(contentSZ, false, logSZ[i]);
}
break;
Expand All @@ -120,47 +121,47 @@ bool SLSEventGroupSerializer::Serialize(BatchedEvents&& group, string& res, stri
return false;
}

static LogGroupSerializer logGroup;
logGroup.PrepareToSerialize(logGroupSZ);
static LogGroupSerializer serializer;
serializer.Prepare(logGroupSZ);
for (size_t i = 0; i < group.mEvents.size(); ++i) {
if (group.mEvents[i].Is<LogEvent>()) {
const auto& logEvent = group.mEvents[i].Cast<LogEvent>();
logGroup.StartToAddLog(logSZ[i]);
logGroup.AddLogTime(logEvent.GetTimestamp());
serializer.StartToAddLog(logSZ[i]);
serializer.AddLogTime(logEvent.GetTimestamp());
for (const auto& kv : logEvent) {
logGroup.AddLogContent(kv.first, kv.second);
serializer.AddLogContent(kv.first, kv.second);
}
if (enableNs && logEvent.GetTimestampNanosecond()) {
logGroup.AddLogTimeNs(logEvent.GetTimestampNanosecond().value());
serializer.AddLogTimeNs(logEvent.GetTimestampNanosecond().value());
}
} else if (group.mEvents[i].Is<MetricEvent>()) {
const auto& metricEvent = group.mEvents[i].Cast<MetricEvent>();
if (metricEvent.Is<std::monostate>()) {
continue;
}
logGroup.StartToAddLog(logSZ[i]);
logGroup.AddLogTime(metricEvent.GetTimestamp());
logGroup.AddLogContentMetricLabel(metricEvent, metricEventContentSZ[i].second);
logGroup.AddLogContentMetricTimeNano(metricEvent);
logGroup.AddLogContent(METRIC_RESERVED_KEY_VALUE, metricEventContentSZ[i].first);
logGroup.AddLogContent(METRIC_RESERVED_KEY_NAME, metricEvent.GetName());
serializer.StartToAddLog(logSZ[i]);
serializer.AddLogTime(metricEvent.GetTimestamp());
serializer.AddLogContentMetricLabel(metricEvent, metricEventContentCache[i].second);
serializer.AddLogContentMetricTimeNano(metricEvent);
serializer.AddLogContent(METRIC_RESERVED_KEY_VALUE, metricEventContentCache[i].first);
serializer.AddLogContent(METRIC_RESERVED_KEY_NAME, metricEvent.GetName());
} else {
errorMsg = "unsupported event type in event group";
return false;
}
}
for (const auto& tag : group.mTags.mInner) {
if (tag.first == LOG_RESERVED_KEY_TOPIC) {
logGroup.AddTopic(tag.second);
serializer.AddTopic(tag.second);
} else if (tag.first == LOG_RESERVED_KEY_SOURCE) {
logGroup.AddSource(tag.second);
serializer.AddSource(tag.second);
} else if (tag.first == LOG_RESERVED_KEY_MACHINE_UUID) {
logGroup.AddMachineUUID(tag.second);
serializer.AddMachineUUID(tag.second);
} else {
logGroup.AddLogTag(tag.first, tag.second);
serializer.AddLogTag(tag.first, tag.second);
}
}
res = std::move(logGroup.GetResult());
res = std::move(serializer.GetResult());
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion core/protobuf/sls/LogGroupSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static inline void fixed32_pack(uint32_t value, string& output) {
}
}

void LogGroupSerializer::PrepareToSerialize(size_t size) {
void LogGroupSerializer::Prepare(size_t size) {
mRes.clear();
mRes.reserve(size);
}
Expand Down
2 changes: 1 addition & 1 deletion core/protobuf/sls/LogGroupSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extern const std::string METRIC_LABELS_KEY_VALUE_SEPARATOR;

class LogGroupSerializer {
public:
void PrepareToSerialize(size_t size);
void Prepare(size_t size);
void StartToAddLog(size_t size);
void AddLogTime(uint32_t logTime);
void AddLogContent(StringView key, StringView value);
Expand Down
2 changes: 1 addition & 1 deletion core/unittest/log_pb/LogGroupSerializerUnittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void LogGroupSerializerUnittest::TestSerialize() {
groupSZ += GetLogTagSize(strlen("key_6"), strlen("value_6"));

LogGroupSerializer logGroup;
logGroup.PrepareToSerialize(groupSZ);
logGroup.Prepare(groupSZ);
logGroup.StartToAddLog(logSZ[0]);
logGroup.AddLogTime(1234567890);
logGroup.AddLogContent("key_1", "value_1");
Expand Down

0 comments on commit a3f452d

Please sign in to comment.