This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from carlpett/input-stdin
Implement initial stdin support
- Loading branch information
Showing
4 changed files
with
74 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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 { | ||
} | ||
|
||
func init() { | ||
registerInput("stdin", newStdinInput) | ||
} | ||
|
||
func newStdinInput() (StreamInput, error) { | ||
return StdinInput{}, nil | ||
} | ||
|
||
func (input StdinInput) StartStream(ch chan<- string) { | ||
reader := bufio.NewReader(os.Stdin) | ||
scanner := bufio.NewScanner(reader) | ||
|
||
for scanner.Scan() { | ||
ch <- scanner.Text() | ||
} | ||
|
||
if *writeOnEOF { | ||
writeMetrics(os.Stdout) | ||
} | ||
if *quitOnEOF { | ||
close(ch) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |