From 75ed7ff84948c427a4003dcfb9923acf4aafbede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Fri, 22 Nov 2024 08:31:48 +0000 Subject: [PATCH 1/8] add UntypedMultiValues --- core/models/MetricEvent.cpp | 44 +++++ core/models/MetricEvent.h | 8 + core/models/MetricValue.cpp | 34 ++++ core/models/MetricValue.h | 19 ++- core/unittest/models/MetricEventUnittest.cpp | 162 +++++++++++++++++-- core/unittest/models/MetricValueUnittest.cpp | 32 ++++ 6 files changed, 286 insertions(+), 13 deletions(-) diff --git a/core/models/MetricEvent.cpp b/core/models/MetricEvent.cpp index 54d0fa3685..787bc3ccb8 100644 --- a/core/models/MetricEvent.cpp +++ b/core/models/MetricEvent.cpp @@ -43,6 +43,50 @@ void MetricEvent::SetNameNoCopy(StringView name) { mName = name; } +double MetricEvent::GetMultiKeyValue(StringView key) { + if (UntypedMultiValues* multiValues = std::get_if(&mValue)) { + if (multiValues->mValues.find(key) != multiValues->mValues.end()) { + return multiValues->mValues.at(key); + } + } + return 0; +} + +bool MetricEvent::HasMultiKeyValue(StringView key) { + if (UntypedMultiValues* multiValues = std::get_if(&mValue)) { + if (multiValues->mValues.find(key) != multiValues->mValues.end()) { + return true; + } + } + return false; +} + +void MetricEvent::SetMultiKeyValue(const std::string& key, double val) { + SetMultiKeyValueNoCopy(GetSourceBuffer()->CopyString(key), val); +} + +void MetricEvent::SetMultiKeyValue(StringView key, double val) { + SetMultiKeyValueNoCopy(GetSourceBuffer()->CopyString(key), val); +} + +void MetricEvent::SetMultiKeyValueNoCopy(const StringBuffer& key, double val) { + SetMultiKeyValueNoCopy(StringView(key.data, key.size), val); +} + +void MetricEvent::SetMultiKeyValueNoCopy(StringView key, double val) { + if (UntypedMultiValues* multiValues = std::get_if(&mValue)) { + multiValues->mValues[key] = val; + } else if (Is()) { + mValue = UntypedMultiValues{{{key, val}}}; + } +} + +void MetricEvent::DelMultiKeyValue(StringView key) { + if (UntypedMultiValues* multiValues = std::get_if(&mValue)) { + multiValues->mValues.erase(key); + } +} + StringView MetricEvent::GetTag(StringView key) const { auto it = mTags.mInner.find(key); if (it != mTags.mInner.end()) { diff --git a/core/models/MetricEvent.h b/core/models/MetricEvent.h index 0c4779ac44..cc6ea332e4 100644 --- a/core/models/MetricEvent.h +++ b/core/models/MetricEvent.h @@ -58,6 +58,14 @@ class MetricEvent : public PipelineEvent { mValue = T{std::forward(args)...}; } + double GetMultiKeyValue(StringView key); + bool HasMultiKeyValue(StringView key); + void SetMultiKeyValue(const std::string& key, double val); + void SetMultiKeyValue(StringView key, double val); + void SetMultiKeyValueNoCopy(const StringBuffer& key, double val); + void SetMultiKeyValueNoCopy(StringView key, double val); + void DelMultiKeyValue(StringView key); + StringView GetTag(StringView key) const; bool HasTag(StringView key) const; void SetTag(StringView key, StringView val); diff --git a/core/models/MetricValue.cpp b/core/models/MetricValue.cpp index 2c8022a831..6604231268 100644 --- a/core/models/MetricValue.cpp +++ b/core/models/MetricValue.cpp @@ -20,6 +20,14 @@ using namespace std; namespace logtail { +size_t UntypedMultiValues::DataSize() const { + size_t totalSize = sizeof(UntypedMultiValues); + for (const auto& pair : mValues) { + totalSize += pair.first.size() + sizeof(pair.second); + } + return totalSize; +} + size_t DataSize(const MetricValue& value) { return visit( [](auto&& arg) { @@ -42,6 +50,24 @@ void UntypedSingleValue::FromJson(const Json::Value& value) { mValue = value.asFloat(); } +Json::Value UntypedMultiValues::ToJson() const { + Json::Value res; + for (auto metric : mValues) { + res[metric.first.to_string()] = metric.second; + } + return res; +} + +void UntypedMultiValues::FromJson(const Json::Value& value) { + mValues.clear(); + for (Json::Value::const_iterator itr = value.begin(); itr != value.end(); ++itr) { + if (itr->asDouble()) { + StringBuffer s = mSourceBuffer->CopyString(itr.key().asString()); + mValues[StringView(s.data, s.size)] = itr->asDouble(); + } + } +} + Json::Value MetricValueToJson(const MetricValue& value) { Json::Value res; visit( @@ -50,6 +76,9 @@ Json::Value MetricValueToJson(const MetricValue& value) { if constexpr (is_same_v) { res["type"] = "untyped_single_value"; res["detail"] = get(value).ToJson(); + } else if constexpr (is_same_v) { + res["type"] = "untyped_multi_values"; + res["detail"] = get(value).ToJson(); } else if constexpr (is_same_v) { res["type"] = "unknown"; } @@ -63,6 +92,11 @@ MetricValue JsonToMetricValue(const string& type, const Json::Value& detail) { UntypedSingleValue v; v.FromJson(detail); return v; + } else if (type == "untyped_multi_values") { + UntypedMultiValues v; + v.mSourceBuffer = std::make_shared(); + v.FromJson(detail); + return v; } else { return MetricValue(); } diff --git a/core/models/MetricValue.h b/core/models/MetricValue.h index 558f96d601..266ca56fc6 100644 --- a/core/models/MetricValue.h +++ b/core/models/MetricValue.h @@ -17,13 +17,18 @@ #pragma once #include +#include #ifdef APSARA_UNIT_TEST_MAIN #include #include + +#include "common/memory/SourceBuffer.h" #endif +#include "models/StringView.h" + namespace logtail { struct UntypedSingleValue { @@ -37,7 +42,19 @@ struct UntypedSingleValue { #endif }; -using MetricValue = std::variant; +struct UntypedMultiValues { + std::map mValues; + + size_t DataSize() const; + +#ifdef APSARA_UNIT_TEST_MAIN + std::shared_ptr mSourceBuffer; + Json::Value ToJson() const; + void FromJson(const Json::Value& value); +#endif +}; + +using MetricValue = std::variant; size_t DataSize(const MetricValue& value); diff --git a/core/unittest/models/MetricEventUnittest.cpp b/core/unittest/models/MetricEventUnittest.cpp index 55556be363..85184ed401 100644 --- a/core/unittest/models/MetricEventUnittest.cpp +++ b/core/unittest/models/MetricEventUnittest.cpp @@ -25,12 +25,17 @@ namespace logtail { class MetricEventUnittest : public ::testing::Test { public: void TestName(); - void TestValue(); + void TestUntypedSingleValue(); + void TestUntypedMultiValues(); void TestTag(); - void TestSize(); + void TestUntypedSingleValueSize(); + void TestUntypedMultiValuesSize(); void TestReset(); - void TestToJson(); - void TestFromJson(); + void TestTypeConflict(); + void TestUntypedSingleValueToJson(); + void TestUntypedMultiValuesToJson(); + void TestUntypedSingleValueFromJson(); + void TestUntypedMultiValuesFromJson(); void TestTagsIterator(); protected: @@ -51,7 +56,7 @@ void MetricEventUnittest::TestName() { APSARA_TEST_EQUAL("test", mMetricEvent->GetName().to_string()); } -void MetricEventUnittest::TestValue() { +void MetricEventUnittest::TestUntypedSingleValue() { mMetricEvent->SetValue(UntypedSingleValue{10.0}); APSARA_TEST_TRUE(mMetricEvent->Is()); APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->mValue); @@ -61,6 +66,25 @@ void MetricEventUnittest::TestValue() { APSARA_TEST_EQUAL(100.0, mMetricEvent->GetValue()->mValue); } +void MetricEventUnittest::TestUntypedMultiValues() { + mMetricEvent->SetValue(UntypedMultiValues{{{"test-1", 10.0}, {"test-2", 2.0}}}); + APSARA_TEST_TRUE(mMetricEvent->Is()); + APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->mValues.at("test-1")); + APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->mValues.at("test-2")); + + map metrics({{"test-3", 15.0}, {"test-4", 24.0}}); + mMetricEvent->SetValue(metrics); + APSARA_TEST_TRUE(mMetricEvent->Is()); + APSARA_TEST_EQUAL(15.0, mMetricEvent->GetValue()->mValues.at("test-3")); + APSARA_TEST_EQUAL(24.0, mMetricEvent->GetValue()->mValues.at("test-4")); + + mMetricEvent->SetMultiKeyValue(string("test-1"), 6.0); + APSARA_TEST_EQUAL(6.0, mMetricEvent->GetMultiKeyValue("test-1")); + + mMetricEvent->DelMultiKeyValue("test-4"); + APSARA_TEST_EQUAL(0, mMetricEvent->GetMultiKeyValue("test-4")); +} + void MetricEventUnittest::TestTag() { { string key = "key1"; @@ -106,7 +130,7 @@ void MetricEventUnittest::TestTag() { } } -void MetricEventUnittest::TestSize() { +void MetricEventUnittest::TestUntypedSingleValueSize() { size_t basicSize = sizeof(time_t) + sizeof(long) + sizeof(UntypedSingleValue) + sizeof(map); mMetricEvent->SetName("test"); basicSize += 4; @@ -126,6 +150,39 @@ void MetricEventUnittest::TestSize() { APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); } +void MetricEventUnittest::TestUntypedMultiValuesSize() { + mMetricEvent->SetName("test"); + mMetricEvent->SetValue(UntypedMultiValues{}); + size_t basicSize = sizeof(time_t) + sizeof(long) + sizeof(UntypedMultiValues) + sizeof(map); + basicSize += 4; + + // add tag, and key not existed + mMetricEvent->SetTag(string("key1"), string("a")); + APSARA_TEST_EQUAL(basicSize + 5U, mMetricEvent->DataSize()); + + // add tag, and key existed + mMetricEvent->SetTag(string("key1"), string("bb")); + APSARA_TEST_EQUAL(basicSize + 6U, mMetricEvent->DataSize()); + + // delete tag + mMetricEvent->DelTag(string("key1")); + APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); + + // add multi values, and key not existed + mMetricEvent->SetMultiKeyValue(string("test-1"), 5.0); + basicSize += 14; + APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); + + // add multi values, and key existed + mMetricEvent->SetMultiKeyValue(string("test-1"), 99.0); + APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); + + // delete multi values + mMetricEvent->DelMultiKeyValue("test-1"); + basicSize -= 14; + APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); +} + void MetricEventUnittest::TestReset() { mMetricEvent->SetTimestamp(12345678901); mMetricEvent->SetName("test"); @@ -139,7 +196,21 @@ void MetricEventUnittest::TestReset() { APSARA_TEST_EQUAL(mMetricEvent->TagsEnd(), mMetricEvent->TagsBegin()); } -void MetricEventUnittest::TestToJson() { +void MetricEventUnittest::TestTypeConflict() { + mMetricEvent->SetTimestamp(12345678901); + mMetricEvent->SetName("test"); + mMetricEvent->SetTag(string("key1"), string("value1")); + + mMetricEvent->SetMultiKeyValue(string("test-1"), 6.0); + APSARA_TEST_EQUAL(6.0, mMetricEvent->GetMultiKeyValue("test-1")); + + mMetricEvent->SetValue(UntypedSingleValue{10.0}); + + mMetricEvent->SetMultiKeyValue(string("test-1"), 6.0); + APSARA_TEST_EQUAL(0, mMetricEvent->GetMultiKeyValue("test-1")); +} + +void MetricEventUnittest::TestUntypedSingleValueToJson() { mMetricEvent->SetTimestamp(12345678901, 0); mMetricEvent->SetName("test"); mMetricEvent->SetValue(UntypedSingleValue{10.0}); @@ -166,7 +237,37 @@ void MetricEventUnittest::TestToJson() { APSARA_TEST_TRUE(eventJson == res); } -void MetricEventUnittest::TestFromJson() { +void MetricEventUnittest::TestUntypedMultiValuesToJson() { + mMetricEvent->SetTimestamp(12345678901, 0); + mMetricEvent->SetName("test"); + mMetricEvent->SetValue(UntypedMultiValues{{{"test-1", 10.0}, {"test-2", 2.0}}}); + mMetricEvent->SetTag(string("key1"), string("value1")); + Json::Value res = mMetricEvent->ToJson(); + + Json::Value eventJson; + string eventStr = R"({ + "name": "test", + "tags": { + "key1": "value1" + }, + "timestamp" : 12345678901, + "timestampNanosecond" : 0, + "type" : 2, + "value": { + "type": "untyped_multi_values", + "detail": { + "test-1": 10.0, + "test-2": 2.0 + } + } + })"; + string errorMsg; + ParseJsonTable(eventStr, eventJson, errorMsg); + + APSARA_TEST_TRUE(eventJson == res); +} + +void MetricEventUnittest::TestUntypedSingleValueFromJson() { Json::Value eventJson; string eventStr = R"({ "name": "test", @@ -192,6 +293,38 @@ void MetricEventUnittest::TestFromJson() { APSARA_TEST_EQUAL("value1", mMetricEvent->GetTag("key1").to_string()); } +void MetricEventUnittest::TestUntypedMultiValuesFromJson() { + Json::Value eventJson; + string eventStr = R"({ + "name": "test", + "tags": { + "key1": "value1" + }, + "timestamp" : 12345678901, + "timestampNanosecond" : 0, + "value": { + "type": "untyped_multi_values", + "detail": { + "test-1": 10.0, + "test-2": 2.0 + } + } + })"; + string errorMsg; + ParseJsonTable(eventStr, eventJson, errorMsg); + mMetricEvent->FromJson(eventJson); + + APSARA_TEST_EQUAL(12345678901, mMetricEvent->GetTimestamp()); + APSARA_TEST_EQUAL(0L, mMetricEvent->GetTimestampNanosecond().value()); + APSARA_TEST_EQUAL("test", mMetricEvent->GetName()); + APSARA_TEST_TRUE(mMetricEvent->Is()); + APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->mValues.at("test-1")); + APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->mValues.at("test-2")); + APSARA_TEST_EQUAL(10.0, mMetricEvent->GetMultiKeyValue("test-1")); + APSARA_TEST_EQUAL(2.0, mMetricEvent->GetMultiKeyValue("test-2")); + APSARA_TEST_EQUAL("value1", mMetricEvent->GetTag("key1").to_string()); +} + void MetricEventUnittest::TestTagsIterator() { string key1 = "key1"; string value1 = "value1"; @@ -216,12 +349,17 @@ void MetricEventUnittest::TestTagsIterator() { } UNIT_TEST_CASE(MetricEventUnittest, TestName) -UNIT_TEST_CASE(MetricEventUnittest, TestValue) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValue) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValues) UNIT_TEST_CASE(MetricEventUnittest, TestTag) -UNIT_TEST_CASE(MetricEventUnittest, TestSize) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValueSize) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesSize) UNIT_TEST_CASE(MetricEventUnittest, TestReset) -UNIT_TEST_CASE(MetricEventUnittest, TestToJson) -UNIT_TEST_CASE(MetricEventUnittest, TestFromJson) +UNIT_TEST_CASE(MetricEventUnittest, TestTypeConflict) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValueToJson) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesToJson) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValueFromJson) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesFromJson) UNIT_TEST_CASE(MetricEventUnittest, TestTagsIterator) } // namespace logtail diff --git a/core/unittest/models/MetricValueUnittest.cpp b/core/unittest/models/MetricValueUnittest.cpp index f55a987962..70a1812742 100644 --- a/core/unittest/models/MetricValueUnittest.cpp +++ b/core/unittest/models/MetricValueUnittest.cpp @@ -76,6 +76,38 @@ void UntypedSingleValueUnittest::TestFromJson() { UNIT_TEST_CASE(UntypedSingleValueUnittest, TestToJson) UNIT_TEST_CASE(UntypedSingleValueUnittest, TestFromJson) +class UntypedMultiValuesUnittest : public ::testing::Test { +public: + void TestToJson(); + void TestFromJson(); +}; + +void UntypedMultiValuesUnittest::TestToJson() { + UntypedMultiValues value{{{"test-1", 10.0}, {"test-2", 2.0}}}; + Json::Value res = value.ToJson(); + + Json::Value valueJson; + valueJson["test-1"] = 10.0; + valueJson["test-2"] = 2.0; + + APSARA_TEST_TRUE(valueJson == res); +} + +void UntypedMultiValuesUnittest::TestFromJson() { + UntypedMultiValues value; + value.mSourceBuffer = std::make_shared(); + Json::Value valueJson; + valueJson["test-1"] = 10.0; + valueJson["test-2"] = 2.0; + value.FromJson(valueJson); + + APSARA_TEST_EQUAL(10.0, value.mValues["test-1"]); + APSARA_TEST_EQUAL(2.0, value.mValues["test-2"]); +} + +UNIT_TEST_CASE(UntypedMultiValuesUnittest, TestToJson) +UNIT_TEST_CASE(UntypedMultiValuesUnittest, TestFromJson) + } // namespace logtail UNIT_TEST_MAIN From 6696eb1f6da2a3007a2d79705b5b3668e05cefa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Sun, 24 Nov 2024 08:56:33 +0000 Subject: [PATCH 2/8] polish --- core/models/MetricEvent.cpp | 46 +---------- core/models/MetricEvent.h | 8 -- core/models/MetricValue.cpp | 67 ++++++++++++--- core/models/MetricValue.h | 31 +++++-- core/unittest/models/MetricEventUnittest.cpp | 86 ++++++++------------ core/unittest/models/MetricValueUnittest.cpp | 47 ++++++++--- 6 files changed, 151 insertions(+), 134 deletions(-) diff --git a/core/models/MetricEvent.cpp b/core/models/MetricEvent.cpp index 787bc3ccb8..c10380626e 100644 --- a/core/models/MetricEvent.cpp +++ b/core/models/MetricEvent.cpp @@ -43,50 +43,6 @@ void MetricEvent::SetNameNoCopy(StringView name) { mName = name; } -double MetricEvent::GetMultiKeyValue(StringView key) { - if (UntypedMultiValues* multiValues = std::get_if(&mValue)) { - if (multiValues->mValues.find(key) != multiValues->mValues.end()) { - return multiValues->mValues.at(key); - } - } - return 0; -} - -bool MetricEvent::HasMultiKeyValue(StringView key) { - if (UntypedMultiValues* multiValues = std::get_if(&mValue)) { - if (multiValues->mValues.find(key) != multiValues->mValues.end()) { - return true; - } - } - return false; -} - -void MetricEvent::SetMultiKeyValue(const std::string& key, double val) { - SetMultiKeyValueNoCopy(GetSourceBuffer()->CopyString(key), val); -} - -void MetricEvent::SetMultiKeyValue(StringView key, double val) { - SetMultiKeyValueNoCopy(GetSourceBuffer()->CopyString(key), val); -} - -void MetricEvent::SetMultiKeyValueNoCopy(const StringBuffer& key, double val) { - SetMultiKeyValueNoCopy(StringView(key.data, key.size), val); -} - -void MetricEvent::SetMultiKeyValueNoCopy(StringView key, double val) { - if (UntypedMultiValues* multiValues = std::get_if(&mValue)) { - multiValues->mValues[key] = val; - } else if (Is()) { - mValue = UntypedMultiValues{{{key, val}}}; - } -} - -void MetricEvent::DelMultiKeyValue(StringView key) { - if (UntypedMultiValues* multiValues = std::get_if(&mValue)) { - multiValues->mValues.erase(key); - } -} - StringView MetricEvent::GetTag(StringView key) const { auto it = mTags.mInner.find(key); if (it != mTags.mInner.end()) { @@ -150,7 +106,7 @@ bool MetricEvent::FromJson(const Json::Value& root) { } SetName(root["name"].asString()); const Json::Value& value = root["value"]; - SetValue(JsonToMetricValue(value["type"].asString(), value["detail"])); + SetValue(JsonToMetricValue(value["type"].asString(), value["detail"], this)); if (root.isMember("tags")) { Json::Value tags = root["tags"]; for (const auto& key : tags.getMemberNames()) { diff --git a/core/models/MetricEvent.h b/core/models/MetricEvent.h index cc6ea332e4..0c4779ac44 100644 --- a/core/models/MetricEvent.h +++ b/core/models/MetricEvent.h @@ -58,14 +58,6 @@ class MetricEvent : public PipelineEvent { mValue = T{std::forward(args)...}; } - double GetMultiKeyValue(StringView key); - bool HasMultiKeyValue(StringView key); - void SetMultiKeyValue(const std::string& key, double val); - void SetMultiKeyValue(StringView key, double val); - void SetMultiKeyValueNoCopy(const StringBuffer& key, double val); - void SetMultiKeyValueNoCopy(StringView key, double val); - void DelMultiKeyValue(StringView key); - StringView GetTag(StringView key) const; bool HasTag(StringView key) const; void SetTag(StringView key, StringView val); diff --git a/core/models/MetricValue.cpp b/core/models/MetricValue.cpp index 6604231268..dfd622d3fa 100644 --- a/core/models/MetricValue.cpp +++ b/core/models/MetricValue.cpp @@ -20,8 +20,55 @@ using namespace std; namespace logtail { -size_t UntypedMultiValues::DataSize() const { - size_t totalSize = sizeof(UntypedMultiValues); +double UntypedMultiFloatValues::GetMultiKeyValue(StringView key) const { + if (mValues.find(key) != mValues.end()) { + return mValues.at(key); + } + return 0; +} + +bool UntypedMultiFloatValues::HasMultiKeyValue(StringView key) const { + return mValues.find(key) != mValues.end(); +} + +void UntypedMultiFloatValues::SetMultiKeyValue(const std::string& key, double val) const { + if (mMetricEventPtr) { + SetMultiKeyValueNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val); + } +} + +void UntypedMultiFloatValues::SetMultiKeyValue(StringView key, double val) const { + if (mMetricEventPtr) { + SetMultiKeyValueNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val); + } +} + +void UntypedMultiFloatValues::SetMultiKeyValueNoCopy(const StringBuffer& key, double val) const { + SetMultiKeyValueNoCopy(StringView(key.data, key.size), val); +} + +void UntypedMultiFloatValues::SetMultiKeyValueNoCopy(StringView key, double val) const { + mValues[key] = val; +} + +void UntypedMultiFloatValues::DelMultiKeyValue(StringView key) const { + mValues.erase(key); +} + +std::map::const_iterator UntypedMultiFloatValues::MultiKeyValusBegin() const { + return mValues.begin(); +} + +std::map::const_iterator UntypedMultiFloatValues::MultiKeyValusEnd() const { + return mValues.end(); +} + +size_t UntypedMultiFloatValues::MultiKeyValusSize() const { + return mValues.size(); +} + +size_t UntypedMultiFloatValues::DataSize() const { + size_t totalSize = sizeof(UntypedMultiFloatValues); for (const auto& pair : mValues) { totalSize += pair.first.size() + sizeof(pair.second); } @@ -50,7 +97,7 @@ void UntypedSingleValue::FromJson(const Json::Value& value) { mValue = value.asFloat(); } -Json::Value UntypedMultiValues::ToJson() const { +Json::Value UntypedMultiFloatValues::ToJson() const { Json::Value res; for (auto metric : mValues) { res[metric.first.to_string()] = metric.second; @@ -58,12 +105,11 @@ Json::Value UntypedMultiValues::ToJson() const { return res; } -void UntypedMultiValues::FromJson(const Json::Value& value) { +void UntypedMultiFloatValues::FromJson(const Json::Value& value) { mValues.clear(); for (Json::Value::const_iterator itr = value.begin(); itr != value.end(); ++itr) { if (itr->asDouble()) { - StringBuffer s = mSourceBuffer->CopyString(itr.key().asString()); - mValues[StringView(s.data, s.size)] = itr->asDouble(); + SetMultiKeyValue(itr.key().asString(), itr->asDouble()); } } } @@ -76,9 +122,9 @@ Json::Value MetricValueToJson(const MetricValue& value) { if constexpr (is_same_v) { res["type"] = "untyped_single_value"; res["detail"] = get(value).ToJson(); - } else if constexpr (is_same_v) { + } else if constexpr (is_same_v) { res["type"] = "untyped_multi_values"; - res["detail"] = get(value).ToJson(); + res["detail"] = get(value).ToJson(); } else if constexpr (is_same_v) { res["type"] = "unknown"; } @@ -87,14 +133,13 @@ Json::Value MetricValueToJson(const MetricValue& value) { return res; } -MetricValue JsonToMetricValue(const string& type, const Json::Value& detail) { +MetricValue JsonToMetricValue(const string& type, const Json::Value& detail, PipelineEvent* mMetricEventPtr) { if (type == "untyped_single_value") { UntypedSingleValue v; v.FromJson(detail); return v; } else if (type == "untyped_multi_values") { - UntypedMultiValues v; - v.mSourceBuffer = std::make_shared(); + UntypedMultiFloatValues v(mMetricEventPtr); v.FromJson(detail); return v; } else { diff --git a/core/models/MetricValue.h b/core/models/MetricValue.h index 266ca56fc6..152381c610 100644 --- a/core/models/MetricValue.h +++ b/core/models/MetricValue.h @@ -16,17 +16,17 @@ #pragma once -#include #include +#include #ifdef APSARA_UNIT_TEST_MAIN #include #include - -#include "common/memory/SourceBuffer.h" #endif +#include "common/memory/SourceBuffer.h" +#include "models/PipelineEvent.h" #include "models/StringView.h" namespace logtail { @@ -42,25 +42,40 @@ struct UntypedSingleValue { #endif }; -struct UntypedMultiValues { - std::map mValues; +struct UntypedMultiFloatValues { + mutable std::map mValues; + PipelineEvent* mMetricEventPtr; + + UntypedMultiFloatValues(PipelineEvent* ptr): mMetricEventPtr(ptr) {} + UntypedMultiFloatValues(std::map values, PipelineEvent* ptr): mValues(values), mMetricEventPtr(ptr) {} + + double GetMultiKeyValue(StringView key) const; + bool HasMultiKeyValue(StringView key) const; + void SetMultiKeyValue(const std::string& key, double val) const; + void SetMultiKeyValue(StringView key, double val) const; + void SetMultiKeyValueNoCopy(const StringBuffer& key, double val) const; + void SetMultiKeyValueNoCopy(StringView key, double val) const; + void DelMultiKeyValue(StringView key) const; + + std::map::const_iterator MultiKeyValusBegin() const; + std::map::const_iterator MultiKeyValusEnd() const; + size_t MultiKeyValusSize() const; size_t DataSize() const; #ifdef APSARA_UNIT_TEST_MAIN - std::shared_ptr mSourceBuffer; Json::Value ToJson() const; void FromJson(const Json::Value& value); #endif }; -using MetricValue = std::variant; +using MetricValue = std::variant; size_t DataSize(const MetricValue& value); #ifdef APSARA_UNIT_TEST_MAIN Json::Value MetricValueToJson(const MetricValue& value); -MetricValue JsonToMetricValue(const std::string& type, const Json::Value& detail); +MetricValue JsonToMetricValue(const std::string& type, const Json::Value& detail, PipelineEvent* mMetricEventPtr); #endif } // namespace logtail diff --git a/core/unittest/models/MetricEventUnittest.cpp b/core/unittest/models/MetricEventUnittest.cpp index 85184ed401..e0f5a48d12 100644 --- a/core/unittest/models/MetricEventUnittest.cpp +++ b/core/unittest/models/MetricEventUnittest.cpp @@ -26,16 +26,15 @@ class MetricEventUnittest : public ::testing::Test { public: void TestName(); void TestUntypedSingleValue(); - void TestUntypedMultiValues(); + void TestUntypedMultiFloatValues(); void TestTag(); void TestUntypedSingleValueSize(); - void TestUntypedMultiValuesSize(); + void TestUntypedMultiFloatValuesSize(); void TestReset(); - void TestTypeConflict(); void TestUntypedSingleValueToJson(); - void TestUntypedMultiValuesToJson(); + void TestUntypedMultiFloatValuesToJson(); void TestUntypedSingleValueFromJson(); - void TestUntypedMultiValuesFromJson(); + void TestUntypedMultiFloatValuesFromJson(); void TestTagsIterator(); protected: @@ -66,23 +65,23 @@ void MetricEventUnittest::TestUntypedSingleValue() { APSARA_TEST_EQUAL(100.0, mMetricEvent->GetValue()->mValue); } -void MetricEventUnittest::TestUntypedMultiValues() { - mMetricEvent->SetValue(UntypedMultiValues{{{"test-1", 10.0}, {"test-2", 2.0}}}); - APSARA_TEST_TRUE(mMetricEvent->Is()); - APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->mValues.at("test-1")); - APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->mValues.at("test-2")); +void MetricEventUnittest::TestUntypedMultiFloatValues() { + mMetricEvent->SetValue(UntypedMultiFloatValues{{{"test-1", 10.0}, {"test-2", 2.0}}, mMetricEvent.get()}); + APSARA_TEST_TRUE(mMetricEvent->Is()); + APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-1")); + APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-2")); map metrics({{"test-3", 15.0}, {"test-4", 24.0}}); - mMetricEvent->SetValue(metrics); - APSARA_TEST_TRUE(mMetricEvent->Is()); - APSARA_TEST_EQUAL(15.0, mMetricEvent->GetValue()->mValues.at("test-3")); - APSARA_TEST_EQUAL(24.0, mMetricEvent->GetValue()->mValues.at("test-4")); + mMetricEvent->SetValue(metrics, mMetricEvent.get()); + APSARA_TEST_TRUE(mMetricEvent->Is()); + APSARA_TEST_EQUAL(15.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-3")); + APSARA_TEST_EQUAL(24.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-4")); - mMetricEvent->SetMultiKeyValue(string("test-1"), 6.0); - APSARA_TEST_EQUAL(6.0, mMetricEvent->GetMultiKeyValue("test-1")); + mMetricEvent->GetValue()->SetMultiKeyValue(string("test-1"), 6.0); + APSARA_TEST_EQUAL(6.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-1")); - mMetricEvent->DelMultiKeyValue("test-4"); - APSARA_TEST_EQUAL(0, mMetricEvent->GetMultiKeyValue("test-4")); + mMetricEvent->GetValue()->DelMultiKeyValue("test-4"); + APSARA_TEST_EQUAL(0, mMetricEvent->GetValue()->GetMultiKeyValue("test-4")); } void MetricEventUnittest::TestTag() { @@ -150,10 +149,10 @@ void MetricEventUnittest::TestUntypedSingleValueSize() { APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); } -void MetricEventUnittest::TestUntypedMultiValuesSize() { +void MetricEventUnittest::TestUntypedMultiFloatValuesSize() { mMetricEvent->SetName("test"); - mMetricEvent->SetValue(UntypedMultiValues{}); - size_t basicSize = sizeof(time_t) + sizeof(long) + sizeof(UntypedMultiValues) + sizeof(map); + mMetricEvent->SetValue(UntypedMultiFloatValues{{}, mMetricEvent.get()}); + size_t basicSize = sizeof(time_t) + sizeof(long) + sizeof(UntypedMultiFloatValues) + sizeof(map); basicSize += 4; // add tag, and key not existed @@ -169,16 +168,16 @@ void MetricEventUnittest::TestUntypedMultiValuesSize() { APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); // add multi values, and key not existed - mMetricEvent->SetMultiKeyValue(string("test-1"), 5.0); + mMetricEvent->GetValue()->SetMultiKeyValue(string("test-1"), 5.0); basicSize += 14; APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); // add multi values, and key existed - mMetricEvent->SetMultiKeyValue(string("test-1"), 99.0); + mMetricEvent->GetValue()->SetMultiKeyValue(string("test-1"), 99.0); APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); // delete multi values - mMetricEvent->DelMultiKeyValue("test-1"); + mMetricEvent->GetValue()->DelMultiKeyValue("test-1"); basicSize -= 14; APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); } @@ -196,20 +195,6 @@ void MetricEventUnittest::TestReset() { APSARA_TEST_EQUAL(mMetricEvent->TagsEnd(), mMetricEvent->TagsBegin()); } -void MetricEventUnittest::TestTypeConflict() { - mMetricEvent->SetTimestamp(12345678901); - mMetricEvent->SetName("test"); - mMetricEvent->SetTag(string("key1"), string("value1")); - - mMetricEvent->SetMultiKeyValue(string("test-1"), 6.0); - APSARA_TEST_EQUAL(6.0, mMetricEvent->GetMultiKeyValue("test-1")); - - mMetricEvent->SetValue(UntypedSingleValue{10.0}); - - mMetricEvent->SetMultiKeyValue(string("test-1"), 6.0); - APSARA_TEST_EQUAL(0, mMetricEvent->GetMultiKeyValue("test-1")); -} - void MetricEventUnittest::TestUntypedSingleValueToJson() { mMetricEvent->SetTimestamp(12345678901, 0); mMetricEvent->SetName("test"); @@ -237,10 +222,10 @@ void MetricEventUnittest::TestUntypedSingleValueToJson() { APSARA_TEST_TRUE(eventJson == res); } -void MetricEventUnittest::TestUntypedMultiValuesToJson() { +void MetricEventUnittest::TestUntypedMultiFloatValuesToJson() { mMetricEvent->SetTimestamp(12345678901, 0); mMetricEvent->SetName("test"); - mMetricEvent->SetValue(UntypedMultiValues{{{"test-1", 10.0}, {"test-2", 2.0}}}); + mMetricEvent->SetValue(UntypedMultiFloatValues{{{"test-1", 10.0}, {"test-2", 2.0}}, mMetricEvent.get()}); mMetricEvent->SetTag(string("key1"), string("value1")); Json::Value res = mMetricEvent->ToJson(); @@ -293,7 +278,7 @@ void MetricEventUnittest::TestUntypedSingleValueFromJson() { APSARA_TEST_EQUAL("value1", mMetricEvent->GetTag("key1").to_string()); } -void MetricEventUnittest::TestUntypedMultiValuesFromJson() { +void MetricEventUnittest::TestUntypedMultiFloatValuesFromJson() { Json::Value eventJson; string eventStr = R"({ "name": "test", @@ -317,11 +302,11 @@ void MetricEventUnittest::TestUntypedMultiValuesFromJson() { APSARA_TEST_EQUAL(12345678901, mMetricEvent->GetTimestamp()); APSARA_TEST_EQUAL(0L, mMetricEvent->GetTimestampNanosecond().value()); APSARA_TEST_EQUAL("test", mMetricEvent->GetName()); - APSARA_TEST_TRUE(mMetricEvent->Is()); - APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->mValues.at("test-1")); - APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->mValues.at("test-2")); - APSARA_TEST_EQUAL(10.0, mMetricEvent->GetMultiKeyValue("test-1")); - APSARA_TEST_EQUAL(2.0, mMetricEvent->GetMultiKeyValue("test-2")); + APSARA_TEST_TRUE(mMetricEvent->Is()); + APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-1")); + APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-2")); + APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-1")); + APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-2")); APSARA_TEST_EQUAL("value1", mMetricEvent->GetTag("key1").to_string()); } @@ -350,16 +335,15 @@ void MetricEventUnittest::TestTagsIterator() { UNIT_TEST_CASE(MetricEventUnittest, TestName) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValue) -UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValues) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiFloatValues) UNIT_TEST_CASE(MetricEventUnittest, TestTag) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValueSize) -UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesSize) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiFloatValuesSize) UNIT_TEST_CASE(MetricEventUnittest, TestReset) -UNIT_TEST_CASE(MetricEventUnittest, TestTypeConflict) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValueToJson) -UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesToJson) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiFloatValuesToJson) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValueFromJson) -UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesFromJson) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiFloatValuesFromJson) UNIT_TEST_CASE(MetricEventUnittest, TestTagsIterator) } // namespace logtail diff --git a/core/unittest/models/MetricValueUnittest.cpp b/core/unittest/models/MetricValueUnittest.cpp index 70a1812742..677d1e8a51 100644 --- a/core/unittest/models/MetricValueUnittest.cpp +++ b/core/unittest/models/MetricValueUnittest.cpp @@ -23,6 +23,18 @@ class MetricValueUnittest : public ::testing::Test { public: void TestToJson(); void TestFromJson(); + +protected: + void SetUp() override { + mSourceBuffer.reset(new SourceBuffer); + mEventGroup.reset(new PipelineEventGroup(mSourceBuffer)); + mMetricEvent = mEventGroup->CreateMetricEvent(); + } + +private: + shared_ptr mSourceBuffer; + unique_ptr mEventGroup; + unique_ptr mMetricEvent; }; void MetricValueUnittest::TestToJson() { @@ -42,7 +54,7 @@ void MetricValueUnittest::TestToJson() { void MetricValueUnittest::TestFromJson() { Json::Value detail(10.0); - MetricValue value = JsonToMetricValue("untyped_single_value", detail); + MetricValue value = JsonToMetricValue("untyped_single_value", detail, mMetricEvent.get()); APSARA_TEST_TRUE(std::holds_alternative(value)); APSARA_TEST_EQUAL(10.0, std::get(value).mValue); @@ -76,14 +88,28 @@ void UntypedSingleValueUnittest::TestFromJson() { UNIT_TEST_CASE(UntypedSingleValueUnittest, TestToJson) UNIT_TEST_CASE(UntypedSingleValueUnittest, TestFromJson) -class UntypedMultiValuesUnittest : public ::testing::Test { +class UntypedMultiFloatValuesUnittest : public ::testing::Test { public: void TestToJson(); void TestFromJson(); + +protected: + void SetUp() override { + mSourceBuffer.reset(new SourceBuffer); + mEventGroup.reset(new PipelineEventGroup(mSourceBuffer)); + mMetricEvent = mEventGroup->CreateMetricEvent(); + } + +private: + shared_ptr mSourceBuffer; + unique_ptr mEventGroup; + unique_ptr mMetricEvent; }; -void UntypedMultiValuesUnittest::TestToJson() { - UntypedMultiValues value{{{"test-1", 10.0}, {"test-2", 2.0}}}; +void UntypedMultiFloatValuesUnittest::TestToJson() { + UntypedMultiFloatValues value(mMetricEvent.get()); + value.SetMultiKeyValue(string("test-1"), 10.0); + value.SetMultiKeyValue(string("test-2"), 2.0); Json::Value res = value.ToJson(); Json::Value valueJson; @@ -93,20 +119,19 @@ void UntypedMultiValuesUnittest::TestToJson() { APSARA_TEST_TRUE(valueJson == res); } -void UntypedMultiValuesUnittest::TestFromJson() { - UntypedMultiValues value; - value.mSourceBuffer = std::make_shared(); +void UntypedMultiFloatValuesUnittest::TestFromJson() { + UntypedMultiFloatValues value(mMetricEvent.get()); Json::Value valueJson; valueJson["test-1"] = 10.0; valueJson["test-2"] = 2.0; value.FromJson(valueJson); - APSARA_TEST_EQUAL(10.0, value.mValues["test-1"]); - APSARA_TEST_EQUAL(2.0, value.mValues["test-2"]); + APSARA_TEST_EQUAL(10.0, value.GetMultiKeyValue("test-1")); + APSARA_TEST_EQUAL(2.0, value.GetMultiKeyValue("test-2")); } -UNIT_TEST_CASE(UntypedMultiValuesUnittest, TestToJson) -UNIT_TEST_CASE(UntypedMultiValuesUnittest, TestFromJson) +UNIT_TEST_CASE(UntypedMultiFloatValuesUnittest, TestToJson) +UNIT_TEST_CASE(UntypedMultiFloatValuesUnittest, TestFromJson) } // namespace logtail From a40dbe2f0059816af9976dbdb9e31c05258606c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Mon, 25 Nov 2024 02:50:12 +0000 Subject: [PATCH 3/8] polish --- core/models/MetricEvent.cpp | 26 +++++++++++- core/models/MetricValue.cpp | 33 --------------- core/models/MetricValue.h | 5 --- core/unittest/models/MetricValueUnittest.cpp | 44 -------------------- 4 files changed, 24 insertions(+), 84 deletions(-) diff --git a/core/models/MetricEvent.cpp b/core/models/MetricEvent.cpp index c10380626e..226dc0712e 100644 --- a/core/models/MetricEvent.cpp +++ b/core/models/MetricEvent.cpp @@ -88,7 +88,21 @@ Json::Value MetricEvent::ToJson(bool enableEventMeta) const { root["timestampNanosecond"] = static_cast(GetTimestampNanosecond().value()); } root["name"] = mName.to_string(); - root["value"] = MetricValueToJson(mValue); + root["value"] = Json::Value(); + visit( + [&](auto&& arg) { + using T = decay_t; + if constexpr (is_same_v) { + root["value"]["type"] = "untyped_single_value"; + root["value"]["detail"] = get(mValue).ToJson(); + } else if constexpr (is_same_v) { + root["value"]["type"] = "untyped_multi_values"; + root["value"]["detail"] = get(mValue).ToJson(); + } else if constexpr (is_same_v) { + root["value"]["type"] = "unknown"; + } + }, + mValue); if (!mTags.mInner.empty()) { Json::Value& tags = root["tags"]; for (const auto& tag : mTags.mInner) { @@ -106,7 +120,15 @@ bool MetricEvent::FromJson(const Json::Value& root) { } SetName(root["name"].asString()); const Json::Value& value = root["value"]; - SetValue(JsonToMetricValue(value["type"].asString(), value["detail"], this)); + if (value["type"].asString() == "untyped_single_value") { + UntypedSingleValue v; + v.FromJson(value["detail"]); + SetValue(v); + } else if (value["type"].asString() == "untyped_multi_values") { + UntypedMultiFloatValues v(this); + v.FromJson(value["detail"]); + SetValue(v); + } if (root.isMember("tags")) { Json::Value tags = root["tags"]; for (const auto& key : tags.getMemberNames()) { diff --git a/core/models/MetricValue.cpp b/core/models/MetricValue.cpp index dfd622d3fa..bb4d4abebf 100644 --- a/core/models/MetricValue.cpp +++ b/core/models/MetricValue.cpp @@ -113,39 +113,6 @@ void UntypedMultiFloatValues::FromJson(const Json::Value& value) { } } } - -Json::Value MetricValueToJson(const MetricValue& value) { - Json::Value res; - visit( - [&](auto&& arg) { - using T = decay_t; - if constexpr (is_same_v) { - res["type"] = "untyped_single_value"; - res["detail"] = get(value).ToJson(); - } else if constexpr (is_same_v) { - res["type"] = "untyped_multi_values"; - res["detail"] = get(value).ToJson(); - } else if constexpr (is_same_v) { - res["type"] = "unknown"; - } - }, - value); - return res; -} - -MetricValue JsonToMetricValue(const string& type, const Json::Value& detail, PipelineEvent* mMetricEventPtr) { - if (type == "untyped_single_value") { - UntypedSingleValue v; - v.FromJson(detail); - return v; - } else if (type == "untyped_multi_values") { - UntypedMultiFloatValues v(mMetricEventPtr); - v.FromJson(detail); - return v; - } else { - return MetricValue(); - } -} #endif } // namespace logtail diff --git a/core/models/MetricValue.h b/core/models/MetricValue.h index 152381c610..bdeb25967f 100644 --- a/core/models/MetricValue.h +++ b/core/models/MetricValue.h @@ -73,9 +73,4 @@ using MetricValue = std::variantCreateMetricEvent(); - } - -private: - shared_ptr mSourceBuffer; - unique_ptr mEventGroup; - unique_ptr mMetricEvent; -}; - -void MetricValueUnittest::TestToJson() { - MetricValue value = UntypedSingleValue{10.0}; - Json::Value res = MetricValueToJson(value); - - Json::Value valueJson; - string valueStr = R"({ - "type": "untyped_single_value", - "detail": 10.0 - })"; - string errorMsg; - ParseJsonTable(valueStr, valueJson, errorMsg); - - APSARA_TEST_TRUE(valueJson == res); -} - -void MetricValueUnittest::TestFromJson() { - Json::Value detail(10.0); - MetricValue value = JsonToMetricValue("untyped_single_value", detail, mMetricEvent.get()); - - APSARA_TEST_TRUE(std::holds_alternative(value)); - APSARA_TEST_EQUAL(10.0, std::get(value).mValue); -} - -UNIT_TEST_CASE(MetricValueUnittest, TestToJson) -UNIT_TEST_CASE(MetricValueUnittest, TestFromJson) - class UntypedSingleValueUnittest : public ::testing::Test { public: void TestToJson(); From a97399b2957bd34adf371f19248b801774e67f6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Mon, 25 Nov 2024 03:02:07 +0000 Subject: [PATCH 4/8] polish --- core/models/MetricEvent.cpp | 6 +- core/models/MetricValue.cpp | 36 +++++----- core/models/MetricValue.h | 22 +++--- core/unittest/models/MetricEventUnittest.cpp | 70 ++++++++++---------- core/unittest/models/MetricValueUnittest.cpp | 22 +++--- 5 files changed, 78 insertions(+), 78 deletions(-) diff --git a/core/models/MetricEvent.cpp b/core/models/MetricEvent.cpp index 226dc0712e..2b7ceaa8b2 100644 --- a/core/models/MetricEvent.cpp +++ b/core/models/MetricEvent.cpp @@ -95,9 +95,9 @@ Json::Value MetricEvent::ToJson(bool enableEventMeta) const { if constexpr (is_same_v) { root["value"]["type"] = "untyped_single_value"; root["value"]["detail"] = get(mValue).ToJson(); - } else if constexpr (is_same_v) { + } else if constexpr (is_same_v) { root["value"]["type"] = "untyped_multi_values"; - root["value"]["detail"] = get(mValue).ToJson(); + root["value"]["detail"] = get(mValue).ToJson(); } else if constexpr (is_same_v) { root["value"]["type"] = "unknown"; } @@ -125,7 +125,7 @@ bool MetricEvent::FromJson(const Json::Value& root) { v.FromJson(value["detail"]); SetValue(v); } else if (value["type"].asString() == "untyped_multi_values") { - UntypedMultiFloatValues v(this); + UntypedMultiValues v(this); v.FromJson(value["detail"]); SetValue(v); } diff --git a/core/models/MetricValue.cpp b/core/models/MetricValue.cpp index bb4d4abebf..0c10fbb0fe 100644 --- a/core/models/MetricValue.cpp +++ b/core/models/MetricValue.cpp @@ -20,55 +20,55 @@ using namespace std; namespace logtail { -double UntypedMultiFloatValues::GetMultiKeyValue(StringView key) const { +double UntypedMultiValues::Get(StringView key) const { if (mValues.find(key) != mValues.end()) { return mValues.at(key); } return 0; } -bool UntypedMultiFloatValues::HasMultiKeyValue(StringView key) const { +bool UntypedMultiValues::Has(StringView key) const { return mValues.find(key) != mValues.end(); } -void UntypedMultiFloatValues::SetMultiKeyValue(const std::string& key, double val) const { +void UntypedMultiValues::Set(const std::string& key, double val) const { if (mMetricEventPtr) { - SetMultiKeyValueNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val); + SetNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val); } } -void UntypedMultiFloatValues::SetMultiKeyValue(StringView key, double val) const { +void UntypedMultiValues::Set(StringView key, double val) const { if (mMetricEventPtr) { - SetMultiKeyValueNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val); + SetNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val); } } -void UntypedMultiFloatValues::SetMultiKeyValueNoCopy(const StringBuffer& key, double val) const { - SetMultiKeyValueNoCopy(StringView(key.data, key.size), val); +void UntypedMultiValues::SetNoCopy(const StringBuffer& key, double val) const { + SetNoCopy(StringView(key.data, key.size), val); } -void UntypedMultiFloatValues::SetMultiKeyValueNoCopy(StringView key, double val) const { +void UntypedMultiValues::SetNoCopy(StringView key, double val) const { mValues[key] = val; } -void UntypedMultiFloatValues::DelMultiKeyValue(StringView key) const { +void UntypedMultiValues::Del(StringView key) const { mValues.erase(key); } -std::map::const_iterator UntypedMultiFloatValues::MultiKeyValusBegin() const { +std::map::const_iterator UntypedMultiValues::MultiKeyValusBegin() const { return mValues.begin(); } -std::map::const_iterator UntypedMultiFloatValues::MultiKeyValusEnd() const { +std::map::const_iterator UntypedMultiValues::MultiKeyValusEnd() const { return mValues.end(); } -size_t UntypedMultiFloatValues::MultiKeyValusSize() const { +size_t UntypedMultiValues::MultiKeyValusSize() const { return mValues.size(); } -size_t UntypedMultiFloatValues::DataSize() const { - size_t totalSize = sizeof(UntypedMultiFloatValues); +size_t UntypedMultiValues::DataSize() const { + size_t totalSize = sizeof(UntypedMultiValues); for (const auto& pair : mValues) { totalSize += pair.first.size() + sizeof(pair.second); } @@ -97,7 +97,7 @@ void UntypedSingleValue::FromJson(const Json::Value& value) { mValue = value.asFloat(); } -Json::Value UntypedMultiFloatValues::ToJson() const { +Json::Value UntypedMultiValues::ToJson() const { Json::Value res; for (auto metric : mValues) { res[metric.first.to_string()] = metric.second; @@ -105,11 +105,11 @@ Json::Value UntypedMultiFloatValues::ToJson() const { return res; } -void UntypedMultiFloatValues::FromJson(const Json::Value& value) { +void UntypedMultiValues::FromJson(const Json::Value& value) { mValues.clear(); for (Json::Value::const_iterator itr = value.begin(); itr != value.end(); ++itr) { if (itr->asDouble()) { - SetMultiKeyValue(itr.key().asString(), itr->asDouble()); + Set(itr.key().asString(), itr->asDouble()); } } } diff --git a/core/models/MetricValue.h b/core/models/MetricValue.h index bdeb25967f..4ed9d0943c 100644 --- a/core/models/MetricValue.h +++ b/core/models/MetricValue.h @@ -42,20 +42,20 @@ struct UntypedSingleValue { #endif }; -struct UntypedMultiFloatValues { +struct UntypedMultiValues { mutable std::map mValues; PipelineEvent* mMetricEventPtr; - UntypedMultiFloatValues(PipelineEvent* ptr): mMetricEventPtr(ptr) {} - UntypedMultiFloatValues(std::map values, PipelineEvent* ptr): mValues(values), mMetricEventPtr(ptr) {} + UntypedMultiValues(PipelineEvent* ptr): mMetricEventPtr(ptr) {} + UntypedMultiValues(std::map values, PipelineEvent* ptr): mValues(values), mMetricEventPtr(ptr) {} - double GetMultiKeyValue(StringView key) const; - bool HasMultiKeyValue(StringView key) const; - void SetMultiKeyValue(const std::string& key, double val) const; - void SetMultiKeyValue(StringView key, double val) const; - void SetMultiKeyValueNoCopy(const StringBuffer& key, double val) const; - void SetMultiKeyValueNoCopy(StringView key, double val) const; - void DelMultiKeyValue(StringView key) const; + double Get(StringView key) const; + bool Has(StringView key) const; + void Set(const std::string& key, double val) const; + void Set(StringView key, double val) const; + void SetNoCopy(const StringBuffer& key, double val) const; + void SetNoCopy(StringView key, double val) const; + void Del(StringView key) const; std::map::const_iterator MultiKeyValusBegin() const; std::map::const_iterator MultiKeyValusEnd() const; @@ -69,7 +69,7 @@ struct UntypedMultiFloatValues { #endif }; -using MetricValue = std::variant; +using MetricValue = std::variant; size_t DataSize(const MetricValue& value); diff --git a/core/unittest/models/MetricEventUnittest.cpp b/core/unittest/models/MetricEventUnittest.cpp index e0f5a48d12..67d483c91b 100644 --- a/core/unittest/models/MetricEventUnittest.cpp +++ b/core/unittest/models/MetricEventUnittest.cpp @@ -26,15 +26,15 @@ class MetricEventUnittest : public ::testing::Test { public: void TestName(); void TestUntypedSingleValue(); - void TestUntypedMultiFloatValues(); + void TestUntypedMultiValues(); void TestTag(); void TestUntypedSingleValueSize(); - void TestUntypedMultiFloatValuesSize(); + void TestUntypedMultiValuesSize(); void TestReset(); void TestUntypedSingleValueToJson(); - void TestUntypedMultiFloatValuesToJson(); + void TestUntypedMultiValuesToJson(); void TestUntypedSingleValueFromJson(); - void TestUntypedMultiFloatValuesFromJson(); + void TestUntypedMultiValuesFromJson(); void TestTagsIterator(); protected: @@ -65,23 +65,23 @@ void MetricEventUnittest::TestUntypedSingleValue() { APSARA_TEST_EQUAL(100.0, mMetricEvent->GetValue()->mValue); } -void MetricEventUnittest::TestUntypedMultiFloatValues() { - mMetricEvent->SetValue(UntypedMultiFloatValues{{{"test-1", 10.0}, {"test-2", 2.0}}, mMetricEvent.get()}); - APSARA_TEST_TRUE(mMetricEvent->Is()); - APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-1")); - APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-2")); +void MetricEventUnittest::TestUntypedMultiValues() { + mMetricEvent->SetValue(UntypedMultiValues{{{"test-1", 10.0}, {"test-2", 2.0}}, mMetricEvent.get()}); + APSARA_TEST_TRUE(mMetricEvent->Is()); + APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->Get("test-1")); + APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->Get("test-2")); map metrics({{"test-3", 15.0}, {"test-4", 24.0}}); - mMetricEvent->SetValue(metrics, mMetricEvent.get()); - APSARA_TEST_TRUE(mMetricEvent->Is()); - APSARA_TEST_EQUAL(15.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-3")); - APSARA_TEST_EQUAL(24.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-4")); + mMetricEvent->SetValue(metrics, mMetricEvent.get()); + APSARA_TEST_TRUE(mMetricEvent->Is()); + APSARA_TEST_EQUAL(15.0, mMetricEvent->GetValue()->Get("test-3")); + APSARA_TEST_EQUAL(24.0, mMetricEvent->GetValue()->Get("test-4")); - mMetricEvent->GetValue()->SetMultiKeyValue(string("test-1"), 6.0); - APSARA_TEST_EQUAL(6.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-1")); + mMetricEvent->GetValue()->Set(string("test-1"), 6.0); + APSARA_TEST_EQUAL(6.0, mMetricEvent->GetValue()->Get("test-1")); - mMetricEvent->GetValue()->DelMultiKeyValue("test-4"); - APSARA_TEST_EQUAL(0, mMetricEvent->GetValue()->GetMultiKeyValue("test-4")); + mMetricEvent->GetValue()->Del("test-4"); + APSARA_TEST_EQUAL(0, mMetricEvent->GetValue()->Get("test-4")); } void MetricEventUnittest::TestTag() { @@ -149,10 +149,10 @@ void MetricEventUnittest::TestUntypedSingleValueSize() { APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); } -void MetricEventUnittest::TestUntypedMultiFloatValuesSize() { +void MetricEventUnittest::TestUntypedMultiValuesSize() { mMetricEvent->SetName("test"); - mMetricEvent->SetValue(UntypedMultiFloatValues{{}, mMetricEvent.get()}); - size_t basicSize = sizeof(time_t) + sizeof(long) + sizeof(UntypedMultiFloatValues) + sizeof(map); + mMetricEvent->SetValue(UntypedMultiValues{{}, mMetricEvent.get()}); + size_t basicSize = sizeof(time_t) + sizeof(long) + sizeof(UntypedMultiValues) + sizeof(map); basicSize += 4; // add tag, and key not existed @@ -168,16 +168,16 @@ void MetricEventUnittest::TestUntypedMultiFloatValuesSize() { APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); // add multi values, and key not existed - mMetricEvent->GetValue()->SetMultiKeyValue(string("test-1"), 5.0); + mMetricEvent->GetValue()->Set(string("test-1"), 5.0); basicSize += 14; APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); // add multi values, and key existed - mMetricEvent->GetValue()->SetMultiKeyValue(string("test-1"), 99.0); + mMetricEvent->GetValue()->Set(string("test-1"), 99.0); APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); // delete multi values - mMetricEvent->GetValue()->DelMultiKeyValue("test-1"); + mMetricEvent->GetValue()->Del("test-1"); basicSize -= 14; APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); } @@ -222,10 +222,10 @@ void MetricEventUnittest::TestUntypedSingleValueToJson() { APSARA_TEST_TRUE(eventJson == res); } -void MetricEventUnittest::TestUntypedMultiFloatValuesToJson() { +void MetricEventUnittest::TestUntypedMultiValuesToJson() { mMetricEvent->SetTimestamp(12345678901, 0); mMetricEvent->SetName("test"); - mMetricEvent->SetValue(UntypedMultiFloatValues{{{"test-1", 10.0}, {"test-2", 2.0}}, mMetricEvent.get()}); + mMetricEvent->SetValue(UntypedMultiValues{{{"test-1", 10.0}, {"test-2", 2.0}}, mMetricEvent.get()}); mMetricEvent->SetTag(string("key1"), string("value1")); Json::Value res = mMetricEvent->ToJson(); @@ -278,7 +278,7 @@ void MetricEventUnittest::TestUntypedSingleValueFromJson() { APSARA_TEST_EQUAL("value1", mMetricEvent->GetTag("key1").to_string()); } -void MetricEventUnittest::TestUntypedMultiFloatValuesFromJson() { +void MetricEventUnittest::TestUntypedMultiValuesFromJson() { Json::Value eventJson; string eventStr = R"({ "name": "test", @@ -302,11 +302,11 @@ void MetricEventUnittest::TestUntypedMultiFloatValuesFromJson() { APSARA_TEST_EQUAL(12345678901, mMetricEvent->GetTimestamp()); APSARA_TEST_EQUAL(0L, mMetricEvent->GetTimestampNanosecond().value()); APSARA_TEST_EQUAL("test", mMetricEvent->GetName()); - APSARA_TEST_TRUE(mMetricEvent->Is()); - APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-1")); - APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-2")); - APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-1")); - APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->GetMultiKeyValue("test-2")); + APSARA_TEST_TRUE(mMetricEvent->Is()); + APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->Get("test-1")); + APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->Get("test-2")); + APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->Get("test-1")); + APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->Get("test-2")); APSARA_TEST_EQUAL("value1", mMetricEvent->GetTag("key1").to_string()); } @@ -335,15 +335,15 @@ void MetricEventUnittest::TestTagsIterator() { UNIT_TEST_CASE(MetricEventUnittest, TestName) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValue) -UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiFloatValues) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValues) UNIT_TEST_CASE(MetricEventUnittest, TestTag) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValueSize) -UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiFloatValuesSize) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesSize) UNIT_TEST_CASE(MetricEventUnittest, TestReset) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValueToJson) -UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiFloatValuesToJson) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesToJson) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValueFromJson) -UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiFloatValuesFromJson) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesFromJson) UNIT_TEST_CASE(MetricEventUnittest, TestTagsIterator) } // namespace logtail diff --git a/core/unittest/models/MetricValueUnittest.cpp b/core/unittest/models/MetricValueUnittest.cpp index 74a37ca533..6aa5e6962e 100644 --- a/core/unittest/models/MetricValueUnittest.cpp +++ b/core/unittest/models/MetricValueUnittest.cpp @@ -44,7 +44,7 @@ void UntypedSingleValueUnittest::TestFromJson() { UNIT_TEST_CASE(UntypedSingleValueUnittest, TestToJson) UNIT_TEST_CASE(UntypedSingleValueUnittest, TestFromJson) -class UntypedMultiFloatValuesUnittest : public ::testing::Test { +class UntypedMultiValuesUnittest : public ::testing::Test { public: void TestToJson(); void TestFromJson(); @@ -62,10 +62,10 @@ class UntypedMultiFloatValuesUnittest : public ::testing::Test { unique_ptr mMetricEvent; }; -void UntypedMultiFloatValuesUnittest::TestToJson() { - UntypedMultiFloatValues value(mMetricEvent.get()); - value.SetMultiKeyValue(string("test-1"), 10.0); - value.SetMultiKeyValue(string("test-2"), 2.0); +void UntypedMultiValuesUnittest::TestToJson() { + UntypedMultiValues value(mMetricEvent.get()); + value.Set(string("test-1"), 10.0); + value.Set(string("test-2"), 2.0); Json::Value res = value.ToJson(); Json::Value valueJson; @@ -75,19 +75,19 @@ void UntypedMultiFloatValuesUnittest::TestToJson() { APSARA_TEST_TRUE(valueJson == res); } -void UntypedMultiFloatValuesUnittest::TestFromJson() { - UntypedMultiFloatValues value(mMetricEvent.get()); +void UntypedMultiValuesUnittest::TestFromJson() { + UntypedMultiValues value(mMetricEvent.get()); Json::Value valueJson; valueJson["test-1"] = 10.0; valueJson["test-2"] = 2.0; value.FromJson(valueJson); - APSARA_TEST_EQUAL(10.0, value.GetMultiKeyValue("test-1")); - APSARA_TEST_EQUAL(2.0, value.GetMultiKeyValue("test-2")); + APSARA_TEST_EQUAL(10.0, value.Get("test-1")); + APSARA_TEST_EQUAL(2.0, value.Get("test-2")); } -UNIT_TEST_CASE(UntypedMultiFloatValuesUnittest, TestToJson) -UNIT_TEST_CASE(UntypedMultiFloatValuesUnittest, TestFromJson) +UNIT_TEST_CASE(UntypedMultiValuesUnittest, TestToJson) +UNIT_TEST_CASE(UntypedMultiValuesUnittest, TestFromJson) } // namespace logtail From 232b332d7d8fe9fa1b69d87fe2510d8b24051635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Mon, 25 Nov 2024 06:16:06 +0000 Subject: [PATCH 5/8] polish --- core/models/MetricEvent.cpp | 6 +- core/models/MetricEvent.h | 5 ++ core/models/MetricValue.cpp | 17 +++--- core/models/MetricValue.h | 20 ++++--- core/unittest/models/MetricEventUnittest.cpp | 61 ++++++++++++++------ core/unittest/models/MetricValueUnittest.cpp | 7 ++- 6 files changed, 79 insertions(+), 37 deletions(-) diff --git a/core/models/MetricEvent.cpp b/core/models/MetricEvent.cpp index 2b7ceaa8b2..9e47a4e517 100644 --- a/core/models/MetricEvent.cpp +++ b/core/models/MetricEvent.cpp @@ -24,7 +24,11 @@ MetricEvent::MetricEvent(PipelineEventGroup* ptr) : PipelineEvent(Type::METRIC, } unique_ptr MetricEvent::Copy() const { - return make_unique(*this); + unique_ptr newPtr = make_unique(*this); + if (newPtr->Is()) { + newPtr->GetMutableValue()->ResetPipelineEvent(newPtr.get()); + } + return newPtr; } void MetricEvent::Reset() { diff --git a/core/models/MetricEvent.h b/core/models/MetricEvent.h index 0c4779ac44..3cb6060649 100644 --- a/core/models/MetricEvent.h +++ b/core/models/MetricEvent.h @@ -48,6 +48,11 @@ class MetricEvent : public PipelineEvent { return std::get_if(&mValue); } + template + constexpr std::add_pointer_t GetMutableValue() noexcept { + return std::get_if(&mValue); + } + template void SetValue(const T& value) { mValue = value; diff --git a/core/models/MetricValue.cpp b/core/models/MetricValue.cpp index 0c10fbb0fe..f9a5f42460 100644 --- a/core/models/MetricValue.cpp +++ b/core/models/MetricValue.cpp @@ -20,38 +20,39 @@ using namespace std; namespace logtail { -double UntypedMultiValues::Get(StringView key) const { +bool UntypedMultiValues::Get(StringView key, double& val) const { if (mValues.find(key) != mValues.end()) { - return mValues.at(key); + val = mValues.at(key); + return true; } - return 0; + return false; } bool UntypedMultiValues::Has(StringView key) const { return mValues.find(key) != mValues.end(); } -void UntypedMultiValues::Set(const std::string& key, double val) const { +void UntypedMultiValues::Set(const std::string& key, double val) { if (mMetricEventPtr) { SetNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val); } } -void UntypedMultiValues::Set(StringView key, double val) const { +void UntypedMultiValues::Set(StringView key, double val) { if (mMetricEventPtr) { SetNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val); } } -void UntypedMultiValues::SetNoCopy(const StringBuffer& key, double val) const { +void UntypedMultiValues::SetNoCopy(const StringBuffer& key, double val) { SetNoCopy(StringView(key.data, key.size), val); } -void UntypedMultiValues::SetNoCopy(StringView key, double val) const { +void UntypedMultiValues::SetNoCopy(StringView key, double val) { mValues[key] = val; } -void UntypedMultiValues::Del(StringView key) const { +void UntypedMultiValues::Del(StringView key) { mValues.erase(key); } diff --git a/core/models/MetricValue.h b/core/models/MetricValue.h index 4ed9d0943c..6d977c0520 100644 --- a/core/models/MetricValue.h +++ b/core/models/MetricValue.h @@ -43,25 +43,27 @@ struct UntypedSingleValue { }; struct UntypedMultiValues { - mutable std::map mValues; + std::map mValues; PipelineEvent* mMetricEventPtr; - UntypedMultiValues(PipelineEvent* ptr): mMetricEventPtr(ptr) {} - UntypedMultiValues(std::map values, PipelineEvent* ptr): mValues(values), mMetricEventPtr(ptr) {} + UntypedMultiValues(PipelineEvent* ptr) : mMetricEventPtr(ptr) {} + UntypedMultiValues(std::map values, PipelineEvent* ptr) + : mValues(values), mMetricEventPtr(ptr) {} - double Get(StringView key) const; + bool Get(StringView key, double& val) const; bool Has(StringView key) const; - void Set(const std::string& key, double val) const; - void Set(StringView key, double val) const; - void SetNoCopy(const StringBuffer& key, double val) const; - void SetNoCopy(StringView key, double val) const; - void Del(StringView key) const; + void Set(const std::string& key, double val); + void Set(StringView key, double val); + void SetNoCopy(const StringBuffer& key, double val); + void SetNoCopy(StringView key, double val); + void Del(StringView key); std::map::const_iterator MultiKeyValusBegin() const; std::map::const_iterator MultiKeyValusEnd() const; size_t MultiKeyValusSize() const; size_t DataSize() const; + void ResetPipelineEvent(PipelineEvent* ptr) { mMetricEventPtr = ptr; } #ifdef APSARA_UNIT_TEST_MAIN Json::Value ToJson() const; diff --git a/core/unittest/models/MetricEventUnittest.cpp b/core/unittest/models/MetricEventUnittest.cpp index 67d483c91b..1226f85f72 100644 --- a/core/unittest/models/MetricEventUnittest.cpp +++ b/core/unittest/models/MetricEventUnittest.cpp @@ -36,6 +36,7 @@ class MetricEventUnittest : public ::testing::Test { void TestUntypedSingleValueFromJson(); void TestUntypedMultiValuesFromJson(); void TestTagsIterator(); + void TestCopy(); protected: void SetUp() override { @@ -68,20 +69,27 @@ void MetricEventUnittest::TestUntypedSingleValue() { void MetricEventUnittest::TestUntypedMultiValues() { mMetricEvent->SetValue(UntypedMultiValues{{{"test-1", 10.0}, {"test-2", 2.0}}, mMetricEvent.get()}); APSARA_TEST_TRUE(mMetricEvent->Is()); - APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->Get("test-1")); - APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->Get("test-2")); + double val; + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-1", val)); + APSARA_TEST_EQUAL(10.0, val); + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-2", val)); + APSARA_TEST_EQUAL(2.0, val); map metrics({{"test-3", 15.0}, {"test-4", 24.0}}); mMetricEvent->SetValue(metrics, mMetricEvent.get()); APSARA_TEST_TRUE(mMetricEvent->Is()); - APSARA_TEST_EQUAL(15.0, mMetricEvent->GetValue()->Get("test-3")); - APSARA_TEST_EQUAL(24.0, mMetricEvent->GetValue()->Get("test-4")); - - mMetricEvent->GetValue()->Set(string("test-1"), 6.0); - APSARA_TEST_EQUAL(6.0, mMetricEvent->GetValue()->Get("test-1")); - - mMetricEvent->GetValue()->Del("test-4"); - APSARA_TEST_EQUAL(0, mMetricEvent->GetValue()->Get("test-4")); + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-3", val)); + APSARA_TEST_EQUAL(15.0, val); + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-4", val)); + APSARA_TEST_EQUAL(24.0, val); + + mMetricEvent->GetMutableValue()->Set(string("test-1"), 6.0); + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-1", val)); + APSARA_TEST_EQUAL(6.0, val); + + mMetricEvent->GetMutableValue()->Del("test-4"); + APSARA_TEST_EQUAL(false, mMetricEvent->GetValue()->Get("test-4", val)); + APSARA_TEST_EQUAL(6.0, val); } void MetricEventUnittest::TestTag() { @@ -168,16 +176,16 @@ void MetricEventUnittest::TestUntypedMultiValuesSize() { APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); // add multi values, and key not existed - mMetricEvent->GetValue()->Set(string("test-1"), 5.0); + mMetricEvent->GetMutableValue()->Set(string("test-1"), 5.0); basicSize += 14; APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); // add multi values, and key existed - mMetricEvent->GetValue()->Set(string("test-1"), 99.0); + mMetricEvent->GetMutableValue()->Set(string("test-1"), 99.0); APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); // delete multi values - mMetricEvent->GetValue()->Del("test-1"); + mMetricEvent->GetMutableValue()->Del("test-1"); basicSize -= 14; APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); } @@ -298,15 +306,16 @@ void MetricEventUnittest::TestUntypedMultiValuesFromJson() { string errorMsg; ParseJsonTable(eventStr, eventJson, errorMsg); mMetricEvent->FromJson(eventJson); + double 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()); - APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->Get("test-1")); - APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->Get("test-2")); - APSARA_TEST_EQUAL(10.0, mMetricEvent->GetValue()->Get("test-1")); - APSARA_TEST_EQUAL(2.0, mMetricEvent->GetValue()->Get("test-2")); + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-1", val)); + APSARA_TEST_EQUAL(10.0, val); + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-2", val)); + APSARA_TEST_EQUAL(2.0, val); APSARA_TEST_EQUAL("value1", mMetricEvent->GetTag("key1").to_string()); } @@ -333,6 +342,23 @@ void MetricEventUnittest::TestTagsIterator() { APSARA_TEST_EQUAL((size_t)3, mMetricEvent->TagsSize()); } +void MetricEventUnittest::TestCopy() { + MetricEvent* oldMetricEvent = mEventGroup->AddMetricEvent(); + oldMetricEvent->SetValue(UntypedMultiValues{{{"test-1", 10.0}, {"test-2", 2.0}}, oldMetricEvent}); + APSARA_TEST_EQUAL(1, mEventGroup->GetEvents().size()); + + PipelineEventGroup newGroup = mEventGroup->Copy(); + MetricEvent newMetricEvent = newGroup.GetEvents().at(0).Cast(); + double val; + + APSARA_TEST_TRUE(newMetricEvent.Is()); + APSARA_TEST_EQUAL(true, newMetricEvent.GetValue()->Get("test-1", val)); + APSARA_TEST_EQUAL(10.0, val); + APSARA_TEST_EQUAL(true, newMetricEvent.GetValue()->Get("test-2", val)); + APSARA_TEST_EQUAL(2.0, val); + APSARA_TEST_NOT_EQUAL(newMetricEvent.GetValue()->mMetricEventPtr, oldMetricEvent->GetValue()->mMetricEventPtr); +} + UNIT_TEST_CASE(MetricEventUnittest, TestName) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValue) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValues) @@ -345,6 +371,7 @@ UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesToJson) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValueFromJson) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesFromJson) UNIT_TEST_CASE(MetricEventUnittest, TestTagsIterator) +UNIT_TEST_CASE(MetricEventUnittest, TestCopy) } // namespace logtail diff --git a/core/unittest/models/MetricValueUnittest.cpp b/core/unittest/models/MetricValueUnittest.cpp index 6aa5e6962e..cdc2b79ff1 100644 --- a/core/unittest/models/MetricValueUnittest.cpp +++ b/core/unittest/models/MetricValueUnittest.cpp @@ -81,9 +81,12 @@ void UntypedMultiValuesUnittest::TestFromJson() { valueJson["test-1"] = 10.0; valueJson["test-2"] = 2.0; value.FromJson(valueJson); + double val; - APSARA_TEST_EQUAL(10.0, value.Get("test-1")); - APSARA_TEST_EQUAL(2.0, value.Get("test-2")); + APSARA_TEST_EQUAL(true, value.Get("test-1", val)); + APSARA_TEST_EQUAL(10.0, val); + APSARA_TEST_EQUAL(true, value.Get("test-2", val)); + APSARA_TEST_EQUAL(2.0, val); } UNIT_TEST_CASE(UntypedMultiValuesUnittest, TestToJson) From db3994f8ba62f8a501113d1cc4cff0d4d441ddd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Mon, 25 Nov 2024 11:00:29 +0000 Subject: [PATCH 6/8] polish --- core/models/MetricEvent.cpp | 14 ++-- core/models/MetricEvent.h | 2 +- core/models/MetricValue.cpp | 36 ++++----- core/models/MetricValue.h | 22 +++--- core/unittest/models/MetricEventUnittest.cpp | 80 ++++++++++---------- core/unittest/models/MetricValueUnittest.cpp | 22 +++--- 6 files changed, 88 insertions(+), 88 deletions(-) diff --git a/core/models/MetricEvent.cpp b/core/models/MetricEvent.cpp index 9e47a4e517..d1ae8e139d 100644 --- a/core/models/MetricEvent.cpp +++ b/core/models/MetricEvent.cpp @@ -25,8 +25,8 @@ MetricEvent::MetricEvent(PipelineEventGroup* ptr) : PipelineEvent(Type::METRIC, unique_ptr MetricEvent::Copy() const { unique_ptr newPtr = make_unique(*this); - if (newPtr->Is()) { - newPtr->GetMutableValue()->ResetPipelineEvent(newPtr.get()); + if (newPtr->Is()) { + newPtr->MutableValue()->ResetPipelineEvent(newPtr.get()); } return newPtr; } @@ -99,9 +99,9 @@ Json::Value MetricEvent::ToJson(bool enableEventMeta) const { if constexpr (is_same_v) { root["value"]["type"] = "untyped_single_value"; root["value"]["detail"] = get(mValue).ToJson(); - } else if constexpr (is_same_v) { - root["value"]["type"] = "untyped_multi_values"; - root["value"]["detail"] = get(mValue).ToJson(); + } else if constexpr (is_same_v) { + root["value"]["type"] = "untyped_multi_double_values"; + root["value"]["detail"] = get(mValue).ToJson(); } else if constexpr (is_same_v) { root["value"]["type"] = "unknown"; } @@ -128,8 +128,8 @@ bool MetricEvent::FromJson(const Json::Value& root) { UntypedSingleValue v; v.FromJson(value["detail"]); SetValue(v); - } else if (value["type"].asString() == "untyped_multi_values") { - UntypedMultiValues v(this); + } else if (value["type"].asString() == "untyped_multi_double_values") { + UntypedMultiDoubleValues v(this); v.FromJson(value["detail"]); SetValue(v); } diff --git a/core/models/MetricEvent.h b/core/models/MetricEvent.h index 3cb6060649..20cc8cb762 100644 --- a/core/models/MetricEvent.h +++ b/core/models/MetricEvent.h @@ -49,7 +49,7 @@ class MetricEvent : public PipelineEvent { } template - constexpr std::add_pointer_t GetMutableValue() noexcept { + constexpr std::add_pointer_t MutableValue() noexcept { return std::get_if(&mValue); } diff --git a/core/models/MetricValue.cpp b/core/models/MetricValue.cpp index f9a5f42460..3a8618c64a 100644 --- a/core/models/MetricValue.cpp +++ b/core/models/MetricValue.cpp @@ -20,7 +20,7 @@ using namespace std; namespace logtail { -bool UntypedMultiValues::Get(StringView key, double& val) const { +bool UntypedMultiDoubleValues::GetValue(StringView key, double& val) const { if (mValues.find(key) != mValues.end()) { val = mValues.at(key); return true; @@ -28,48 +28,48 @@ bool UntypedMultiValues::Get(StringView key, double& val) const { return false; } -bool UntypedMultiValues::Has(StringView key) const { +bool UntypedMultiDoubleValues::HasValue(StringView key) const { return mValues.find(key) != mValues.end(); } -void UntypedMultiValues::Set(const std::string& key, double val) { +void UntypedMultiDoubleValues::SetValue(const std::string& key, double val) { if (mMetricEventPtr) { - SetNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val); + SetValueNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val); } } -void UntypedMultiValues::Set(StringView key, double val) { +void UntypedMultiDoubleValues::SetValue(StringView key, double val) { if (mMetricEventPtr) { - SetNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val); + SetValueNoCopy(mMetricEventPtr->GetSourceBuffer()->CopyString(key), val); } } -void UntypedMultiValues::SetNoCopy(const StringBuffer& key, double val) { - SetNoCopy(StringView(key.data, key.size), val); +void UntypedMultiDoubleValues::SetValueNoCopy(const StringBuffer& key, double val) { + SetValueNoCopy(StringView(key.data, key.size), val); } -void UntypedMultiValues::SetNoCopy(StringView key, double val) { +void UntypedMultiDoubleValues::SetValueNoCopy(StringView key, double val) { mValues[key] = val; } -void UntypedMultiValues::Del(StringView key) { +void UntypedMultiDoubleValues::DelValue(StringView key) { mValues.erase(key); } -std::map::const_iterator UntypedMultiValues::MultiKeyValusBegin() const { +std::map::const_iterator UntypedMultiDoubleValues::MultiKeyValusBegin() const { return mValues.begin(); } -std::map::const_iterator UntypedMultiValues::MultiKeyValusEnd() const { +std::map::const_iterator UntypedMultiDoubleValues::MultiKeyValusEnd() const { return mValues.end(); } -size_t UntypedMultiValues::MultiKeyValusSize() const { +size_t UntypedMultiDoubleValues::MultiKeyValusSize() const { return mValues.size(); } -size_t UntypedMultiValues::DataSize() const { - size_t totalSize = sizeof(UntypedMultiValues); +size_t UntypedMultiDoubleValues::DataSize() const { + size_t totalSize = sizeof(UntypedMultiDoubleValues); for (const auto& pair : mValues) { totalSize += pair.first.size() + sizeof(pair.second); } @@ -98,7 +98,7 @@ void UntypedSingleValue::FromJson(const Json::Value& value) { mValue = value.asFloat(); } -Json::Value UntypedMultiValues::ToJson() const { +Json::Value UntypedMultiDoubleValues::ToJson() const { Json::Value res; for (auto metric : mValues) { res[metric.first.to_string()] = metric.second; @@ -106,11 +106,11 @@ Json::Value UntypedMultiValues::ToJson() const { return res; } -void UntypedMultiValues::FromJson(const Json::Value& value) { +void UntypedMultiDoubleValues::FromJson(const Json::Value& value) { mValues.clear(); for (Json::Value::const_iterator itr = value.begin(); itr != value.end(); ++itr) { if (itr->asDouble()) { - Set(itr.key().asString(), itr->asDouble()); + SetValue(itr.key().asString(), itr->asDouble()); } } } diff --git a/core/models/MetricValue.h b/core/models/MetricValue.h index 6d977c0520..67774c7e2f 100644 --- a/core/models/MetricValue.h +++ b/core/models/MetricValue.h @@ -42,21 +42,21 @@ struct UntypedSingleValue { #endif }; -struct UntypedMultiValues { +struct UntypedMultiDoubleValues { std::map mValues; PipelineEvent* mMetricEventPtr; - UntypedMultiValues(PipelineEvent* ptr) : mMetricEventPtr(ptr) {} - UntypedMultiValues(std::map values, PipelineEvent* ptr) + UntypedMultiDoubleValues(PipelineEvent* ptr) : mMetricEventPtr(ptr) {} + UntypedMultiDoubleValues(std::map values, PipelineEvent* ptr) : mValues(values), mMetricEventPtr(ptr) {} - bool Get(StringView key, double& val) const; - bool Has(StringView key) const; - void Set(const std::string& key, double val); - void Set(StringView key, double val); - void SetNoCopy(const StringBuffer& key, double val); - void SetNoCopy(StringView key, double val); - void Del(StringView key); + bool GetValue(StringView key, double& 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 DelValue(StringView key); std::map::const_iterator MultiKeyValusBegin() const; std::map::const_iterator MultiKeyValusEnd() const; @@ -71,7 +71,7 @@ struct UntypedMultiValues { #endif }; -using MetricValue = std::variant; +using MetricValue = std::variant; size_t DataSize(const MetricValue& value); diff --git a/core/unittest/models/MetricEventUnittest.cpp b/core/unittest/models/MetricEventUnittest.cpp index 1226f85f72..4735a65733 100644 --- a/core/unittest/models/MetricEventUnittest.cpp +++ b/core/unittest/models/MetricEventUnittest.cpp @@ -26,15 +26,15 @@ class MetricEventUnittest : public ::testing::Test { public: void TestName(); void TestUntypedSingleValue(); - void TestUntypedMultiValues(); + void TestUntypedMultiDoubleValues(); void TestTag(); void TestUntypedSingleValueSize(); - void TestUntypedMultiValuesSize(); + void TestUntypedMultiDoubleValuesSize(); void TestReset(); void TestUntypedSingleValueToJson(); - void TestUntypedMultiValuesToJson(); + void TestUntypedMultiDoubleValuesToJson(); void TestUntypedSingleValueFromJson(); - void TestUntypedMultiValuesFromJson(); + void TestUntypedMultiDoubleValuesFromJson(); void TestTagsIterator(); void TestCopy(); @@ -66,29 +66,29 @@ void MetricEventUnittest::TestUntypedSingleValue() { APSARA_TEST_EQUAL(100.0, mMetricEvent->GetValue()->mValue); } -void MetricEventUnittest::TestUntypedMultiValues() { - mMetricEvent->SetValue(UntypedMultiValues{{{"test-1", 10.0}, {"test-2", 2.0}}, mMetricEvent.get()}); - APSARA_TEST_TRUE(mMetricEvent->Is()); +void MetricEventUnittest::TestUntypedMultiDoubleValues() { + mMetricEvent->SetValue(UntypedMultiDoubleValues{{{"test-1", 10.0}, {"test-2", 2.0}}, mMetricEvent.get()}); + APSARA_TEST_TRUE(mMetricEvent->Is()); double val; - APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-1", val)); + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->GetValue("test-1", val)); APSARA_TEST_EQUAL(10.0, val); - APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-2", val)); + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->GetValue("test-2", val)); APSARA_TEST_EQUAL(2.0, val); map metrics({{"test-3", 15.0}, {"test-4", 24.0}}); - mMetricEvent->SetValue(metrics, mMetricEvent.get()); - APSARA_TEST_TRUE(mMetricEvent->Is()); - APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-3", val)); + mMetricEvent->SetValue(metrics, mMetricEvent.get()); + APSARA_TEST_TRUE(mMetricEvent->Is()); + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->GetValue("test-3", val)); APSARA_TEST_EQUAL(15.0, val); - APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-4", val)); + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->GetValue("test-4", val)); APSARA_TEST_EQUAL(24.0, val); - mMetricEvent->GetMutableValue()->Set(string("test-1"), 6.0); - APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-1", val)); + mMetricEvent->MutableValue()->SetValue(string("test-1"), 6.0); + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->GetValue("test-1", val)); APSARA_TEST_EQUAL(6.0, val); - mMetricEvent->GetMutableValue()->Del("test-4"); - APSARA_TEST_EQUAL(false, mMetricEvent->GetValue()->Get("test-4", val)); + mMetricEvent->MutableValue()->DelValue("test-4"); + APSARA_TEST_EQUAL(false, mMetricEvent->GetValue()->GetValue("test-4", val)); APSARA_TEST_EQUAL(6.0, val); } @@ -157,10 +157,10 @@ void MetricEventUnittest::TestUntypedSingleValueSize() { APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); } -void MetricEventUnittest::TestUntypedMultiValuesSize() { +void MetricEventUnittest::TestUntypedMultiDoubleValuesSize() { mMetricEvent->SetName("test"); - mMetricEvent->SetValue(UntypedMultiValues{{}, mMetricEvent.get()}); - size_t basicSize = sizeof(time_t) + sizeof(long) + sizeof(UntypedMultiValues) + sizeof(map); + mMetricEvent->SetValue(UntypedMultiDoubleValues{{}, mMetricEvent.get()}); + size_t basicSize = sizeof(time_t) + sizeof(long) + sizeof(UntypedMultiDoubleValues) + sizeof(map); basicSize += 4; // add tag, and key not existed @@ -176,16 +176,16 @@ void MetricEventUnittest::TestUntypedMultiValuesSize() { APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); // add multi values, and key not existed - mMetricEvent->GetMutableValue()->Set(string("test-1"), 5.0); + mMetricEvent->MutableValue()->SetValue(string("test-1"), 5.0); basicSize += 14; APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); // add multi values, and key existed - mMetricEvent->GetMutableValue()->Set(string("test-1"), 99.0); + mMetricEvent->MutableValue()->SetValue(string("test-1"), 99.0); APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); // delete multi values - mMetricEvent->GetMutableValue()->Del("test-1"); + mMetricEvent->MutableValue()->DelValue("test-1"); basicSize -= 14; APSARA_TEST_EQUAL(basicSize, mMetricEvent->DataSize()); } @@ -230,10 +230,10 @@ void MetricEventUnittest::TestUntypedSingleValueToJson() { APSARA_TEST_TRUE(eventJson == res); } -void MetricEventUnittest::TestUntypedMultiValuesToJson() { +void MetricEventUnittest::TestUntypedMultiDoubleValuesToJson() { mMetricEvent->SetTimestamp(12345678901, 0); mMetricEvent->SetName("test"); - mMetricEvent->SetValue(UntypedMultiValues{{{"test-1", 10.0}, {"test-2", 2.0}}, mMetricEvent.get()}); + mMetricEvent->SetValue(UntypedMultiDoubleValues{{{"test-1", 10.0}, {"test-2", 2.0}}, mMetricEvent.get()}); mMetricEvent->SetTag(string("key1"), string("value1")); Json::Value res = mMetricEvent->ToJson(); @@ -247,7 +247,7 @@ void MetricEventUnittest::TestUntypedMultiValuesToJson() { "timestampNanosecond" : 0, "type" : 2, "value": { - "type": "untyped_multi_values", + "type": "untyped_multi_double_values", "detail": { "test-1": 10.0, "test-2": 2.0 @@ -286,7 +286,7 @@ void MetricEventUnittest::TestUntypedSingleValueFromJson() { APSARA_TEST_EQUAL("value1", mMetricEvent->GetTag("key1").to_string()); } -void MetricEventUnittest::TestUntypedMultiValuesFromJson() { +void MetricEventUnittest::TestUntypedMultiDoubleValuesFromJson() { Json::Value eventJson; string eventStr = R"({ "name": "test", @@ -296,7 +296,7 @@ void MetricEventUnittest::TestUntypedMultiValuesFromJson() { "timestamp" : 12345678901, "timestampNanosecond" : 0, "value": { - "type": "untyped_multi_values", + "type": "untyped_multi_double_values", "detail": { "test-1": 10.0, "test-2": 2.0 @@ -311,10 +311,10 @@ void MetricEventUnittest::TestUntypedMultiValuesFromJson() { APSARA_TEST_EQUAL(12345678901, mMetricEvent->GetTimestamp()); APSARA_TEST_EQUAL(0L, mMetricEvent->GetTimestampNanosecond().value()); APSARA_TEST_EQUAL("test", mMetricEvent->GetName()); - APSARA_TEST_TRUE(mMetricEvent->Is()); - APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-1", val)); + APSARA_TEST_TRUE(mMetricEvent->Is()); + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->GetValue("test-1", val)); APSARA_TEST_EQUAL(10.0, val); - APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->Get("test-2", val)); + APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->GetValue("test-2", val)); APSARA_TEST_EQUAL(2.0, val); APSARA_TEST_EQUAL("value1", mMetricEvent->GetTag("key1").to_string()); } @@ -344,32 +344,32 @@ void MetricEventUnittest::TestTagsIterator() { void MetricEventUnittest::TestCopy() { MetricEvent* oldMetricEvent = mEventGroup->AddMetricEvent(); - oldMetricEvent->SetValue(UntypedMultiValues{{{"test-1", 10.0}, {"test-2", 2.0}}, oldMetricEvent}); + oldMetricEvent->SetValue(UntypedMultiDoubleValues{{{"test-1", 10.0}, {"test-2", 2.0}}, oldMetricEvent}); APSARA_TEST_EQUAL(1, mEventGroup->GetEvents().size()); PipelineEventGroup newGroup = mEventGroup->Copy(); MetricEvent newMetricEvent = newGroup.GetEvents().at(0).Cast(); double val; - APSARA_TEST_TRUE(newMetricEvent.Is()); - APSARA_TEST_EQUAL(true, newMetricEvent.GetValue()->Get("test-1", val)); + APSARA_TEST_TRUE(newMetricEvent.Is()); + APSARA_TEST_EQUAL(true, newMetricEvent.GetValue()->GetValue("test-1", val)); APSARA_TEST_EQUAL(10.0, val); - APSARA_TEST_EQUAL(true, newMetricEvent.GetValue()->Get("test-2", val)); + APSARA_TEST_EQUAL(true, newMetricEvent.GetValue()->GetValue("test-2", val)); APSARA_TEST_EQUAL(2.0, val); - APSARA_TEST_NOT_EQUAL(newMetricEvent.GetValue()->mMetricEventPtr, oldMetricEvent->GetValue()->mMetricEventPtr); + APSARA_TEST_NOT_EQUAL(newMetricEvent.GetValue()->mMetricEventPtr, oldMetricEvent->GetValue()->mMetricEventPtr); } UNIT_TEST_CASE(MetricEventUnittest, TestName) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValue) -UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValues) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiDoubleValues) UNIT_TEST_CASE(MetricEventUnittest, TestTag) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValueSize) -UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesSize) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiDoubleValuesSize) UNIT_TEST_CASE(MetricEventUnittest, TestReset) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValueToJson) -UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesToJson) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiDoubleValuesToJson) UNIT_TEST_CASE(MetricEventUnittest, TestUntypedSingleValueFromJson) -UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiValuesFromJson) +UNIT_TEST_CASE(MetricEventUnittest, TestUntypedMultiDoubleValuesFromJson) UNIT_TEST_CASE(MetricEventUnittest, TestTagsIterator) UNIT_TEST_CASE(MetricEventUnittest, TestCopy) diff --git a/core/unittest/models/MetricValueUnittest.cpp b/core/unittest/models/MetricValueUnittest.cpp index cdc2b79ff1..8163760d84 100644 --- a/core/unittest/models/MetricValueUnittest.cpp +++ b/core/unittest/models/MetricValueUnittest.cpp @@ -44,7 +44,7 @@ void UntypedSingleValueUnittest::TestFromJson() { UNIT_TEST_CASE(UntypedSingleValueUnittest, TestToJson) UNIT_TEST_CASE(UntypedSingleValueUnittest, TestFromJson) -class UntypedMultiValuesUnittest : public ::testing::Test { +class UntypedMultiDoubleValuesUnittest : public ::testing::Test { public: void TestToJson(); void TestFromJson(); @@ -62,10 +62,10 @@ class UntypedMultiValuesUnittest : public ::testing::Test { unique_ptr mMetricEvent; }; -void UntypedMultiValuesUnittest::TestToJson() { - UntypedMultiValues value(mMetricEvent.get()); - value.Set(string("test-1"), 10.0); - value.Set(string("test-2"), 2.0); +void UntypedMultiDoubleValuesUnittest::TestToJson() { + UntypedMultiDoubleValues value(mMetricEvent.get()); + value.SetValue(string("test-1"), 10.0); + value.SetValue(string("test-2"), 2.0); Json::Value res = value.ToJson(); Json::Value valueJson; @@ -75,22 +75,22 @@ void UntypedMultiValuesUnittest::TestToJson() { APSARA_TEST_TRUE(valueJson == res); } -void UntypedMultiValuesUnittest::TestFromJson() { - UntypedMultiValues value(mMetricEvent.get()); +void UntypedMultiDoubleValuesUnittest::TestFromJson() { + UntypedMultiDoubleValues value(mMetricEvent.get()); Json::Value valueJson; valueJson["test-1"] = 10.0; valueJson["test-2"] = 2.0; value.FromJson(valueJson); double val; - APSARA_TEST_EQUAL(true, value.Get("test-1", val)); + APSARA_TEST_EQUAL(true, value.GetValue("test-1", val)); APSARA_TEST_EQUAL(10.0, val); - APSARA_TEST_EQUAL(true, value.Get("test-2", val)); + APSARA_TEST_EQUAL(true, value.GetValue("test-2", val)); APSARA_TEST_EQUAL(2.0, val); } -UNIT_TEST_CASE(UntypedMultiValuesUnittest, TestToJson) -UNIT_TEST_CASE(UntypedMultiValuesUnittest, TestFromJson) +UNIT_TEST_CASE(UntypedMultiDoubleValuesUnittest, TestToJson) +UNIT_TEST_CASE(UntypedMultiDoubleValuesUnittest, TestFromJson) } // namespace logtail From d1d43c511e6d480d78d39a4d1fd14354bab7fa36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Mon, 25 Nov 2024 11:06:06 +0000 Subject: [PATCH 7/8] fix bug --- core/models/MetricValue.cpp | 6 +++--- core/models/MetricValue.h | 6 +++--- core/runner/FlusherRunner.cpp | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/models/MetricValue.cpp b/core/models/MetricValue.cpp index 3a8618c64a..c262367e07 100644 --- a/core/models/MetricValue.cpp +++ b/core/models/MetricValue.cpp @@ -56,15 +56,15 @@ void UntypedMultiDoubleValues::DelValue(StringView key) { mValues.erase(key); } -std::map::const_iterator UntypedMultiDoubleValues::MultiKeyValusBegin() const { +std::map::const_iterator UntypedMultiDoubleValues::ValusBegin() const { return mValues.begin(); } -std::map::const_iterator UntypedMultiDoubleValues::MultiKeyValusEnd() const { +std::map::const_iterator UntypedMultiDoubleValues::ValusEnd() const { return mValues.end(); } -size_t UntypedMultiDoubleValues::MultiKeyValusSize() const { +size_t UntypedMultiDoubleValues::ValusSize() const { return mValues.size(); } diff --git a/core/models/MetricValue.h b/core/models/MetricValue.h index 67774c7e2f..b3caef108c 100644 --- a/core/models/MetricValue.h +++ b/core/models/MetricValue.h @@ -58,9 +58,9 @@ struct UntypedMultiDoubleValues { void SetValueNoCopy(StringView key, double val); void DelValue(StringView key); - std::map::const_iterator MultiKeyValusBegin() const; - std::map::const_iterator MultiKeyValusEnd() const; - size_t MultiKeyValusSize() const; + std::map::const_iterator ValusBegin() const; + std::map::const_iterator ValusEnd() const; + size_t ValusSize() const; size_t DataSize() const; void ResetPipelineEvent(PipelineEvent* ptr) { mMetricEventPtr = ptr; } diff --git a/core/runner/FlusherRunner.cpp b/core/runner/FlusherRunner.cpp index 1f9f808371..42a1b452fa 100644 --- a/core/runner/FlusherRunner.cpp +++ b/core/runner/FlusherRunner.cpp @@ -186,13 +186,13 @@ void FlusherRunner::Dispatch(SenderQueueItem* item) { if (!BOOL_FLAG(enable_full_drain_mode) && Application::GetInstance()->IsExiting() && item->mFlusher->Name() == "flusher_sls") { DiskBufferWriter::GetInstance()->PushToDiskBuffer(item, 3); - SenderQueueManager::GetInstance()->RemoveItem(item->mFlusher->GetQueueKey(), item); + SenderQueueManager::GetInstance()->RemoveItem(item->mQueueKey, item); } else { PushToHttpSink(item); } break; default: - SenderQueueManager::GetInstance()->RemoveItem(item->mFlusher->GetQueueKey(), item); + SenderQueueManager::GetInstance()->RemoveItem(item->mQueueKey, item); break; } } From 750f1551979ec8ba6bc3238656495ab44a5630c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=84=E9=A3=8F?= Date: Mon, 25 Nov 2024 11:51:40 +0000 Subject: [PATCH 8/8] polish --- core/models/MetricEvent.h | 8 ++++++++ core/unittest/models/MetricEventUnittest.cpp | 11 ++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/core/models/MetricEvent.h b/core/models/MetricEvent.h index 20cc8cb762..63ed412e14 100644 --- a/core/models/MetricEvent.h +++ b/core/models/MetricEvent.h @@ -63,6 +63,14 @@ class MetricEvent : public PipelineEvent { mValue = T{std::forward(args)...}; } + void SetValue(const std::map& multiDoubleValues) { + mValue = UntypedMultiDoubleValues{multiDoubleValues, this}; + } + + void SetValue(const UntypedMultiDoubleValues& multiDoubleValues) { + mValue = UntypedMultiDoubleValues{multiDoubleValues.mValues, this}; + } + StringView GetTag(StringView key) const; bool HasTag(StringView key) const; void SetTag(StringView key, StringView val); diff --git a/core/unittest/models/MetricEventUnittest.cpp b/core/unittest/models/MetricEventUnittest.cpp index 4735a65733..ada8af3239 100644 --- a/core/unittest/models/MetricEventUnittest.cpp +++ b/core/unittest/models/MetricEventUnittest.cpp @@ -67,7 +67,8 @@ void MetricEventUnittest::TestUntypedSingleValue() { } void MetricEventUnittest::TestUntypedMultiDoubleValues() { - mMetricEvent->SetValue(UntypedMultiDoubleValues{{{"test-1", 10.0}, {"test-2", 2.0}}, mMetricEvent.get()}); + UntypedMultiDoubleValues v({{"test-1", 10.0}, {"test-2", 2.0}}, nullptr); + mMetricEvent->SetValue(v); APSARA_TEST_TRUE(mMetricEvent->Is()); double val; APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->GetValue("test-1", val)); @@ -76,7 +77,7 @@ void MetricEventUnittest::TestUntypedMultiDoubleValues() { APSARA_TEST_EQUAL(2.0, val); map metrics({{"test-3", 15.0}, {"test-4", 24.0}}); - mMetricEvent->SetValue(metrics, mMetricEvent.get()); + mMetricEvent->SetValue(metrics); APSARA_TEST_TRUE(mMetricEvent->Is()); APSARA_TEST_EQUAL(true, mMetricEvent->GetValue()->GetValue("test-3", val)); APSARA_TEST_EQUAL(15.0, val); @@ -159,7 +160,7 @@ void MetricEventUnittest::TestUntypedSingleValueSize() { void MetricEventUnittest::TestUntypedMultiDoubleValuesSize() { mMetricEvent->SetName("test"); - mMetricEvent->SetValue(UntypedMultiDoubleValues{{}, mMetricEvent.get()}); + mMetricEvent->SetValue(map{}); size_t basicSize = sizeof(time_t) + sizeof(long) + sizeof(UntypedMultiDoubleValues) + sizeof(map); basicSize += 4; @@ -233,7 +234,7 @@ void MetricEventUnittest::TestUntypedSingleValueToJson() { void MetricEventUnittest::TestUntypedMultiDoubleValuesToJson() { mMetricEvent->SetTimestamp(12345678901, 0); mMetricEvent->SetName("test"); - mMetricEvent->SetValue(UntypedMultiDoubleValues{{{"test-1", 10.0}, {"test-2", 2.0}}, mMetricEvent.get()}); + mMetricEvent->SetValue(map{{"test-1", 10.0}, {"test-2", 2.0}}); mMetricEvent->SetTag(string("key1"), string("value1")); Json::Value res = mMetricEvent->ToJson(); @@ -344,7 +345,7 @@ void MetricEventUnittest::TestTagsIterator() { void MetricEventUnittest::TestCopy() { MetricEvent* oldMetricEvent = mEventGroup->AddMetricEvent(); - oldMetricEvent->SetValue(UntypedMultiDoubleValues{{{"test-1", 10.0}, {"test-2", 2.0}}, oldMetricEvent}); + oldMetricEvent->SetValue(map{{"test-1", 10.0}, {"test-2", 2.0}}); APSARA_TEST_EQUAL(1, mEventGroup->GetEvents().size()); PipelineEventGroup newGroup = mEventGroup->Copy();