@@ -167,15 +167,9 @@ class TLabelsMaintainer : public TActorBootstrapped<TLabelsMaintainer> {
167167 }
168168
169169 void RemoveLabels (const TActorContext &ctx)
170- {
171- RemoveDatabaseLabels (ctx);
172- RemoveAttributeLabels (ctx);
173- }
174-
175- void RemoveDatabaseLabels (const TActorContext &ctx)
176170 {
177171 auto root = AppData (ctx)->Counters ;
178- for (auto &service : DatabaseSensorServices ) {
172+ for (auto &service : AllSensorServices ) {
179173 LOG_DEBUG_S (ctx, NKikimrServices::LABELS_MAINTAINER,
180174 " Removing database labels from " << service << " counters" );
181175
@@ -192,97 +186,99 @@ class TLabelsMaintainer : public TActorBootstrapped<TLabelsMaintainer> {
192186 }
193187 }
194188
195- void RemoveAttributeLabels (const TActorContext &ctx)
189+ void AddLabels (const TActorContext &ctx)
196190 {
197- auto root = AppData (ctx)->Counters ;
198- for (auto &service : DatabaseAttributeSensorServices) {
199- LOG_DEBUG_S (ctx, NKikimrServices::LABELS_MAINTAINER,
200- " Removing database attribute labels from " << service << " counters" );
191+ // NOTE: order of labels should match skip order in GetServiceCounters
192+ TSmallVec<std::pair<TString, TString>> dbLabels;
193+ TSmallVec<std::pair<TString, TString>> attrLabels;
201194
202- ReplaceSubgroup (root, service);
195+ if (DatabaseLabelsEnabled && CurrentDatabaseLabel) {
196+ if (GroupAllMetrics) {
197+ dbLabels.push_back ({DATABASE_LABEL, " " });
198+ } else {
199+ dbLabels.push_back ({DATABASE_LABEL, CurrentDatabaseLabel});
200+ }
201+
202+ dbLabels.push_back ({SLOT_LABEL, " static" });
203+ if (!CurrentHostLabel.empty ()) {
204+ dbLabels.push_back ({HOST_LABEL, CurrentHostLabel});
205+ }
203206 }
204- }
205207
206- void AddLabels (const TActorContext &ctx)
207- {
208- AddDatabaseLabels (ctx);
209- AddAttributeLabels (ctx);
208+ if (DatabaseAttributeLabelsEnabled) {
209+ for (auto &attr : GetDatabaseAttributeLabels ()) {
210+ if (CurrentAttributes.contains (attr)) {
211+ attrLabels.push_back (*CurrentAttributes.find (attr));
212+ }
213+ }
214+ }
215+
216+ if (!dbLabels.empty () || !attrLabels.empty ()) {
217+ AddLabelsToServices (ctx, AllSensorServices, dbLabels, attrLabels);
218+ }
210219 }
211220
212- void AddDatabaseLabels (const TActorContext &ctx)
221+ void AddLabelsToServices (const TActorContext& ctx,
222+ const THashSet<TString>& services,
223+ const TSmallVec<std::pair<TString, TString>>& dbLabels,
224+ const TSmallVec<std::pair<TString, TString>>& attrLabels)
213225 {
214- if (!DatabaseLabelsEnabled)
215- return ;
216-
217- if (!CurrentDatabaseLabel)
218- return ;
219-
220226 auto root = AppData (ctx)->Counters ;
221- TSmallVec<std::pair<TString, TString>> labels;
222- if (GroupAllMetrics) {
223- labels. push_back ({DATABASE_LABEL, " " } );
224- } else {
225- labels. push_back ({DATABASE_LABEL, CurrentDatabaseLabel}) ;
226- }
227+ for ( const auto & service : services) {
228+ bool needDbLabels = DatabaseSensorServices. contains (service) && !dbLabels. empty ();
229+ bool needAttrLabels = DatabaseAttributeSensorServices. contains (service) && !attrLabels. empty ( );
230+ if (!needDbLabels && !needAttrLabels) {
231+ continue ;
232+ }
227233
228- labels.push_back ({SLOT_LABEL, " static" });
229- if (!CurrentHostLabel.empty ()) {
230- labels.push_back ({" host" , CurrentHostLabel});
231- }
234+ const auto [svc, subSvc] = ExtractSubServiceName (service);
232235
233- AddLabelsToServices (ctx, labels, DatabaseSensorServices);
234- }
236+ // Find current subgroup and corresponding root and label
237+ auto serviceRoot = root;
238+ std::pair<TString, TString> serviceLabel = { " counters" , svc };
239+ auto oldGroup = serviceRoot->GetSubgroup (serviceLabel.first , serviceLabel.second );
240+ if (!subSvc.empty ()) {
241+ serviceRoot = oldGroup;
242+ serviceLabel = { " subsystem" , subSvc };
243+ oldGroup = serviceRoot->GetSubgroup (serviceLabel.first , serviceLabel.second );
244+ }
235245
236- void AddLabelsToServices (const TActorContext& ctx, const TSmallVec<std::pair<TString, TString>> &labels, const THashSet<TString> &services) {
237- if (!labels.empty ()) {
238- auto root = AppData (ctx)->Counters ;
239- for (auto &service: services) {
240- LOG_DEBUG_S (ctx, NKikimrServices::LABELS_MAINTAINER,
241- " Add labels to service " << service << " counters"
242- << " labels=" << PrintLabels (labels));
243- const auto &[svc, subSvc] = ExtractSubServiceName (service);
244- auto oldGroup = root->GetSubgroup (" counters" , svc);
245- if (!subSvc.empty ())
246- oldGroup = oldGroup->GetSubgroup (" subsystem" , subSvc);
247- TIntrusivePtr<::NMonitoring::TDynamicCounters> serviceGroup = new ::NMonitoring::TDynamicCounters;
248- TIntrusivePtr<::NMonitoring::TDynamicCounters> curGroup = serviceGroup;
249-
250- const auto * actualLabels = &labels;
251-
252- TSmallVec<std::pair<TString, TString>> ydbLabels;
253- if (DatabaseAttributeSensorServices.contains (service)) {
254- // explicitly remove "slot" label for external services ("ydb")
255- ydbLabels = labels;
256- if (auto it = std::find_if (ydbLabels.begin (), ydbLabels.end (), [](auto & el){ return el.first == SLOT_LABEL; });
257- it != ydbLabels.end ())
258- {
259- ydbLabels.erase (it);
260- actualLabels = &ydbLabels;
261- }
246+ TIntrusivePtr<::NMonitoring::TDynamicCounters> newGroup = new ::NMonitoring::TDynamicCounters;
247+ TIntrusivePtr<::NMonitoring::TDynamicCounters> curGroup = newGroup;
248+
249+ std::pair<TString, TString> lastLabel;
250+ auto processLabel = [&](const auto & label) {
251+ // Explicitly remove "slot" label for external services ("ydb")
252+ if (DatabaseAttributeSensorServices.contains (service) && label.first == SLOT_LABEL) {
253+ return ;
262254 }
263255
264- for ( size_t i = 0 ; i < actualLabels-> size () - 1 ; ++i ) {
265- curGroup = curGroup->GetSubgroup ((*actualLabels)[i] .first , (*actualLabels)[i] .second );
256+ if (!lastLabel. first . empty () ) {
257+ curGroup = curGroup->GetSubgroup (lastLabel .first , lastLabel .second );
266258 }
267- curGroup->RegisterSubgroup (actualLabels->back ().first , actualLabels->back ().second , oldGroup);
259+ lastLabel = label;
260+ };
268261
269- auto rt = GetServiceCountersRoot (root, service);
270- rt->ReplaceSubgroup (subSvc.empty () ? " counters" : " subsystem" , subSvc.empty () ? svc : subSvc, serviceGroup);
262+ if (needDbLabels) {
263+ for (const auto & label : dbLabels) {
264+ processLabel (label);
265+ }
271266 }
272- }
273- }
274267
275- void AddAttributeLabels (const TActorContext &ctx)
276- {
277- if (!DatabaseAttributeLabelsEnabled)
278- return ;
268+ if (needAttrLabels) {
269+ for (const auto & label : attrLabels) {
270+ processLabel (label);
271+ }
272+ }
279273
280- TSmallVec<std::pair<TString, TString>> labels;
281- for ( auto &attr : GetDatabaseAttributeLabels ())
282- if (CurrentAttributes. contains (attr))
283- labels. push_back (*CurrentAttributes. find (attr));
274+ if (lastLabel. first . empty ()) {
275+ // No labels to add
276+ continue ;
277+ }
284278
285- AddLabelsToServices (ctx, labels, DatabaseAttributeSensorServices);
279+ curGroup->RegisterSubgroup (lastLabel.first , lastLabel.second , oldGroup);
280+ serviceRoot->ReplaceSubgroup (serviceLabel.first , serviceLabel.second , newGroup);
281+ }
286282 }
287283
288284 void ApplyConfig (const NKikimrConfig::TMonitoringConfig &config,
@@ -322,6 +318,10 @@ class TLabelsMaintainer : public TActorBootstrapped<TLabelsMaintainer> {
322318 if (DatabaseAttributeSensorServices.empty ())
323319 DatabaseAttributeSensorServices = GetDatabaseAttributeSensorServices ();
324320
321+ AllSensorServices.clear ();
322+ AllSensorServices.insert (DatabaseSensorServices.begin (), DatabaseSensorServices.end ());
323+ AllSensorServices.insert (DatabaseAttributeSensorServices.begin (), DatabaseAttributeSensorServices.end ());
324+
325325 if (!InitializedLocalOptions) {
326326 InitializedLocalOptions = true ;
327327 DataCenter = config.GetDataCenter ();
@@ -376,6 +376,7 @@ class TLabelsMaintainer : public TActorBootstrapped<TLabelsMaintainer> {
376376
377377 THashSet<TString> DatabaseSensorServices;
378378 THashSet<TString> DatabaseAttributeSensorServices;
379+ THashSet<TString> AllSensorServices;
379380
380381 TString NoneDatabasetLabelValue;
381382 TString MultipleDatabaseLabelValue;
0 commit comments