From 8e8f8c1f47b6bec7762a227f557771c619345551 Mon Sep 17 00:00:00 2001 From: earendilfr Date: Tue, 10 Jun 2025 22:00:43 +0200 Subject: [PATCH] Add two option: - add the possibility to indicate a custom label name for a specific oid - add the possibility to indicate a static number as index for a specific oid Signed-off-by: earendilfr --- collector/collector.go | 6 +++++- generator/config.go | 1 + generator/tree.go | 10 ++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/collector/collector.go b/collector/collector.go index f345be0a..02beeae8 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -971,7 +971,11 @@ func indexesToLabels(indexOids []int, metric *config.Metric, oidToPdu map[string } oid := lookup.Oid for _, label := range lookup.Labels { - oid = fmt.Sprintf("%s.%s", oid, listToOid(labelOids[label])) + if _, err := strconv.ParseUint(label, 10, 64); err == nil { + oid = fmt.Sprintf("%s.%v", oid, label) + } else { + oid = fmt.Sprintf("%s.%s", oid, listToOid(labelOids[label])) + } } if pdu, ok := oidToPdu[oid]; ok { t := lookup.Type diff --git a/generator/config.go b/generator/config.go index a0781e27..dbb976f3 100644 --- a/generator/config.go +++ b/generator/config.go @@ -85,4 +85,5 @@ type Lookup struct { SourceIndexes []string `yaml:"source_indexes"` Lookup string `yaml:"lookup"` DropSourceIndexes bool `yaml:"drop_source_indexes,omitempty"` + CustomLabelName string `yaml:"custom_label_name,omitempty"` } diff --git a/generator/tree.go b/generator/tree.go index 636445ef..90f4fb78 100644 --- a/generator/tree.go +++ b/generator/tree.go @@ -445,6 +445,13 @@ func generateConfigModule(cfg *ModuleConfig, node *Node, nameToNode map[string]* } } } + if foundIndexes != len(lookup.SourceIndexes) { + for _, index := range lookup.SourceIndexes { + if _, err := strconv.ParseUint(index, 10, 64); err == nil { + foundIndexes++ + } + } + } if foundIndexes == len(lookup.SourceIndexes) { if _, ok := nameToNode[lookup.Lookup]; !ok { return nil, fmt.Errorf("unknown index '%s'", lookup.Lookup) @@ -459,6 +466,9 @@ func generateConfigModule(cfg *ModuleConfig, node *Node, nameToNode map[string]* Type: typ, Oid: indexNode.Oid, } + if len(lookup.CustomLabelName) > 0 { + l.Labelname = sanitizeLabelName(lookup.CustomLabelName) + } for _, oldIndex := range lookup.SourceIndexes { l.Labels = append(l.Labels, sanitizeLabelName(oldIndex)) }