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

add metric new #1015

Merged
merged 43 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
8be811c
add metric
linrunqi08 Jul 14, 2023
9132164
refine code
linrunqi08 Jul 17, 2023
7ee4a51
refine code
linrunqi08 Jul 17, 2023
f3c5677
refine code
linrunqi08 Jul 17, 2023
356047e
refine code
linrunqi08 Jul 17, 2023
4e1e329
refine code
linrunqi08 Jul 17, 2023
0a60735
add exportor
linrunqi08 Jul 24, 2023
0dabb6d
add exportor
linrunqi08 Jul 24, 2023
c712ff2
refine code and test
linrunqi08 Jul 24, 2023
a9e5c2f
refine code
linrunqi08 Jul 24, 2023
a9fd256
fix comment
linrunqi08 Jul 31, 2023
378b8df
fix comment
linrunqi08 Jul 31, 2023
c5b4c60
fix raw ptr
linrunqi08 Jul 31, 2023
7b934c3
fix comment
linrunqi08 Aug 2, 2023
a98305c
Merge branch 'main' of https://github.com/alibaba/ilogtail into featu…
linrunqi08 Aug 2, 2023
e6c8a8e
add change log
linrunqi08 Aug 2, 2023
2d21c4e
add group by region
linrunqi08 Aug 7, 2023
54d1fae
fix comments
linrunqi08 Aug 15, 2023
404b1af
Merge branch 'main' of https://github.com/alibaba/ilogtail into featu…
linrunqi08 Aug 15, 2023
baf9989
fix cooment
linrunqi08 Aug 17, 2023
3d55184
fix cooment
linrunqi08 Aug 17, 2023
ff73c03
fix comments
linrunqi08 Aug 17, 2023
3a25ef5
Merge branch 'main' of https://github.com/alibaba/ilogtail into featu…
linrunqi08 Aug 17, 2023
e22d5aa
refine code
linrunqi08 Aug 21, 2023
7a725d8
refine code
linrunqi08 Aug 21, 2023
6cc915b
Merge branch 'main' of https://github.com/alibaba/ilogtail into featu…
linrunqi08 Aug 21, 2023
b0892d8
refine code
linrunqi08 Aug 21, 2023
2fb349f
refine code
linrunqi08 Aug 22, 2023
ad2c7ee
refine code
linrunqi08 Aug 22, 2023
6944fce
refine code
linrunqi08 Aug 23, 2023
ea21800
refine code
linrunqi08 Aug 23, 2023
6ca91dc
refine code
linrunqi08 Aug 23, 2023
17b5be6
refine code
linrunqi08 Aug 23, 2023
b3f437e
refine code
linrunqi08 Aug 23, 2023
9db4193
refine code
linrunqi08 Aug 23, 2023
f34a75f
refine code
linrunqi08 Aug 23, 2023
2485093
refine coder
linrunqi08 Aug 23, 2023
bc44e7a
refine coder
linrunqi08 Aug 23, 2023
de2f991
refine code
linrunqi08 Aug 23, 2023
6316ad5
refine code
linrunqi08 Aug 23, 2023
4ae61bb
refine code
linrunqi08 Aug 23, 2023
e7eaad0
refine code
linrunqi08 Aug 23, 2023
dee1a1e
refine code
linrunqi08 Aug 24, 2023
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
198 changes: 105 additions & 93 deletions core/monitor/LogtaiMetric.cpp
linrunqi08 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ using namespace sls_logs;
namespace logtail {


const uint64_t BaseMetric::GetValue() const {
const uint64_t MetricNameValue::GetValue() const {
return mVal;
}

const std::string& BaseMetric::GetName() const {
const std::string& MetricNameValue::GetName() const {
return mName;
}

Counter::Counter(const std::string &name, uint64_t val = 0) : BaseMetric(name, val) {}
Counter::Counter(const std::string& name, uint64_t val = 0) : MetricNameValue(name, val) {
}

Counter* Counter::CopyAndReset() {
return new Counter(mName, mVal.exchange(0));
Expand All @@ -29,7 +30,8 @@ void Counter::SetValue(uint64_t value) {
mVal += value;
}

Gauge::Gauge(const std::string &name, uint64_t val = 0) : BaseMetric(name, val) {}
Gauge::Gauge(const std::string& name, uint64_t val = 0) : MetricNameValue(name, val) {
}

Gauge* Gauge::CopyAndReset() {
return new Gauge(mName, mVal.exchange(0));
Expand All @@ -39,106 +41,117 @@ void Gauge::SetValue(uint64_t value) {
mVal = value;
}

MetricsRecord::MetricsRecord(LabelsPtr labels) : mLabels(labels), mDeleted(false) {
}

Metrics::Metrics(const std::vector<std::pair<std::string, std::string>>&& labels) : mLabels(labels), mDeleted(false) {}

Metrics::Metrics() : mDeleted(false) {}
MetricsRecord::MetricsRecord() : mDeleted(false) {
}

MetricPtr Metrics::CreateCounter(const std::string& name) {
MetricPtr counterPtr = std::make_shared<Counter>(name);
MetricNameValuePtr MetricsRecord::CreateCounter(const std::string& name) {
MetricNameValuePtr counterPtr = std::make_shared<Counter>(name);
mValues.emplace_back(counterPtr);
return counterPtr;
}

MetricPtr Metrics::CreateGauge(const std::string& name) {
MetricPtr gaugePtr = std::make_shared<Gauge>(name);
MetricNameValuePtr MetricsRecord::CreateGauge(const std::string& name) {
MetricNameValuePtr gaugePtr = std::make_shared<Gauge>(name);
mValues.emplace_back(gaugePtr);
return gaugePtr;
}

void Metrics::MarkDeleted() {
void MetricsRecord::MarkDeleted() {
mDeleted = true;
}

bool Metrics::IsDeleted() {
const bool MetricsRecord::IsDeleted() const {
linrunqi08 marked this conversation as resolved.
Show resolved Hide resolved
return mDeleted;
}

const std::vector<std::pair<std::string, std::string>>& Metrics::GetLabels() const {
const LabelsPtr& MetricsRecord::GetLabels() const {
return mLabels;
}

const std::vector<MetricPtr>& Metrics::GetValues() const {
const std::vector<MetricNameValuePtr>& MetricsRecord::GetMetricNameValues() const {
return mValues;
}

Metrics* Metrics::CopyAndReset() {
std::vector<std::pair<std::string, std::string>> newLabels(mLabels);
Metrics* metrics = new Metrics(std::move(newLabels));
for (auto &item: mValues) {
MetricPtr newPtr(item->CopyAndReset());
MetricsRecord* MetricsRecord::CopyAndReset() {
MetricsRecord* metrics = new MetricsRecord(mLabels);
for (auto& item : mValues) {
MetricNameValuePtr newPtr(item->CopyAndReset());
metrics->mValues.emplace_back(newPtr);
}
return metrics;
}

Metrics* Metrics::GetNext() {
MetricsRecord* MetricsRecord::GetNext() const {
return mNext;
}

void Metrics::SetNext(Metrics* next) {
mNext = next;
void MetricsRecord::SetNext(MetricsRecord* next) {
mNext = next;
}

MetricsRef::MetricsRef() {}
MetricsRecordRef::MetricsRecordRef() {
}
linrunqi08 marked this conversation as resolved.
Show resolved Hide resolved

MetricsRef::~MetricsRef() {
MetricsRecordRef::~MetricsRecordRef() {
if (mMetrics) {
mMetrics->MarkDeleted();
}
}

void MetricsRef::Init(const std::vector<std::pair<std::string, std::string>>& Labels) {
void MetricsRecordRef::Init(const std::vector<std::pair<std::string, std::string>>& labels) {
if (!mMetrics) {
mMetrics = WriteMetrics::GetInstance()->CreateMetrics(std::move(Labels));
mMetrics = WriteMetrics::GetInstance()->CreateMetricsRecords(
std::make_shared<std::vector<std::pair<std::string, std::string>>>(labels));
linrunqi08 marked this conversation as resolved.
Show resolved Hide resolved
linrunqi08 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Metrics* MetricsRef::Get() {
MetricsRecord* MetricsRecordRef::operator->() const {
linrunqi08 marked this conversation as resolved.
Show resolved Hide resolved
return mMetrics;
}

WriteMetrics::WriteMetrics() {}
WriteMetrics::WriteMetrics() {
}
linrunqi08 marked this conversation as resolved.
Show resolved Hide resolved

Metrics* WriteMetrics::CreateMetrics(const std::vector<std::pair<std::string, std::string>>& labels) {
Metrics* cur = new Metrics(std::move(labels));
std::lock_guard<std::mutex> lock(mMutex);
WriteMetrics::~WriteMetrics() {
Clear();
}

Metrics* oldHead = mHead;
MetricsRecord* WriteMetrics::CreateMetricsRecords(LabelsPtr labels) {
MetricsRecord* cur = new MetricsRecord(labels);
std::lock_guard<std::mutex> lock(mMutex);
cur->SetNext(mHead);
mHead = cur;
mHead->SetNext(oldHead);

return cur;
}

Metrics* WriteMetrics::GetHead() {
MetricsRecord* WriteMetrics::GetHead() const {
return mHead;
}

Metrics* WriteMetrics::DoSnapshot() {
void WriteMetrics::Clear() {
while (mHead) {
linrunqi08 marked this conversation as resolved.
Show resolved Hide resolved
MetricsRecord* toDeleted = mHead;
mHead = mHead->GetNext();
delete toDeleted;
}
}

MetricsRecord* WriteMetrics::DoSnapshot() {
// new read head
Metrics* snapshot = nullptr;
Metrics* toDeleteHead = nullptr;
Metrics* tmp = nullptr;

Metrics emptyHead;
Metrics* preTmp = nullptr;

Metrics* tmpHead = nullptr;
Metrics* tmpHeadNext = nullptr;
// find the first not deleted node and set as new mHead
MetricsRecord* snapshot = nullptr;
MetricsRecord* toDeleteHead = nullptr;
MetricsRecord* tmp = nullptr;

MetricsRecord emptyHead;
MetricsRecord* preTmp = nullptr;

MetricsRecord* tmpHead = nullptr;
// find the first undeleted node and set as new mHead
{
std::lock_guard<std::mutex> lock(mMutex);
std::lock_guard<std::mutex> lock(mMutex);
emptyHead.SetNext(mHead);
preTmp = &emptyHead;
tmp = preTmp->GetNext();
Expand All @@ -151,40 +164,23 @@ Metrics* WriteMetrics::DoSnapshot() {
tmp = preTmp->GetNext();
} else {
// find head
if (!findHead) {
mHead = tmp;
preTmp = tmp;
tmp = tmp->GetNext();

mHead->SetNext(nullptr);
findHead = true;
// find head next
} else {
mHead->SetNext(tmp);
preTmp = tmp;
tmp = tmp->GetNext();
break;
}
mHead = tmp;
preTmp = tmp;
tmp = tmp->GetNext();
findHead = true;
break;
}
}
// if no undeleted node, set null to mHead
if (!findHead) {
mHead = nullptr;
} else {
tmpHead = mHead;
tmpHeadNext = mHead->GetNext();
}
tmpHead = mHead;
linrunqi08 marked this conversation as resolved.
Show resolved Hide resolved
}

// copy head
if (tmpHead) {
Metrics* newMetrics = tmpHead->CopyAndReset();
newMetrics->SetNext(snapshot);
snapshot = newMetrics;
}
// copy head next
if (tmpHeadNext) {
Metrics* newMetrics = tmpHead->CopyAndReset();
MetricsRecord* newMetrics = tmpHead->CopyAndReset();
newMetrics->SetNext(snapshot);
snapshot = newMetrics;
}
Expand All @@ -196,31 +192,37 @@ Metrics* WriteMetrics::DoSnapshot() {
toDeleteHead = tmp;
tmp = preTmp->GetNext();
} else {
Metrics* newMetrics = tmp->CopyAndReset();
MetricsRecord* newMetrics = tmp->CopyAndReset();
newMetrics->SetNext(snapshot);
snapshot = newMetrics;
preTmp = tmp;
tmp = tmp->GetNext();
}
}

while(toDeleteHead) {
Metrics* toDeleted = toDeleteHead;
while (toDeleteHead) {
MetricsRecord* toDelete = toDeleteHead;
toDeleteHead = toDeleteHead->GetNext();
delete toDeleted;
delete toDelete;
}
return snapshot;
}

ReadMetrics::ReadMetrics() {}
ReadMetrics::ReadMetrics() {
}

ReadMetrics::~ReadMetrics() {
Clear();
}

void ReadMetrics::ReadAsLogGroup(std::map<std::string, sls_logs::LogGroup*>& logGroupMap) {
void ReadMetrics::ReadAsLogGroup(std::map<std::string, sls_logs::LogGroup*>& logGroupMap) const {
ReadLock lock(mReadWriteLock);
Metrics* tmp = mHead;
while(tmp) {
MetricsRecord* tmp = mHead;
while (tmp) {
Log* logPtr = nullptr;
for (auto &item: tmp->GetLabels()) {
std::pair<std::string, std::string> pair = item;
// for (auto &item: tmp->GetLabels()) {
for (auto item = tmp->GetLabels()->begin(); item != tmp->GetLabels()->end(); ++item) {
std::pair<std::string, std::string> pair = *item;
if (METRIC_FIELD_REGION == pair.first) {
std::map<std::string, sls_logs::LogGroup*>::iterator iter;
std::string region = pair.second;
Expand Down Expand Up @@ -248,16 +250,18 @@ void ReadMetrics::ReadAsLogGroup(std::map<std::string, sls_logs::LogGroup*>& log
}
}
auto now = GetCurrentLogtailTime();
SetLogTime(logPtr, AppConfig::GetInstance()->EnableLogTimeAutoAdjust() ? now.tv_sec + GetTimeDelta() : now.tv_sec, now.tv_nsec);
for (auto &item: tmp->GetLabels()) {
std::pair<std::string, std::string> pair = item;
SetLogTime(logPtr,
AppConfig::GetInstance()->EnableLogTimeAutoAdjust() ? now.tv_sec + GetTimeDelta() : now.tv_sec,
now.tv_nsec);
for (auto item = tmp->GetLabels()->begin(); item != tmp->GetLabels()->end(); ++item) {
std::pair<std::string, std::string> pair = *item;
Log_Content* contentPtr = logPtr->add_contents();
contentPtr->set_key(LABEL_PREFIX + pair.first);
contentPtr->set_value(pair.second);
}

for (auto &item: tmp->GetValues()) {
MetricPtr counter = item;
for (auto& item : tmp->GetMetricNameValues()) {
MetricNameValuePtr counter = item;
Log_Content* contentPtr = logPtr->add_contents();
contentPtr->set_key(VALUE_PREFIX + counter->GetName());
contentPtr->set_value(ToString(counter->GetValue()));
Expand All @@ -274,24 +278,32 @@ void ReadMetrics::ReadAsLogGroup(std::map<std::string, sls_logs::LogGroup*>& log


void ReadMetrics::UpdateMetrics() {
Metrics* snapshot = WriteMetrics::GetInstance()->DoSnapshot();
Metrics* toDelete;
MetricsRecord* snapshot = WriteMetrics::GetInstance()->DoSnapshot();
MetricsRecord* toDelete;
{
// Only lock when change head
WriteLock lock(mReadWriteLock);
toDelete = mHead;
mHead = snapshot;
}
// delete old linklist
while(toDelete) {
Metrics* obj = toDelete;
while (toDelete) {
MetricsRecord* obj = toDelete;
toDelete = toDelete->GetNext();
delete obj;
}
}

Metrics* ReadMetrics::GetHead() {
MetricsRecord* ReadMetrics::GetHead() const {
linrunqi08 marked this conversation as resolved.
Show resolved Hide resolved
return mHead;
}

}
void ReadMetrics::Clear() {
while (mHead) {
MetricsRecord* toDelete = mHead;
mHead = mHead->GetNext();
delete toDelete;
}
}

} // namespace logtail
Loading
Loading