|
| 1 | +package exporter |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/prometheus/client_golang/prometheus" |
| 9 | + log "github.com/sirupsen/logrus" |
| 10 | +) |
| 11 | + |
| 12 | +type ( |
| 13 | + Exporter struct { |
| 14 | + uri string |
| 15 | + name string |
| 16 | + version string |
| 17 | + metrics Metrics |
| 18 | + |
| 19 | + data []string |
| 20 | + } |
| 21 | + |
| 22 | + Metrics []struct { |
| 23 | + help *prometheus.Desc |
| 24 | + value float64 |
| 25 | + vtype prometheus.ValueType |
| 26 | + } |
| 27 | +) |
| 28 | + |
| 29 | +func NewCollector(name, uri, version string) *Exporter { |
| 30 | + return &Exporter{ |
| 31 | + name: name, |
| 32 | + uri: uri, |
| 33 | + version: version, |
| 34 | + metrics: Metrics{}, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { |
| 39 | + for _, metric := range e.metrics { |
| 40 | + ch <- metric.help |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func (e *Exporter) Collect(ch chan<- prometheus.Metric) { |
| 45 | + e.process(ch) |
| 46 | + |
| 47 | + for _, metric := range e.metrics { |
| 48 | + ch <- prometheus.MustNewConstMetric(metric.help, metric.vtype, metric.value) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +var ( |
| 53 | + // s = []string{"processor_rate_limit_1::dropped::0", "registrar_states::cleanup::0", "registrar_states::update::0", "registrar_writes::success::0", "system_cpu::cores::4", "system_load::1::0.71", "system_load::15::0.47", "system_load::5::0.53", "system_load_norm::1::0.1775", "system_load_norm::15::0.1175", "system_load_norm::5::0.1325"} |
| 54 | + vl, future []string |
| 55 | + |
| 56 | + fqname string |
| 57 | + labels prometheus.Labels |
| 58 | + mType prometheus.ValueType |
| 59 | +) |
| 60 | + |
| 61 | +func (e *Exporter) process(ch chan<- prometheus.Metric) { |
| 62 | + body, err := FetchMetrics(e.uri) |
| 63 | + if err != nil { |
| 64 | + // set target down on error |
| 65 | + ch <- prometheus.MustNewConstMetric( |
| 66 | + prometheus.NewDesc(fmt.Sprintf("%s_up", e.name), "Target up", nil, nil), prometheus.GaugeValue, float64(0)) |
| 67 | + log.Debugf("Failed getting metrics endpoint of target: %s ", err.Error()) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + log.Debugln("Service scrapped up") |
| 72 | + ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(fmt.Sprintf("%s_up", e.name), "Target Up", nil, nil), prometheus.GaugeValue, float64(1)) |
| 73 | + |
| 74 | + // inject service version is exists |
| 75 | + if e.version != "" { |
| 76 | + ch <- prometheus.MustNewConstMetric( |
| 77 | + prometheus.NewDesc(fmt.Sprintf("%s_version_info", e.name), fmt.Sprintf("%s service info", e.name), nil, |
| 78 | + prometheus.Labels{"version": e.version}), prometheus.GaugeValue, float64(1)) |
| 79 | + } |
| 80 | + |
| 81 | + // parse response body |
| 82 | + p := Parser{data: make([]string, 0)} |
| 83 | + p.parse(body) |
| 84 | + e.data = p.data |
| 85 | + |
| 86 | + sort.Slice(e.data, func(i, j int) bool { return e.data[i] < e.data[j] }) |
| 87 | + |
| 88 | + stats := make(Metrics, len(e.data)) |
| 89 | + for k, v := range e.data { |
| 90 | + vl = split(v, "::") |
| 91 | + |
| 92 | + i, err := strconv.ParseFloat(vl[2], 64) |
| 93 | + if err != nil { |
| 94 | + // a string was found |
| 95 | + log.Debugln(err) |
| 96 | + i = 0 |
| 97 | + } |
| 98 | + |
| 99 | + // check last filed for the specific ValueType |
| 100 | + if contains(vl[1], "total") { |
| 101 | + mType = prometheus.CounterValue |
| 102 | + } else { |
| 103 | + if len(vl) > 3 { |
| 104 | + mType = prometheus.UntypedValue |
| 105 | + } else { |
| 106 | + mType = prometheus.GaugeValue |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // build Full-Qualified Name |
| 111 | + e.build(k) |
| 112 | + |
| 113 | + // setup metrics |
| 114 | + stats[k].help = prometheus.NewDesc( |
| 115 | + fqname, "", nil, labels) |
| 116 | + stats[k].value = float64(i) |
| 117 | + stats[k].vtype = mType |
| 118 | + } |
| 119 | + |
| 120 | + e.metrics = stats |
| 121 | +} |
| 122 | + |
| 123 | +func (e *Exporter) build(k int) { |
| 124 | + labels = nil |
| 125 | + fp := split(vl[0], "_") |
| 126 | + |
| 127 | + if len(fp) == 2 { |
| 128 | + |
| 129 | + if k < (len(e.data) - 1) { |
| 130 | + future = split(e.data[k+1], "::") |
| 131 | + if vl[0] == future[0] { |
| 132 | + |
| 133 | + fqname = prometheus.BuildFQName( |
| 134 | + e.name, fmt.Sprintf("%s", join(fp[0:len(fp)-1], "_")), fp[len(fp)-1]) |
| 135 | + labels = prometheus.Labels{"metric": vl[1]} |
| 136 | + } else { |
| 137 | + future = split(e.data[k-1], "::") |
| 138 | + if vl[0] == future[0] { |
| 139 | + fqname = prometheus.BuildFQName( |
| 140 | + e.name, fmt.Sprintf("%s", join(fp[0:len(fp)-1], "_")), fp[len(fp)-1]) |
| 141 | + labels = prometheus.Labels{"metric": vl[1]} |
| 142 | + } else { |
| 143 | + fqname = prometheus.BuildFQName(e.name, fmt.Sprintf("%s_%s", fp[0], fp[1]), vl[1]) |
| 144 | + } |
| 145 | + } |
| 146 | + } else if k == (len(e.data) - 1) { |
| 147 | + future = split(e.data[k-1], "::") |
| 148 | + if vl[0] == future[0] { |
| 149 | + fqname = prometheus.BuildFQName( |
| 150 | + e.name, fmt.Sprintf("%s", join(fp[0:len(fp)-1], "_")), fp[len(fp)-1]) |
| 151 | + labels = prometheus.Labels{"metric": vl[1]} |
| 152 | + } else { |
| 153 | + fqname = prometheus.BuildFQName(e.name, fmt.Sprintf("%s_%s", fp[0], fp[1]), vl[1]) |
| 154 | + } |
| 155 | + } else { |
| 156 | + fqname = prometheus.BuildFQName(e.name, fmt.Sprintf("%s_%s", fp[0], fp[1]), vl[1]) |
| 157 | + } |
| 158 | + |
| 159 | + } else if len(fp) >= 3 { |
| 160 | + |
| 161 | + if k < (len(e.data) - 1) { |
| 162 | + future = split(e.data[k+1], "::") |
| 163 | + if vl[0] == future[0] { |
| 164 | + |
| 165 | + fqname = prometheus.BuildFQName( |
| 166 | + e.name, fmt.Sprintf("%s", join(fp[0:len(fp)-1], "_")), fp[len(fp)-1]) |
| 167 | + labels = prometheus.Labels{"metric": vl[1]} |
| 168 | + } else { |
| 169 | + |
| 170 | + if k == 0 { |
| 171 | + fqname = prometheus.BuildFQName(e.name, fmt.Sprintf("%s_%s", fp[0], fp[1]), vl[1]) |
| 172 | + } else { |
| 173 | + future = split(e.data[k-1], "::") |
| 174 | + if vl[0] == future[0] { |
| 175 | + fqname = prometheus.BuildFQName( |
| 176 | + e.name, fmt.Sprintf("%s", join(fp[0:len(fp)-1], "_")), fp[len(fp)-1]) |
| 177 | + labels = prometheus.Labels{"metric": vl[1]} |
| 178 | + } else { |
| 179 | + fqname = prometheus.BuildFQName(e.name, fmt.Sprintf("%s_%s", fp[0], fp[1]), vl[1]) |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } else if k == (len(e.data) - 1) { |
| 184 | + future = split(e.data[k-1], "::") |
| 185 | + if vl[0] == future[0] { |
| 186 | + labels = prometheus.Labels{"metric": vl[1]} |
| 187 | + } |
| 188 | + } |
| 189 | + fqname = prometheus.BuildFQName( |
| 190 | + e.name, fmt.Sprintf("%s", join(fp[0:len(fp)-1], "_")), fp[len(fp)-1]) |
| 191 | + } |
| 192 | +} |
0 commit comments