From ec80fc013f7ba421686d17d60e33b7906ea50fdb Mon Sep 17 00:00:00 2001
From: Matthias Rampke <mr@soundcloud.com>
Date: Tue, 9 Jan 2018 12:27:42 +0000
Subject: [PATCH] Log to syslog

when the exporter runs, we can assume that syslog is available. I don't
think stdout goes anywhere. Add and expand a few log messages. Log every
stats line as it is received, before processing.
---
 README.md   | 3 +++
 exporter.go | 4 +++-
 main.go     | 7 +++++++
 3 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index f6f2315..7a7d586 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,5 @@
 # rsyslog_exporter [![Build Status](https://travis-ci.org/digitalocean/rsyslog_exporter.svg?branch=master)](https://travis-ci.org/digitalocean/rsyslog_exporter)
+
 A [prometheus](http://prometheus.io/) exporter for [rsyslog](http://rsyslog.com). It accepts rsyslog [impstats](http://www.rsyslog.com/doc/master/configuration/modules/impstats.html) metrics in JSON format over stdin via the rsyslog [omprog](http://www.rsyslog.com/doc/v8-stable/configuration/modules/omprog.html) plugin and transforms and exposes them for consumption by Prometheus.
 
 ## Rsyslog Configuration
@@ -21,6 +22,8 @@ ruleset(name="process_stats") {
 }
 ```
 
+The exporter itself logs back via syslog, this cannot be configured at the moment.
+
 ## Provided Metrics
 The following metrics provided by the rsyslog impstats module are tracked by rsyslog_exporter:
 
diff --git a/exporter.go b/exporter.go
index 5d2fdf2..76f353d 100644
--- a/exporter.go
+++ b/exporter.go
@@ -138,13 +138,15 @@ func (re *rsyslogExporter) Collect(ch chan<- prometheus.Metric) {
 
 func (re *rsyslogExporter) run() {
 	for re.scanner.Scan() {
+		log.Printf("received line: %s", re.scanner.Bytes())
 		err := re.handleStatLine(re.scanner.Bytes())
 		if err != nil {
-			log.Print(err)
+			log.Printf("error handling stats line: %v", err)
 		}
 	}
 	if err := re.scanner.Err(); err != nil {
 		log.Printf("error reading input: %v", err)
 	}
+	log.Print("input ended, exiting normally")
 	os.Exit(0)
 }
diff --git a/main.go b/main.go
index 3d83d1a..fcd26d9 100644
--- a/main.go
+++ b/main.go
@@ -3,6 +3,7 @@ package main
 import (
 	"flag"
 	"log"
+	"log/syslog"
 	"net/http"
 	"os"
 	"os/signal"
@@ -16,6 +17,11 @@ var (
 )
 
 func main() {
+	logwriter, e := syslog.New(syslog.LOG_NOTICE, "rsyslog_exporter")
+	if e == nil {
+		log.SetOutput(logwriter)
+	}
+
 	flag.Parse()
 	exporter := newRsyslogExporter()
 
@@ -23,6 +29,7 @@ func main() {
 		c := make(chan os.Signal, 1)
 		signal.Notify(c, os.Interrupt)
 		<-c
+		log.Print("interrupt received, exiting")
 		os.Exit(0)
 	}()
 
-- 
GitLab