| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "log/slog" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | "sync" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/prometheus/client_golang/prometheus" |
| 11 | ) |
| 12 | |
| 13 | const ( |
| 14 | exporterNamespace = "varnish" |
| 15 | ) |
| 16 | |
| 17 | // prometheusExporter |
| 18 | |
| 19 | type prometheusExporter struct { |
| 20 | sync.RWMutex |
| 21 | |
| 22 | up prometheus.Gauge |
| 23 | version prometheus.Gauge |
| 24 | } |
| 25 | |
| 26 | func NewPrometheusExporter() *prometheusExporter { |
| 27 | return &prometheusExporter{ |
| 28 | up: prometheus.NewGauge(prometheus.GaugeOpts{ |
| 29 | Namespace: exporterNamespace, |
| 30 | Name: "up", |
| 31 | Help: "Was the last scrape of varnish successful.", |
| 32 | }), |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func (pe *prometheusExporter) Initialize() error { |
| 37 | pe.version = prometheus.NewGauge(prometheus.GaugeOpts{ |
| 38 | Namespace: exporterNamespace, |
| 39 | Name: "version", |
| 40 | Help: "Varnish version information", |
| 41 | ConstLabels: VarnishVersion.Labels(), |
| 42 | }) |
| 43 | pe.version.Set(1) |
| 44 | return nil |
| 45 | } |
| 46 | |
| 47 | // Implements prometheus.Collector |
| 48 | func (pe *prometheusExporter) Describe(ch chan<- *prometheus.Desc) { |
| 49 | start := time.Now() |
| 50 | |
| 51 | ch <- pe.up.Desc() |
| 52 | if pe.version != nil { |
| 53 | ch <- pe.version.Desc() |
| 54 | } |
| 55 | |
| 56 | slog.Debug("prometheus.Collector.Describe", "duration", time.Since(start)) |
| 57 | } |
| 58 | |
| 59 | // Implements prometheus.Collector |
| 60 | func (pe *prometheusExporter) Collect(ch chan<- prometheus.Metric) { |
| 61 | start := time.Now() |
| 62 | |
| 63 | pe.Lock() |
| 64 | defer pe.Unlock() |
| 65 | |
| 66 | // Rare case of varnish not being installed in the system |
| 67 | // when we started, but installed while we are running. |
| 68 | if !VarnishVersion.Valid() { |
| 69 | if VarnishVersion.Initialize() == nil { |
| 70 | pe.version = prometheus.NewGauge(prometheus.GaugeOpts{ |
| 71 | Namespace: exporterNamespace, |
| 72 | Name: "version", |
| 73 | Help: "Varnish version information", |
| 74 | ConstLabels: VarnishVersion.Labels(), |
| 75 | }) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | hadError := ExitHandler.HasError() |
| 80 | |
| 81 | _, err := ScrapeVarnish(ch) |
| 82 | ExitHandler.Set(err) |
| 83 | |
| 84 | if err == nil { |
| 85 | if hadError { |
| 86 | slog.Info("Successful scrape") |
| 87 | } |
| 88 | pe.up.Set(1) |
| 89 | } else { |
| 90 | pe.up.Set(0) |
| 91 | } |
| 92 | |
| 93 | ch <- pe.up |
| 94 | if pe.version != nil { |
| 95 | ch <- pe.version |
| 96 | } |
| 97 | |
| 98 | if err != nil { |
| 99 | slog.Error("prometheus.Collector.Collect scrape failed", "error", err) |
| 100 | } |
| 101 | slog.Debug("prometheus.Collector.Collect", "duration", time.Since(start)) |
| 102 | } |
| 103 | |
| 104 | // utils |
| 105 | |
| 106 | type group struct { |
| 107 | name string |
| 108 | prefixes []string |
| 109 | } |
| 110 | |
| 111 | var ( |
| 112 | groups = []group{ |
| 113 | {name: "backend", prefixes: []string{ |
| 114 | "vbe.", |
| 115 | }}, |
| 116 | {name: "mempool", prefixes: []string{ |
| 117 | "mempool.", |
| 118 | }}, |
| 119 | {name: "lck", prefixes: []string{ |
| 120 | "lck.", |
| 121 | }}, |
| 122 | {name: "sma", prefixes: []string{ |
| 123 | "sma.", |
| 124 | }}, |
| 125 | {name: "smf", prefixes: []string{ |
| 126 | "smf.", |
| 127 | }}, |
| 128 | {name: "mgt", prefixes: []string{ |
| 129 | "mgt.", |
| 130 | }}, |
| 131 | {name: "main", prefixes: []string{ |
| 132 | "main.", |
| 133 | }}, |
| 134 | } |
| 135 | ) |
| 136 | |
| 137 | type grouping struct { |
| 138 | newPrefix string |
| 139 | prefix string |
| 140 | total string |
| 141 | desc string |
| 142 | labelKey string |
| 143 | } |
| 144 | |
| 145 | var ( |
| 146 | fqGroupPrefixes = []*grouping{ |
| 147 | { |
| 148 | prefix: "main_fetch", |
| 149 | total: "main_s_fetch", |
| 150 | desc: "Number of fetches", |
| 151 | }, |
| 152 | { |
| 153 | newPrefix: "main_sessions", |
| 154 | prefix: "main_sess", |
| 155 | total: "main_s_sess", |
| 156 | desc: "Number of sessions", |
| 157 | }, |
| 158 | { |
| 159 | newPrefix: "main_worker_threads", |
| 160 | prefix: "main_n_wrk", |
| 161 | total: "main_n_wrk", |
| 162 | desc: "Number of worker threads", |
| 163 | }, |
| 164 | } |
| 165 | fqNames = map[string]string{ |
| 166 | "varnish_lck_colls": "varnish_lock_collisions", |
| 167 | "varnish_lck_creat": "varnish_lock_created", |
| 168 | "varnish_lck_destroy": "varnish_lock_destroyed", |
| 169 | "varnish_lck_locks": "varnish_lock_operations", |
| 170 | } |
| 171 | fqIdentifiers = map[string]string{ |
| 172 | "varnish_lock_collisions": "target", |
| 173 | "varnish_lock_created": "target", |
| 174 | "varnish_lock_destroyed": "target", |
| 175 | "varnish_lock_operations": "target", |
| 176 | "varnish_sma_c_bytes": "type", |
| 177 | "varnish_sma_c_fail": "type", |
| 178 | "varnish_sma_c_freed": "type", |
| 179 | "varnish_sma_c_req": "type", |
| 180 | "varnish_sma_g_alloc": "type", |
| 181 | "varnish_sma_g_bytes": "type", |
| 182 | "varnish_sma_g_space": "type", |
| 183 | "varnish_smf_c_bytes": "type", |
| 184 | "varnish_smf_c_fail": "type", |
| 185 | "varnish_smf_c_freed": "type", |
| 186 | "varnish_smf_c_req": "type", |
| 187 | "varnish_smf_g_alloc": "type", |
| 188 | "varnish_smf_g_bytes": "type", |
| 189 | "varnish_smf_g_smf_frag": "type", |
| 190 | "varnish_smf_g_smf_large": "type", |
| 191 | "varnish_smf_g_smf": "type", |
| 192 | "varnish_smf_g_space": "type", |
| 193 | } |
| 194 | ) |
| 195 | |
| 196 | var ( |
| 197 | // (prefix:)<uuid>.<name> |
| 198 | regexBackendUUID = regexp.MustCompile(`([[0-9A-Za-z]{8}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[89ABab][0-9A-Za-z]{3}-[0-9A-Za-z]{12})(.*)`) |
| 199 | // <name>(<ip>,(<something>),<port>) |
| 200 | regexBackendParen = regexp.MustCompile(`(.*)\((.*)\)`) |
| 201 | ) |
| 202 | |
| 203 | func findLabelValue(name string, keys, values []string) string { |
| 204 | for i, key := range keys { |
| 205 | if key == name { |
| 206 | if i < len(values) { |
| 207 | return values[i] |
| 208 | } |
| 209 | return "" |
| 210 | } |
| 211 | } |
| 212 | return "" |
| 213 | } |
| 214 | |
| 215 | func cleanBackendName(name string) string { |
| 216 | name = strings.Trim(name, ".") |
| 217 | for _, prefix := range []string{"boot.", "root:"} { |
| 218 | if startsWith(name, prefix, caseInsensitive) { |
| 219 | name = name[len(prefix):] |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // reload_2019-08-29T100458.<name> as by varnish_reload_vcl in 4.x |
| 224 | // reload_20191014_091124_78599.<name> as by varnishreload in 6+ |
| 225 | if strings.HasPrefix(name, "reload_") { |
| 226 | dot := strings.Index(name, ".") |
| 227 | if dot != -1 { |
| 228 | name = name[dot+1:] |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return name |
| 233 | } |
| 234 | |
| 235 | // https://prometheus.io/docs/practices/naming/ |
| 236 | func computePrometheusInfo(vName, vGroup, vIdentifier, vDescription string) (name, description string, labelKeys, labelValues []string) { |
| 237 | { |
| 238 | // Varnish >= 5.2 no longer has 'ident', parse from full vName |
| 239 | // as "<group>.<ident>.<name>" |
| 240 | if len(vIdentifier) == 0 && strings.Count(vName, ".") > 1 { |
| 241 | vIdentifier = prometheusTrimGroupPrefix(strings.ToLower(vName)) |
| 242 | vIdentifier = vIdentifier[0:strings.LastIndex(vIdentifier, ".")] |
| 243 | } |
| 244 | } |
| 245 | // name and description |
| 246 | { |
| 247 | fq := strings.ToLower(vName) |
| 248 | // Remove unique identifiers from name to group similar metrics by labeling |
| 249 | if len(vIdentifier) > 0 { |
| 250 | fq = strings.Replace(fq, "."+strings.ToLower(vIdentifier), "", -1) |
| 251 | } |
| 252 | // Make sure our group is prefixed only once |
| 253 | fq = prometheusTrimGroupPrefix(fq) |
| 254 | // Build fq name |
| 255 | name = exporterNamespace + "_" + vGroup + "_" + strings.Replace(fq, ".", "_", -1) |
| 256 | if swapName := fqNames[name]; len(swapName) > 0 { |
| 257 | name = swapName |
| 258 | } |
| 259 | description = vDescription |
| 260 | } |
| 261 | // labels: can alter final name and description |
| 262 | { |
| 263 | if len(vIdentifier) > 0 { |
| 264 | if isVBE := startsWith(vName, "VBE.", caseSensitive); isVBE { |
| 265 | if hits := regexBackendUUID.FindAllStringSubmatch(vIdentifier, -1); len(hits) > 0 && len(hits[0]) >= 3 { |
| 266 | labelKeys, labelValues = append(labelKeys, "backend"), append(labelValues, cleanBackendName(hits[0][2])) |
| 267 | labelKeys, labelValues = append(labelKeys, "server"), append(labelValues, hits[0][1]) |
| 268 | } else if hits := regexBackendParen.FindAllStringSubmatch(vIdentifier, -1); len(hits) > 0 && len(hits[0]) >= 3 { |
| 269 | labelKeys, labelValues = append(labelKeys, "backend"), append(labelValues, cleanBackendName(hits[0][1])) |
| 270 | labelKeys, labelValues = append(labelKeys, "server"), append(labelValues, strings.Replace(hits[0][2], ",,", ":", 1)) |
| 271 | } |
| 272 | // We must be consistent with the number of labels and their names inside this scrape and between scrapes, or we will get this error: |
| 273 | // https://github.com/prometheus/client_golang/blob/3fb8ace93bc4ccddea55af62320c2fd109252880/prometheus/registry.go#L704-L707 |
| 274 | if len(labelKeys) == 0 { |
| 275 | labelKeys, labelValues = append(labelKeys, "backend"), append(labelValues, cleanBackendName(vIdentifier)) |
| 276 | labelKeys, labelValues = append(labelKeys, "server"), append(labelValues, "unknown") |
| 277 | } |
| 278 | } |
| 279 | if len(labelKeys) == 0 { |
| 280 | labelKey := fqIdentifiers[name] |
| 281 | if len(labelKey) == 0 { |
| 282 | labelKey = "id" |
| 283 | } |
| 284 | labelKeys, labelValues = append(labelKeys, labelKey), append(labelValues, vIdentifier) |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // create groupings by moving part of the fq name as a label and optional total |
| 289 | for _, grouping := range fqGroupPrefixes { |
| 290 | fqTotal := exporterNamespace + "_" + grouping.total |
| 291 | fqPrefix := exporterNamespace + "_" + grouping.prefix |
| 292 | fqNewName := fqPrefix |
| 293 | if len(grouping.newPrefix) > 0 { |
| 294 | fqNewName = exporterNamespace + "_" + grouping.newPrefix |
| 295 | } |
| 296 | if name == fqTotal { |
| 297 | // @note total should not be a label as it breaks aggregation |
| 298 | name, description = fqNewName+"_total", grouping.desc |
| 299 | break |
| 300 | } else if len(name) > len(fqPrefix)+1 && strings.HasPrefix(name, fqPrefix+"_") { |
| 301 | labelKey := "type" |
| 302 | if len(grouping.labelKey) > 0 { |
| 303 | labelKey = grouping.labelKey |
| 304 | } |
| 305 | labelKeys, labelValues = append(labelKeys, labelKey), append(labelValues, name[len(fqPrefix)+1:]) |
| 306 | name, description = fqNewName, grouping.desc |
| 307 | break |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | return name, description, labelKeys, labelValues |
| 312 | } |
| 313 | |
| 314 | func prometheusTrimGroupPrefix(name string) string { |
| 315 | nameLower := strings.ToLower(name) |
| 316 | for _, group := range groups { |
| 317 | for _, prefix := range group.prefixes { |
| 318 | if startsWith(nameLower, prefix, caseSensitive) { |
| 319 | return name[len(prefix):] |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | return name |
| 324 | } |
| 325 | |
| 326 | // Always returns at least one main label |
| 327 | func prometheusGroup(vName string) string { |
| 328 | vNameLower := strings.ToLower(vName) |
| 329 | for _, group := range groups { |
| 330 | if startsWithAny(vNameLower, group.prefixes, caseSensitive) { |
| 331 | return group.name |
| 332 | } |
| 333 | } |
| 334 | return "main" |
| 335 | } |