Skip to content

Commit b4041b2

Browse files
authored
Upgrade CI, add reset metrics for prom (#1)
1 parent a373d22 commit b4041b2

11 files changed

+89
-48
lines changed

.github/workflows/golangci-lint.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
- v*
66
branches:
77
- master
8+
- main
89
pull_request:
910
jobs:
1011
golangci:
@@ -13,10 +14,10 @@ jobs:
1314
steps:
1415
- uses: actions/checkout@v2
1516
- name: golangci-lint
16-
uses: golangci/golangci-lint-action@v0.2.0
17+
uses: golangci/golangci-lint-action@v2.3.0
1718
with:
1819
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
19-
version: v1.27
20+
version: v1.32.2
2021

2122
# Optional: golangci-lint command line arguments.
2223
# args: ./the-only-dir-to-analyze/...
@@ -25,4 +26,4 @@ jobs:
2526
# The secret `GITHUB_TOKEN` is automatically created by GitHub,
2627
# no need to create it manually.
2728
# https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#about-the-github_token-secret
28-
github-token: ${{ secrets.GITHUB_TOKEN }}
29+
github-token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yml

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ on:
55
- v*
66
branches:
77
- master
8+
- main
89
pull_request:
910
env:
1011
GO111MODULE: "on"
1112
jobs:
1213
test:
1314
strategy:
1415
matrix:
15-
go-version: [1.11.x, 1.12.x, 1.13.x, 1.14.x]
16+
go-version: [1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x]
1617
runs-on: ubuntu-latest
1718
steps:
1819
- name: Install Go
@@ -30,7 +31,10 @@ jobs:
3031
- name: Populate dependencies
3132
run: '(test -d vendor && echo vendor found) || go mod vendor'
3233
- name: Test
33-
run: go test -mod=vendor -v -cover -race -coverprofile=coverage.txt -covermode=atomic ./...
34+
run: make test-unit
3435
- name: Upload code coverage
35-
if: matrix.go-version == '1.14.x'
36-
uses: codecov/codecov-action@v1
36+
if: matrix.go-version == '1.15.x'
37+
uses: codecov/codecov-action@v1
38+
with:
39+
file: ./unit.coverprofile
40+
flags: unittests

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/.idea
2-
/bench-*
2+
/bench-*
3+
/unit.coverprofile

.golangci.yml

-34
This file was deleted.

Makefile

+33-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
1-
GOLANGCI_LINT_VERSION := "v1.30.0"
1+
GOLANGCI_LINT_VERSION := "v1.32.2"
2+
3+
# The head of Makefile determines location of dev-go to include standard targets.
24
GO ?= go
3-
# detecting GOPATH and removing trailing "/" if any
4-
GOPATH = $(realpath $(shell go env GOPATH))
55
export GO111MODULE = on
66

7-
lint:
8-
@test -s $(GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION) || (curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /tmp $(GOLANGCI_LINT_VERSION) && mv /tmp/golangci-lint $(GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION))
9-
@$(GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION) run ./... --fix
7+
ifneq "$(GOFLAGS)" ""
8+
$(info GOFLAGS: ${GOFLAGS})
9+
endif
10+
11+
ifneq "$(wildcard ./vendor )" ""
12+
$(info Using vendor)
13+
modVendor = -mod=vendor
14+
ifeq (,$(findstring -mod,$(GOFLAGS)))
15+
export GOFLAGS := ${GOFLAGS} ${modVendor}
16+
endif
17+
ifneq "$(wildcard ./vendor/github.com/bool64/dev)" ""
18+
DEVGO_PATH := ./vendor/github.com/bool64/dev
19+
endif
20+
endif
21+
22+
ifeq ($(DEVGO_PATH),)
23+
DEVGO_PATH := $(shell GO111MODULE=on $(GO) list ${modVendor} -f '{{.Dir}}' -m github.com/bool64/dev)
24+
ifeq ($(DEVGO_PATH),)
25+
$(info Module github.com/bool64/dev not found, downloading.)
26+
DEVGO_PATH := $(shell export GO111MODULE=on && $(GO) mod tidy && $(GO) list -f '{{.Dir}}' -m github.com/bool64/dev)
27+
endif
28+
endif
29+
30+
-include $(DEVGO_PATH)/makefiles/main.mk
31+
-include $(DEVGO_PATH)/makefiles/test-unit.mk
32+
-include $(DEVGO_PATH)/makefiles/lint.mk
33+
-include $(DEVGO_PATH)/makefiles/github-actions.mk
34+
35+
## Run tests
36+
test: test-unit
1037

1138
bench:
1239
@$(GO) test -bench=. -count=10 -run=^a ./... | tee /dev/tty >bench-$(shell git symbolic-ref HEAD --short | tr / - 2>/dev/null).txt

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This library provides context-driven stats tracker.
55
[![Build Status](https://github.com/bool64/stats/workflows/test/badge.svg)](https://github.com/bool64/stats/actions?query=branch%3Amaster+workflow%3Atest)
66
[![Coverage Status](https://codecov.io/gh/bool64/stats/branch/master/graph/badge.svg)](https://codecov.io/gh/bool64/stats)
77
[![GoDevDoc](https://img.shields.io/badge/dev-doc-00ADD8?logo=go)](https://pkg.go.dev/github.com/bool64/stats)
8+
[![time tracker](https://wakatime.com/badge/github/bool64/stats.svg)](https://wakatime.com/badge/github/bool64/stats)
89
![Code lines](https://sloc.xyz/github/bool64/stats/?category=code)
910
![Comments](https://sloc.xyz/github/bool64/stats/?category=comments)
1011

dev_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package stats_test
2+
3+
import _ "github.com/bool64/dev"

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/bool64/stats
33
go 1.13
44

55
require (
6+
github.com/bool64/dev v0.1.7
67
github.com/kr/pretty v0.2.0 // indirect
78
github.com/prometheus/client_golang v1.7.1
89
github.com/stretchr/testify v1.5.1

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
66
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
77
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
88
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
9+
github.com/bool64/dev v0.1.7 h1:1xs4ff6lUFIt7euQCmfjU0Xi7IiSmwcQyMokKdq6qAM=
10+
github.com/bool64/dev v0.1.7/go.mod h1:pn52JC52uSgpazChx9CeXyG+S3sW2V36HHoLNBbscdg=
911
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
1012
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
1113
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

prom/tracker.go

+29
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func NewStatsTracker(registry *prometheus.Registry) (*Tracker, error) {
4949
type Tracker struct {
5050
mu sync.Mutex
5151
collectors map[identity]func(map[string]string, float64)
52+
registered []prometheus.Collector
5253
histograms map[string]prometheus.HistogramOpts
5354
summaries map[string]prometheus.SummaryOpts
5455
counters map[string]prometheus.CounterOpts
@@ -75,6 +76,22 @@ func (t *Tracker) Set(ctx context.Context, name string, absolute float64, labels
7576
t.collect(ctx, true, name, absolute, labelsAndValues)
7677
}
7778

79+
// Reset unregisters and removes all registered collectors.
80+
func (t *Tracker) Reset() {
81+
t.mu.Lock()
82+
defer t.mu.Unlock()
83+
84+
for k := range t.collectors {
85+
delete(t.collectors, k)
86+
}
87+
88+
for _, c := range t.registered {
89+
t.Registry.Unregister(c)
90+
}
91+
92+
t.registered = t.registered[:0]
93+
}
94+
7895
// DeclareHistogram registers histogram metric for given name.
7996
func (t *Tracker) DeclareHistogram(name string, opts prometheus.HistogramOpts) {
8097
t.mu.Lock()
@@ -211,6 +228,7 @@ func (t *Tracker) collect(ctx context.Context, isGauge bool, name string, value
211228
collect(lb.values, value)
212229
}
213230

231+
// nolint: dupl // Code uses unique symbols.
214232
func (t *Tracker) summary(
215233
ctx context.Context,
216234
canonicalID identity,
@@ -232,13 +250,16 @@ func (t *Tracker) summary(
232250
t.ErrLogger(ctx, err, labels)
233251
}
234252

253+
t.registered = append(t.registered, summary)
254+
235255
*collect = func(labelValues map[string]string, value float64) {
236256
summary.With(labelValues).Observe(value)
237257
}
238258

239259
return true
240260
}
241261

262+
// nolint: dupl // Code uses unique symbols.
242263
func (t *Tracker) histogram(
243264
ctx context.Context,
244265
canonicalID identity,
@@ -260,13 +281,16 @@ func (t *Tracker) histogram(
260281
t.ErrLogger(ctx, err, labels)
261282
}
262283

284+
t.registered = append(t.registered, histogram)
285+
263286
*collect = func(labelValues map[string]string, value float64) {
264287
histogram.With(labelValues).Observe(value)
265288
}
266289

267290
return true
268291
}
269292

293+
// nolint: dupl // Code uses unique symbols.
270294
func (t *Tracker) counter(
271295
ctx context.Context,
272296
canonicalID identity,
@@ -285,13 +309,16 @@ func (t *Tracker) counter(
285309
t.ErrLogger(ctx, err, labels)
286310
}
287311

312+
t.registered = append(t.registered, counter)
313+
288314
*collect = func(labelValues map[string]string, value float64) {
289315
counter.With(labelValues).Add(value)
290316
}
291317

292318
return true
293319
}
294320

321+
// nolint: dupl // Code uses unique symbols.
295322
func (t *Tracker) gauge(
296323
ctx context.Context,
297324
canonicalID identity,
@@ -310,6 +337,8 @@ func (t *Tracker) gauge(
310337
t.ErrLogger(ctx, err, labels)
311338
}
312339

340+
t.registered = append(t.registered, gauge)
341+
313342
*collect = func(labelValues map[string]string, value float64) {
314343
gauge.With(labelValues).Set(value)
315344
}

prom/tracker_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,10 @@ func TestTracker(t *testing.T) {
100100

101101
err = testutil.GatherAndCompare(registry, bytes.NewBufferString(expected))
102102
assert.NoError(t, err)
103+
104+
// No metrics after reset.
105+
tr.Reset()
106+
107+
err = testutil.GatherAndCompare(registry, bytes.NewBufferString(""))
108+
assert.NoError(t, err)
103109
}

0 commit comments

Comments
 (0)