You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2020/08/31 15:15:00 UTC

[GitHub] [trafficcontrol] rob05c commented on a change in pull request #4993: Adds support for CSV parsing in astats

rob05c commented on a change in pull request #4993:
URL: https://github.com/apache/trafficcontrol/pull/4993#discussion_r480198408



##########
File path: traffic_monitor/cache/astats_csv.go
##########
@@ -0,0 +1,114 @@
+package cache
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 (
+	"bufio"
+	"errors"
+	"fmt"
+	"io"
+	"strconv"
+	"strings"
+
+	"github.com/apache/trafficcontrol/lib/go-log"
+)
+
+type astatsDataCsv struct {
+	Ats map[string]interface{}
+}
+
+func astatsCsvParseCsv(cacheName string, data io.Reader, pollCTX interface{}) (Statistics, map[string]interface{}, error) {
+	var stats Statistics
+	var err error
+	if data == nil {
+		log.Warnf("Cannot read stats data for cache '%s' - nil data reader", cacheName)
+		return stats, nil, errors.New("handler got nil reader")
+	}
+
+	var atsData astatsDataCsv
+	atsData.Ats = make(map[string]interface{})
+	scanner := bufio.NewScanner(data)
+
+	for scanner.Scan() {
+
+		line := scanner.Text()
+		delim := strings.IndexByte(line, ',')
+
+		// No delimiter found, skip this line as invalid
+		if delim < 0 {
+			continue
+		}
+		// Special cases where we just want the string value
+		if strings.Contains(line[0:delim], "proc.") || strings.Contains(line[0:delim], "inf.name") {
+			atsData.Ats[line[0:delim]] = line[delim+1 : len(line)]
+		} else {
+			value, err := strconv.ParseFloat(line[delim+1:len(line)], 64)
+
+			// Skip values that dont parse
+			if err != nil {
+				continue
+			}
+			atsData.Ats[line[0:delim]] = value
+		}
+	}
+
+	if len(atsData.Ats) < 1 {
+		return stats, nil, errors.New("No 'global' data object found in stats_over_http payload")
+	}
+
+	statMap := atsData.Ats
+
+	// Handle system specific values and remove them from the map for precomputing to not have issues
+	if stats.Loadavg, err = LoadavgFromRawLine(statMap["proc.loadavg"].(string)); err != nil {
+		return stats, nil, fmt.Errorf("Error parsing loadavg for cache '%s': %v", cacheName, err)

Review comment:
       To add some context as to why: it's so they compose well. For example, if the parent function did
   `return errors.New("doing foo: " + err.Error())`, it ends up as a pretty sentence, "doing foo: parsing loadavg...", and the final `log.Errorln` can/should start with a capital (and could add a period at the end, though people don't usually bother with that).
   
   As a nitpick to the nitpick, the word "Error" here is superfluous for the same reason.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org