collect file contents before exporting metrics

This commit is contained in:
Hector 2021-10-12 20:36:32 +01:00
parent bb813d312d
commit 3302da1c92
3 changed files with 48 additions and 45 deletions

View File

@ -8,18 +8,20 @@ import (
type Collector struct {
enabled bool
folderPath string
fileMap map[string]fileMetrics
fileMap map[string]*fileData
}
type fileMetrics struct {
type fileData struct {
readErrors int
fileName string
fileContents []byte
}
func NewCollector(appSettings *cfg.AppSettings) *Collector {
return &Collector{
enabled: appSettings.FileCollectorEnabled,
folderPath: appSettings.FileCollectorPath,
fileMap: make(map[string]fileMetrics),
fileMap: make(map[string]*fileData),
}
}
@ -31,6 +33,7 @@ func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
if c.enabled {
c.collectFileContents()
c.collectFileErrors(ch)
}
}

View File

@ -1,6 +1,12 @@
package textfile
import "github.com/prometheus/client_golang/prometheus"
import (
"github.com/prometheus/client_golang/prometheus"
"io/ioutil"
"log"
"path/filepath"
"strings"
)
const namespace = "textfile"
@ -12,10 +18,38 @@ var (
)
)
func (c *Collector) collectFileContents() {
files, err := ioutil.ReadDir(c.folderPath)
if err != nil {
log.Printf("error reading directory '%s': %v", c.folderPath, err)
return
}
for _, file := range files {
fileName := file.Name()
if !strings.HasSuffix(fileName, ".prom") {
continue
}
c.fileMap[fileName] = &fileData{
readErrors: 0,
fileName: fileName,
}
fullPath := filepath.Join(c.folderPath, file.Name())
content, err := ioutil.ReadFile(fullPath)
if err != nil {
c.appendErrorForPath(fileName)
log.Printf("error reading contents of file '%s': %v", fileName, err)
}
c.fileMap[fileName].fileContents = content
}
}
func (c *Collector) collectFileErrors(ch chan<- prometheus.Metric) {
for fileName, metrics := range c.fileMap {
for _, f := range c.fileMap {
ch <- prometheus.MustNewConstMetric(
metricReadError, prometheus.GaugeValue, float64(metrics.readErrors), fileName,
metricReadError, prometheus.GaugeValue, float64(f.readErrors), f.fileName,
)
}
}

View File

@ -1,11 +1,8 @@
package textfile
import (
"io/ioutil"
"log"
"net/http"
"path/filepath"
"strings"
)
func (c *Collector) WriteTextFileMetrics(w http.ResponseWriter, r *http.Request) {
@ -13,46 +10,15 @@ func (c *Collector) WriteTextFileMetrics(w http.ResponseWriter, r *http.Request)
return
}
files, err := ioutil.ReadDir(c.folderPath)
for _, f := range c.fileMap {
_, err := w.Write(f.fileContents)
if err != nil {
c.appendErrorForPath(c.folderPath)
log.Printf("error reading directory '%s': %v", c.folderPath, err)
return
}
for _, file := range files {
fileName := file.Name()
if !strings.HasSuffix(fileName, ".prom") {
continue
}
c.setErrorCountForPath(fileName, 0)
fullPath := filepath.Join(c.folderPath, file.Name())
content, err := ioutil.ReadFile(fullPath)
if err != nil {
c.appendErrorForPath(fileName)
log.Printf("error reading contents of file '%s': %v", fileName, err)
}
_, err = w.Write(content)
if err != nil {
c.appendErrorForPath(fileName)
log.Printf("error writing file contents to response writer '%s': %v", fileName, err)
c.appendErrorForPath(f.fileName)
log.Printf("error writing file contents to response writer '%s': %v", f.fileName, err)
}
}
}
func (c *Collector) appendErrorForPath(path string) {
if _, ok := c.fileMap[path]; !ok {
c.setErrorCountForPath(path, 1)
} else {
c.setErrorCountForPath(path, c.fileMap[path].readErrors + 1)
}
}
func (c *Collector) setErrorCountForPath(path string, count int) {
c.fileMap[path] = fileMetrics{
readErrors: count,
}
c.fileMap[path].readErrors++
}