Skip to content

Commit

Permalink
Merge pull request #19 from infrawatch/pass_linting
Browse files Browse the repository at this point in the history
Initial round of passing lint tests in sg-core
  • Loading branch information
leifmadsen authored Dec 2, 2020
2 parents f50b8e7 + eb8809d commit c6ef123
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build/test-framework/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ go get -u honnef.co/go/tools/cmd/staticcheck
# run code validation tools
echo " *** Running pre-commit code validation"
echo " --- [TODO] Tests expected to fail currently. Changes required to pass all testing. Disable for now."
#./build/test-framework/pre-commit
./build/test-framework/pre-commit

# run unit tests
echo " *** Running test suite"
Expand Down
8 changes: 4 additions & 4 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

const UNIX_SOCKET_PATH string = "/tmp/smartgateway"
const unixSocketPath string = "/tmp/smartgateway"

func startPromHttp(host string, port int) (registry *prometheus.Registry) {
func startPromHTTP(host string, port int) (registry *prometheus.Registry) {
registry = prometheus.NewRegistry()

//Set up Metric Exporter
Expand Down Expand Up @@ -79,7 +79,7 @@ func main() {
port := inetCommand.Int("port", 0, "Port to use, otherwise OS will choose")

// Add Flags for shared command
socketPath := unixCommand.String("path", UNIX_SOCKET_PATH, "Path/file for the shared memeory socket")
socketPath := unixCommand.String("path", unixSocketPath, "Path/file for the shared memeory socket")

flag.Parse()

Expand Down Expand Up @@ -148,7 +148,7 @@ func main() {
defer pprof.StopCPUProfile()
}

registry := startPromHttp(*promhost, *promport)
registry := startPromHTTP(*promhost, *promport)

if inetCommand.Parsed() {
ip := net.ParseIP(*ipAddress)
Expand Down
6 changes: 3 additions & 3 deletions cmd/udpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func main() {
os.Exit(1)
}

destIp := args[0]
netIP := net.ParseIP(destIp)
destIP := args[0]
netIP := net.ParseIP(destIP)
if netIP == nil {
fmt.Fprintf(os.Stderr, "Invalid target IP addres %s...", destIp)
fmt.Fprintf(os.Stderr, "Invalid target IP addres %s...", destIP)
usage()
os.Exit(1)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/cacheutil/cacheserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ func (cs *CacheServer) Run(ctx context.Context) error {
goto done
default:
e := cs.entries.Front()
n := cs.entries.Front()
for {
if e == nil {
break
}

if e.Value.(Expiry).Expired() {
e.Value.(Expiry).Delete()
n = e.Next()
n := e.Next()
cs.entries.Remove(e)
e = n
continue
Expand Down
2 changes: 1 addition & 1 deletion pkg/cacheutil/cacheserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type LabelSeries struct {
}

func (ls *LabelSeries) Expired() bool {
return time.Now().Sub(ls.lastArrival).Seconds() >= ls.interval
return time.Since(ls.lastArrival).Seconds() >= ls.interval
}

func (ls *LabelSeries) Delete() {
Expand Down
7 changes: 4 additions & 3 deletions pkg/collectd/collectd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
jsoniter "github.com/json-iterator/go"
)

type CollectdMeta struct {
type collectdMeta struct {
X map[string]interface{} `json:"-"` // Rest of the fields should go here.
}

Expand All @@ -23,16 +23,17 @@ type Collectd struct {
PluginInstance string `json:"plugin_instance,omitempty"`
Type string `json:"type"`
TypeInstance string `json:"type_instance,omitempty"`
Meta CollectdMeta `json:"meta,omitempty"`
Meta collectdMeta `json:"meta,omitempty"`
}

// ParseInputString ...
func (c *Collectd) ParseInputString(jsonString string) (*[]Collectd, error) {
jsonBlob := []byte(jsonString)

return c.ParseInputByte(jsonBlob)
}

//ParseInputJSON ...
//ParseInputByte ...
func (c *Collectd) ParseInputByte(jsonBlob []byte) (*[]Collectd, error) {
collect := []Collectd{}
//var json = jsoniter.ConfigCompatibleWithStandardLibrary.BorrowIterator(jsonBlob)
Expand Down
1 change: 1 addition & 0 deletions pkg/collectd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const cpuMetricTemplate = `{"values": [%d], "dstypes": ["derive"], "dsnames": ["
"time": %f, "interval": %f, "host": "%s", "plugin": "cpu",
"plugin_instance": "%d","type": "cpu","type_instance": "user"}`

// GenCPUMetric ...
func GenCPUMetric(interval int, host string, count int) (mesg []byte) {
msgBuffer := make([]byte, 0, 1024)

Expand Down
1 change: 1 addition & 0 deletions pkg/inetserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func init() {
msgBuffer = make([]byte, maxBufferSize)
}

// Listen ...
func Listen(ctx context.Context, address string, w *bufio.Writer) (err error) {
prometheus.MustRegister(msgRecvd)

Expand Down
1 change: 1 addition & 0 deletions pkg/udpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"
)

// SendMetrics ...
func SendMetrics(ctx context.Context, address string, count int, hostsCount int, mesg []byte) (err error) {
// Resolve the UDP address so that we can make use of DialUDP
// with an actual IP and port instead of a name (in case a
Expand Down
24 changes: 14 additions & 10 deletions pkg/unixserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

//AMQPHandler ...
// PromIntf ...
type PromIntf struct {
totalMetricsReceived uint64
totalAmqpReceived uint64
Expand All @@ -24,7 +24,7 @@ type PromIntf struct {
totalDecodeErrorsDesc *prometheus.Desc
}

//NewAMQPHandler ...
// NewPromIntf ...
func NewPromIntf(source string) *PromIntf {
plabels := prometheus.Labels{}
plabels["source"] = source
Expand Down Expand Up @@ -59,7 +59,7 @@ func (a *PromIntf) IncTotalMetricsReceived() {
a.totalMetricsReceived++
}

//IncTtoalAmqpReceived ...
//IncTotalAmqpReceived ...
func (a *PromIntf) IncTotalAmqpReceived() {
a.totalAmqpReceived++
}
Expand All @@ -69,12 +69,12 @@ func (a *PromIntf) AddTotalReceived(num int) {
a.totalMetricsReceived += uint64(num)
}

//GetTotalReceived ...
//GetTotalMetricsReceived ...
func (a *PromIntf) GetTotalMetricsReceived() uint64 {
return a.totalMetricsReceived
}

//GetTotalReceived ...
//GetTotalAmqpReceived ...
func (a *PromIntf) GetTotalAmqpReceived() uint64 {
return a.totalAmqpReceived
}
Expand Down Expand Up @@ -130,15 +130,18 @@ func genMetricName(cd *collectd.Collectd, index int) (name string) {
return
}

// CDMetricDescription ...
type CDMetricDescription struct {
metricName string
metricDesc *prometheus.Desc
}

// CDMetricDescriptions ...
type CDMetricDescriptions struct {
descriptions map[string]*CDMetricDescription
}

// NewCDMetricDescriptions ...
func NewCDMetricDescriptions() (metricDescriptions *CDMetricDescriptions) {
metricDescriptions = &CDMetricDescriptions{make(map[string]*CDMetricDescription)}

Expand Down Expand Up @@ -184,7 +187,7 @@ func (cdls *CDLabelSeries) keepAlive() {
}

func (cdls *CDLabelSeries) staleTime() float64 {
return time.Now().Sub(cdls.lastArrival).Seconds()
return time.Since(cdls.lastArrival).Seconds()
}

// Expired implements cacheutil.Expiry
Expand All @@ -205,20 +208,23 @@ type CDMetric struct {
deleteFn deleteFn
}

// NewCDMetric ...
func NewCDMetric() *CDMetric {
return &CDMetric{
labels: make(map[string]*CDLabelSeries),
mu: sync.RWMutex{},
}
}

// Set ...
func (cdm *CDMetric) Set(labelName string, cdlm *CDLabelSeries) {
cdm.mu.Lock()
defer cdm.mu.Unlock()

cdm.labels[labelName] = cdlm
}

// Get ...
func (cdm *CDMetric) Get(labelName string) *CDLabelSeries {
cdm.mu.RLock()
defer cdm.mu.RUnlock()
Expand All @@ -230,10 +236,7 @@ func (cdm *CDMetric) Expired() bool {
cdm.mu.RLock()
defer cdm.mu.RUnlock()

if len(cdm.labels) == 0 {
return true
}
return false
return len(cdm.labels) == 0
}

// Delete implements cacheutil.Expiry
Expand Down Expand Up @@ -385,6 +388,7 @@ func (a *CDMetrics) Collect(ch chan<- prometheus.Metric) {
}
}

// Listen ...
func Listen(ctx context.Context, address string, w *bufio.Writer, registry *prometheus.Registry, usetimestamp bool) (err error) {
var laddr net.UnixAddr

Expand Down

0 comments on commit c6ef123

Please sign in to comment.