-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (77 loc) · 2.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
)
var stdoutWriter io.Writer = os.Stdout
type writeFunc func(io.Writer) error
func withOutput(path string, fn writeFunc) error {
if path == "" {
return fn(stdoutWriter)
}
return withFileOutput(path, fn)
}
type cliFlags struct {
showVersion bool
showInputs bool
outputFile string
dirs bool
dirEntryPattern string
}
func (f *cliFlags) register(fs *flag.FlagSet) {
fs.BoolVar(&f.showVersion, "version", false, "Output version information and exit")
fs.BoolVar(&f.showInputs, "show-inputs", false, "Emit comment with paths of input files")
fs.StringVar(&f.outputFile, "output", "", "Write merged metrics to given file instead of standard output")
fs.BoolVar(&f.dirs, "dirs", false, "Read metrics from regular files in directories given as command arguments")
fs.StringVar(&f.dirEntryPattern, "dir-entry-pattern", "[^.]*.prom", "Glob pattern for directory entries")
}
func (f *cliFlags) inputs(fs *flag.FlagSet) ([]inputWrapper, error) {
if f.dirs {
return inputWrappersFromDirs(flag.Args(), f.dirEntryPattern)
}
var paths []string
if fs.NArg() == 0 {
paths = []string{stdinPlaceholder}
} else {
paths = fs.Args()
}
return inputWrappersFromPaths(paths), nil
}
func main() {
flag.Usage = func() {
w := flag.CommandLine.Output()
fmt.Fprintf(w, "Usage: %s [file...]\n", os.Args[0])
fmt.Fprintln(w, `
Combine one or multiple Prometheus text format inputs. Metric families sharing
a name must also have the same type. The lexicographically lowest help string
per family is used. The resulting metrics are not validated.
If no input files are given standard input is read. Use "-" as a placeholder to
combine standard input with regular files.
Flags:`)
flag.PrintDefaults()
}
var cf cliFlags
cf.register(flag.CommandLine)
flag.Parse()
if cf.showVersion {
showVersion(os.Stdout)
return
}
inputs, err := cf.inputs(flag.CommandLine)
if err != nil {
log.Fatal(err)
}
merged, err := readAndMerge(context.Background(), inputs)
if err != nil {
log.Fatal(err)
}
if err := withOutput(cf.outputFile, func(w io.Writer) error {
return merged.write(w, cf.showInputs)
}); err != nil {
log.Fatalf("Writing output failed: %v", err)
}
}