2015-09-26 17:36:40 +02:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-01-25 13:28:58 +01:00
|
|
|
// +build !notextfile
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-09-03 16:59:56 +02:00
|
|
|
"sort"
|
2015-01-25 13:28:58 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-12-31 17:19:37 +01:00
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
2015-09-17 14:05:56 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
2015-10-30 21:20:06 +01:00
|
|
|
"github.com/prometheus/common/expfmt"
|
2017-12-23 20:21:58 +01:00
|
|
|
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
2015-01-25 13:28:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-08-12 15:07:24 +02:00
|
|
|
textFileDirectory = kingpin.Flag("collector.textfile.directory", "Directory to read text files with metrics from.").Default("").String()
|
2017-12-23 20:21:58 +01:00
|
|
|
mtimeDesc = prometheus.NewDesc(
|
2018-01-22 14:02:19 +01:00
|
|
|
"node_textfile_mtime_seconds",
|
2017-12-23 20:21:58 +01:00
|
|
|
"Unixtime mtime of textfiles successfully read.",
|
|
|
|
[]string{"file"},
|
|
|
|
nil,
|
|
|
|
)
|
2015-01-25 13:28:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type textFileCollector struct {
|
2015-09-03 16:59:56 +02:00
|
|
|
path string
|
2017-12-23 20:21:58 +01:00
|
|
|
// Only set for testing to get predictable output.
|
2019-12-31 17:19:37 +01:00
|
|
|
mtime *float64
|
|
|
|
logger log.Logger
|
2015-01-25 13:28:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-09-28 15:06:26 +02:00
|
|
|
registerCollector("textfile", defaultEnabled, NewTextFileCollector)
|
2015-01-25 13:28:58 +01:00
|
|
|
}
|
|
|
|
|
2017-02-28 17:44:53 +01:00
|
|
|
// NewTextFileCollector returns a new Collector exposing metrics read from files
|
|
|
|
// in the given textfile directory.
|
2019-12-31 17:19:37 +01:00
|
|
|
func NewTextFileCollector(logger log.Logger) (Collector, error) {
|
2015-09-03 16:59:56 +02:00
|
|
|
c := &textFileCollector{
|
2019-12-31 17:19:37 +01:00
|
|
|
path: *textFileDirectory,
|
|
|
|
logger: logger,
|
2015-09-03 16:59:56 +02:00
|
|
|
}
|
2017-12-23 20:21:58 +01:00
|
|
|
return c, nil
|
|
|
|
}
|
2015-09-03 16:59:56 +02:00
|
|
|
|
2019-12-31 17:19:37 +01:00
|
|
|
func convertMetricFamily(metricFamily *dto.MetricFamily, ch chan<- prometheus.Metric, logger log.Logger) {
|
2017-12-23 20:21:58 +01:00
|
|
|
var valType prometheus.ValueType
|
|
|
|
var val float64
|
|
|
|
|
|
|
|
allLabelNames := map[string]struct{}{}
|
|
|
|
for _, metric := range metricFamily.Metric {
|
|
|
|
labels := metric.GetLabel()
|
|
|
|
for _, label := range labels {
|
|
|
|
if _, ok := allLabelNames[label.GetName()]; !ok {
|
|
|
|
allLabelNames[label.GetName()] = struct{}{}
|
2017-12-06 17:05:40 +01:00
|
|
|
}
|
2017-12-23 20:21:58 +01:00
|
|
|
}
|
2015-01-25 13:28:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-23 20:21:58 +01:00
|
|
|
for _, metric := range metricFamily.Metric {
|
2017-12-24 11:54:33 +01:00
|
|
|
if metric.TimestampMs != nil {
|
2019-12-31 17:19:37 +01:00
|
|
|
level.Warn(logger).Log("msg", "Ignoring unsupported custom timestamp on textfile collector metric", "metric", metric)
|
2017-12-24 11:54:33 +01:00
|
|
|
}
|
|
|
|
|
2017-12-23 20:21:58 +01:00
|
|
|
labels := metric.GetLabel()
|
|
|
|
var names []string
|
|
|
|
var values []string
|
|
|
|
for _, label := range labels {
|
|
|
|
names = append(names, label.GetName())
|
|
|
|
values = append(values, label.GetValue())
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := range allLabelNames {
|
|
|
|
present := false
|
|
|
|
for _, name := range names {
|
|
|
|
if k == name {
|
|
|
|
present = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 16:58:53 +01:00
|
|
|
if !present {
|
2017-12-23 20:21:58 +01:00
|
|
|
names = append(names, k)
|
|
|
|
values = append(values, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
metricType := metricFamily.GetType()
|
|
|
|
switch metricType {
|
|
|
|
case dto.MetricType_COUNTER:
|
|
|
|
valType = prometheus.CounterValue
|
|
|
|
val = metric.Counter.GetValue()
|
|
|
|
|
|
|
|
case dto.MetricType_GAUGE:
|
|
|
|
valType = prometheus.GaugeValue
|
|
|
|
val = metric.Gauge.GetValue()
|
|
|
|
|
|
|
|
case dto.MetricType_UNTYPED:
|
|
|
|
valType = prometheus.UntypedValue
|
|
|
|
val = metric.Untyped.GetValue()
|
|
|
|
|
|
|
|
case dto.MetricType_SUMMARY:
|
|
|
|
quantiles := map[float64]float64{}
|
|
|
|
for _, q := range metric.Summary.Quantile {
|
|
|
|
quantiles[q.GetQuantile()] = q.GetValue()
|
|
|
|
}
|
|
|
|
ch <- prometheus.MustNewConstSummary(
|
|
|
|
prometheus.NewDesc(
|
|
|
|
*metricFamily.Name,
|
|
|
|
metricFamily.GetHelp(),
|
|
|
|
names, nil,
|
|
|
|
),
|
|
|
|
metric.Summary.GetSampleCount(),
|
|
|
|
metric.Summary.GetSampleSum(),
|
|
|
|
quantiles, values...,
|
|
|
|
)
|
|
|
|
case dto.MetricType_HISTOGRAM:
|
|
|
|
buckets := map[float64]uint64{}
|
|
|
|
for _, b := range metric.Histogram.Bucket {
|
|
|
|
buckets[b.GetUpperBound()] = b.GetCumulativeCount()
|
|
|
|
}
|
|
|
|
ch <- prometheus.MustNewConstHistogram(
|
|
|
|
prometheus.NewDesc(
|
|
|
|
*metricFamily.Name,
|
|
|
|
metricFamily.GetHelp(),
|
|
|
|
names, nil,
|
|
|
|
),
|
|
|
|
metric.Histogram.GetSampleCount(),
|
|
|
|
metric.Histogram.GetSampleSum(),
|
|
|
|
buckets, values...,
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
panic("unknown metric type")
|
|
|
|
}
|
|
|
|
if metricType == dto.MetricType_GAUGE || metricType == dto.MetricType_COUNTER || metricType == dto.MetricType_UNTYPED {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
|
|
|
*metricFamily.Name,
|
|
|
|
metricFamily.GetHelp(),
|
|
|
|
names, nil,
|
|
|
|
),
|
|
|
|
valType, val, values...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2015-01-25 13:28:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-23 20:21:58 +01:00
|
|
|
func (c *textFileCollector) exportMTimes(mtimes map[string]time.Time, ch chan<- prometheus.Metric) {
|
2019-11-22 19:20:52 +01:00
|
|
|
if len(mtimes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-23 20:21:58 +01:00
|
|
|
// Export the mtimes of the successful files.
|
2019-11-22 19:20:52 +01:00
|
|
|
// Sorting is needed for predictable output comparison in tests.
|
|
|
|
filenames := make([]string, 0, len(mtimes))
|
|
|
|
for filename := range mtimes {
|
|
|
|
filenames = append(filenames, filename)
|
|
|
|
}
|
|
|
|
sort.Strings(filenames)
|
2017-12-23 20:21:58 +01:00
|
|
|
|
2019-11-22 19:20:52 +01:00
|
|
|
for _, filename := range filenames {
|
|
|
|
mtime := float64(mtimes[filename].UnixNano() / 1e9)
|
|
|
|
if c.mtime != nil {
|
|
|
|
mtime = *c.mtime
|
2017-12-23 20:21:58 +01:00
|
|
|
}
|
2019-11-22 19:20:52 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(mtimeDesc, prometheus.GaugeValue, mtime, filename)
|
2017-12-23 20:21:58 +01:00
|
|
|
}
|
2015-01-25 13:28:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-23 20:21:58 +01:00
|
|
|
// Update implements the Collector interface.
|
|
|
|
func (c *textFileCollector) Update(ch chan<- prometheus.Metric) error {
|
2019-11-22 19:20:52 +01:00
|
|
|
// Iterate over files and accumulate their metrics, but also track any
|
|
|
|
// parsing errors so an error metric can be reported.
|
|
|
|
var errored bool
|
2015-09-03 16:59:56 +02:00
|
|
|
files, err := ioutil.ReadDir(c.path)
|
|
|
|
if err != nil && c.path != "" {
|
2019-11-22 19:20:52 +01:00
|
|
|
errored = true
|
2019-12-31 17:19:37 +01:00
|
|
|
level.Error(c.logger).Log("msg", "failed to read textfile collector directory", "path", c.path, "err", err)
|
2015-09-03 16:59:56 +02:00
|
|
|
}
|
2018-02-27 19:43:38 +01:00
|
|
|
|
2019-11-22 19:20:52 +01:00
|
|
|
mtimes := make(map[string]time.Time, len(files))
|
2015-01-25 13:28:58 +01:00
|
|
|
for _, f := range files {
|
|
|
|
if !strings.HasSuffix(f.Name(), ".prom") {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-05 13:20:30 +02:00
|
|
|
|
2019-11-22 19:20:52 +01:00
|
|
|
mtime, err := c.processFile(f.Name(), ch)
|
2019-04-18 17:47:04 +02:00
|
|
|
if err != nil {
|
2019-11-22 19:20:52 +01:00
|
|
|
errored = true
|
2019-12-31 17:19:37 +01:00
|
|
|
level.Error(c.logger).Log("msg", "failed to collect textfile data", "file", f.Name(), "err", err)
|
2019-04-18 17:47:04 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-02-27 19:43:38 +01:00
|
|
|
|
2019-11-22 19:20:52 +01:00
|
|
|
mtimes[f.Name()] = *mtime
|
2015-01-25 13:28:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-23 20:21:58 +01:00
|
|
|
c.exportMTimes(mtimes, ch)
|
2015-09-03 16:59:56 +02:00
|
|
|
|
2015-01-25 13:28:58 +01:00
|
|
|
// Export if there were errors.
|
2019-11-22 19:20:52 +01:00
|
|
|
var errVal float64
|
|
|
|
if errored {
|
|
|
|
errVal = 1.0
|
|
|
|
}
|
|
|
|
|
2017-12-23 20:21:58 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
|
|
|
"node_textfile_scrape_error",
|
|
|
|
"1 if there was an error opening or reading a file, 0 otherwise",
|
|
|
|
nil, nil,
|
|
|
|
),
|
2019-11-22 19:20:52 +01:00
|
|
|
prometheus.GaugeValue, errVal,
|
2017-12-23 20:21:58 +01:00
|
|
|
)
|
2019-11-22 19:20:52 +01:00
|
|
|
|
2017-12-23 20:21:58 +01:00
|
|
|
return nil
|
2015-01-25 13:28:58 +01:00
|
|
|
}
|
2018-10-05 13:20:30 +02:00
|
|
|
|
2019-11-22 19:20:52 +01:00
|
|
|
// processFile processes a single file, returning its modification time on success.
|
|
|
|
func (c *textFileCollector) processFile(name string, ch chan<- prometheus.Metric) (*time.Time, error) {
|
|
|
|
path := filepath.Join(c.path, name)
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to open textfile data file %q: %v", path, err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var parser expfmt.TextParser
|
|
|
|
families, err := parser.TextToMetricFamilies(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse textfile data from %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasTimestamps(families) {
|
|
|
|
return nil, fmt.Errorf("textfile %q contains unsupported client-side timestamps, skipping entire file", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mf := range families {
|
|
|
|
if mf.Help == nil {
|
|
|
|
help := fmt.Sprintf("Metric read from %s", path)
|
|
|
|
mf.Help = &help
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mf := range families {
|
2019-12-31 17:19:37 +01:00
|
|
|
convertMetricFamily(mf, ch, c.logger)
|
2019-11-22 19:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only stat the file once it has been parsed and validated, so that
|
|
|
|
// a failure does not appear fresh.
|
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to stat %q: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t := stat.ModTime()
|
|
|
|
return &t, nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 13:20:30 +02:00
|
|
|
// hasTimestamps returns true when metrics contain unsupported timestamps.
|
|
|
|
func hasTimestamps(parsedFamilies map[string]*dto.MetricFamily) bool {
|
|
|
|
for _, mf := range parsedFamilies {
|
|
|
|
for _, m := range mf.Metric {
|
|
|
|
if m.TimestampMs != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|