collect file contents before exporting metrics
This commit is contained in:
parent
bb813d312d
commit
3302da1c92
@ -8,18 +8,20 @@ import (
|
|||||||
type Collector struct {
|
type Collector struct {
|
||||||
enabled bool
|
enabled bool
|
||||||
folderPath string
|
folderPath string
|
||||||
fileMap map[string]fileMetrics
|
fileMap map[string]*fileData
|
||||||
}
|
}
|
||||||
|
|
||||||
type fileMetrics struct {
|
type fileData struct {
|
||||||
readErrors int
|
readErrors int
|
||||||
|
fileName string
|
||||||
|
fileContents []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCollector(appSettings *cfg.AppSettings) *Collector {
|
func NewCollector(appSettings *cfg.AppSettings) *Collector {
|
||||||
return &Collector{
|
return &Collector{
|
||||||
enabled: appSettings.FileCollectorEnabled,
|
enabled: appSettings.FileCollectorEnabled,
|
||||||
folderPath: appSettings.FileCollectorPath,
|
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) {
|
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
|
||||||
if c.enabled {
|
if c.enabled {
|
||||||
|
c.collectFileContents()
|
||||||
c.collectFileErrors(ch)
|
c.collectFileErrors(ch)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
package textfile
|
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"
|
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) {
|
func (c *Collector) collectFileErrors(ch chan<- prometheus.Metric) {
|
||||||
for fileName, metrics := range c.fileMap {
|
for _, f := range c.fileMap {
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
metricReadError, prometheus.GaugeValue, float64(metrics.readErrors), fileName,
|
metricReadError, prometheus.GaugeValue, float64(f.readErrors), f.fileName,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
package textfile
|
package textfile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Collector) WriteTextFileMetrics(w http.ResponseWriter, r *http.Request) {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
files, err := ioutil.ReadDir(c.folderPath)
|
for _, f := range c.fileMap {
|
||||||
if err != nil {
|
_, err := w.Write(f.fileContents)
|
||||||
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 {
|
if err != nil {
|
||||||
c.appendErrorForPath(fileName)
|
c.appendErrorForPath(f.fileName)
|
||||||
log.Printf("error reading contents of file '%s': %v", fileName, err)
|
log.Printf("error writing file contents to response writer '%s': %v", f.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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Collector) appendErrorForPath(path string) {
|
func (c *Collector) appendErrorForPath(path string) {
|
||||||
if _, ok := c.fileMap[path]; !ok {
|
c.fileMap[path].readErrors++
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user