|
| 1 | +package exporter |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/prometheus/client_golang/prometheus" |
| 7 | +) |
| 8 | + |
| 9 | +var ( |
| 10 | + fqname string |
| 11 | + labels prometheus.Labels = nil |
| 12 | +) |
| 13 | + |
| 14 | +func (e *Exporter) build(m []string, k int) (string, prometheus.Labels) { |
| 15 | + metricSlices := split(m[0], "_") |
| 16 | + |
| 17 | + if len(metricSlices) == 1 { |
| 18 | + fqname, labels = e.buildShortMetrics(m, k, metricSlices[0]) |
| 19 | + } else if len(metricSlices) >= 2 { |
| 20 | + fqname, labels = e.buildLongMetrics(m, metricSlices, k) |
| 21 | + } |
| 22 | + |
| 23 | + return fqname, labels |
| 24 | +} |
| 25 | + |
| 26 | +func (e *Exporter) buildShortMetrics(m []string, k int, part string) (string, prometheus.Labels) { |
| 27 | + labels = nil |
| 28 | + |
| 29 | + if k < (len(e.data) - 1) { |
| 30 | + future := split(e.data[k+1], RAW_METRIC_DELIM) |
| 31 | + if m[0] == future[0] { |
| 32 | + fqname = prometheus.BuildFQName(e.name, "", part) |
| 33 | + labels = prometheus.Labels{"metric": m[1]} |
| 34 | + |
| 35 | + } else { |
| 36 | + if k == 0 { |
| 37 | + fqname = prometheus.BuildFQName(e.name, part, m[1]) |
| 38 | + |
| 39 | + } else { |
| 40 | + future = split(e.data[k-1], RAW_METRIC_DELIM) |
| 41 | + if m[0] == future[0] { |
| 42 | + fqname = prometheus.BuildFQName(e.name, "", part) |
| 43 | + labels = prometheus.Labels{"metric": m[1]} |
| 44 | + |
| 45 | + } else { |
| 46 | + fqname = prometheus.BuildFQName(e.name, part, m[1]) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } else if k == (len(e.data) - 1) { |
| 51 | + future := split(e.data[k-1], RAW_METRIC_DELIM) |
| 52 | + if m[0] == future[0] { |
| 53 | + fqname = prometheus.BuildFQName(e.name, "", part) |
| 54 | + labels = prometheus.Labels{"metric": m[1]} |
| 55 | + |
| 56 | + } else { |
| 57 | + fqname = prometheus.BuildFQName(e.name, part, m[1]) |
| 58 | + } |
| 59 | + } else { |
| 60 | + fqname = prometheus.BuildFQName(e.name, part, m[1]) |
| 61 | + } |
| 62 | + |
| 63 | + return fqname, labels |
| 64 | +} |
| 65 | + |
| 66 | +func (e *Exporter) buildLongMetrics(m, slices []string, k int) (string, prometheus.Labels) { |
| 67 | + labels = nil |
| 68 | + |
| 69 | + if k < (len(e.data) - 1) { |
| 70 | + future := split(e.data[k+1], RAW_METRIC_DELIM) |
| 71 | + if m[0] == future[0] { |
| 72 | + fqname = prometheus.BuildFQName( |
| 73 | + e.name, fmt.Sprintf("%s", join(slices[0:len(slices)-1], "_")), slices[len(slices)-1]) |
| 74 | + labels = prometheus.Labels{"metric": m[1]} |
| 75 | + |
| 76 | + } else { |
| 77 | + if k == 0 { |
| 78 | + fqname = prometheus.BuildFQName(e.name, fmt.Sprintf("%s", join(slices[0:], "_")), m[1]) |
| 79 | + |
| 80 | + } else { |
| 81 | + future := split(e.data[k-1], RAW_METRIC_DELIM) |
| 82 | + if m[0] == future[0] { |
| 83 | + fqname = prometheus.BuildFQName( |
| 84 | + e.name, fmt.Sprintf("%s", join(slices[0:len(slices)-1], "_")), slices[len(slices)-1]) |
| 85 | + labels = prometheus.Labels{"metric": m[1]} |
| 86 | + |
| 87 | + } else { |
| 88 | + fqname = prometheus.BuildFQName(e.name, fmt.Sprintf("%s", join(slices[0:], "_")), m[1]) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } else if k == (len(e.data) - 1) { |
| 93 | + future := split(e.data[k-1], RAW_METRIC_DELIM) |
| 94 | + if m[0] == future[0] { |
| 95 | + fqname = prometheus.BuildFQName( |
| 96 | + e.name, fmt.Sprintf("%s", join(slices[0:len(slices)-1], "_")), slices[len(slices)-1]) |
| 97 | + labels = prometheus.Labels{"metric": m[1]} |
| 98 | + |
| 99 | + } else { |
| 100 | + fqname = prometheus.BuildFQName(e.name, fmt.Sprintf("%s", join(slices[0:], "_")), m[1]) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return fqname, labels |
| 105 | +} |
0 commit comments