Skip to content

fix: improve the judgment of the legitimacy of prometheus metrics #2228

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion core/prometheus/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ bool IsValidMetric(const StringView& line) {
if (c == ' ' || c == '\t') {
continue;
}
if (c == '#') {
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == ':')) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个判断条件有什么规范依据吗

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prometheus 指标名称规范

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return false;
}
return true;
Expand Down
10 changes: 9 additions & 1 deletion core/prometheus/component/StreamScraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ void StreamScraper::PushEventGroup(PipelineEventGroup&& eGroup) const {
}
}

void StreamScraper::ClearEventGroup() {
mEventGroup.MutableEvents().clear();
}

void StreamScraper::SendMetrics() {
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_TIMESTAMP_MILLISEC,
ToString(mScrapeTimestampMilliSec));
Expand All @@ -150,7 +154,11 @@ void StreamScraper::SetAutoMetricMeta(double scrapeDurationSeconds, bool upState
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_STATE, scrapeState);
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_TIMESTAMP_MILLISEC,
ToString(mScrapeTimestampMilliSec));
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SAMPLES_SCRAPED, ToString(mScrapeSamplesScraped));
if (upState) {
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SAMPLES_SCRAPED, ToString(mScrapeSamplesScraped));
} else {
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SAMPLES_SCRAPED, ToString(0));
}
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_DURATION, ToString(scrapeDurationSeconds));
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_RESPONSE_SIZE, ToString(mRawSize));
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_UP_STATE, ToString(upState));
Expand Down
1 change: 1 addition & 0 deletions core/prometheus/component/StreamScraper.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class StreamScraper {
void SendMetrics();
void Reset();
void SetAutoMetricMeta(double scrapeDurationSeconds, bool upState, const std::string& scrapeState);
void ClearEventGroup();

size_t mRawSize = 0;
static size_t mMaxSampleLength;
Expand Down
2 changes: 2 additions & 0 deletions core/prometheus/schedulers/ScrapeScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ void ScrapeScheduler::OnMetricResult(HttpResponse& response, uint64_t) {
streamScraper->mStreamIndex++;
if (upState) {
streamScraper->FlushCache();
} else {
streamScraper->ClearEventGroup();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为啥clear? clear之后,SendMetrics,里面不都是空数据吗

}
streamScraper->SetAutoMetricMeta(scrapeDurationSeconds, upState, scrapeState);
streamScraper->SendMetrics();
Expand Down
Loading