Skip to content

Commit

Permalink
add Counter with index prefix && add index group label in all metric (#3
Browse files Browse the repository at this point in the history
)

Co-authored-by: akrupin <[email protected]>
  • Loading branch information
bmgeek and akrupin authored Dec 10, 2021
1 parent a72d598 commit 33b6f8a
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@ package main

import (
"fmt"
"regexp"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
)

var (
indexGroupLastTotalBytes = make(map[string]float64)
)

type Collector struct {
client *Client

indexSize *prometheus.Desc
docsCount *prometheus.Desc
indexSize *prometheus.Desc
indexGroupSize *prometheus.Desc
docsCount *prometheus.Desc
}

func NewCollector(address, project string, insecure bool) (*Collector, error) {
namespace := "oneday_elasticsearch"
labels := []string{"index"}
labels := []string{"index", "index_group"}
labels_group := []string{"index_group"}

client, err := NewClient([]string{address}, insecure)
if err != nil {
Expand All @@ -41,6 +49,10 @@ func NewCollector(address, project string, insecure bool) (*Collector, error) {
prometheus.BuildFQName(namespace, "indices_store", "size_bytes_primary"),
"Size of each index to date", labels, constLabels,
),
indexGroupSize: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "indices_group_store", "size_bytes"),
"Size of each index to date", labels_group, constLabels,
),
docsCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "indices_docs", "total"),
"Count of docs for each index to date", labels, constLabels,
Expand All @@ -50,6 +62,7 @@ func NewCollector(address, project string, insecure bool) (*Collector, error) {

func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.indexSize
ch <- c.indexGroupSize
ch <- c.docsCount
}

Expand All @@ -59,17 +72,38 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
log.Fatal("error getting indices stats: ", err)
}

indexGroupSize := make(map[string]float64)
for index, v := range indices {
// Find date -Y.m.d (-2021.12.01) and replace
reFindDateTime := regexp.MustCompile(`-\d+.\d+.\d+$`)
// Create variable with index prefix
indexGrouplabel := strings.ToLower(reFindDateTime.ReplaceAllString(index, ""))

data := v.(map[string]interface{})
primaries := data["primaries"].(map[string]interface{})

docs := primaries["docs"].(map[string]interface{})
count := docs["count"].(float64)
ch <- prometheus.MustNewConstMetric(c.docsCount, prometheus.GaugeValue, count, index)
ch <- prometheus.MustNewConstMetric(c.docsCount, prometheus.GaugeValue, count, index, indexGrouplabel)

store := primaries["store"].(map[string]interface{})
size := store["size_in_bytes"].(float64)
ch <- prometheus.MustNewConstMetric(c.indexSize, prometheus.GaugeValue, size, index)
ch <- prometheus.MustNewConstMetric(c.indexSize, prometheus.GaugeValue, size, index, indexGrouplabel)

var lastIndexDifferenceBytes float64
if _, ok := indexGroupLastTotalBytes[index]; ok {
if size > indexGroupLastTotalBytes[index] {
lastIndexDifferenceBytes = size - indexGroupLastTotalBytes[index]
} else {
lastIndexDifferenceBytes = 0
}
}

indexGroupLastTotalBytes[index] = size
indexGroupSize[indexGrouplabel] += lastIndexDifferenceBytes
}

for indexGroup, v := range indexGroupSize {
ch <- prometheus.MustNewConstMetric(c.indexGroupSize, prometheus.CounterValue, v, indexGroup)
}
}

0 comments on commit 33b6f8a

Please sign in to comment.