Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Commit

Permalink
Add quit-on-eof and write-on-eof flags to input stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
carlpett committed Feb 5, 2019
1 parent 4ff492b commit affeabe
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,21 @@ Example:
# Input modules
A number of input modules are available, described here with their parameters.

## Standard input
Reads from standard input.

The `stdin` input has two commandline flags:

- `input.stdin.quit-on-eof` (Optional, default `false`): If `true`, the exporter will quit after getting to end of file. By default, it will keep running, and the metrics endpoint will still be available.
- `input.stdin.write-on-eof` (Optional, default `false`): If `true`, all the collected metrics will be writted to stdout after getting to end of file. Useful in combination with `quit-on-eof` for configuration testing.

## File
Reads lines from a file.

The `file` input has two parameters:

- `-input.file.path` (Required) : The path to the file to read from.
- `-input.file.mode` (Optional, default `tail`): `tail` or `dryrun`. In `tail` mode, the file is opened and any new lines written to the file is processed. In `dryrun` mode, the entire file is read and processed. When the end of the file is encountered, the current metrics state is written to standard out, and the exporter exits. Useful for debugging and configuration testing.
- `-input.file.mode` (_Deprecated_, Optional, default `tail`): `tail` or `dryrun`. In `tail` mode, the file is opened and any new lines written to the file is processed. In `dryrun` mode, the entire file is read and processed. When the end of the file is encountered, the current metrics state is written to standard out, and the exporter exits. Useful for debugging and configuration testing. *NOTE:* The `dryrun` mode is deprecated, and using the `stdin` input along with `input.stdin.write-on-eof` is recommended as a replacement.

## Socket
Opens a socket for reading lines.
Expand Down Expand Up @@ -162,10 +170,5 @@ The `namedpipe` input has one parameter:

- `input.namedpipe.path` (Required): The path where the pipe should be created. This may require elevated privileges to execute a `mkfifo` syscall.

## Standard input
Reads from standard input.

The `stdin` input does not take any parameters.

# Building
The project uses [govendor](https://github.com/kardianos/govendor) for dependency management. To build the exporter, call `govendor build +p`.
15 changes: 1 addition & 14 deletions input/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package input

import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"os"

"github.com/hpcloud/tail"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/log"
)

Expand Down Expand Up @@ -66,17 +63,7 @@ func (input DryrunFileInput) StartStream(ch chan<- string) {
}

fmt.Println("Finished reading file, dumping final metrics endpoint output:")
metfam, err := prometheus.DefaultGatherer.Gather()
if err != nil {
log.Fatal(err)
}
out := &bytes.Buffer{}
for _, met := range metfam {
if _, err := expfmt.MetricFamilyToText(out, met); err != nil {
log.Fatal(err)
}
}
fmt.Println(out)
writeMetrics(os.Stdout)
}

func (input TailingFileInput) StartStream(ch chan<- string) {
Expand Down
13 changes: 13 additions & 0 deletions input/stdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package input

import (
"bufio"
"flag"
"os"
)

var (
writeOnEOF = flag.Bool("input.stdin.write-on-eof", false, "If all metrics should be written to stdout after seeing end of file")
quitOnEOF = flag.Bool("input.stdin.quit-on-eof", false, "If the exporter should exit after seeing end of file")
)

type StdinInput struct {
}

Expand All @@ -23,4 +29,11 @@ func (input StdinInput) StartStream(ch chan<- string) {
for scanner.Scan() {
ch <- scanner.Text()
}

if *writeOnEOF {
writeMetrics(os.Stdout)
}
if *quitOnEOF {
close(ch)
}
}
25 changes: 25 additions & 0 deletions input/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package input

import (
"bytes"
"fmt"
"io"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/log"
)

func writeMetrics(w io.Writer) {
metfam, err := prometheus.DefaultGatherer.Gather()
if err != nil {
log.Fatal(err)
}
out := &bytes.Buffer{}
for _, met := range metfam {
if _, err := expfmt.MetricFamilyToText(out, met); err != nil {
log.Fatal(err)
}
}
fmt.Fprintln(w, out)
}

0 comments on commit affeabe

Please sign in to comment.