Skip to content

Commit

Permalink
feat: add lc_target_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
catdogpandas committed Jan 14, 2025
1 parent dbdc8c6 commit 7eea950
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 15 deletions.
15 changes: 11 additions & 4 deletions core/plugin/processor/inner/ProcessorPromRelabelMetricNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
#include "plugin/processor/inner/ProcessorPromRelabelMetricNative.h"

#include <cstddef>

#include "json/json.h"
#include <json/json.h>

#include "common/Flags.h"
#include "common/StringTools.h"
Expand Down Expand Up @@ -186,18 +185,26 @@ void ProcessorPromRelabelMetricNative::UpdateAutoMetrics(const PipelineEventGrou

void ProcessorPromRelabelMetricNative::AddAutoMetrics(PipelineEventGroup& eGroup,
const prom::AutoMetric& autoMetric) const {
auto targetTags = eGroup.GetTags();
if (!eGroup.HasMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_TIMESTAMP_MILLISEC)) {
LOG_ERROR(sLogger, ("scrape_timestamp_milliseconds is not set", ""));
return;
}
auto targetTags = eGroup.GetTags();
auto toDelete = GetToDeleteTargetLabels(targetTags);
for (const auto& item : toDelete) {
targetTags.erase(item);
}
if (!eGroup.HasMetadata(EventGroupMetaKey::PROMETHEUS_STREAM_ID)) {
LOG_ERROR(sLogger, ("prometheus stream id", ""));
return;
}
targetTags[prometheus::LC_TARGET_HASH] = eGroup.GetMetadata(EventGroupMetaKey::PROMETHEUS_STREAM_ID);

StringView scrapeTimestampMilliSecStr = eGroup.GetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_TIMESTAMP_MILLISEC);
auto timestampMilliSec = StringTo<uint64_t>(scrapeTimestampMilliSecStr.to_string());
auto timestamp = timestampMilliSec / 1000;
auto nanoSec = timestampMilliSec % 1000 * 1000000;


AddMetric(
eGroup, prometheus::SCRAPE_DURATION_SECONDS, autoMetric.mScrapeDurationSeconds, timestamp, nanoSec, targetTags);

Expand Down
1 change: 1 addition & 0 deletions core/prometheus/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const uint64_t RefeshIntervalSeconds = 5;
const char* const META = "__meta_";
const char* const UNDEFINED = "undefined";
const std::string PROMETHEUS = "prometheus";
const char* const LC_TARGET_HASH = "lc_target_hash";

// relabel config
const char* const SOURCE_LABELS = "source_labels";
Expand Down
4 changes: 2 additions & 2 deletions core/prometheus/component/StreamScraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void StreamScraper::PushEventGroup(PipelineEventGroup&& eGroup) const {
void StreamScraper::SendMetrics() {
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_SCRAPE_TIMESTAMP_MILLISEC,
ToString(mScrapeTimestampMilliSec));
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_STREAM_ID, GetId() + ToString(mScrapeTimestampMilliSec));
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_STREAM_ID, GetId());

SetTargetLabels(mEventGroup);
PushEventGroup(std::move(mEventGroup));
Expand All @@ -127,7 +127,7 @@ void StreamScraper::SetAutoMetricMeta(double scrapeDurationSeconds, bool upState
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));
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_STREAM_ID, GetId() + ToString(mScrapeTimestampMilliSec));
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_STREAM_ID, GetId());
mEventGroup.SetMetadata(EventGroupMetaKey::PROMETHEUS_STREAM_TOTAL, ToString(mStreamIndex));
}
std::string StreamScraper::GetId() {
Expand Down
4 changes: 0 additions & 4 deletions core/prometheus/schedulers/ScrapeScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ string ScrapeScheduler::GetId() const {
return mTargetInfo.mHash;
}

string ScrapeScheduler::GetHashForOperator() const {
return mTargetInfo.mHashForOperator;
}

void ScrapeScheduler::SetComponent(shared_ptr<Timer> timer, EventPool* eventPool) {
mTimer = std::move(timer);
mEventPool = eventPool;
Expand Down
2 changes: 0 additions & 2 deletions core/prometheus/schedulers/ScrapeScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ struct PromTargetInfo {
Labels mLabels;
std::string mInstance;
std::string mHash;
std::string mHashForOperator;
uint64_t mRebalanceMs = 0;
};

Expand All @@ -55,7 +54,6 @@ class ScrapeScheduler : public BaseScheduler {
void OnMetricResult(HttpResponse&, uint64_t timestampMilliSec);

std::string GetId() const;
std::string GetHashForOperator() const;

void SetComponent(std::shared_ptr<Timer> timer, EventPool* eventPool);
uint64_t GetLastScrapeSize() const { return mScrapeResponseSizeBytes; }
Expand Down
5 changes: 2 additions & 3 deletions core/prometheus/schedulers/TargetSubscriberScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ bool TargetSubscriberScheduler::ParseScrapeSchedulerGroup(const std::string& con
rawHashStream << std::setw(16) << std::setfill('0') << std::hex << labels.Hash();
string rawAddress = labels.Get(prometheus::ADDRESS_LABEL_NAME);
targetInfo.mHash = mScrapeConfigPtr->mJobName + rawAddress + rawHashStream.str();
targetInfo.mHashForOperator = targetInfo.mHash;
targetInfo.mInstance = targets[0];

labels.Set(prometheus::JOB, mJobName);
Expand All @@ -208,7 +207,7 @@ bool TargetSubscriberScheduler::ParseScrapeSchedulerGroup(const std::string& con
targetInfo.mLabels = labels;

if (element.isMember(prometheus::TARGET_HASH) && element[prometheus::TARGET_HASH].isString()) {
targetInfo.mHashForOperator = element[prometheus::TARGET_HASH].asString();
targetInfo.mHash = element[prometheus::TARGET_HASH].asString();
}

if (element.isMember(prometheus::REBALANCE_MS) && element[prometheus::REBALANCE_MS].isUInt64()) {
Expand Down Expand Up @@ -363,7 +362,7 @@ string TargetSubscriberScheduler::TargetsInfoToString() const {
ReadLock lock(mRWLock);
for (const auto& [k, v] : mScrapeSchedulerMap) {
Json::Value targetInfo;
targetInfo[prometheus::HASH] = v->GetHashForOperator();
targetInfo[prometheus::HASH] = v->GetId();
targetInfo[prometheus::SIZE] = v->GetLastScrapeSize();
if (needToUpdate) {
mDelaySeconds += v->mExecDelayCount;
Expand Down

0 comments on commit 7eea950

Please sign in to comment.