Skip to content

Commit

Permalink
init (#2047)
Browse files Browse the repository at this point in the history
  • Loading branch information
Takuka0311 authored Jan 17, 2025
1 parent 3527418 commit e563035
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 60 deletions.
2 changes: 1 addition & 1 deletion core/models/MetricEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class MetricEvent : public PipelineEvent {
mValue = T{std::forward<Args>(args)...};
}

void SetValue(const std::map<StringView, double>& multiDoubleValues) {
void SetValue(const std::map<StringView, UntypedMultiDoubleValue>& multiDoubleValues) {
mValue = UntypedMultiDoubleValues{multiDoubleValues, this};
}

Expand Down
31 changes: 20 additions & 11 deletions core/models/MetricValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using namespace std;

namespace logtail {

bool UntypedMultiDoubleValues::GetValue(StringView key, double& val) const {
bool UntypedMultiDoubleValues::GetValue(StringView key, UntypedMultiDoubleValue& val) const {
if (mValues.find(key) != mValues.end()) {
val = mValues.at(key);
return true;
Expand All @@ -32,35 +32,35 @@ bool UntypedMultiDoubleValues::HasValue(StringView key) const {
return mValues.find(key) != mValues.end();
}

void UntypedMultiDoubleValues::SetValue(const std::string& key, double val) {
void UntypedMultiDoubleValues::SetValue(const std::string& key, UntypedMultiDoubleValue val) {
if (mMetricEventPtr) {
SetValueNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val);
}
}

void UntypedMultiDoubleValues::SetValue(StringView key, double val) {
void UntypedMultiDoubleValues::SetValue(StringView key, UntypedMultiDoubleValue val) {
if (mMetricEventPtr) {
SetValueNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val);
}
}

void UntypedMultiDoubleValues::SetValueNoCopy(const StringBuffer& key, double val) {
void UntypedMultiDoubleValues::SetValueNoCopy(const StringBuffer& key, UntypedMultiDoubleValue val) {
SetValueNoCopy(StringView(key.data, key.size), val);
}

void UntypedMultiDoubleValues::SetValueNoCopy(StringView key, double val) {
void UntypedMultiDoubleValues::SetValueNoCopy(StringView key, UntypedMultiDoubleValue val) {
mValues[key] = val;
}

void UntypedMultiDoubleValues::DelValue(StringView key) {
mValues.erase(key);
}

std::map<StringView, double>::const_iterator UntypedMultiDoubleValues::ValuesBegin() const {
std::map<StringView, UntypedMultiDoubleValue>::const_iterator UntypedMultiDoubleValues::ValuesBegin() const {
return mValues.begin();
}

std::map<StringView, double>::const_iterator UntypedMultiDoubleValues::ValuesEnd() const {
std::map<StringView, UntypedMultiDoubleValue>::const_iterator UntypedMultiDoubleValues::ValuesEnd() const {
return mValues.end();
}

Expand Down Expand Up @@ -101,17 +101,26 @@ void UntypedSingleValue::FromJson(const Json::Value& value) {
Json::Value UntypedMultiDoubleValues::ToJson() const {
Json::Value res;
for (auto metric : mValues) {
res[metric.first.to_string()] = metric.second;
string type = "unknown";
if (metric.second.MetricType == UntypedValueMetricType::MetricTypeCounter)
type = "counter";
else if (metric.second.MetricType == UntypedValueMetricType::MetricTypeGauge)
type = "gauge";
res[metric.first.to_string()]["type"] = type;
res[metric.first.to_string()]["value"] = metric.second.Value;
}
return res;
}

void UntypedMultiDoubleValues::FromJson(const Json::Value& value) {
mValues.clear();
for (Json::Value::const_iterator itr = value.begin(); itr != value.end(); ++itr) {
if (itr->asDouble()) {
SetValue(itr.key().asString(), itr->asDouble());
}
UntypedValueMetricType type;
if (itr->get("type", "unknown").asString() == "counter")
type = UntypedValueMetricType::MetricTypeCounter;
else if (itr->get("type", "unknown").asString() == "gauge")
type = UntypedValueMetricType::MetricTypeGauge;
SetValue(itr.key().asString(), UntypedMultiDoubleValue{type, itr->get("value", 0).asDouble()});
}
}
#endif
Expand Down
25 changes: 16 additions & 9 deletions core/models/MetricValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,31 @@ struct UntypedSingleValue {
#endif
};

enum UntypedValueMetricType { MetricTypeCounter, MetricTypeGauge };

struct UntypedMultiDoubleValue {
UntypedValueMetricType MetricType;
double Value;
};

struct UntypedMultiDoubleValues {
std::map<StringView, double> mValues;
std::map<StringView, UntypedMultiDoubleValue> mValues;
PipelineEvent* mMetricEventPtr;

UntypedMultiDoubleValues(PipelineEvent* ptr) : mMetricEventPtr(ptr) {}
UntypedMultiDoubleValues(std::map<StringView, double> values, PipelineEvent* ptr)
UntypedMultiDoubleValues(std::map<StringView, UntypedMultiDoubleValue> values, PipelineEvent* ptr)
: mValues(values), mMetricEventPtr(ptr) {}

bool GetValue(StringView key, double& val) const;
bool GetValue(StringView key, UntypedMultiDoubleValue& val) const;
bool HasValue(StringView key) const;
void SetValue(const std::string& key, double val);
void SetValue(StringView key, double val);
void SetValueNoCopy(const StringBuffer& key, double val);
void SetValueNoCopy(StringView key, double val);
void SetValue(const std::string& key, UntypedMultiDoubleValue val);
void SetValue(StringView key, UntypedMultiDoubleValue val);
void SetValueNoCopy(const StringBuffer& key, UntypedMultiDoubleValue val);
void SetValueNoCopy(StringView key, UntypedMultiDoubleValue val);
void DelValue(StringView key);

std::map<StringView, double>::const_iterator ValuesBegin() const;
std::map<StringView, double>::const_iterator ValuesEnd() const;
std::map<StringView, UntypedMultiDoubleValue>::const_iterator ValuesBegin() const;
std::map<StringView, UntypedMultiDoubleValue>::const_iterator ValuesEnd() const;
size_t ValusSize() const;

size_t DataSize() const;
Expand Down
1 change: 1 addition & 0 deletions core/monitor/SelfMonitorServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ void SelfMonitorServer::SendMetrics() {

PipelineEventGroup pipelineEventGroup(std::make_shared<SourceBuffer>());
pipelineEventGroup.SetTagNoCopy(LOG_RESERVED_KEY_SOURCE, LoongCollectorMonitor::mIpAddr);
pipelineEventGroup.SetTag(LOG_RESERVED_KEY_TOPIC, "__metric__");
ReadAsPipelineEventGroup(pipelineEventGroup);

shared_ptr<Pipeline> pipeline
Expand Down
6 changes: 4 additions & 2 deletions core/monitor/metric_models/SelfMonitorMetricEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,13 @@ void SelfMonitorMetricEvent::ReadAsMetricEvent(MetricEvent* metricEventPtr) {
// values
metricEventPtr->SetValue(UntypedMultiDoubleValues{{}, nullptr});
for (auto counter = mCounters.begin(); counter != mCounters.end(); counter++) {
metricEventPtr->MutableValue<UntypedMultiDoubleValues>()->SetValue(counter->first, counter->second);
metricEventPtr->MutableValue<UntypedMultiDoubleValues>()->SetValue(
counter->first, {UntypedValueMetricType::MetricTypeCounter, double(counter->second)});
counter->second = 0;
}
for (auto gauge = mGauges.begin(); gauge != mGauges.end(); gauge++) {
metricEventPtr->MutableValue<UntypedMultiDoubleValues>()->SetValue(gauge->first, gauge->second);
metricEventPtr->MutableValue<UntypedMultiDoubleValues>()->SetValue(
gauge->first, {UntypedValueMetricType::MetricTypeGauge, gauge->second});
}
// set flags
mLastSendInterval = 0;
Expand Down
2 changes: 1 addition & 1 deletion core/pipeline/serializer/JsonSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ bool JsonEventGroupSerializer::Serialize(BatchedEvents&& group, string& res, str
for (auto value = e.GetValue<UntypedMultiDoubleValues>()->ValuesBegin();
value != e.GetValue<UntypedMultiDoubleValues>()->ValuesEnd();
value++) {
eventJson[METRIC_RESERVED_KEY_VALUE][value->first.to_string()] = value->second;
eventJson[METRIC_RESERVED_KEY_VALUE][value->first.to_string()] = value->second.Value;
}
}
Json::StreamWriterBuilder writer;
Expand Down
86 changes: 59 additions & 27 deletions core/unittest/models/MetricEventUnittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,40 @@ void MetricEventUnittest::TestUntypedSingleValue() {
}

void MetricEventUnittest::TestUntypedMultiDoubleValues() {
UntypedMultiDoubleValues v({{"test-1", 10.0}, {"test-2", 2.0}}, nullptr);
UntypedMultiDoubleValues v({{"test-1", {UntypedValueMetricType::MetricTypeCounter, 10.0}},
{"test-2", {UntypedValueMetricType::MetricTypeGauge, 2.0}}},
nullptr);
mMetricEvent->SetValue(v);
APSARA_TEST_TRUE(mMetricEvent->Is<UntypedMultiDoubleValues>());
double val;
UntypedMultiDoubleValue val;
APSARA_TEST_EQUAL(true, mMetricEvent->GetValue<UntypedMultiDoubleValues>()->GetValue("test-1", val));
APSARA_TEST_EQUAL(10.0, val);
APSARA_TEST_EQUAL(UntypedValueMetricType::MetricTypeCounter, val.MetricType);
APSARA_TEST_EQUAL(10.0, val.Value);
APSARA_TEST_EQUAL(true, mMetricEvent->GetValue<UntypedMultiDoubleValues>()->GetValue("test-2", val));
APSARA_TEST_EQUAL(2.0, val);
APSARA_TEST_EQUAL(UntypedValueMetricType::MetricTypeGauge, val.MetricType);
APSARA_TEST_EQUAL(2.0, val.Value);

map<StringView, double> metrics({{"test-3", 15.0}, {"test-4", 24.0}});
map<StringView, UntypedMultiDoubleValue> metrics({{"test-3", {UntypedValueMetricType::MetricTypeCounter, 15.0}},
{"test-4", {UntypedValueMetricType::MetricTypeGauge, 24.0}}});
mMetricEvent->SetValue(metrics);
APSARA_TEST_TRUE(mMetricEvent->Is<UntypedMultiDoubleValues>());
APSARA_TEST_EQUAL(true, mMetricEvent->GetValue<UntypedMultiDoubleValues>()->GetValue("test-3", val));
APSARA_TEST_EQUAL(15.0, val);
APSARA_TEST_EQUAL(UntypedValueMetricType::MetricTypeCounter, val.MetricType);
APSARA_TEST_EQUAL(15.0, val.Value);
APSARA_TEST_EQUAL(true, mMetricEvent->GetValue<UntypedMultiDoubleValues>()->GetValue("test-4", val));
APSARA_TEST_EQUAL(24.0, val);
APSARA_TEST_EQUAL(UntypedValueMetricType::MetricTypeGauge, val.MetricType);
APSARA_TEST_EQUAL(24.0, val.Value);

mMetricEvent->MutableValue<UntypedMultiDoubleValues>()->SetValue(string("test-1"), 6.0);
mMetricEvent->MutableValue<UntypedMultiDoubleValues>()->SetValue(string("test-1"),
{UntypedValueMetricType::MetricTypeCounter, 6.0});
APSARA_TEST_EQUAL(true, mMetricEvent->GetValue<UntypedMultiDoubleValues>()->GetValue("test-1", val));
APSARA_TEST_EQUAL(6.0, val);
APSARA_TEST_EQUAL(UntypedValueMetricType::MetricTypeCounter, val.MetricType);
APSARA_TEST_EQUAL(6.0, val.Value);

mMetricEvent->MutableValue<UntypedMultiDoubleValues>()->DelValue("test-4");
APSARA_TEST_EQUAL(false, mMetricEvent->GetValue<UntypedMultiDoubleValues>()->GetValue("test-4", val));
APSARA_TEST_EQUAL(6.0, val);
APSARA_TEST_EQUAL(UntypedValueMetricType::MetricTypeCounter, val.MetricType);
APSARA_TEST_EQUAL(6.0, val.Value);
}

void MetricEventUnittest::TestTag() {
Expand Down Expand Up @@ -160,7 +170,7 @@ void MetricEventUnittest::TestUntypedSingleValueSize() {

void MetricEventUnittest::TestUntypedMultiDoubleValuesSize() {
mMetricEvent->SetName("test");
mMetricEvent->SetValue(map<StringView, double>{});
mMetricEvent->SetValue(map<StringView, UntypedMultiDoubleValue>{});
size_t basicSize
= sizeof(time_t) + sizeof(long) + sizeof(UntypedMultiDoubleValues) + sizeof(map<StringView, StringView>);
basicSize += 4;
Expand All @@ -178,17 +188,19 @@ void MetricEventUnittest::TestUntypedMultiDoubleValuesSize() {
APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize());

// add multi values, and key not existed
mMetricEvent->MutableValue<UntypedMultiDoubleValues>()->SetValue(string("test-1"), 5.0);
basicSize += 14;
mMetricEvent->MutableValue<UntypedMultiDoubleValues>()->SetValue(string("test-1"),
{UntypedValueMetricType::MetricTypeCounter, 5.0});
basicSize += 22;
APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize());

// add multi values, and key existed
mMetricEvent->MutableValue<UntypedMultiDoubleValues>()->SetValue(string("test-1"), 99.0);
mMetricEvent->MutableValue<UntypedMultiDoubleValues>()->SetValue(string("test-1"),
{UntypedValueMetricType::MetricTypeCounter, 99.0});
APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize());

// delete multi values
mMetricEvent->MutableValue<UntypedMultiDoubleValues>()->DelValue("test-1");
basicSize -= 14;
basicSize -= 22;
APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize());
}

Expand Down Expand Up @@ -235,7 +247,9 @@ void MetricEventUnittest::TestUntypedSingleValueToJson() {
void MetricEventUnittest::TestUntypedMultiDoubleValuesToJson() {
mMetricEvent->SetTimestamp(12345678901, 0);
mMetricEvent->SetName("test");
mMetricEvent->SetValue(map<StringView, double>{{"test-1", 10.0}, {"test-2", 2.0}});
mMetricEvent->SetValue(
map<StringView, UntypedMultiDoubleValue>{{"test-1", {UntypedValueMetricType::MetricTypeCounter, 10.0}},
{"test-2", {UntypedValueMetricType::MetricTypeGauge, 2.0}}});
mMetricEvent->SetTag(string("key1"), string("value1"));
Json::Value res = mMetricEvent->ToJson();

Expand All @@ -251,8 +265,14 @@ void MetricEventUnittest::TestUntypedMultiDoubleValuesToJson() {
"value": {
"type": "untyped_multi_double_values",
"detail": {
"test-1": 10.0,
"test-2": 2.0
"test-1": {
"type": "counter",
"value": 10.0
},
"test-2": {
"type": "gauge",
"value": 2.0
}
}
}
})";
Expand Down Expand Up @@ -300,24 +320,32 @@ void MetricEventUnittest::TestUntypedMultiDoubleValuesFromJson() {
"value": {
"type": "untyped_multi_double_values",
"detail": {
"test-1": 10.0,
"test-2": 2.0
"test-1": {
"type": "counter",
"value": 10.0
},
"test-2": {
"type": "gauge",
"value": 2.0
}
}
}
})";
string errorMsg;
ParseJsonTable(eventStr, eventJson, errorMsg);
mMetricEvent->FromJson(eventJson);
double val;
UntypedMultiDoubleValue val;

APSARA_TEST_EQUAL(12345678901, mMetricEvent->GetTimestamp());
APSARA_TEST_EQUAL(0L, mMetricEvent->GetTimestampNanosecond().value());
APSARA_TEST_EQUAL("test", mMetricEvent->GetName());
APSARA_TEST_TRUE(mMetricEvent->Is<UntypedMultiDoubleValues>());
APSARA_TEST_EQUAL(true, mMetricEvent->GetValue<UntypedMultiDoubleValues>()->GetValue("test-1", val));
APSARA_TEST_EQUAL(10.0, val);
APSARA_TEST_EQUAL(UntypedValueMetricType::MetricTypeCounter, val.MetricType);
APSARA_TEST_EQUAL(10.0, val.Value);
APSARA_TEST_EQUAL(true, mMetricEvent->GetValue<UntypedMultiDoubleValues>()->GetValue("test-2", val));
APSARA_TEST_EQUAL(2.0, val);
APSARA_TEST_EQUAL(UntypedValueMetricType::MetricTypeGauge, val.MetricType);
APSARA_TEST_EQUAL(2.0, val.Value);
APSARA_TEST_EQUAL("value1", mMetricEvent->GetTag("key1").to_string());
}

Expand Down Expand Up @@ -346,18 +374,22 @@ void MetricEventUnittest::TestTagsIterator() {

void MetricEventUnittest::TestCopy() {
MetricEvent* oldMetricEvent = mEventGroup->AddMetricEvent();
oldMetricEvent->SetValue(map<StringView, double>{{"test-1", 10.0}, {"test-2", 2.0}});
oldMetricEvent->SetValue(
map<StringView, UntypedMultiDoubleValue>{{"test-1", {UntypedValueMetricType::MetricTypeCounter, 10.0}},
{"test-2", {UntypedValueMetricType::MetricTypeGauge, 2.0}}});
APSARA_TEST_EQUAL(1U, mEventGroup->GetEvents().size());

PipelineEventGroup newGroup = mEventGroup->Copy();
MetricEvent newMetricEvent = newGroup.GetEvents().at(0).Cast<MetricEvent>();
double val;
UntypedMultiDoubleValue val;

APSARA_TEST_TRUE(newMetricEvent.Is<UntypedMultiDoubleValues>());
APSARA_TEST_EQUAL(true, newMetricEvent.GetValue<UntypedMultiDoubleValues>()->GetValue("test-1", val));
APSARA_TEST_EQUAL(10.0, val);
APSARA_TEST_EQUAL(UntypedValueMetricType::MetricTypeCounter, val.MetricType);
APSARA_TEST_EQUAL(10.0, val.Value);
APSARA_TEST_EQUAL(true, newMetricEvent.GetValue<UntypedMultiDoubleValues>()->GetValue("test-2", val));
APSARA_TEST_EQUAL(2.0, val);
APSARA_TEST_EQUAL(UntypedValueMetricType::MetricTypeGauge, val.MetricType);
APSARA_TEST_EQUAL(2.0, val.Value);
APSARA_TEST_NOT_EQUAL(newMetricEvent.GetValue<UntypedMultiDoubleValues>()->mMetricEventPtr,
oldMetricEvent->GetValue<UntypedMultiDoubleValues>()->mMetricEventPtr);
}
Expand Down
28 changes: 19 additions & 9 deletions core/unittest/models/MetricValueUnittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,39 @@ class UntypedMultiDoubleValuesUnittest : public ::testing::Test {

void UntypedMultiDoubleValuesUnittest::TestToJson() {
UntypedMultiDoubleValues value(mMetricEvent.get());
value.SetValue(string("test-1"), 10.0);
value.SetValue(string("test-2"), 2.0);
value.SetValue(string("test-1"), {UntypedValueMetricType::MetricTypeCounter, 10.0});
value.SetValue(string("test-2"), {UntypedValueMetricType::MetricTypeGauge, 2.0});
Json::Value res = value.ToJson();

Json::Value valueJson;
valueJson["test-1"] = 10.0;
valueJson["test-2"] = 2.0;
valueJson["test-1"] = Json::Value();
valueJson["test-1"]["type"] = "counter";
valueJson["test-1"]["value"] = 10.0;
valueJson["test-2"] = Json::Value();
valueJson["test-2"]["type"] = "gauge";
valueJson["test-2"]["value"] = 2.0;

APSARA_TEST_TRUE(valueJson == res);
}

void UntypedMultiDoubleValuesUnittest::TestFromJson() {
UntypedMultiDoubleValues value(mMetricEvent.get());
Json::Value valueJson;
valueJson["test-1"] = 10.0;
valueJson["test-2"] = 2.0;
valueJson["test-1"] = Json::Value();
valueJson["test-1"]["type"] = "counter";
valueJson["test-1"]["value"] = 10.0;
valueJson["test-2"] = Json::Value();
valueJson["test-2"]["type"] = "gauge";
valueJson["test-2"]["value"] = 2.0;
value.FromJson(valueJson);
double val;
UntypedMultiDoubleValue val;

APSARA_TEST_EQUAL(true, value.GetValue("test-1", val));
APSARA_TEST_EQUAL(10.0, val);
APSARA_TEST_EQUAL(UntypedValueMetricType::MetricTypeCounter, val.MetricType);
APSARA_TEST_EQUAL(10.0, val.Value);
APSARA_TEST_EQUAL(true, value.GetValue("test-2", val));
APSARA_TEST_EQUAL(2.0, val);
APSARA_TEST_EQUAL(UntypedValueMetricType::MetricTypeGauge, val.MetricType);
APSARA_TEST_EQUAL(2.0, val.Value);
}

UNIT_TEST_CASE(UntypedMultiDoubleValuesUnittest, TestToJson)
Expand Down

0 comments on commit e563035

Please sign in to comment.