Skip to content

Commit 8f570b0

Browse files
authored
Move Prometheus adapter to an external module (#4)
1 parent 671a471 commit 8f570b0

15 files changed

+76
-1148
lines changed

.github/workflows/bench.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
bench:
1414
strategy:
1515
matrix:
16-
go-version: [ 1.15.x ]
16+
go-version: [ 1.16.x ]
1717
runs-on: ubuntu-latest
1818
steps:
1919
- name: Install Go

.github/workflows/golangci-lint.yml

+17-8
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,25 @@ jobs:
1414
steps:
1515
- uses: actions/checkout@v2
1616
- name: golangci-lint
17-
uses: golangci/golangci-lint-action@v2.3.0
17+
uses: golangci/golangci-lint-action@v2.5.1
1818
with:
1919
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
20-
version: v1.34.1
20+
version: v1.37.1
21+
22+
# Optional: working directory, useful for monorepos
23+
# working-directory: somedir
2124

2225
# Optional: golangci-lint command line arguments.
23-
# args: ./the-only-dir-to-analyze/...
26+
# args: --issues-exit-code=0
27+
28+
# Optional: show only new issues if it's a pull request. The default value is `false`.
29+
# only-new-issues: true
30+
31+
# Optional: if set to true then the action will use pre-installed Go.
32+
# skip-go-installation: true
33+
34+
# Optional: if set to true then the action don't cache or restore ~/go/pkg.
35+
# skip-pkg-cache: true
2436

25-
# Required: the token is used for fetching a list of releases of golangci-lint.
26-
# The secret `GITHUB_TOKEN` is automatically created by GitHub,
27-
# no need to create it manually.
28-
# https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#about-the-github_token-secret
29-
github-token: ${{ secrets.GITHUB_TOKEN }}
37+
# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
38+
# skip-build-cache: true

.github/workflows/test-unit.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
test:
1212
strategy:
1313
matrix:
14-
go-version: [ 1.13.x, 1.14.x, 1.15.x ]
14+
go-version: [ 1.13.x, 1.14.x, 1.15.x, 1.16.x ]
1515
runs-on: ubuntu-latest
1616
steps:
1717
- name: Install Go
@@ -52,7 +52,7 @@ jobs:
5252
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
5353
run: cp unit.txt unit-base.txt
5454
- name: Comment Test Coverage
55-
if: matrix.go-version == '1.15.x'
55+
if: matrix.go-version == '1.16.x'
5656
uses: marocchino/sticky-pull-request-comment@v2
5757
with:
5858
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -68,7 +68,7 @@ jobs:
6868
</details>
6969
7070
- name: Upload code coverage
71-
if: matrix.go-version == '1.15.x'
71+
if: matrix.go-version == '1.16.x'
7272
uses: codecov/codecov-action@v1
7373
with:
7474
file: ./unit.coverprofile

README.md

+6-22
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ This library provides context-driven stats tracker.
1313

1414
* Loosely coupled with underlying implementation.
1515
* Context-driven labels control.
16-
* Zero allocation implementation for Prometheus client.
17-
* Simple interface with variadic number of key-value pairs for labels.
16+
* Zero allocation implementation for [Prometheus client](https://github.com/bool64/prom-stats).
17+
* A simple interface with variadic number of key-value pairs for labels.
1818
* Easily mockable interface free from 3rd party dependencies.
1919

2020
## Example
@@ -49,25 +49,9 @@ tr.Add(ctx, "my_latency_seconds", 1.23)
4949
tr.Set(ctx, "temperature", 33.3)
5050
```
5151

52-
## Performance
52+
## Versioning
5353

54-
Sample benchmark result with Dell XPS 7590 i9-9980HK on Ubuntu 19 and go1.14.2.
54+
This project adheres to [Semantic Versioning](https://semver.org/#semantic-versioning-200).
5555

56-
```
57-
name time/op
58-
Tracker_Add-16 814ns ± 6%
59-
RawPrometheus-16 801ns ± 1%
60-
61-
name alloc/op
62-
Tracker_Add-16 0.00B
63-
RawPrometheus-16 336B ± 0%
64-
65-
name allocs/op
66-
Tracker_Add-16 0.00
67-
RawPrometheus-16 2.00 ± 0%
68-
```
69-
70-
## Caveats
71-
72-
Prometheus client does not support metrics with same name and different label sets.
73-
If you add a label to context, make sure you have it in all cases, at least with empty value `""`.
56+
Before version `1.0.0`, breaking changes are tagged with `MINOR` bump, features and fixes are tagged with `PATCH` bump.
57+
After version `1.0.0`, breaking changes are tagged with `MAJOR` bump.

context_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package stats_test
2+
3+
import (
4+
"context"
5+
"github.com/bool64/stats"
6+
"github.com/stretchr/testify/assert"
7+
"testing"
8+
)
9+
10+
func TestAddKeysAndValues(t *testing.T) {
11+
ctx := stats.AddKeysAndValues(context.Background(), "k1", "one", "k2", "two")
12+
assert.Equal(t, []string{"k1", "one", "k2", "two"}, stats.KeysAndValues(ctx))
13+
14+
ctx2 := stats.AddKeysAndValues(ctx, "k3", "three")
15+
assert.Equal(t, []string{"k1", "one", "k2", "two", "k3", "three"}, stats.KeysAndValues(ctx2))
16+
17+
ctx3 := stats.AddKeysAndValues(ctx2, "k4", "four")
18+
assert.Equal(t, []string{"k1", "one", "k2", "two", "k3", "three", "k4", "four"}, stats.KeysAndValues(ctx3))
19+
20+
ctx4 := stats.AddKeysAndValues(ctx2, "k4a", "four-a")
21+
assert.Equal(t, []string{"k1", "one", "k2", "two", "k3", "three", "k4a", "four-a"}, stats.KeysAndValues(ctx4))
22+
23+
assert.Equal(t, []string{"k1", "one", "k2", "two", "k3", "three", "k4", "four"}, stats.KeysAndValues(ctx3))
24+
}
25+
26+
func BenchmarkAddKeysAndValues(b *testing.B) {
27+
b.ReportAllocs()
28+
for i := 0; i < b.N; i++ {
29+
ctx := stats.AddKeysAndValues(context.Background(), "k1", "one", "k2", "two")
30+
_ = stats.KeysAndValues(ctx)
31+
32+
ctx2 := stats.AddKeysAndValues(ctx, "k3", "three")
33+
_ = stats.KeysAndValues(ctx2)
34+
35+
ctx3 := stats.AddKeysAndValues(ctx2, "k4", "four")
36+
_ = stats.KeysAndValues(ctx3)
37+
38+
ctx4 := stats.AddKeysAndValues(ctx2, "k4a", "four-a")
39+
_ = stats.KeysAndValues(ctx4)
40+
}
41+
}

go.mod

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

55
require (
6-
github.com/bool64/dev v0.1.12
7-
github.com/kr/pretty v0.2.0 // indirect
8-
github.com/prometheus/client_golang v1.9.0
9-
github.com/stretchr/testify v1.5.1
10-
golang.org/x/sys v0.0.0-20210108172913-0df2131ae363 // indirect
11-
google.golang.org/protobuf v1.25.0 // indirect
6+
github.com/bool64/dev v0.1.20
7+
github.com/stretchr/testify v1.4.0
128
)

0 commit comments

Comments
 (0)