prometheus-docker-labels-di.../prom_file_handler.go

51 lines
1 KiB
Go
Raw Normal View History

2021-02-02 21:21:12 +01:00
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"syscall"
)
type promHandler struct {
config envConfig
}
func newPromFileHandler(config envConfig) (*promHandler, error) {
if !strings.HasSuffix(config.PrometheusConfigFilePath, ".json") {
2021-09-13 15:24:52 +02:00
return nil, fmt.Errorf("file does not have .json extension")
2021-02-02 21:21:12 +01:00
}
err := syscall.Access(config.PrometheusConfigFilePath, syscall.O_RDWR)
if err != nil {
return nil, err
}
handler := &promHandler{
config: config,
}
return handler, nil
}
func (h *promHandler) write(containersScrapeConfig map[string]containerScrapeConfig) error {
targetGroups := make([]containerScrapeConfig, 0, len(containersScrapeConfig))
for _, v := range containersScrapeConfig {
targetGroups = append(targetGroups, v)
}
data, err := json.Marshal(targetGroups)
if err != nil {
return err
}
log.Debugf("Write %s to %s", string(data), h.config.PrometheusConfigFilePath)
err = ioutil.WriteFile(h.config.PrometheusConfigFilePath, data, 0644)
if err != nil {
return err
}
return err
}