From 3c436a7cc21509eaac9274e67d7d507fbd123431 Mon Sep 17 00:00:00 2001 From: mamiller Date: Fri, 2 Jul 2021 13:16:49 -0400 Subject: [PATCH 1/4] allow exporting dates as timestamps grom GLOBAL STATUS Signed-off-by: mamiller --- collector/collector.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/collector/collector.go b/collector/collector.go index 386b98da..4987e1b6 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -19,6 +19,7 @@ import ( "regexp" "strconv" "strings" + "time" "github.com/prometheus/client_golang/prometheus" ) @@ -31,6 +32,8 @@ const ( // Query to check whether user/table/client stats are enabled. userstatCheckQuery = `SHOW GLOBAL VARIABLES WHERE Variable_Name='userstat' OR Variable_Name='userstat_running'` + // date layout + dateLayout = "Jan 02 15:04:05 2006 GMT" ) var logRE = regexp.MustCompile(`.+\.(\d+)$`) @@ -58,6 +61,10 @@ func parseStatus(data sql.RawBytes) (float64, bool) { case "non-primary", "disconnected": return 0, true } + if ts, err := time.Parse(dateLayout, string(data)); err == nil { + unixStamp := float64(ts.Unix()) + return unixStamp, err == nil + } if logNum := logRE.Find(data); logNum != nil { value, err := strconv.ParseFloat(string(logNum), 64) return value, err == nil From 7fb4c7cb10cef70815dd7b244591a4266a1b331f Mon Sep 17 00:00:00 2001 From: mamiller Date: Tue, 6 Jul 2021 15:00:14 -0400 Subject: [PATCH 2/4] the time package actually expects MST as the timezone layout indicator Signed-off-by: mamiller --- collector/collector.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/collector/collector.go b/collector/collector.go index 4987e1b6..e5dc992c 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -33,7 +33,7 @@ const ( userstatCheckQuery = `SHOW GLOBAL VARIABLES WHERE Variable_Name='userstat' OR Variable_Name='userstat_running'` // date layout - dateLayout = "Jan 02 15:04:05 2006 GMT" + dateLayout = "Jan 02 15:04:05 2006 MST" ) var logRE = regexp.MustCompile(`.+\.(\d+)$`) @@ -62,8 +62,7 @@ func parseStatus(data sql.RawBytes) (float64, bool) { return 0, true } if ts, err := time.Parse(dateLayout, string(data)); err == nil { - unixStamp := float64(ts.Unix()) - return unixStamp, err == nil + return float64(ts.Unix()), true } if logNum := logRE.Find(data); logNum != nil { value, err := strconv.ParseFloat(string(logNum), 64) From 0a9bdb90dfa747711e4dd03264521d07e3c311ea Mon Sep 17 00:00:00 2001 From: mamiller Date: Tue, 6 Jul 2021 17:58:13 -0400 Subject: [PATCH 3/4] handle multiple TS formats in GLOBAL STATUS Signed-off-by: mamiller --- collector/collector.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/collector/collector.go b/collector/collector.go index e5dc992c..b3d5a261 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -32,11 +32,10 @@ const ( // Query to check whether user/table/client stats are enabled. userstatCheckQuery = `SHOW GLOBAL VARIABLES WHERE Variable_Name='userstat' OR Variable_Name='userstat_running'` - // date layout - dateLayout = "Jan 02 15:04:05 2006 MST" ) var logRE = regexp.MustCompile(`.+\.(\d+)$`) +var sslMetricDateRe = regexp.MustCompile(`.+\d{2}:\d{2}:\d{2} \d{4} [A-Z]{3}$`) func newDesc(subsystem, name, help string) *prometheus.Desc { return prometheus.NewDesc( @@ -46,6 +45,7 @@ func newDesc(subsystem, name, help string) *prometheus.Desc { } func parseStatus(data sql.RawBytes) (float64, bool) { + var dateLayout = "2006-01-02 15:04:05" dataString := strings.ToLower(string(data)) switch dataString { case "yes", "on": @@ -61,6 +61,9 @@ func parseStatus(data sql.RawBytes) (float64, bool) { case "non-primary", "disconnected": return 0, true } + if sslMetricDateRe.FindString(string(data)) != "" { + dateLayout = "Jan 02 15:04:05 2006 MST" + } if ts, err := time.Parse(dateLayout, string(data)); err == nil { return float64(ts.Unix()), true } From 144a4ad595dce14ef520af9cedd28a0a5a9897b1 Mon Sep 17 00:00:00 2001 From: mamiller Date: Thu, 8 Jul 2021 18:20:43 -0400 Subject: [PATCH 4/4] suggested changes Signed-off-by: mamiller --- collector/collector.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/collector/collector.go b/collector/collector.go index b3d5a261..dfe692fd 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -35,7 +35,6 @@ const ( ) var logRE = regexp.MustCompile(`.+\.(\d+)$`) -var sslMetricDateRe = regexp.MustCompile(`.+\d{2}:\d{2}:\d{2} \d{4} [A-Z]{3}$`) func newDesc(subsystem, name, help string) *prometheus.Desc { return prometheus.NewDesc( @@ -45,7 +44,6 @@ func newDesc(subsystem, name, help string) *prometheus.Desc { } func parseStatus(data sql.RawBytes) (float64, bool) { - var dateLayout = "2006-01-02 15:04:05" dataString := strings.ToLower(string(data)) switch dataString { case "yes", "on": @@ -61,10 +59,10 @@ func parseStatus(data sql.RawBytes) (float64, bool) { case "non-primary", "disconnected": return 0, true } - if sslMetricDateRe.FindString(string(data)) != "" { - dateLayout = "Jan 02 15:04:05 2006 MST" + if ts, err := time.Parse("Jan 02 15:04:05 2006 MST", string(data)); err == nil { + return float64(ts.Unix()), true } - if ts, err := time.Parse(dateLayout, string(data)); err == nil { + if ts, err := time.Parse("2006-01-02 15:04:05", string(data)); err == nil { return float64(ts.Unix()), true } if logNum := logRE.Find(data); logNum != nil {