fix(zabbix): prevent silent removal of itemTagFilter when no tags match regex (#2248)
Added a test to ensure that when a regex itemTagFilter does not match any tags, the GetItems function returns an empty list instead of all items. This addresses a bug where an empty tag filter would lead to unintended behavior by removing the filter silently. Fixes #2247
This commit is contained in:
@@ -105,16 +105,24 @@ func (ds *Zabbix) GetItems(
|
||||
}
|
||||
|
||||
if isRegex(itemTagFilter) {
|
||||
ds.logger.Debug("Processing regex item tag filter", "filterPattern", itemTagFilter, "hostCount", len(hostids))
|
||||
tags, err := ds.GetItemTags(ctx, groupFilter, hostFilter, itemTagFilter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ds.logger.Debug("GetItemTags completed", "matchedTagCount", len(tags), "filterPattern", itemTagFilter)
|
||||
// If regex filter doesn't match any tags, return empty items list
|
||||
// to prevent silently removing the filter and returning all items
|
||||
if len(tags) == 0 {
|
||||
return []*Item{}, nil
|
||||
}
|
||||
var tagStrs []string
|
||||
for _, t := range tags {
|
||||
tagStrs = append(tagStrs, itemTagToString(t))
|
||||
}
|
||||
itemTagFilter = strings.Join(tagStrs, ",")
|
||||
}
|
||||
ds.logger.Debug("Fetching items with filters", "hostCount", len(hostids), "itemType", itemType, "showDisabled", showDisabled, "hasItemTagFilter", len(itemTagFilter) > 0)
|
||||
allItems, err = ds.GetAllItems(ctx, hostids, nil, itemType, showDisabled, itemTagFilter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user