Compare commits

..

No commits in common. "v3" and "v3.8.0" have entirely different histories.
v3 ... v3.8.0

14 changed files with 440 additions and 677 deletions

View File

@ -1,20 +0,0 @@
name: "autoapprove"
on:
pull_request_target:
types: [assigned, opened, synchronize, reopened]
permissions:
pull-requests: write
contents: write
jobs:
autoapprove:
runs-on: ubuntu-latest
steps:
- name: approve
uses: hmarr/auto-approve-action@v3
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
id: approve
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

View File

@ -1,21 +0,0 @@
name: "automerge"
on:
pull_request_target:
types: [assigned, opened, synchronize, reopened]
permissions:
pull-requests: write
contents: write
jobs:
automerge:
runs-on: ubuntu-latest
if: github.actor == 'vtolstov'
steps:
- name: merge
id: merge
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.TOKEN}}

View File

@ -3,20 +3,19 @@ on:
push: push:
branches: branches:
- master - master
- v3
jobs: jobs:
test: test:
name: test name: test
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: setup - name: setup
uses: actions/setup-go@v3 uses: actions/setup-go@v2
with: with:
go-version: 1.17 go-version: 1.16
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v2
- name: cache - name: cache
uses: actions/cache@v3 uses: actions/cache@v2
with: with:
path: ~/go/pkg/mod path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
@ -32,9 +31,9 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v2
- name: lint - name: lint
uses: golangci/golangci-lint-action@v3.4.0 uses: golangci/golangci-lint-action@v2
continue-on-error: true continue-on-error: true
with: with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.

View File

@ -43,14 +43,14 @@ jobs:
steps: steps:
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v2
- name: setup - name: setup
uses: actions/setup-go@v3 uses: actions/setup-go@v2
with: with:
go-version: 1.17 go-version: 1.16
# Initializes the CodeQL tools for scanning. # Initializes the CodeQL tools for scanning.
- name: init - name: init
uses: github/codeql-action/init@v2 uses: github/codeql-action/init@v1
with: with:
languages: ${{ matrix.language }} languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file. # If you wish to specify custom queries, you can do so here or in a config file.
@ -61,7 +61,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below) # If this step fails, then you should remove it and run the build manually (see below)
- name: autobuild - name: autobuild
uses: github/codeql-action/autobuild@v2 uses: github/codeql-action/autobuild@v1
# Command-line programs to run using the OS shell. # Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl # 📚 https://git.io/JvXDl
@ -75,4 +75,4 @@ jobs:
# make release # make release
- name: analyze - name: analyze
uses: github/codeql-action/analyze@v2 uses: github/codeql-action/analyze@v1

View File

@ -1,4 +1,4 @@
name: "dependabot-automerge" name: "prautomerge"
on: on:
pull_request_target: pull_request_target:
@ -9,17 +9,21 @@ permissions:
contents: write contents: write
jobs: jobs:
automerge: dependabot:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.actor == 'dependabot[bot]' if: ${{ github.actor == 'dependabot[bot]' }}
steps: steps:
- name: metadata - name: metadata
id: metadata id: metadata
uses: dependabot/fetch-metadata@v1.3.6 uses: dependabot/fetch-metadata@v1.1.1
with: with:
github-token: "${{ secrets.TOKEN }}" github-token: "${{ secrets.TOKEN }}"
- name: approve
run: gh pr review --approve "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.TOKEN}}
- name: merge - name: merge
id: merge
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}} if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
run: gh pr merge --auto --merge "$PR_URL" run: gh pr merge --auto --merge "$PR_URL"
env: env:

View File

@ -3,20 +3,19 @@ on:
pull_request: pull_request:
branches: branches:
- master - master
- v3
jobs: jobs:
test: test:
name: test name: test
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: setup - name: setup
uses: actions/setup-go@v3 uses: actions/setup-go@v2
with: with:
go-version: 1.17 go-version: 1.16
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v2
- name: cache - name: cache
uses: actions/cache@v3 uses: actions/cache@v2
with: with:
path: ~/go/pkg/mod path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
@ -32,9 +31,9 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: checkout - name: checkout
uses: actions/checkout@v3 uses: actions/checkout@v2
- name: lint - name: lint
uses: golangci/golangci-lint-action@v3.4.0 uses: golangci/golangci-lint-action@v2
continue-on-error: true continue-on-error: true
with: with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.

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))
}

23
go.mod
View File

@ -1,23 +1,10 @@
module go.unistack.org/micro-meter-prometheus/v3 module go.unistack.org/micro-meter-prometheus/v3
go 1.22.0 go 1.16
require ( require (
github.com/prometheus/client_golang v1.20.4 github.com/prometheus/client_golang v1.11.0
github.com/prometheus/client_model v0.6.1 github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.59.1 github.com/prometheus/common v0.26.0
go.unistack.org/micro/v3 v3.10.91 go.unistack.org/micro/v3 v3.8.7
google.golang.org/protobuf v1.34.2
)
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
golang.org/x/sys v0.25.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.58.2 // indirect
) )

192
go.sum
View File

@ -1,44 +1,156 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang-jwt/jwt/v4 v4.1.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= github.com/prometheus/client_golang v1.11.0 h1:HNkLOAEQMIDv/K+04rukrLx6ch7msSRwf3/SASFAGtQ=
github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJoX0= github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q= github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo= github.com/prometheus/common v0.26.0 h1:iMAkS2TDoNWnKM+Kopnx/8tnEStIfpYA0ur0xQzzhMQ=
go.unistack.org/micro/v3 v3.10.91 h1:vuJY4tXwpqimwIkEJ3TozMYNVQQs+C5QMlQWPgSY/YM= github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
go.unistack.org/micro/v3 v3.10.91/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= github.com/silas/dag v0.0.0-20210626123444-3804bac2d6d4/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
google.golang.org/grpc v1.58.2 h1:SXUpjxeVF3FKrTYQI4f4KvbGD5u2xccdYdurwowix5I= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
google.golang.org/grpc v1.58.2/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= go.unistack.org/micro-proto/v3 v3.1.0 h1:q39FwjFiRZn+Ux/tt+d3bJTmDtsQQWa+3SLYVo1vLfA=
go.unistack.org/micro-proto/v3 v3.1.0/go.mod h1:DpRhYCBXlmSJ/AAXTmntvlh7kQkYU6eFvlmYAx4BQS8=
go.unistack.org/micro/v3 v3.8.7 h1:k1zOpJ3uS8MxdhK8annRsa5J/LW7MpqPjwYuekW61wE=
go.unistack.org/micro/v3 v3.8.7/go.mod h1:KMMmOmbgo/D52/rCAbqeKbBsgEEbSKM69he54J3ZIuA=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 h1:JWgyZ1qgdTaF3N3oxC+MdTV7qvEEgHo3otj+HB5CM7Q=
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

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

@ -3,9 +3,7 @@ package prometheus
import ( import (
"fmt" "fmt"
"io" "io"
"regexp"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -13,195 +11,192 @@ import (
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/v3/meter"
xpool "go.unistack.org/micro/v3/util/xpool"
"google.golang.org/protobuf/types/known/timestamppb"
) )
var _ meter.Meter = (*prometheusMeter)(nil)
type prometheusMeter struct { type prometheusMeter struct {
opts meter.Options opts meter.Options
set prometheus.Registerer set prometheus.Registerer
counter map[uint64]*prometheusCounter counter map[string]prometheusCounter
floatCounter map[uint64]*prometheusFloatCounter floatCounter map[string]prometheusFloatCounter
gauge map[uint64]*prometheusGauge gauge map[string]prometheusGauge
histogram map[uint64]*prometheusHistogram histogram map[string]prometheusHistogram
summary map[uint64]*prometheusSummary summary map[string]prometheusSummary
mfPool xpool.Pool[*dto.MetricFamily] sync.Mutex
mu sync.Mutex
} }
func NewMeter(opts ...meter.Option) *prometheusMeter { func NewMeter(opts ...meter.Option) meter.Meter {
return &prometheusMeter{ return &prometheusMeter{
set: prometheus.NewRegistry(), // prometheus.DefaultRegisterer, set: prometheus.DefaultRegisterer,
opts: meter.NewOptions(opts...), opts: meter.NewOptions(opts...),
counter: make(map[uint64]*prometheusCounter), counter: make(map[string]prometheusCounter),
floatCounter: make(map[uint64]*prometheusFloatCounter), floatCounter: make(map[string]prometheusFloatCounter),
gauge: make(map[uint64]*prometheusGauge), gauge: make(map[string]prometheusGauge),
histogram: make(map[uint64]*prometheusHistogram), histogram: make(map[string]prometheusHistogram),
summary: make(map[uint64]*prometheusSummary), summary: make(map[string]prometheusSummary),
mfPool: xpool.NewPool[*dto.MetricFamily](func() *dto.MetricFamily {
return &dto.MetricFamily{}
}),
} }
} }
func (m *prometheusMeter) buildMetric(name string, labels ...string) string {
if len(m.opts.MetricPrefix) > 0 {
name = m.opts.MetricPrefix + name
}
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...)
if len(m.opts.LabelPrefix) == 0 {
return meter.BuildName(name, nlabels...)
}
for idx := 0; idx < nl; idx++ {
nlabels[idx] = m.opts.LabelPrefix + nlabels[idx]
idx++
}
return meter.BuildName(name, nlabels...)
}
func (m *prometheusMeter) buildName(name string) string {
if len(m.opts.MetricPrefix) > 0 {
name = m.opts.MetricPrefix + name
}
return name
}
func (m *prometheusMeter) buildLabels(labels ...string) []string {
nl := len(m.opts.Labels) + len(labels)
if nl == 0 {
return nil
}
nlabels := make([]string, 0, nl)
nlabels = append(nlabels, m.opts.Labels...)
nlabels = append(nlabels, labels...)
for idx := 0; idx < nl; idx++ {
nlabels[idx] = m.opts.LabelPrefix + nlabels[idx]
idx++
}
return nlabels
}
func (m *prometheusMeter) Name() string { func (m *prometheusMeter) Name() string {
return m.opts.Name return m.opts.Name
} }
func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter { func (m *prometheusMeter) mapLabels(labels ...string) map[string]string {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) labels = m.buildLabels(labels...)
h := newHash(name, clabels) elementMap := make(map[string]string, len(labels)/2)
m.mu.Lock() for idx := 0; idx < len(labels); idx++ {
c, ok := m.counter[h] elementMap[labels[idx]] = labels[idx+1]
// fmt.Printf("counter name %s hash %v labels %v\n", name, h, labels) idx++
m.mu.Unlock()
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[h] = c
m.mu.Unlock()
} }
return elementMap
}
func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
m.Lock()
defer m.Unlock()
nm := m.buildMetric(name, labels...)
c, ok := m.counter[nm]
if !ok {
nc := prometheus.NewGauge(prometheus.GaugeOpts{Name: m.buildName(name), ConstLabels: m.mapLabels(labels...)})
m.set.MustRegister(nc)
c = prometheusCounter{c: nc}
m.counter[nm] = c
}
return c return c
} }
func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter { func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) m.Lock()
h := newHash(name, clabels) defer m.Unlock()
m.mu.Lock()
c, ok := m.floatCounter[h] nm := m.buildMetric(name, labels...)
m.mu.Unlock() c, ok := m.floatCounter[nm]
if !ok { if !ok {
var n float64 nc := prometheus.NewGauge(prometheus.GaugeOpts{Name: m.buildName(name), ConstLabels: m.mapLabels(labels...)})
c = &prometheusFloatCounter{ m.set.MustRegister(nc)
name: name, c = prometheusFloatCounter{c: nc}
c: &dto.Metric{ m.floatCounter[nm] = c
Gauge: &dto.Gauge{Value: &n},
Label: labelMetric(clabels),
},
}
m.mu.Lock()
m.floatCounter[h] = c
m.mu.Unlock()
} }
return c return c
} }
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 {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) m.Lock()
h := newHash(name, clabels) defer m.Unlock()
m.mu.Lock()
c, ok := m.gauge[h] nm := m.buildMetric(name, labels...)
m.mu.Unlock() c, ok := m.gauge[nm]
if !ok { if !ok {
var n float64 nc := prometheus.NewGauge(prometheus.GaugeOpts{Name: m.buildName(name), ConstLabels: m.mapLabels(labels...)})
c = &prometheusGauge{ m.set.MustRegister(nc)
name: name, c = prometheusGauge{c: nc}
c: &dto.Metric{ m.gauge[nm] = c
Gauge: &dto.Gauge{Value: &n},
Label: labelMetric(clabels),
},
}
m.mu.Lock()
m.gauge[h] = c
m.mu.Unlock()
} }
return c return c
} }
func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram { func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) m.Lock()
h := newHash(name, clabels) defer m.Unlock()
m.mu.Lock()
c, ok := m.histogram[h] nm := m.buildMetric(name, labels...)
m.mu.Unlock() c, ok := m.histogram[nm]
if !ok { if !ok {
var n uint64 nc := prometheus.NewHistogram(prometheus.HistogramOpts{Name: m.buildName(name), ConstLabels: m.mapLabels(labels...)})
var s float64 m.set.MustRegister(nc)
buckets := make([]float64, len(prometheus.DefBuckets)) c = prometheusHistogram{c: nc}
copy(buckets, prometheus.DefBuckets) m.histogram[nm] = c
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[h] = c
m.mu.Unlock()
} }
return c return c
} }
func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary { func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) m.Lock()
h := newHash(name, clabels) defer m.Unlock()
m.mu.Lock()
c, ok := m.summary[h] nm := m.buildMetric(name, labels...)
m.mu.Unlock() c, ok := m.summary[nm]
if !ok { if !ok {
var n uint64 nc := prometheus.NewSummary(prometheus.SummaryOpts{Name: m.buildName(name), ConstLabels: m.mapLabels(labels...)})
var s float64 m.set.MustRegister(nc)
c = &prometheusSummary{ c = prometheusSummary{c: nc}
name: name, m.summary[nm] = c
c: &dto.Metric{
Summary: &dto.Summary{
SampleCount: &n,
SampleSum: &s,
CreatedTimestamp: timestamppb.Now(),
},
Label: labelMetric(clabels),
},
}
m.mu.Lock()
m.summary[h] = c
m.mu.Unlock()
} }
return c return c
} }
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 {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) m.Lock()
h := newHash(name, clabels) defer m.Unlock()
m.mu.Lock()
c, ok := m.summary[h] nm := m.buildMetric(name, labels...)
m.mu.Lock() c, ok := m.summary[nm]
if !ok { if !ok {
var n uint64 nc := prometheus.NewSummary(prometheus.SummaryOpts{
var s float64 Name: m.buildName(name),
c = &prometheusSummary{ ConstLabels: m.mapLabels(labels...),
name: name, MaxAge: window,
c: &dto.Metric{ Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
Summary: &dto.Summary{ })
SampleCount: &n, m.set.MustRegister(nc)
SampleSum: &s, c = prometheusSummary{c: nc}
}, m.summary[nm] = c
Label: labelMetric(clabels),
},
}
m.mu.Lock()
m.summary[h] = c
m.mu.Unlock()
} }
return c return c
} }
@ -210,13 +205,6 @@ func (m *prometheusMeter) Init(opts ...meter.Option) error {
o(&m.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 return nil
} }
@ -226,6 +214,11 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
o(&options) o(&options)
} }
if options.WriteProcessMetrics || options.WriteFDMetrics {
c := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})
_ = m.set.Register(c)
}
g, ok := m.set.(prometheus.Gatherer) g, ok := m.set.(prometheus.Gatherer)
if !ok { if !ok {
return fmt.Errorf("set type %T not prometheus.Gatherer", m.set) return fmt.Errorf("set type %T not prometheus.Gatherer", m.set)
@ -236,66 +229,9 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
return err return err
} }
enc := expfmt.NewEncoder(w, expfmt.NewFormat(expfmt.TypeTextPlain)) enc := expfmt.NewEncoder(w, expfmt.FmtText)
m.mu.Lock()
for _, mc := range m.counter {
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)
mfs = append(mfs, mf)
}
for _, mc := range m.floatCounter {
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)
mfs = append(mfs, mf)
}
for _, mc := range m.gauge {
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)
mfs = append(mfs, mf)
}
for _, c := range m.histogram {
mf := m.mfPool.Get()
mf.Name = &c.name
mf.Type = dto.MetricType_HISTOGRAM.Enum()
mf.Metric = append(mf.Metric, c.c)
mfs = append(mfs, mf)
}
for _, mc := range m.summary {
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)
mfs = append(mfs, mf)
}
m.mu.Unlock()
for _, mf := range mfs { for _, mf := range mfs {
_ = enc.Encode(mf) _ = enc.Encode(mf)
mf.Reset()
m.mfPool.Put(mf)
} }
if closer, ok := enc.(io.Closer); ok { if closer, ok := enc.(io.Closer); ok {
@ -312,13 +248,12 @@ func (m *prometheusMeter) Clone(opts ...meter.Option) meter.Meter {
} }
return &prometheusMeter{ return &prometheusMeter{
set: m.set, set: m.set,
opts: options, opts: options,
floatCounter: m.floatCounter, counter: m.counter,
counter: m.counter, gauge: m.gauge,
gauge: m.gauge, histogram: m.histogram,
histogram: m.histogram, summary: m.summary,
summary: m.summary,
} }
} }
@ -339,30 +274,93 @@ func (m *prometheusMeter) Set(opts ...meter.Option) meter.Meter {
return nm return nm
} }
func labelMetric(labels []string) []*dto.LabelPair { type prometheusCounter struct {
nl := make([]string, len(labels)) c prometheus.Gauge
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
} }
func newHash(n string, l []string) uint64 { func (c prometheusCounter) Add(n int) {
h := uint64(14695981039346656037) c.c.Add(float64(n))
for i := 0; i < len(n); i++ { }
h ^= uint64(n[i])
h *= 1099511628211 func (c prometheusCounter) Dec() {
} c.c.Dec()
for _, s := range l { }
for i := 0; i < len(s); i++ {
h ^= uint64(s[i]) func (c prometheusCounter) Inc() {
h *= 1099511628211 c.c.Inc()
} }
}
return h 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
}
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
}
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
}
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
}
func (c prometheusSummary) Update(n float64) {
c.c.Observe(n)
}
func (c prometheusSummary) UpdateDuration(n time.Time) {
c.c.Observe(time.Since(n).Seconds())
} }

View File

@ -3,143 +3,40 @@ package prometheus
import ( import (
"bytes" "bytes"
"context" "context"
"fmt"
"testing" "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/client"
"go.unistack.org/micro/v3/codec" "go.unistack.org/micro/v3/codec"
"go.unistack.org/micro/v3/meter" "go.unistack.org/micro/v3/meter"
"go.unistack.org/micro/v3/meter/wrapper"
) )
func TestHash(t *testing.T) { func TestBuildName(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() m := NewMeter()
name := "test" im := m.(*prometheusMeter)
m.Histogram(name, "endpoint").Update(1) check := `micro_foo{micro_aaa="b",micro_bar="baz",micro_ccc="d"}`
m.Histogram(name, "endpoint").Update(1) name := im.buildMetric("foo", "bar", "baz", "aaa", "b", "ccc", "d")
m.Histogram(name, "endpoint").Update(5) if name != check {
m.Histogram(name, "endpoint").Update(10) t.Fatalf("metric name error: %s != %s", name, check)
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`)) { cnt := m.Counter("counter", "key", "val")
t.Fatalf("invalid metrics output: %s", mbuf.Bytes()) cnt.Inc()
}
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 {
t.Fatal(err)
}
buf := bytes.NewBuffer(nil)
_ = m.Write(buf)
if !bytes.Contains(buf.Bytes(), []byte(`go_goroutine`)) {
t.Fatalf("invalid metrics output: %s", buf.Bytes())
}
} }
func TestWrapper(t *testing.T) { func TestWrapper(t *testing.T) {
m := NewMeter() // meter.Labels("test_key", "test_val")) m := NewMeter() // meter.Labels("test_key", "test_val"))
w := wrapper.NewClientWrapper(
wrapper.ServiceName("svc1"),
wrapper.ServiceVersion("0.0.1"),
wrapper.ServiceID("12345"),
wrapper.Meter(m),
)
ctx := context.Background() ctx := context.Background()
c := client.NewClient(client.Meter(m)) c := client.NewClient(client.Wrap(w))
if err := c.Init(); err != nil { if err := c.Init(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -149,28 +46,7 @@ func TestWrapper(t *testing.T) {
_, _ = rsp, err _, _ = rsp, err
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_client_request_inflight{endpoint="Service.Method"} 0`)) { if !bytes.Contains(buf.Bytes(), []byte(`micro_client_request_inflight{micro_endpoint="svc2.Service.Method"} 0`)) {
t.Fatalf("invalid metrics output: %s", buf.Bytes())
}
}
func TestMultiple(t *testing.T) {
m := NewMeter() // meter.Labels("test_key", "test_val"))
m.Counter("micro_server", "endpoint", "ep1", "path", "/path1").Inc()
m.Counter("micro_server", "endpoint", "ep1", "path", "/path1").Inc()
m.Counter("micro_server", "endpoint", "ep2", "path", "/path2").Inc()
m.Counter("micro_server", "endpoint", "ep2", "path", "/path2").Inc()
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{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.Fatalf("invalid metrics output: %s", buf.Bytes()) t.Fatalf("invalid metrics output: %s", buf.Bytes())
} }
} }

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)
}