@@ -71,7 +71,12 @@ void ProcessorPromRelabelMetricNative::Process(PipelineEventGroup& metricGroup)
71
71
metricGroup.DelTag (k);
72
72
}
73
73
74
- AddAutoMetrics (metricGroup);
74
+ if (metricGroup.HasMetadata (EventGroupMetaKey::PROMETHEUS_STREAM_TOTAL)) {
75
+ auto autoMetric = prom::AutoMetric ();
76
+ UpdateAutoMetrics (metricGroup, autoMetric);
77
+ AddAutoMetrics (metricGroup, autoMetric);
78
+ }
79
+
75
80
76
81
// delete all tags
77
82
for (const auto & [k, v] : targetTags) {
@@ -138,78 +143,89 @@ vector<StringView> ProcessorPromRelabelMetricNative::GetToDeleteTargetLabels(con
138
143
return toDelete;
139
144
}
140
145
141
- void ProcessorPromRelabelMetricNative::AddAutoMetrics (PipelineEventGroup& metricGroup) {
142
- // if up is set, then add self monitor metrics
143
- if (metricGroup.GetMetadata (EventGroupMetaKey::PROMETHEUS_UP_STATE).empty ()) {
144
- return ;
146
+ void ProcessorPromRelabelMetricNative::UpdateAutoMetrics (const PipelineEventGroup& eGroup,
147
+ prom::AutoMetric& autoMetric) const {
148
+ if (eGroup.HasMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_DURATION)) {
149
+ autoMetric.mScrapeDurationSeconds
150
+ = StringTo<double >(eGroup.GetMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_DURATION).to_string ());
145
151
}
152
+ if (eGroup.HasMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_RESPONSE_SIZE)) {
153
+ autoMetric.mScrapeResponseSizeBytes
154
+ = StringTo<uint64_t >(eGroup.GetMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_RESPONSE_SIZE).to_string ());
155
+ }
156
+ autoMetric.mScrapeSamplesLimit = mScrapeConfigPtr ->mSampleLimit ;
157
+ if (eGroup.HasMetadata (EventGroupMetaKey::PROMETHEUS_SAMPLES_SCRAPED)) {
158
+ autoMetric.mScrapeSamplesScraped
159
+ = StringTo<uint64_t >(eGroup.GetMetadata (EventGroupMetaKey::PROMETHEUS_SAMPLES_SCRAPED).to_string ());
160
+ }
161
+ autoMetric.mScrapeTimeoutSeconds = mScrapeConfigPtr ->mScrapeTimeoutSeconds ;
146
162
147
- auto targetTags = metricGroup.GetTags ();
163
+ if (eGroup.HasMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_STATE)) {
164
+ autoMetric.mScrapeState = eGroup.GetMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_STATE).to_string ();
165
+ }
166
+
167
+ if (eGroup.HasMetadata (EventGroupMetaKey::PROMETHEUS_UP_STATE)) {
168
+ autoMetric.mUp = StringTo<bool >(eGroup.GetMetadata (EventGroupMetaKey::PROMETHEUS_UP_STATE).to_string ());
169
+ }
170
+ }
171
+
172
+ void ProcessorPromRelabelMetricNative::AddAutoMetrics (PipelineEventGroup& eGroup,
173
+ const prom::AutoMetric& autoMetric) const {
174
+ auto targetTags = eGroup.GetTags ();
175
+ if (!eGroup.HasMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_TIMESTAMP_MILLISEC)) {
176
+ LOG_ERROR (sLogger , (" scrape_timestamp_milliseconds is not set" , " " ));
177
+ return ;
178
+ }
148
179
149
- StringView scrapeTimestampMilliSecStr
150
- = metricGroup.GetMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_TIMESTAMP_MILLISEC);
180
+ StringView scrapeTimestampMilliSecStr = eGroup.GetMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_TIMESTAMP_MILLISEC);
151
181
auto timestampMilliSec = StringTo<uint64_t >(scrapeTimestampMilliSecStr.to_string ());
152
182
auto timestamp = timestampMilliSec / 1000 ;
153
183
auto nanoSec = timestampMilliSec % 1000 * 1000000 ;
154
184
155
- uint64_t samplesPostMetricRelabel = metricGroup.GetEvents ().size ();
156
-
157
- auto scrapeDurationSeconds
158
- = StringTo<double >(metricGroup.GetMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_DURATION).to_string ());
159
-
160
- AddMetric (metricGroup, prometheus::SCRAPE_DURATION_SECONDS, scrapeDurationSeconds, timestamp, nanoSec, targetTags);
161
185
162
- auto scrapeResponseSize
163
- = StringTo<uint64_t >(metricGroup.GetMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_RESPONSE_SIZE).to_string ());
164
- AddMetric (metricGroup, prometheus::SCRAPE_RESPONSE_SIZE_BYTES, scrapeResponseSize, timestamp, nanoSec, targetTags);
186
+ AddMetric (
187
+ eGroup, prometheus::SCRAPE_DURATION_SECONDS, autoMetric.mScrapeDurationSeconds , timestamp, nanoSec, targetTags);
165
188
166
- if (mScrapeConfigPtr ->mSampleLimit > 0 ) {
167
- AddMetric (metricGroup,
168
- prometheus::SCRAPE_SAMPLES_LIMIT,
169
- mScrapeConfigPtr ->mSampleLimit ,
170
- timestamp,
171
- nanoSec,
172
- targetTags);
173
- }
174
-
175
- AddMetric (metricGroup,
176
- prometheus::SCRAPE_SAMPLES_POST_METRIC_RELABELING,
177
- samplesPostMetricRelabel,
189
+ AddMetric (eGroup,
190
+ prometheus::SCRAPE_RESPONSE_SIZE_BYTES,
191
+ autoMetric.mScrapeResponseSizeBytes ,
178
192
timestamp,
179
193
nanoSec,
180
194
targetTags);
181
195
182
- auto samplesScraped
183
- = StringTo<uint64_t >(metricGroup.GetMetadata (EventGroupMetaKey::PROMETHEUS_SAMPLES_SCRAPED).to_string ());
196
+ if (autoMetric.mScrapeSamplesLimit > 0 ) {
197
+ AddMetric (
198
+ eGroup, prometheus::SCRAPE_SAMPLES_LIMIT, autoMetric.mScrapeSamplesLimit , timestamp, nanoSec, targetTags);
199
+ }
184
200
185
- AddMetric (metricGroup, prometheus::SCRAPE_SAMPLES_SCRAPED, samplesScraped, timestamp, nanoSec, targetTags);
201
+ // AddMetric(eGroup,
202
+ // prometheus::SCRAPE_SAMPLES_POST_METRIC_RELABELING,
203
+ // autoMetric.mPostRelabel,
204
+ // timestamp,
205
+ // nanoSec,
206
+ // targetTags);
186
207
187
- AddMetric (metricGroup,
188
- prometheus::SCRAPE_TIMEOUT_SECONDS,
189
- mScrapeConfigPtr ->mScrapeTimeoutSeconds ,
190
- timestamp,
191
- nanoSec,
192
- targetTags);
208
+ AddMetric (
209
+ eGroup, prometheus::SCRAPE_SAMPLES_SCRAPED, autoMetric.mScrapeSamplesScraped , timestamp, nanoSec, targetTags);
193
210
194
- // up metric must be the last one
195
- bool upState = StringTo< bool >(metricGroup. GetMetadata (EventGroupMetaKey::PROMETHEUS_UP_STATE). to_string () );
211
+ AddMetric (
212
+ eGroup, prometheus::SCRAPE_TIMEOUT_SECONDS, autoMetric. mScrapeTimeoutSeconds , timestamp, nanoSec, targetTags );
196
213
197
- if (metricGroup.HasMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_STATE)) {
198
- auto scrapeState = metricGroup.GetMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_STATE);
199
- AddMetric (metricGroup, prometheus::SCRAPE_STATE, 1.0 * upState, timestamp, nanoSec, targetTags);
200
- auto & last = metricGroup.MutableEvents ()[metricGroup.GetEvents ().size () - 1 ];
201
- last.Cast <MetricEvent>().SetTag (METRIC_LABEL_KEY_STATUS, scrapeState);
202
- }
214
+ AddMetric (eGroup, prometheus::SCRAPE_STATE, 1.0 * autoMetric.mUp , timestamp, nanoSec, targetTags);
215
+ auto & last = eGroup.MutableEvents ()[eGroup.GetEvents ().size () - 1 ];
216
+ auto scrapeState = eGroup.GetMetadata (EventGroupMetaKey::PROMETHEUS_SCRAPE_STATE);
217
+ last.Cast <MetricEvent>().SetTag (METRIC_LABEL_KEY_STATUS, scrapeState);
203
218
204
- AddMetric (metricGroup, prometheus::UP, 1.0 * upState, timestamp, nanoSec, targetTags);
219
+ // up metric must be the last one
220
+ AddMetric (eGroup, prometheus::UP, 1.0 * autoMetric.mUp , timestamp, nanoSec, targetTags);
205
221
}
206
222
207
223
void ProcessorPromRelabelMetricNative::AddMetric (PipelineEventGroup& metricGroup,
208
224
const string& name,
209
225
double value,
210
226
time_t timestamp,
211
227
uint32_t nanoSec,
212
- const GroupTags& targetTags) {
228
+ const GroupTags& targetTags) const {
213
229
auto * metricEvent = metricGroup.AddMetricEvent (true );
214
230
metricEvent->SetName (name);
215
231
metricEvent->SetValue <UntypedSingleValue>(value);
0 commit comments