19 Commits

Author SHA1 Message Date
7a656210f7 fix bug with build metrics name (#165)
All checks were successful
coverage / build (push) Successful in 2m26s
sync / sync (push) Successful in 1m53s
test / test (push) Successful in 4m24s
2025-05-16 12:53:41 +03:00
vtolstov
cf310138cb Apply Code Coverage Badge 2025-05-05 16:34:47 +00:00
05740fa208 [v4] update ci (#164)
All checks were successful
coverage / build (push) Successful in 6m27s
sync / sync (push) Successful in 10m7s
test / test (push) Successful in 15m15s
* update ci

* update ci

* added commit hash check to avoid unnecessary repository cloning
2025-05-05 19:24:55 +03:00
b7f2d44c6f improve sync
Some checks failed
coverage / build (push) Failing after 5m17s
test / test (push) Successful in 7m5s
sync / sync (push) Successful in 13s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-04-27 21:37:39 +03:00
734bf3cb75 protect meter by mutex
All checks were successful
test / test (push) Successful in 2m52s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-03-07 16:55:38 +03:00
5dba791473 fixup test
All checks were successful
test / test (push) Successful in 1m58s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-03-04 12:04:22 +03:00
ffa3e0753f move to v4
All checks were successful
test / test (push) Successful in 1m49s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-03-04 11:40:51 +03:00
434c2e31a3 Merge pull request 'Update workflows' (#110) from atolstikhin/micro-meter-prometheus:master into master
Some checks failed
test / test (push) Failing after 10m47s
Reviewed-on: #110
2024-12-15 17:49:54 +03:00
09528896b1 Merge branch 'master' into master
Some checks failed
dependabot-automerge / automerge (pull_request) Has been skipped
automerge / automerge (pull_request) Failing after 14s
autoapprove / autoapprove (pull_request) Failing after 30s
lint / lint (pull_request) Successful in 47s
test / test (pull_request) Successful in 13m17s
2024-12-15 17:35:40 +03:00
c798df52a1 fixup lint
Some checks failed
build / lint (push) Successful in 25s
build / test (push) Failing after 29s
codeql / analyze (go) (push) Failing after 2m27s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-12-15 17:33:16 +03:00
Aleksandr Tolstikhin
118517eb72 Update workflows
Some checks failed
automerge / automerge (pull_request) Has been skipped
dependabot-automerge / automerge (pull_request) Has been skipped
autoapprove / autoapprove (pull_request) Successful in 6s
lint / lint (pull_request) Failing after 51s
test / test (pull_request) Successful in 45s
2024-12-14 01:36:02 +07:00
3d0ec5c6ea recover go std metrics
Some checks failed
build / test (push) Failing after 1m37s
build / lint (push) Successful in 9m16s
codeql / analyze (go) (push) Failing after 14m44s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-04-04 16:52:52 +03:00
1862ee5380 fixup golang std metrics
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-04-04 16:51:48 +03:00
4cd610a474 update deps
Some checks failed
build / test (push) Has been cancelled
build / lint (push) Has been cancelled
codeql / analyze (go) (push) Has been cancelled
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-27 11:42:49 +03:00
b64ea019ed Merge pull request 'micro v4 options fix' (#107) from options into master
Some checks failed
build / test (push) Failing after 48s
build / lint (push) Successful in 12s
codeql / analyze (go) (push) Failing after 1m30s
Reviewed-on: #107
2023-08-16 15:42:16 +03:00
4cdca6cd83 micro v4 options fix
Some checks failed
autoapprove / autoapprove (pull_request) Failing after 8s
automerge / automerge (pull_request) Failing after 4s
codeql / analyze (go) (pull_request) Failing after 1m46s
dependabot-automerge / automerge (pull_request) Has been skipped
prbuild / test (pull_request) Failing after 52s
prbuild / lint (pull_request) Successful in 14s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-16 15:41:33 +03:00
bd8eca5f5e Merge pull request 'move to micro v4' (#106) from v4 into master
Some checks failed
build / test (push) Failing after 4s
build / lint (push) Failing after 5s
codeql / analyze (go) (push) Failing after 7s
Reviewed-on: #106
2023-05-07 19:36:08 +03:00
04d67b540c move to micro v4
Some checks failed
autoapprove / autoapprove (pull_request) Failing after 3s
automerge / automerge (pull_request) Failing after 7s
dependabot-automerge / automerge (pull_request) Has been skipped
codeql / analyze (go) (pull_request) Failing after 9s
prbuild / test (pull_request) Failing after 1m0s
prbuild / lint (pull_request) Failing after 5s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-05-07 19:35:39 +03:00
44b2cb9572 Merge branch 'v3'
Some checks failed
build / test (push) Failing after 6s
build / lint (push) Failing after 4s
codeql / analyze (go) (push) Failing after 5s
2023-05-07 19:34:13 +03:00
16 changed files with 228 additions and 71 deletions

28
.github/autoapprove.yml vendored Normal file
View File

@@ -0,0 +1,28 @@
name: "autoapprove"
on:
pull_request_target:
types: [assigned, opened, synchronize, reopened]
workflow_run:
workflows: ["prbuild"]
types:
- completed
permissions:
pull-requests: write
contents: write
jobs:
autoapprove:
runs-on: ubuntu-latest
steps:
- name: approve
run: [ "curl -o tea https://dl.gitea.com/tea/main/tea-main-linux-amd64",
"chmod +x ./tea",
"./tea login add --name unistack --token ${{ secrets.GITHUB_TOKEN }} --url https://git.unistack.org",
"./tea pr --repo ${{ github.event.repository.name }}"
]
if: github.actor == 'vtolstov'
id: approve
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

53
.github/workflows/job_coverage.yml vendored Normal file
View File

@@ -0,0 +1,53 @@
name: coverage
on:
push:
branches: [ main, v3, v4 ]
paths-ignore:
- '.github/**'
- '.gitea/**'
pull_request:
branches: [ main, v3, v4 ]
jobs:
build:
if: github.server_url != 'https://github.com'
runs-on: ubuntu-latest
steps:
- name: checkout code
uses: actions/checkout@v4
with:
filter: 'blob:none'
- name: setup go
uses: actions/setup-go@v5
with:
cache-dependency-path: "**/*.sum"
go-version: 'stable'
- name: test coverage
run: |
go test -v -cover ./... -covermode=count -coverprofile coverage.out -coverpkg ./...
go tool cover -func coverage.out -o coverage.out
- name: coverage badge
uses: tj-actions/coverage-badge-go@v2
with:
green: 80
filename: coverage.out
- uses: stefanzweifel/git-auto-commit-action@v4
name: autocommit
with:
commit_message: Apply Code Coverage Badge
skip_fetch: false
skip_checkout: false
file_pattern: ./README.md
- name: push
if: steps.auto-commit-action.outputs.changes_detected == 'true'
uses: ad-m/github-push-action@master
with:
github_token: ${{ github.token }}
branch: ${{ github.ref }}

View File

@@ -3,10 +3,10 @@ name: lint
on: on:
pull_request: pull_request:
types: [opened, reopened, synchronize] types: [opened, reopened, synchronize]
branches: branches: [ master, v3, v4 ]
- master paths-ignore:
- v3 - '.github/**'
- v4 - '.gitea/**'
jobs: jobs:
lint: lint:
@@ -20,10 +20,10 @@ jobs:
uses: actions/setup-go@v5 uses: actions/setup-go@v5
with: with:
cache-dependency-path: "**/*.sum" cache-dependency-path: "**/*.sum"
go-version: 'stable' go-version: 'stable'
- name: setup deps - name: setup deps
run: go get -v ./... run: go get -v ./...
- name: run lint - name: run lint
uses: https://github.com/golangci/golangci-lint-action@v6 uses: golangci/golangci-lint-action@v6
with: with:
version: 'latest' version: 'latest'

94
.github/workflows/job_sync.yml vendored Normal file
View File

@@ -0,0 +1,94 @@
name: sync
on:
schedule:
- cron: '*/5 * * * *'
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
sync:
if: github.server_url != 'https://github.com'
runs-on: ubuntu-latest
steps:
- name: init
run: |
git config --global user.email "vtolstov <vtolstov@users.noreply.github.com>"
git config --global user.name "github-actions[bot]"
echo "machine git.unistack.org login vtolstov password ${{ secrets.TOKEN_GITEA }}" >> /root/.netrc
echo "machine github.com login vtolstov password ${{ secrets.TOKEN_GITHUB }}" >> /root/.netrc
- name: check master
id: check_master
run: |
src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/master | cut -f1)
dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/master | cut -f1)
echo "src_hash=$src_hash"
echo "dst_hash=$dst_hash"
if [ "$src_hash" != "$dst_hash" ]; then
echo "sync_needed=true" >> $GITHUB_OUTPUT
else
echo "sync_needed=false" >> $GITHUB_OUTPUT
fi
- name: sync master
if: steps.check_master.outputs.sync_needed == 'true'
run: |
git clone --filter=blob:none --filter=tree:0 --branch master --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
cd repo
git remote add --no-tags --fetch --track master upstream https://github.com/${GITHUB_REPOSITORY}
git pull --rebase upstream master
git push upstream master --progress
git push origin master --progress
cd ../
rm -rf repo
- name: check v3
id: check_v3
run: |
src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/v3 | cut -f1)
dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/v3 | cut -f1)
echo "src_hash=$src_hash"
echo "dst_hash=$dst_hash"
if [ "$src_hash" != "$dst_hash" ]; then
echo "sync_needed=true" >> $GITHUB_OUTPUT
else
echo "sync_needed=false" >> $GITHUB_OUTPUT
fi
- name: sync v3
if: steps.check_v3.outputs.sync_needed == 'true'
run: |
git clone --filter=blob:none --filter=tree:0 --branch v3 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
cd repo
git remote add --no-tags --fetch --track v3 upstream https://github.com/${GITHUB_REPOSITORY}
git pull --rebase upstream v3
git push upstream v3 --progress
git push origin v3 --progress
cd ../
rm -rf repo
- name: check v4
id: check_v4
run: |
src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/v4 | cut -f1)
dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/v4 | cut -f1)
echo "src_hash=$src_hash"
echo "dst_hash=$dst_hash"
if [ "$src_hash" != "$dst_hash" ]; then
echo "sync_needed=true" >> $GITHUB_OUTPUT
else
echo "sync_needed=false" >> $GITHUB_OUTPUT
fi
- name: sync v4
if: steps.check_v4.outputs.sync_needed == 'true'
run: |
git clone --filter=blob:none --filter=tree:0 --branch v4 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
cd repo
git remote add --no-tags --fetch --track v4 upstream https://github.com/${GITHUB_REPOSITORY}
git pull --rebase upstream v4
git push upstream v4 --progress
git push origin v4 --progress
cd ../
rm -rf repo

View File

@@ -3,15 +3,12 @@ name: test
on: on:
pull_request: pull_request:
types: [opened, reopened, synchronize] types: [opened, reopened, synchronize]
branches: branches: [ master, v3, v4 ]
- master
- v3
- v4
push: push:
branches: branches: [ master, v3, v4 ]
- master paths-ignore:
- v3 - '.github/**'
- v4 - '.gitea/**'
jobs: jobs:
test: test:

View File

@@ -3,15 +3,12 @@ name: test
on: on:
pull_request: pull_request:
types: [opened, reopened, synchronize] types: [opened, reopened, synchronize]
branches: branches: [ master, v3, v4 ]
- master
- v3
- v4
push: push:
branches: branches: [ master, v3, v4 ]
- master paths-ignore:
- v3 - '.github/**'
- v4 - '.gitea/**'
jobs: jobs:
test: test:
@@ -35,19 +32,19 @@ jobs:
go-version: 'stable' go-version: 'stable'
- name: setup go work - name: setup go work
env: env:
GOWORK: /workspace/${{ github.repository_owner }}/go.work GOWORK: ${{ github.workspace }}/go.work
run: | run: |
go work init go work init
go work use . go work use .
go work use micro-tests go work use micro-tests
- name: setup deps - name: setup deps
env: env:
GOWORK: /workspace/${{ github.repository_owner }}/go.work GOWORK: ${{ github.workspace }}/go.work
run: go get -v ./... run: go get -v ./...
- name: run tests - name: run tests
env: env:
INTEGRATION_TESTS: yes INTEGRATION_TESTS: yes
GOWORK: /workspace/${{ github.repository_owner }}/go.work GOWORK: ${{ github.workspace }}/go.work
run: | run: |
cd micro-tests cd micro-tests
go test -mod readonly -v ./... || true go test -mod readonly -v ./... || true

View File

@@ -1,5 +1,5 @@
run: run:
concurrency: 8 concurrency: 8
deadline: 5m timeout: 5m
issues-exit-code: 1 issues-exit-code: 1
tests: true tests: true

2
README.md Normal file
View File

@@ -0,0 +1,2 @@
# micro-meter-prometheus
![Coverage](https://img.shields.io/badge/Coverage-31.9%25-yellow)

5
go.mod
View File

@@ -1,13 +1,14 @@
module go.unistack.org/micro-meter-prometheus/v3 module go.unistack.org/micro-meter-prometheus/v4
go 1.22.0 go 1.22.0
toolchain go1.24.0
require ( require (
github.com/prometheus/client_golang v1.21.0 github.com/prometheus/client_golang v1.21.0
github.com/prometheus/client_model v0.6.1 github.com/prometheus/client_model v0.6.1
github.com/prometheus/common v0.62.0 github.com/prometheus/common v0.62.0
go.unistack.org/micro/v3 v3.11.41 go.unistack.org/micro/v4 v4.1.2
) )
require ( require (

4
go.sum
View File

@@ -22,8 +22,8 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.unistack.org/micro/v3 v3.11.41 h1:dP4sBLIZpMo+MWGe5bbESewK8wBzYm4Yik/67x4dEtQ= go.unistack.org/micro/v4 v4.1.2 h1:9SOlPYyPNNFpg1A7BsvhDyQm3gysLH1AhWbDCp1hyoY=
go.unistack.org/micro/v3 v3.11.41/go.mod h1:POGU5hstnAT9LH70m8FalyQSNi2GfIew71K75JenIZk= go.unistack.org/micro/v4 v4.1.2/go.mod h1:lr3oYED8Ay1vjK68QqRw30QOtdk/ffpZqMFDasOUhKw=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM= google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=

View File

@@ -13,7 +13,7 @@ import (
"github.com/prometheus/client_golang/prometheus/collectors" "github.com/prometheus/client_golang/prometheus/collectors"
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt" "github.com/prometheus/common/expfmt"
"go.unistack.org/micro/v3/meter" "go.unistack.org/micro/v4/meter"
) )
var _ meter.Meter = &prometheusMeter{} var _ meter.Meter = &prometheusMeter{}
@@ -26,7 +26,7 @@ type prometheusMeter struct {
gauge map[string]*gauges gauge map[string]*gauges
histogram map[string]*histograms histogram map[string]*histograms
summary map[string]*summaries summary map[string]*summaries
sync.Mutex mu *sync.Mutex
} }
type counters struct { type counters struct {
@@ -79,6 +79,7 @@ func NewMeter(opts ...meter.Option) *prometheusMeter {
gauge: make(map[string]*gauges), gauge: make(map[string]*gauges),
histogram: make(map[string]*histograms), histogram: make(map[string]*histograms),
summary: make(map[string]*summaries), summary: make(map[string]*summaries),
mu: &sync.Mutex{},
} }
} }
@@ -100,8 +101,8 @@ func (m *prometheusMeter) Name() string {
} }
func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter { func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
m.Lock() m.mu.Lock()
defer m.Unlock() defer m.mu.Unlock()
nm := m.buildMetric(name) nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...) labels = append(m.opts.Labels, labels...)
cd, ok := m.counter[nm] cd, ok := m.counter[nm]
@@ -123,8 +124,8 @@ func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
} }
func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter { func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter {
m.Lock() m.mu.Lock()
defer m.Unlock() defer m.mu.Unlock()
nm := m.buildMetric(name) nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...) labels = append(m.opts.Labels, labels...)
cd, ok := m.floatCounter[nm] cd, ok := m.floatCounter[nm]
@@ -146,8 +147,8 @@ func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.Floa
} }
func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge { func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge {
m.Lock() m.mu.Lock()
defer m.Unlock() defer m.mu.Unlock()
nm := m.buildMetric(name) nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...) labels = append(m.opts.Labels, labels...)
cd, ok := m.gauge[nm] cd, ok := m.gauge[nm]
@@ -169,8 +170,8 @@ func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string
} }
func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram { func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram {
m.Lock() m.mu.Lock()
defer m.Unlock() defer m.mu.Unlock()
nm := m.buildMetric(name) nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...) labels = append(m.opts.Labels, labels...)
cd, ok := m.histogram[nm] cd, ok := m.histogram[nm]
@@ -192,8 +193,8 @@ func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogr
} }
func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary { func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
m.Lock() m.mu.Lock()
defer m.Unlock() defer m.mu.Unlock()
nm := m.buildMetric(name) nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...) labels = append(m.opts.Labels, labels...)
cd, ok := m.summary[nm] cd, ok := m.summary[nm]
@@ -215,8 +216,8 @@ func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
} }
func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary { func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary {
m.Lock() m.mu.Lock()
defer m.Unlock() defer m.mu.Unlock()
nm := m.buildMetric(name) nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...) labels = append(m.opts.Labels, labels...)
cd, ok := m.summary[nm] cd, ok := m.summary[nm]
@@ -277,6 +278,8 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
enc := expfmt.NewEncoder(w, expfmt.NewFormat(expfmt.TypeTextPlain)) enc := expfmt.NewEncoder(w, expfmt.NewFormat(expfmt.TypeTextPlain))
m.mu.Lock()
for name, metrics := range m.counter { for name, metrics := range m.counter {
mf := &dto.MetricFamily{ mf := &dto.MetricFamily{
Name: newName(name), Name: newName(name),
@@ -352,6 +355,8 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
mfs = append(mfs, mf) mfs = append(mfs, mf)
} }
m.mu.Unlock()
for _, mf := range mfs { for _, mf := range mfs {
_ = enc.Encode(mf) _ = enc.Encode(mf)
} }
@@ -370,6 +375,7 @@ func (m *prometheusMeter) Clone(opts ...meter.Option) meter.Meter {
} }
return &prometheusMeter{ return &prometheusMeter{
mu: m.mu,
set: m.set, set: m.set,
opts: options, opts: options,
floatCounter: m.floatCounter, floatCounter: m.floatCounter,

View File

@@ -5,35 +5,9 @@ import (
"fmt" "fmt"
"testing" "testing"
"go.unistack.org/micro/v3/meter" "go.unistack.org/micro/v4/meter"
) )
func TestBuildMetric(t *testing.T) {
m := NewMeter(meter.Labels("service_name", "test", "service_version", "0.0.0.1"))
if err := m.Init(); err != nil {
t.Fatal(err)
}
name := m.buildMetric("micro_server")
if name != `micro_server{service_name="test",service_version="0.0.0.1"}` {
t.Fatal("invalid name")
}
}
func TestWithDefaultLabels(t *testing.T) {
m := NewMeter(meter.Labels("service_name", "test", "service_version", "0.0.0.1"))
if err := m.Init(); err != nil {
t.Fatal(err)
}
m.Counter("micro_server", "endpoint", "ep3", "path", "/path3", "status", "success").Inc()
buf := bytes.NewBuffer(nil)
_ = m.Write(buf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false))
if !bytes.Contains(buf.Bytes(), []byte(`micro_server{service_name="test",service_version="0.0.0.1",endpoint="ep3",path="/path3",status="success"} 1`)) {
t.Fatalf("invalid metrics output: %s", buf.Bytes())
}
}
func TestStd(t *testing.T) { func TestStd(t *testing.T) {
m := NewMeter(meter.WriteProcessMetrics(true), meter.WriteFDMetrics(true)) m := NewMeter(meter.WriteProcessMetrics(true), meter.WriteFDMetrics(true))
if err := m.Init(); err != nil { if err := m.Init(); err != nil {
@@ -72,6 +46,7 @@ func TestMultiple(t *testing.T) {
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
_ = m.Write(buf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false)) _ = m.Write(buf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false))
if !bytes.Contains(buf.Bytes(), []byte(`micro_server{endpoint="ep1",path="/path1"} 2`)) { if !bytes.Contains(buf.Bytes(), []byte(`micro_server{endpoint="ep1",path="/path1"} 2`)) {
// t.Fatal("XXXX")
t.Fatalf("invalid metrics output: %s", buf.Bytes()) t.Fatalf("invalid metrics output: %s", buf.Bytes())
} }
} }
@@ -83,12 +58,16 @@ func TestCounterSet(t *testing.T) {
m.Counter("forte_accounts_total", "channel_code", "crm").Set(value) m.Counter("forte_accounts_total", "channel_code", "crm").Set(value)
fmt.Println(uint64(float64(value)))
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
_ = m.Write(buf) _ = m.Write(buf)
output := buf.String() output := buf.String()
fmt.Println(output)
expectedOutput := fmt.Sprintf(`%s{channel_code="crm"} %d`, "forte_accounts_total", value) expectedOutput := fmt.Sprintf(`%s{channel_code="crm"} %d`, "forte_accounts_total", value)
if !bytes.Contains(buf.Bytes(), []byte(expectedOutput)) { if !bytes.Contains(buf.Bytes(), []byte(expectedOutput)) {