Skip to content

Commit

Permalink
Better flag name, add prom-tag-filter flag
Browse files Browse the repository at this point in the history
  • Loading branch information
negbie committed Oct 26, 2019
1 parent d0d4ac0 commit 487069c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

$template fancy,"%syslogseverity% %hostname% %programname%%msg%\n"

action(type="omprog" name="fancy" template="fancy" output="/var/log/fancy.log" binary="/opt/fancy -lokiurl http://lokihost:3100")
action(type="omprog" name="fancy" template="fancy" output="/var/log/fancy.log" binary="/opt/fancy --loki-url http://lokihost:3100")
```
5. Make sure you have set the right Loki URL
6. Restart `rsyslog`. systemctl restart rsyslog
Expand Down
4 changes: 2 additions & 2 deletions example/rsyslog/rsyslog.conf
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ ruleset(name="ruleset_fancy"
queue.workerThreadMinimumMessages="60000"
queue.maxDiskSpace="2g"
queue.saveOnShutdown="on") {
action(type="omprog" name="fancy" template="fancy" output="/var/log/fancy.log" binary="/opt/fancy -metriconly") # This will only collect metrics. Use it only once!
action(type="omprog" name="fancy" template="fancy" output="/var/log/fancy.log" binary="/opt/fancy --metric-only") # This will only collect metrics. Use it only once!
if $syslogseverity <= '4' then {
action(type="omprog" name="fancy" template="fancy" output="/var/log/fancy.log" binary="/opt/fancy -lokiurl http://localhost:3100")
action(type="omprog" name="fancy" template="fancy" output="/var/log/fancy.log" binary="/opt/fancy --loki-url http://localhost:3100")
}

# Create more rules here
Expand Down
47 changes: 28 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,33 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

const version = "1.4"
const version = "1.5"
const scanSize = 20

func main() {
fs := flag.NewFlagSet("fancy", flag.ExitOnError)
var (
lokiURL = fs.String("lokiurl", "http://localhost:3100", "Loki Server URL")
chanSize = fs.Int("chansize", 10000, "Loki buffered channel capacity")
batchSize = fs.Int("batchsize", 100*1024, "Loki will batch these bytes before sending them")
batchWait = fs.Int("batchwait", 4, "Loki will send logs after these seconds")
cmd = fs.String("cmd", "", "Send input msg to external command and use it's output as new msg")
metricOnly = fs.Bool("metriconly", false, "Only metrics for Prometheus will be exposed")
promAddr = fs.String("promaddr", ":9090", "Prometheus scrape endpoint address")
promTag = fs.String("promtag", "", "Will be used as a tag label for the fancy_input_scan_total metric")
cmd = fs.String("cmd", "", "Send input msg to external command and use it's output as new msg")
lokiURL = fs.String("loki-url", "http://localhost:3100", "Loki Server URL")
chanSize = fs.Int("chan-size", 10000, "Loki buffered channel capacity")
batchSize = fs.Int("batch-size", 100*1024, "Loki will batch these bytes before sending them")
batchWait = fs.Int("batch-wait", 4, "Loki will send logs after these seconds")
metricOnly = fs.Bool("metric-only", false, "Only metrics for Prometheus will be exposed")
promAddr = fs.String("prom-addr", ":9090", "Prometheus scrape endpoint address")
promTag = fs.String("prom-tag", "", "Will be used as a tag label for the fancy_input_scan_total metric")
promTagFilter = fs.String("prom-tag-filter", "", "Use prom-tag only when msg contains this string")
)
fs.Parse(os.Args[1:])

t := time.Now()
defer fmt.Fprintf(os.Stderr, "%v end fancy with flags %s\n", t, os.Args[1:])

input := &Input{
cmd: strings.Fields(*cmd),
promTag: *promTag,
metricOnly: *metricOnly,
scanChan: make(chan [scanSize][]byte, 1000),
cmd: strings.Fields(*cmd),
promTag: *promTag,
promTagFilter: []byte(*promTagFilter),
metricOnly: *metricOnly,
scanChan: make(chan [scanSize][]byte, 1000),
}

if *metricOnly {
Expand Down Expand Up @@ -82,12 +84,13 @@ var (
)

type Input struct {
scanChan chan [scanSize][]byte
lineChan chan *LogLine
metricOnly bool
cmd []string
promTag string
cache Cache
scanChan chan [scanSize][]byte
lineChan chan *LogLine
metricOnly bool
cmd []string
promTag string
promTagFilter []byte
cache Cache
}

type Cache struct {
Expand Down Expand Up @@ -134,6 +137,12 @@ func (in *Input) process() {
}

if in.metricOnly {
if len(in.promTagFilter) > 0 {
if !bytes.Contains(ll.Raw[ll.MsgPos:], in.promTagFilter) {
in.promTag = ""
}
}

rawSize := float64(len(ll.Raw))
logScanNumber.WithLabelValues(ll.Hostname, ll.Program, ll.Severity, in.promTag).Inc()
logScanSize.WithLabelValues(ll.Hostname, ll.Program).Add(rawSize)
Expand Down
7 changes: 4 additions & 3 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ func Test_parseLine(t *testing.T) {
func Benchmark_parseLine(b *testing.B) {
input := &Input{
//cmd: []string{"tr", "[a-z]", "[A-Z]"},
metricOnly: true,
lineChan: make(chan *LogLine, 1000),
scanChan: make(chan [scanSize][]byte, 1000),
metricOnly: true,
promTagFilter: []byte("val1"),
lineChan: make(chan *LogLine, 1000),
scanChan: make(chan [scanSize][]byte, 1000),
}

var stdout bytes.Buffer
Expand Down

0 comments on commit 487069c

Please sign in to comment.