20 Commits
v3.8.18 ... v4

Author SHA1 Message Date
vtolstov
948d223b82 Apply Code Coverage Badge 2025-05-16 09:55:45 +00:00
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
21 changed files with 631 additions and 605 deletions

View File

@@ -1,14 +0,0 @@
---
name: Question
about: Ask a question about go-micro
title: ''
labels: ''
assignees: ''
---
Before asking, please check if your question has already been answered:
1. Check the documentation - https://micro.mu/docs/
2. Check the examples and plugins - https://github.com/micro/examples & https://github.com/micro/go-plugins
3. Search existing issues

View File

@@ -1,6 +1,6 @@
---
name: Bug report
about: For reporting bugs in go-micro
about: For reporting bugs in micro
title: "[BUG]"
labels: ''
assignees: ''
@@ -16,9 +16,3 @@ assignees: ''
**How to reproduce the bug:**
If possible, please include a minimal code snippet here.
**Environment:**
Go Version: please paste `go version` output here
```
please paste `go env` output here
```

View File

@@ -1,6 +1,6 @@
---
name: Feature request / Enhancement
about: If you have a need not served by go-micro
about: If you have a need not served by micro
title: "[FEATURE]"
labels: ''
assignees: ''
@@ -14,4 +14,4 @@ A clear and concise description of what the problem is. Ex. I'm always frustrate
A clear and concise description of what you want to happen.
**Additional context**
Add any other context or screenshots about the feature request here.
Add any other context or screenshots about the feature request here.

8
.github/ISSUE_TEMPLATE/question.md vendored Normal file
View File

@@ -0,0 +1,8 @@
---
name: Question
about: Ask a question about micro
title: ''
labels: ''
assignees: ''
---

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

View File

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

View File

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

2
README.md Normal file
View File

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

View File

@@ -1,93 +0,0 @@
package prometheus
import (
"math"
"sync/atomic"
"unsafe"
dto "github.com/prometheus/client_model/go"
)
type prometheusCounter struct {
name string
c *dto.Metric
n float64
}
func (c *prometheusCounter) Add(n int) {
addFloat64(&(c.n), float64(n))
}
func (c *prometheusCounter) Dec() {
addFloat64(&(c.n), float64(-1))
}
func (c *prometheusCounter) Inc() {
addFloat64(&(c.n), float64(1))
}
func (c *prometheusCounter) Get() uint64 {
return uint64(getFloat64(&(c.n)))
}
func (c *prometheusCounter) Set(n uint64) {
setFloat64(&(c.n), math.Float64frombits(n))
}
type prometheusFloatCounter struct {
name string
c *dto.Metric
n float64
}
func (c *prometheusFloatCounter) Add(n float64) {
addFloat64(&(c.n), n)
}
func (c *prometheusFloatCounter) Dec() {
addFloat64(&(c.n), float64(-1))
}
func (c *prometheusFloatCounter) Inc() {
addFloat64(&(c.n), float64(1))
}
func (c *prometheusFloatCounter) Get() float64 {
return getFloat64(&(c.n))
}
func (c *prometheusFloatCounter) Set(n float64) {
setFloat64(&(c.n), n)
}
func (c *prometheusFloatCounter) Sub(n float64) {
addFloat64(&(c.n), -n)
}
func setFloat64(_addr *float64, value float64) float64 {
addr := (*uint64)(unsafe.Pointer(_addr))
for {
x := atomic.LoadUint64(addr)
if atomic.CompareAndSwapUint64(addr, x, math.Float64bits(value)) {
return value
}
}
}
func addFloat64(_addr *float64, delta float64) float64 {
addr := (*uint64)(unsafe.Pointer(_addr))
for {
x := atomic.LoadUint64(addr)
y := math.Float64frombits(x) + delta
if atomic.CompareAndSwapUint64(addr, x, math.Float64bits(y)) {
return y
}
}
}
func getFloat64(_addr *float64) float64 {
addr := (*uint64)(unsafe.Pointer(_addr))
x := atomic.LoadUint64(addr)
y := math.Float64frombits(x)
return y
}

View File

@@ -1,13 +0,0 @@
package prometheus
import dto "github.com/prometheus/client_model/go"
type prometheusGauge struct {
name string
c *dto.Metric
n float64
}
func (c *prometheusGauge) Get() float64 {
return getFloat64(&(c.n))
}

22
go.mod
View File

@@ -1,27 +1,21 @@
module go.unistack.org/micro-meter-prometheus/v3
module go.unistack.org/micro-meter-prometheus/v4
go 1.22.0
toolchain go1.24.0
require (
github.com/prometheus/client_golang v1.20.4
github.com/prometheus/client_golang v1.21.0
github.com/prometheus/client_model v0.6.1
github.com/prometheus/common v0.59.1
go.unistack.org/micro/v3 v3.10.91
google.golang.org/protobuf v1.35.2
github.com/prometheus/common v0.62.0
go.unistack.org/micro/v4 v4.1.2
)
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/stretchr/testify v1.10.0 // indirect
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/sys v0.28.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 // indirect
google.golang.org/grpc v1.68.1 // indirect
golang.org/x/sys v0.30.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
)

30
go.sum
View File

@@ -3,32 +3,30 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI=
github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.21.0 h1:DIsaGmiaBkSangBgMtWdNfxbMNdku5IK6iNhrEqWvdA=
github.com/prometheus/client_golang v1.21.0/go.mod h1:U9NM32ykUErtVBxdvD3zfi+EuFkkaBvMb09mIfe0Zgg=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJoX0=
github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0=
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
go.unistack.org/micro/v3 v3.10.91 h1:vuJY4tXwpqimwIkEJ3TozMYNVQQs+C5QMlQWPgSY/YM=
go.unistack.org/micro/v3 v3.10.91/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 h1:8ZmaLZE4XWrtU3MyClkYqqtl6Oegr3235h7jxsDyqCY=
google.golang.org/grpc v1.68.1 h1:oI5oTa11+ng8r8XMMN7jAOmWfPZWbYpCFaMUTACxkM0=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.unistack.org/micro/v4 v4.1.2 h1:9SOlPYyPNNFpg1A7BsvhDyQm3gysLH1AhWbDCp1hyoY=
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/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/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -1,39 +0,0 @@
package prometheus
import (
"sync/atomic"
"time"
dto "github.com/prometheus/client_model/go"
)
type prometheusHistogram struct {
name string
c *dto.Metric
}
func (c prometheusHistogram) Reset() {
}
func (c prometheusHistogram) Update(n float64) {
atomic.AddUint64(c.c.Histogram.SampleCount, 1)
addFloat64(c.c.Histogram.SampleSum, n)
for _, b := range c.c.Histogram.Bucket {
if n > *b.UpperBound {
continue
}
atomic.AddUint64(b.CumulativeCount, 1)
}
}
func (c prometheusHistogram) UpdateDuration(n time.Time) {
x := time.Since(n).Seconds()
atomic.AddUint64(c.c.Histogram.SampleCount, 1)
addFloat64(c.c.Histogram.SampleSum, x)
for _, b := range c.c.Histogram.Bucket {
if x > *b.UpperBound {
continue
}
atomic.AddUint64(b.CumulativeCount, 1)
}
}

View File

@@ -2,216 +2,254 @@ package prometheus
import (
"fmt"
"hash/fnv"
"io"
"regexp"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"go.unistack.org/micro/v3/meter"
xpool "go.unistack.org/micro/v3/util/xpool"
"google.golang.org/protobuf/types/known/timestamppb"
"go.unistack.org/micro/v4/meter"
)
var _ meter.Meter = (*prometheusMeter)(nil)
var _ meter.Meter = &prometheusMeter{}
type prometheusMeter struct {
opts meter.Options
set prometheus.Registerer
counter sync.Map
floatCounter sync.Map
gauge sync.Map
histogram sync.Map
summary sync.Map
mfPool xpool.Pool[*dto.MetricFamily]
mu sync.Mutex
counter map[string]*counters
floatCounter map[string]*floatCounters
gauge map[string]*gauges
histogram map[string]*histograms
summary map[string]*summaries
mu *sync.Mutex
}
type counters struct {
cs map[uint64]*prometheusCounter
}
type gauges struct {
cs map[uint64]*prometheusGauge
}
type histograms struct {
cs map[uint64]*prometheusHistogram
}
type summaries struct {
cs map[uint64]*prometheusSummary
}
type floatCounters struct {
cs map[uint64]*prometheusFloatCounter
}
/*
func newFloat64(v float64) *float64 {
nv := v
return &nv
}
*/
func newString(v string) *string {
nv := v
return &nv
}
func newName(name string) *string {
idx := strings.Index(name, "{")
if idx <= 0 {
return newString(name)
}
return newString(name[:idx])
}
func NewMeter(opts ...meter.Option) *prometheusMeter {
return &prometheusMeter{
set: prometheus.NewRegistry(), // prometheus.DefaultRegisterer,
opts: meter.NewOptions(opts...),
mfPool: xpool.NewPool[*dto.MetricFamily](func() *dto.MetricFamily {
return &dto.MetricFamily{}
}),
set: prometheus.NewRegistry(), // prometheus.DefaultRegisterer,
opts: meter.NewOptions(opts...),
counter: make(map[string]*counters),
floatCounter: make(map[string]*floatCounters),
gauge: make(map[string]*gauges),
histogram: make(map[string]*histograms),
summary: make(map[string]*summaries),
mu: &sync.Mutex{},
}
}
func (m *prometheusMeter) buildMetric(name string, labels ...string) string {
nl := len(m.opts.Labels) + len(labels)
if nl == 0 {
return name
}
nlabels := make([]string, 0, nl)
nlabels = append(nlabels, m.opts.Labels...)
nlabels = append(nlabels, labels...)
return meter.BuildName(name, nlabels...)
}
func (m *prometheusMeter) Name() string {
return m.opts.Name
}
func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels)
m.mu.Lock()
c, ok := m.counter.Load(h)
// fmt.Printf("counter name %s hash %v labels %v\n", name, h, labels)
m.mu.Unlock()
defer m.mu.Unlock()
nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...)
cd, ok := m.counter[nm]
h := newHash(labels)
if !ok {
var n float64
c = &prometheusCounter{
name: name,
c: &dto.Metric{
Gauge: &dto.Gauge{Value: &n},
Label: labelMetric(clabels),
},
}
m.mu.Lock()
m.counter.Store(h, c)
m.mu.Unlock()
cd = &counters{cs: make(map[uint64]*prometheusCounter)}
c := &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.counter[nm] = cd
return c
}
return c.(*prometheusCounter)
c, ok := cd.cs[h]
if !ok {
c = &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.counter[nm] = cd
}
return c
}
func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels)
m.mu.Lock()
c, ok := m.counter.Load(h)
m.mu.Unlock()
defer m.mu.Unlock()
nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...)
cd, ok := m.floatCounter[nm]
h := newHash(labels)
if !ok {
var n float64
c = &prometheusFloatCounter{
name: name,
c: &dto.Metric{
Gauge: &dto.Gauge{Value: &n},
Label: labelMetric(clabels),
},
}
m.mu.Lock()
m.counter.Store(h, c)
m.mu.Unlock()
cd = &floatCounters{cs: make(map[uint64]*prometheusFloatCounter)}
c := &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.floatCounter[nm] = cd
return c
}
return c.(*prometheusFloatCounter)
c, ok := cd.cs[h]
if !ok {
c = &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.floatCounter[nm] = cd
}
return c
}
func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels)
m.mu.Lock()
c, ok := m.gauge.Load(h)
m.mu.Unlock()
defer m.mu.Unlock()
nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...)
cd, ok := m.gauge[nm]
h := newHash(labels)
if !ok {
var n float64
c = &prometheusGauge{
name: name,
c: &dto.Metric{
Gauge: &dto.Gauge{Value: &n},
Label: labelMetric(clabels),
},
}
m.mu.Lock()
m.gauge.Store(h, c)
m.mu.Unlock()
cd = &gauges{cs: make(map[uint64]*prometheusGauge)}
c := &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.gauge[nm] = cd
return c
}
return c.(*prometheusGauge)
c, ok := cd.cs[h]
if !ok {
c = &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.gauge[nm] = cd
}
return c
}
func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels)
m.mu.Lock()
c, ok := m.histogram.Load(h)
m.mu.Unlock()
defer m.mu.Unlock()
nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...)
cd, ok := m.histogram[nm]
h := newHash(labels)
if !ok {
var n uint64
var s float64
buckets := make([]float64, len(prometheus.DefBuckets))
copy(buckets, prometheus.DefBuckets)
mdto := &dto.Metric{
Histogram: &dto.Histogram{
SampleCount: &n,
SampleSum: &s,
CreatedTimestamp: timestamppb.Now(),
Bucket: make([]*dto.Bucket, len(buckets)),
},
Label: labelMetric(clabels),
}
for idx, b := range buckets {
var cc uint64
mdto.Histogram.Bucket[idx] = &dto.Bucket{CumulativeCount: &cc, UpperBound: &b}
}
c = &prometheusHistogram{
name: name,
c: mdto,
}
m.mu.Lock()
m.histogram.Store(h, c)
m.mu.Unlock()
cd = &histograms{cs: make(map[uint64]*prometheusHistogram)}
c := &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.histogram[nm] = cd
return c
}
return c.(*prometheusHistogram)
c, ok := cd.cs[h]
if !ok {
c = &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.histogram[nm] = cd
}
return c
}
func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels)
m.mu.Lock()
c, ok := m.summary.Load(h)
m.mu.Unlock()
defer m.mu.Unlock()
nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...)
cd, ok := m.summary[nm]
h := newHash(labels)
if !ok {
var n uint64
var s float64
c = &prometheusSummary{
name: name,
c: &dto.Metric{
Summary: &dto.Summary{
SampleCount: &n,
SampleSum: &s,
CreatedTimestamp: timestamppb.Now(),
},
Label: labelMetric(clabels),
},
}
m.mu.Lock()
m.summary.Store(h, c)
m.mu.Unlock()
cd = &summaries{cs: make(map[uint64]*prometheusSummary)}
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.summary[nm] = cd
return c
}
return c.(*prometheusSummary)
c, ok := cd.cs[h]
if !ok {
c = &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: nm}), labels: labels}
cd.cs[h] = c
m.summary[nm] = cd
}
return c
}
func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels)
m.mu.Lock()
c, ok := m.summary.Load(h)
m.mu.Lock()
defer m.mu.Unlock()
nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...)
cd, ok := m.summary[nm]
h := newHash(labels)
if !ok {
var n uint64
var s float64
c = &prometheusSummary{
name: name,
c: &dto.Metric{
Summary: &dto.Summary{
SampleCount: &n,
SampleSum: &s,
},
Label: labelMetric(clabels),
},
}
m.mu.Lock()
m.summary.Store(h, c)
m.mu.Unlock()
cd = &summaries{cs: make(map[uint64]*prometheusSummary)}
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
Name: nm,
MaxAge: window,
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}), labels: labels}
cd.cs[h] = c
m.summary[nm] = cd
return c
}
return c.(*prometheusSummary)
c, ok := cd.cs[h]
if !ok {
c = &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
Name: nm,
MaxAge: window,
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}), labels: labels}
cd.cs[h] = c
m.summary[nm] = cd
}
return c
}
func (m *prometheusMeter) Init(opts ...meter.Option) error {
for _, o := range opts {
o(&m.opts)
}
if m.opts.WriteProcessMetrics || m.opts.WriteFDMetrics {
pc := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})
_ = m.set.Register(pc)
gc := collectors.NewGoCollector(collectors.WithGoCollectorRuntimeMetrics(collectors.GoRuntimeMetricsRule{Matcher: regexp.MustCompile("/.*")}))
_ = m.set.Register(gc)
}
return nil
}
@@ -221,6 +259,13 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
o(&options)
}
if options.WriteProcessMetrics || options.WriteFDMetrics {
pc := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})
_ = m.set.Register(pc)
gc := collectors.NewGoCollector(collectors.WithGoCollectorRuntimeMetrics(collectors.GoRuntimeMetricsRule{Matcher: regexp.MustCompile("/.*")}))
_ = m.set.Register(gc)
}
g, ok := m.set.(prometheus.Gatherer)
if !ok {
return fmt.Errorf("set type %T not prometheus.Gatherer", m.set)
@@ -235,72 +280,85 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
m.mu.Lock()
m.counter.Range(func(_ any, v any) bool {
mc := v.(*prometheusCounter)
mf := m.mfPool.Get()
mf.Name = &mc.name
mf.Type = dto.MetricType_GAUGE.Enum()
n := getFloat64(&(mc.n))
mc.c.Gauge.Value = &n
mf.Metric = append(mf.Metric, mc.c)
for name, metrics := range m.counter {
mf := &dto.MetricFamily{
Name: newName(name),
Type: dto.MetricType_GAUGE.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
}
for _, c := range metrics.cs {
m := &dto.Metric{}
_ = c.c.Write(m)
fillMetric(m, c.labels)
mf.Metric = append(mf.Metric, m)
}
mfs = append(mfs, mf)
return true
})
}
m.floatCounter.Range(func(_ any, v any) bool {
mc := v.(*prometheusFloatCounter)
mf := m.mfPool.Get()
mf.Name = &mc.name
mf.Type = dto.MetricType_GAUGE.Enum()
n := getFloat64(&(mc.n))
mc.c.Gauge.Value = &n
mf.Metric = append(mf.Metric, mc.c)
for name, metrics := range m.gauge {
mf := &dto.MetricFamily{
Name: newName(name),
Type: dto.MetricType_GAUGE.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
}
for _, c := range metrics.cs {
m := &dto.Metric{}
_ = c.c.Write(m)
fillMetric(m, c.labels)
mf.Metric = append(mf.Metric, m)
}
mfs = append(mfs, mf)
return true
})
}
m.gauge.Range(func(_ any, v any) bool {
mc := v.(*prometheusGauge)
mf := m.mfPool.Get()
mf.Name = &mc.name
mf.Type = dto.MetricType_GAUGE.Enum()
n := getFloat64(&(mc.n))
mc.c.Gauge.Value = &n
mf.Metric = append(mf.Metric, mc.c)
for name, metrics := range m.floatCounter {
mf := &dto.MetricFamily{
Name: newName(name),
Type: dto.MetricType_GAUGE.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
}
for _, c := range metrics.cs {
m := &dto.Metric{}
_ = c.c.Write(m)
fillMetric(m, c.labels)
mf.Metric = append(mf.Metric, m)
}
mfs = append(mfs, mf)
return true
})
}
m.histogram.Range(func(_ any, v any) bool {
c := v.(*prometheusHistogram)
mf := m.mfPool.Get()
mf.Name = &c.name
mf.Type = dto.MetricType_HISTOGRAM.Enum()
mf.Metric = append(mf.Metric, c.c)
for name, metrics := range m.histogram {
mf := &dto.MetricFamily{
Name: newName(name),
Type: dto.MetricType_HISTOGRAM.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
}
for _, c := range metrics.cs {
m := &dto.Metric{}
_ = c.c.Write(m)
fillMetric(m, c.labels)
mf.Metric = append(mf.Metric, m)
}
mfs = append(mfs, mf)
return true
})
}
m.summary.Range(func(_ any, v any) bool {
mc := v.(*prometheusSummary)
mf := m.mfPool.Get()
mf.Name = &mc.name
mf.Type = dto.MetricType_SUMMARY.Enum()
sc := atomic.LoadUint64(&(mc.sampleCount))
mc.c.Summary.SampleCount = &sc
ss := getFloat64(&(mc.SampleSum))
mc.c.Summary.SampleSum = &ss
mf.Metric = append(mf.Metric, mc.c)
for name, metrics := range m.summary {
mf := &dto.MetricFamily{
Name: newName(name),
Type: dto.MetricType_SUMMARY.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
}
for _, c := range metrics.cs {
m := &dto.Metric{}
_ = c.c.Write(m)
fillMetric(m, c.labels)
mf.Metric = append(mf.Metric, m)
}
mfs = append(mfs, mf)
return true
})
}
m.mu.Unlock()
for _, mf := range mfs {
_ = enc.Encode(mf)
mf.Reset()
m.mfPool.Put(mf)
}
if closer, ok := enc.(io.Closer); ok {
@@ -317,6 +375,7 @@ func (m *prometheusMeter) Clone(opts ...meter.Option) meter.Meter {
}
return &prometheusMeter{
mu: m.mu,
set: m.set,
opts: options,
floatCounter: m.floatCounter,
@@ -344,30 +403,117 @@ func (m *prometheusMeter) Set(opts ...meter.Option) meter.Meter {
return nm
}
func labelMetric(labels []string) []*dto.LabelPair {
nl := make([]string, len(labels))
copy(nl, labels)
dtoLabels := make([]*dto.LabelPair, 0, len(nl)/2)
for idx := 0; idx < len(nl); idx += 2 {
dtoLabels = append(dtoLabels, &dto.LabelPair{
Name: &(nl[idx]),
Value: &(nl[idx+1]),
})
}
return dtoLabels
type prometheusCounter struct {
c prometheus.Gauge
labels []string
}
func newHash(n string, l []string) uint64 {
h := uint64(14695981039346656037)
for i := 0; i < len(n); i++ {
h ^= uint64(n[i])
h *= 1099511628211
}
for _, s := range l {
for i := 0; i < len(s); i++ {
h ^= uint64(s[i])
h *= 1099511628211
}
}
return h
func (c *prometheusCounter) Add(n int) {
c.c.Add(float64(n))
}
func (c *prometheusCounter) Dec() {
c.c.Dec()
}
func (c *prometheusCounter) Inc() {
c.c.Inc()
}
func (c *prometheusCounter) Get() uint64 {
m := &dto.Metric{}
if err := c.c.Write(m); err != nil {
return 0
}
return uint64(m.GetGauge().GetValue())
}
func (c *prometheusCounter) Set(n uint64) {
c.c.Set(float64(n))
}
type prometheusFloatCounter struct {
c prometheus.Gauge
labels []string
}
func (c prometheusFloatCounter) Add(n float64) {
c.c.Add(n)
}
func (c prometheusFloatCounter) Get() float64 {
m := &dto.Metric{}
if err := c.c.Write(m); err != nil {
return 0
}
return m.GetGauge().GetValue()
}
func (c prometheusFloatCounter) Set(n float64) {
c.c.Set(n)
}
func (c prometheusFloatCounter) Sub(n float64) {
c.c.Add(-n)
}
type prometheusGauge struct {
c prometheus.Gauge
labels []string
}
func (c prometheusGauge) Get() float64 {
m := &dto.Metric{}
if err := c.c.Write(m); err != nil {
return 0
}
return float64(m.GetGauge().GetValue())
}
type prometheusHistogram struct {
c prometheus.Histogram
labels []string
}
func (c prometheusHistogram) Reset() {
}
func (c prometheusHistogram) Update(n float64) {
c.c.Observe(n)
}
func (c prometheusHistogram) UpdateDuration(n time.Time) {
c.c.Observe(time.Since(n).Seconds())
}
type prometheusSummary struct {
c prometheus.Summary
labels []string
}
func (c prometheusSummary) Update(n float64) {
c.c.Observe(n)
}
func (c prometheusSummary) UpdateDuration(n time.Time) {
c.c.Observe(time.Since(n).Seconds())
}
func newHash(labels []string) uint64 {
h := fnv.New64a()
for _, l := range labels {
h.Write([]byte(l))
}
return h.Sum64()
}
func fillMetric(m *dto.Metric, labels []string) *dto.Metric {
m.Label = make([]*dto.LabelPair, 0, len(labels)/2)
for idx := 0; idx < len(labels); idx += 2 {
m.Label = append(m.Label, &dto.LabelPair{
Name: newString(labels[idx]),
Value: newString(labels[idx+1]),
})
}
return m
}

View File

@@ -2,126 +2,12 @@ package prometheus
import (
"bytes"
"context"
"fmt"
"testing"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"go.unistack.org/micro/v3/client"
"go.unistack.org/micro/v3/codec"
"go.unistack.org/micro/v3/meter"
"go.unistack.org/micro/v4/meter"
)
func TestHash(t *testing.T) {
m := NewMeter() // meter.Labels("test_key", "test_val"))
buf := bytes.NewBuffer(nil)
for i := 0; i < 100000; i++ {
go func() {
m.Counter("micro_server_request_total", "code", "16",
"endpoint", "/clientprofile.ClientProfileService/GetClientProfile",
"status", "failure").Inc()
m.Counter("micro_server_request_total", "code", "16",
"endpoint", "/clientproduct.ClientProductService/GetDepositProducts",
"status", "failure").Inc()
m.Counter("micro_server_request_total", "code", "16",
"endpoint", "/operationsinfo.OperationsInfoService/GetOperations",
"status", "failure").Inc()
}()
}
_ = m.Write(buf)
t.Logf("h1: %s\n", buf.Bytes())
}
func TestHistogram(t *testing.T) {
m := NewMeter()
name := "test"
m.Histogram(name, "endpoint").Update(1)
m.Histogram(name, "endpoint").Update(1)
m.Histogram(name, "endpoint").Update(5)
m.Histogram(name, "endpoint").Update(10)
m.Histogram(name, "endpoint").Update(10)
m.Histogram(name, "endpoint").Update(30)
mbuf := bytes.NewBuffer(nil)
_ = m.Write(mbuf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false))
/*
if !bytes.Contains(buf.Bytes(), []byte(`micro_server_sum{endpoint="ep1",path="/path1"} 20`)) {
t.Fatalf("invalid metrics output: %s", buf.Bytes())
}
if !bytes.Contains(buf.Bytes(), []byte(`micro_server_count{endpoint="ep1",path="/path1"} 2`)) {
t.Fatalf("invalid metrics output: %s", buf.Bytes())
}
*/
p := prometheus.NewHistogram(prometheus.HistogramOpts{Name: name})
p.Observe(1)
p.Observe(1)
p.Observe(5)
p.Observe(10)
p.Observe(10)
p.Observe(30)
mdto := &dto.Metric{}
_ = p.Write(mdto)
pbuf := bytes.NewBuffer(nil)
enc := expfmt.NewEncoder(pbuf, expfmt.NewFormat(expfmt.TypeTextPlain))
mf := &dto.MetricFamily{Name: &name, Type: dto.MetricType_HISTOGRAM.Enum(), Metric: []*dto.Metric{mdto}}
_ = enc.Encode(mf)
if !bytes.Equal(mbuf.Bytes(), pbuf.Bytes()) {
fmt.Printf("m\n%s\n", mbuf.Bytes())
fmt.Printf("m\n%s\n", pbuf.Bytes())
}
}
func TestSummary(t *testing.T) {
t.Skip()
name := "micro_server"
m := NewMeter()
m.Summary("micro_server").Update(1)
m.Summary("micro_server").Update(1)
m.Summary("micro_server").Update(5)
m.Summary("micro_server").Update(10)
m.Summary("micro_server").Update(10)
m.Summary("micro_server").Update(30)
mbuf := bytes.NewBuffer(nil)
_ = m.Write(mbuf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false))
if !bytes.Contains(mbuf.Bytes(), []byte(`micro_server_sum 57`)) {
t.Fatalf("invalid metrics output: %s", mbuf.Bytes())
}
if !bytes.Contains(mbuf.Bytes(), []byte(`micro_server_count 6`)) {
t.Fatalf("invalid metrics output: %s", mbuf.Bytes())
}
objectives := make(map[float64]float64)
for _, c := range meter.DefaultSummaryQuantiles {
objectives[c] = c
}
p := prometheus.NewSummary(prometheus.SummaryOpts{Name: name, Objectives: objectives, MaxAge: meter.DefaultSummaryWindow})
p.Observe(1)
p.Observe(1)
p.Observe(5)
p.Observe(10)
p.Observe(10)
p.Observe(30)
mdto := &dto.Metric{}
_ = p.Write(mdto)
pbuf := bytes.NewBuffer(nil)
enc := expfmt.NewEncoder(pbuf, expfmt.NewFormat(expfmt.TypeTextPlain))
mf := &dto.MetricFamily{Name: &name, Type: dto.MetricType_SUMMARY.Enum(), Metric: []*dto.Metric{mdto}}
_ = enc.Encode(mf)
if !bytes.Equal(mbuf.Bytes(), pbuf.Bytes()) {
fmt.Printf("m\n%s\n", mbuf.Bytes())
fmt.Printf("m\n%s\n", pbuf.Bytes())
}
}
func TestStd(t *testing.T) {
m := NewMeter(meter.WriteProcessMetrics(true), meter.WriteFDMetrics(true))
if err := m.Init(); err != nil {
@@ -134,24 +20,16 @@ func TestStd(t *testing.T) {
}
}
func TestWrapper(t *testing.T) {
m := NewMeter() // meter.Labels("test_key", "test_val"))
ctx := context.Background()
c := client.NewClient(client.Meter(m))
if err := c.Init(); err != nil {
t.Fatal(err)
}
rsp := &codec.Frame{}
req := &codec.Frame{}
err := c.Call(ctx, c.NewRequest("svc2", "Service.Method", req), rsp)
_, _ = rsp, err
buf := bytes.NewBuffer(nil)
_ = m.Write(buf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false))
if !bytes.Contains(buf.Bytes(), []byte(`micro_client_request_inflight{endpoint="Service.Method"} 0`)) {
t.Fatalf("invalid metrics output: %s", buf.Bytes())
func TestBuildName(t *testing.T) {
m := NewMeter()
check := `micro_foo{aaa="b",bar="baz",ccc="d"}`
name := m.buildMetric("micro_foo", "bar", "baz", "aaa", "b", "ccc", "d")
if name != check {
t.Fatalf("metric name error: %s != %s", name, check)
}
cnt := m.Counter("counter", "key", "val")
cnt.Inc()
}
func TestMultiple(t *testing.T) {
@@ -168,9 +46,31 @@ func TestMultiple(t *testing.T) {
buf := bytes.NewBuffer(nil)
_ = m.Write(buf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false))
if !bytes.Contains(buf.Bytes(), []byte(`micro_server{endpoint="ep1",path="/path1"} 2`)) {
t.Fatalf("invalid metrics output: %s", buf.Bytes())
}
if !bytes.Contains(buf.Bytes(), []byte(`micro_server{endpoint="ep3",path="/path3",status="success"} 1`)) {
// t.Fatal("XXXX")
t.Fatalf("invalid metrics output: %s", buf.Bytes())
}
}
func TestCounterSet(t *testing.T) {
m := NewMeter()
value := uint64(42)
m.Counter("forte_accounts_total", "channel_code", "crm").Set(value)
fmt.Println(uint64(float64(value)))
buf := bytes.NewBuffer(nil)
_ = m.Write(buf)
output := buf.String()
fmt.Println(output)
expectedOutput := fmt.Sprintf(`%s{channel_code="crm"} %d`, "forte_accounts_total", value)
if !bytes.Contains(buf.Bytes(), []byte(expectedOutput)) {
t.Fatalf("invalid metrics output: expected %q, got %q", expectedOutput, output)
}
}

View File

@@ -1,26 +0,0 @@
package prometheus
import (
"sync/atomic"
"time"
dto "github.com/prometheus/client_model/go"
)
type prometheusSummary struct {
name string
c *dto.Metric
sampleCount uint64
SampleSum float64
}
func (c *prometheusSummary) Update(n float64) {
atomic.AddUint64(&(c.sampleCount), 1)
addFloat64(&(c.SampleSum), n)
}
func (c *prometheusSummary) UpdateDuration(t time.Time) {
n := time.Since(t).Seconds()
atomic.AddUint64(&(c.sampleCount), 1)
addFloat64(&(c.SampleSum), n)
}