You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by el...@apache.org on 2019/07/24 14:43:39 UTC

[trafficcontrol] branch master updated: Fix grove log fractional seconds

This is an automated email from the ASF dual-hosted git repository.

elsloo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 87f1546  Fix grove log fractional seconds
87f1546 is described below

commit 87f15461b8fbad7d894130b4738c3430c3e60f22
Author: Robert Butts <ro...@apache.org>
AuthorDate: Tue Jul 23 21:51:48 2019 -0600

    Fix grove log fractional seconds
---
 CHANGELOG.md                 |  1 +
 grove/plugin/ats_log.go      |  6 ++---
 grove/plugin/ats_log_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 070856b..f12da5b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -54,6 +54,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
 - Issue #3605: Fixed Traffic Monitor custom ports in health polling URL.
 - Issue 3587: Fixed Traffic Ops Golang reverse proxy and Riak logs to be consistent with the format of other error logs.
 - Database migrations have been collapsed. Rollbacks to migrations that previously existed are no longer possible.
+- Issue #3750: Fixed Grove access log fractional seconds.
 - Issue #3646: Fixed Traffic Monitor Thresholds.
 
 ## [3.0.0] - 2018-10-30
diff --git a/grove/plugin/ats_log.go b/grove/plugin/ats_log.go
index b40f7a4..9cfc393 100644
--- a/grove/plugin/ats_log.go
+++ b/grove/plugin/ats_log.go
@@ -117,10 +117,10 @@ func atsEventLogStr(
 ) string {
 	unixNano := timestamp.UnixNano()
 	unixSec := unixNano / NSPerSec
-	unixFrac := 1 / (unixNano % NSPerSec)
+	unixFrac := (unixNano / (NSPerSec / 1000)) - (unixSec * 1000) // gives fractional seconds to three decimal points, like the ATS logs.
 	unixFracStr := strconv.FormatInt(unixFrac, 10)
-	if len(unixFracStr) > 3 {
-		unixFracStr = unixFracStr[:3]
+	for len(unixFracStr) < 3 {
+		unixFracStr = "0" + unixFracStr // leading zeros, so e.g. a fraction of '42' becomes '1234.042' not '1234.42'
 	}
 	cfsc := "FIN"
 	if !clientRespSuccess {
diff --git a/grove/plugin/ats_log_test.go b/grove/plugin/ats_log_test.go
new file mode 100644
index 0000000..757310e
--- /dev/null
+++ b/grove/plugin/ats_log_test.go
@@ -0,0 +1,57 @@
+package plugin
+
+/*
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+import (
+	"fmt"
+	"strings"
+	"testing"
+	"time"
+)
+
+func TestATSLogTimeFractionalSeconds(t *testing.T) {
+	testTimes := []int64{
+		1563936732547355432,
+		1563937732000355432,
+		1563936732000000000,
+		1463136732999000000,
+		1563916732009000000,
+		1503936232090000000,
+		1563936732099000000,
+		1563936722900000000,
+		1563236282909000000,
+	}
+	for _, testTime := range testTimes {
+		timestamp := time.Unix(0, testTime)
+
+		logStr := atsEventLogStr(timestamp, "", "", "", "", "", "", "", "", "", 0, 0, 0, 0, 0, false, false, "", "", "", "", "", 0)
+
+		logFields := strings.Fields(logStr)
+		if len(logFields) < 1 {
+			t.Fatalf("atsEventLogStr expected >1 fields, actual %v", len(logFields))
+		}
+
+		timeField := logFields[0]
+
+		// the time field should be the Unix timestamp in seconds, as a float with 3 decimal places.
+		unixNano := timestamp.UnixNano()
+		unixSec := float64(unixNano) / float64(NSPerSec)
+		unixSecThreeDecimalPts := fmt.Sprintf("%.3f", unixSec)
+
+		if timeField != unixSecThreeDecimalPts {
+			t.Errorf("atsEventLogStr time expected '%v' actual '%v'", unixSecThreeDecimalPts, timeField)
+		}
+	}
+}