Skip to content

Commit

Permalink
Cleanup Makefile, Travis config, and Linting (#13)
Browse files Browse the repository at this point in the history
* Cleanup Makefile, Travis config, and Linting

* Create bin directory in GOPATH
  • Loading branch information
rodaine authored Jul 6, 2017
1 parent ee0de5c commit 04b5328
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 150 deletions.
68 changes: 2 additions & 66 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,67 +1,3 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml
data_sparse_backup

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# IDEA files
.idea
runtime.iml
.DS_Store

# Emacs Rulz! (false)
*~

# Vim Rulz! (true)
*.swp
vendor
vendor
cover.*
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
sudo: required
language: go
go: 1.7.1
go:
- 1.7.4
- 1.8.2

install: make bootstrap
script: make compile-test
before_install: mkdir -p $GOPATH/bin
install: make install
script: make lint test
42 changes: 31 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
.PHONY: bootstrap
bootstrap:
script/install-glide
# the name of this package
PKG := $(shell go list .)

.PHONY: install
install: glide #download dependencies (including test deps) for the package
glide install

.PHONY: update
update:
glide up
glide install
update: glide #updates dependencies used by the package and installs them
glide update

.PHONY: glide
glide: # ensure the glide package tool is installed
which glide || (curl https://glide.sh/get | sh)

.PHONY: lint
lint: #lints the package for common code smells
which golint || go get -u github.com/golang/lint/golint
test -z "$(gofmt -d -s ./*.go)" || (gofmt -d -s ./*.go && exit 1)
golint -set_exit_status
go tool vet -all -shadow -shadowstrict *.go

.PHONY: test
test: # runs all tests against the package with race detection and coverage percentage
go test -race -cover

.PHONY: quick
quick: # runs all tests without coverage or the race detector
go test

.PHONY: compile-test
compile-test:
go test -cover -race $(shell glide nv)
gofmt -l .
go tool vet *.go
.PHONY: cover
cover: # runs all tests against the package, generating a coverage report and opening it in the default browser
go test -race -covermode=atomic -coverprofile=cover.out
go tool cover -html cover.out -o cover.html
open cover.html
4 changes: 2 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions logging_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package stats

import logger "github.com/Sirupsen/logrus"

type loggingSink struct {
}
type loggingSink struct{}

// NewLoggingSink returns a Sink that flushes stats to os.StdErr.
func NewLoggingSink() Sink {
return &loggingSink{}
}
Expand Down
19 changes: 12 additions & 7 deletions mock_sink.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package stats

type mockSink struct {
// MockSink describes an in-memory Sink used for testing.
type MockSink struct {
Counters map[string]uint64
Timers map[string]uint64
Gauges map[string]uint64
}

// NewMockSink returns a Mock Sink that flushes stats to in memory maps.
func NewMockSink() (m *mockSink) {
m = &mockSink{
// NewMockSink returns a MockSink that flushes stats to in-memory maps. An
// instance of MockSink is not safe for concurrent use.
func NewMockSink() (m *MockSink) {
m = &MockSink{
Counters: make(map[string]uint64),
Timers: make(map[string]uint64),
Gauges: make(map[string]uint64),
Expand All @@ -17,14 +19,17 @@ func NewMockSink() (m *mockSink) {
return
}

func (m *mockSink) FlushCounter(name string, value uint64) {
// FlushCounter satisfies the Sink interface.
func (m *MockSink) FlushCounter(name string, value uint64) {
m.Counters[name] += value
}

func (m *mockSink) FlushGauge(name string, value uint64) {
// FlushGauge satisfies the Sink interface.
func (m *MockSink) FlushGauge(name string, value uint64) {
m.Gauges[name] = value
}

func (m *mockSink) FlushTimer(name string, value float64) {
// FlushTimer satisfies the Sink interface.
func (m *MockSink) FlushTimer(name string, value float64) {
m.Timers[name]++
}
23 changes: 0 additions & 23 deletions script/install-glide

This file was deleted.

55 changes: 29 additions & 26 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func NewDefaultStore() Store {
newStore = NewStore(NewLoggingSink(), false)
go newStore.Start(time.NewTicker(10 * time.Second))
} else {
newStore = NewStore(NewTcpStatsdSink(), true)
newStore = NewStore(NewTCPStatsdSink(), true)
go newStore.Start(time.NewTicker(time.Duration(settings.FlushIntervalS) * time.Second))
}
return newStore
Expand Down Expand Up @@ -364,19 +364,20 @@ func (s *statStore) NewCounter(name string) Counter {

if ok {
return c
} else {
c = &counter{}
}

s.countersMtx.Lock()
s.counters[name] = c
s.countersMtx.Unlock()
c = &counter{}

if s.export && expvar.Get(name) == nil {
expvar.Publish(name, c)
}
s.countersMtx.Lock()
s.counters[name] = c
s.countersMtx.Unlock()

return c
if s.export && expvar.Get(name) == nil {
expvar.Publish(name, c)
}

return c

}

func (s *statStore) NewCounterWithTags(name string, tags map[string]string) Counter {
Expand All @@ -403,19 +404,20 @@ func (s *statStore) NewGauge(name string) Gauge {

if ok {
return g
} else {
g = &gauge{}
}

s.gaugesMtx.Lock()
s.gauges[name] = g
s.gaugesMtx.Unlock()
g = &gauge{}

if s.export && expvar.Get(name) == nil {
expvar.Publish(name, g)
}
s.gaugesMtx.Lock()
s.gauges[name] = g
s.gaugesMtx.Unlock()

return g
if s.export && expvar.Get(name) == nil {
expvar.Publish(name, g)
}

return g

}

func (s *statStore) NewGaugeWithTags(name string, tags map[string]string) Gauge {
Expand All @@ -442,15 +444,16 @@ func (s *statStore) NewTimer(name string) Timer {

if ok {
return t
} else {
t = &timer{name: name, sink: s.sink}
}

s.timersMtx.Lock()
s.timers[name] = t
s.timersMtx.Unlock()
t = &timer{name: name, sink: s.sink}

s.timersMtx.Lock()
s.timers[name] = t
s.timersMtx.Unlock()

return t

return t
}
}

func (s *statStore) NewTimerWithTags(name string, tags map[string]string) Timer {
Expand Down
20 changes: 10 additions & 10 deletions tcp_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const (
logOnEveryNDropped = 1000
)

// NewTcpStatsdSink returns a Sink that is backed by a go channel with a limit of 1000 messages.
func NewTcpStatsdSink() Sink {
// NewTCPStatsdSink returns a Sink that is backed by a go channel with a limit of 1000 messages.
func NewTCPStatsdSink() Sink {
sink := &tcpStatsdSink{
outc: make(chan string, 1000),
}
Expand Down Expand Up @@ -71,27 +71,27 @@ func (s *tcpStatsdSink) FlushTimer(name string, value float64) {
}
}

func (w *tcpStatsdSink) run() {
func (s *tcpStatsdSink) run() {
settings := GetSettings()
var writer *bufio.Writer
var err error
for {
if w.conn == nil {
w.conn, err = net.Dial("tcp", fmt.Sprintf("%s:%d", settings.StatsdHost,
if s.conn == nil {
s.conn, err = net.Dial("tcp", fmt.Sprintf("%s:%d", settings.StatsdHost,
settings.StatsdPort))
if err != nil {
logger.Warnf("statsd connection error: %s", err)
time.Sleep(3 * time.Second)
continue
}
writer = bufio.NewWriter(w.conn)
writer = bufio.NewWriter(s.conn)
}

// Receive from the channel and check if the channel has been closed
metric, ok := <-w.outc
metric, ok := <-s.outc
if !ok {
logger.Warnf("Closing statsd client")
w.conn.Close()
s.conn.Close()
return
}

Expand All @@ -100,8 +100,8 @@ func (w *tcpStatsdSink) run() {

if err != nil {
logger.Warnf("Writing to statsd failed: %s", err)
_ = w.conn.Close() // Ignore close failures
w.conn = nil
_ = s.conn.Close() // Ignore close failures
s.conn = nil
writer = nil
}
}
Expand Down

0 comments on commit 04b5328

Please sign in to comment.