Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: MetricEvent support UntypedMultiValues #1908

Merged
merged 9 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions core/models/MetricEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,21 @@ Json::Value MetricEvent::ToJson(bool enableEventMeta) const {
root["timestampNanosecond"] = static_cast<int32_t>(GetTimestampNanosecond().value());
}
root["name"] = mName.to_string();
root["value"] = MetricValueToJson(mValue);
root["value"] = Json::Value();
visit(
[&](auto&& arg) {
using T = decay_t<decltype(arg)>;
if constexpr (is_same_v<T, UntypedSingleValue>) {
root["value"]["type"] = "untyped_single_value";
root["value"]["detail"] = get<UntypedSingleValue>(mValue).ToJson();
} else if constexpr (is_same_v<T, UntypedMultiFloatValues>) {
root["value"]["type"] = "untyped_multi_values";
root["value"]["detail"] = get<UntypedMultiFloatValues>(mValue).ToJson();
} else if constexpr (is_same_v<T, monostate>) {
root["value"]["type"] = "unknown";
}
},
mValue);
if (!mTags.mInner.empty()) {
Json::Value& tags = root["tags"];
for (const auto& tag : mTags.mInner) {
Expand All @@ -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"]));
if (value["type"].asString() == "untyped_single_value") {
UntypedSingleValue v;
v.FromJson(value["detail"]);
SetValue(v);
} else if (value["type"].asString() == "untyped_multi_values") {
Takuka0311 marked this conversation as resolved.
Show resolved Hide resolved
UntypedMultiFloatValues v(this);
v.FromJson(value["detail"]);
SetValue(v);
}
if (root.isMember("tags")) {
Json::Value tags = root["tags"];
for (const auto& key : tags.getMemberNames()) {
Expand Down
84 changes: 65 additions & 19 deletions core/models/MetricValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,61 @@ using namespace std;

namespace logtail {

double UntypedMultiFloatValues::GetMultiKeyValue(StringView key) const {
if (mValues.find(key) != mValues.end()) {
return mValues.at(key);
}
return 0;
Takuka0311 marked this conversation as resolved.
Show resolved Hide resolved
}

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<StringView, double>::const_iterator UntypedMultiFloatValues::MultiKeyValusBegin() const {
return mValues.begin();
}

std::map<StringView, double>::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);
}
return totalSize;
}

size_t DataSize(const MetricValue& value) {
return visit(
[](auto&& arg) {
Expand All @@ -42,29 +97,20 @@ void UntypedSingleValue::FromJson(const Json::Value& value) {
mValue = value.asFloat();
}

Json::Value MetricValueToJson(const MetricValue& value) {
Json::Value UntypedMultiFloatValues::ToJson() const {
Json::Value res;
visit(
[&](auto&& arg) {
using T = decay_t<decltype(arg)>;
if constexpr (is_same_v<T, UntypedSingleValue>) {
res["type"] = "untyped_single_value";
res["detail"] = get<UntypedSingleValue>(value).ToJson();
} else if constexpr (is_same_v<T, monostate>) {
res["type"] = "unknown";
}
},
value);
for (auto metric : mValues) {
res[metric.first.to_string()] = metric.second;
}
return res;
}

MetricValue JsonToMetricValue(const string& type, const Json::Value& detail) {
if (type == "untyped_single_value") {
UntypedSingleValue v;
v.FromJson(detail);
return v;
} else {
return MetricValue();
void UntypedMultiFloatValues::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());
}
}
}
#endif
Expand Down
35 changes: 31 additions & 4 deletions core/models/MetricValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <map>
#include <variant>

#ifdef APSARA_UNIT_TEST_MAIN
Expand All @@ -24,6 +25,10 @@
#include <string>
#endif

#include "common/memory/SourceBuffer.h"
#include "models/PipelineEvent.h"
#include "models/StringView.h"

namespace logtail {

struct UntypedSingleValue {
Expand All @@ -37,13 +42,35 @@ struct UntypedSingleValue {
#endif
};

using MetricValue = std::variant<std::monostate, UntypedSingleValue>;
struct UntypedMultiFloatValues {
mutable std::map<StringView, double> mValues;
Takuka0311 marked this conversation as resolved.
Show resolved Hide resolved
PipelineEvent* mMetricEventPtr;
Takuka0311 marked this conversation as resolved.
Show resolved Hide resolved

size_t DataSize(const MetricValue& value);
UntypedMultiFloatValues(PipelineEvent* ptr): mMetricEventPtr(ptr) {}
UntypedMultiFloatValues(std::map<StringView, double> values, PipelineEvent* ptr): mValues(values), mMetricEventPtr(ptr) {}
Takuka0311 marked this conversation as resolved.
Show resolved Hide resolved

double GetMultiKeyValue(StringView key) const;
Takuka0311 marked this conversation as resolved.
Show resolved Hide resolved
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<StringView, double>::const_iterator MultiKeyValusBegin() const;
std::map<StringView, double>::const_iterator MultiKeyValusEnd() const;
size_t MultiKeyValusSize() const;

size_t DataSize() const;

#ifdef APSARA_UNIT_TEST_MAIN
Json::Value MetricValueToJson(const MetricValue& value);
MetricValue JsonToMetricValue(const std::string& type, const Json::Value& detail);
Json::Value ToJson() const;
void FromJson(const Json::Value& value);
#endif
};

using MetricValue = std::variant<std::monostate, UntypedSingleValue, UntypedMultiFloatValues>;

size_t DataSize(const MetricValue& value);

} // namespace logtail
Loading
Loading