Debian Codegraph @

metrics/default_label_strategy.go

raw symbols SCIP
1 package metrics
2
3 import (
4 "regexp"
5 "strings"
6
7 "github.com/czerwonk/bird_exporter/protocol"
8 )
9
10 // DefaultLabelStrategy defines the labels to add to an metric and its data retrieval method
11 type DefaultLabelStrategy struct {
12 descriptionLabels bool
13 descriptionLabelsRegex *regexp.Regexp
14 }
15
16 func NewDefaultLabelStrategy(descriptionLabels bool, descriptionLabelsRegex string) *DefaultLabelStrategy {
17 return &DefaultLabelStrategy{
18 descriptionLabels: descriptionLabels,
19 descriptionLabelsRegex: regexp.MustCompile(descriptionLabelsRegex),
20 }
21 }
22
23 // LabelNames returns the list of label names
24 func (d *DefaultLabelStrategy) LabelNames(p *protocol.Protocol) []string {
25 res := []string{"name", "proto", "ip_version", "import_filter", "export_filter"}
26 if d.descriptionLabels && p.Description != "" {
27 res = append(res, labelKeysFromDescription(p.Description, d)...)
28 }
29
30 return res
31 }
32
33 // LabelValues returns the values for a protocol
34 func (d *DefaultLabelStrategy) LabelValues(p *protocol.Protocol) []string {
35 res := []string{p.Name, protoString(p), p.IPVersion, p.ImportFilter, p.ExportFilter}
36 if d.descriptionLabels && p.Description != "" {
37 res = append(res, labelValuesFromDescription(p.Description, d)...)
38 }
39
40 return res
41 }
42
43 func labelKeysFromDescription(desc string, d *DefaultLabelStrategy) []string {
44 res := []string{}
45
46 matches := d.descriptionLabelsRegex.FindAllStringSubmatch(desc, -1)
47 for _, submatch := range matches {
48 res = append(res, strings.TrimSpace(submatch[1]))
49 }
50
51 return res
52 }
53
54 func labelValuesFromDescription(desc string, d *DefaultLabelStrategy) []string {
55 res := []string{}
56
57 matches := d.descriptionLabelsRegex.FindAllStringSubmatch(desc, -1)
58 for _, submatch := range matches {
59 res = append(res, strings.TrimSpace(submatch[2]))
60 }
61
62 return res
63 }
64
65 func protoString(p *protocol.Protocol) string {
66 switch p.Proto {
67 case protocol.BGP:
68 return "BGP"
69 case protocol.OSPF:
70 if p.IPVersion == "4" {
71 return "OSPF"
72 }
73 return "OSPFv3"
74 case protocol.Static:
75 return "Static"
76 case protocol.Kernel:
77 return "Kernel"
78 case protocol.Direct:
79 return "Direct"
80 case protocol.Babel:
81 return "Babel"
82 case protocol.RPKI:
83 return "RPKI"
84 case protocol.BFD:
85 return "BFD"
86 }
87
88 return ""
89 }