Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
fc9be7fb46 | |||
1dc55cc0c9 | |||
21494e0e7a | |||
0975e8fb4d | |||
8dfa97b54f | |||
439d5cf125 | |||
e02f291db2 |
8
.github/ISSUE_TEMPLATE/bug_report.md
vendored
8
.github/ISSUE_TEMPLATE/bug_report.md
vendored
@@ -1,6 +1,6 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: For reporting bugs in micro
|
||||
about: For reporting bugs in go-micro
|
||||
title: "[BUG]"
|
||||
labels: ''
|
||||
assignees: ''
|
||||
@@ -16,3 +16,9 @@ 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
|
||||
```
|
||||
|
@@ -1,6 +1,6 @@
|
||||
---
|
||||
name: Feature request / Enhancement
|
||||
about: If you have a need not served by micro
|
||||
about: If you have a need not served by go-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.
|
||||
|
10
.github/ISSUE_TEMPLATE/question.md
vendored
10
.github/ISSUE_TEMPLATE/question.md
vendored
@@ -1,8 +1,14 @@
|
||||
---
|
||||
name: Question
|
||||
about: Ask a question about micro
|
||||
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
|
||||
|
28
.github/autoapprove.yml
vendored
28
.github/autoapprove.yml
vendored
@@ -1,28 +0,0 @@
|
||||
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 }}
|
19
.github/dependabot.yml
vendored
Normal file
19
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
# To get started with Dependabot version updates, you'll need to specify which
|
||||
# package ecosystems to update and where the package manifests are located.
|
||||
# Please see the documentation for all configuration options:
|
||||
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
||||
|
||||
version: 2
|
||||
updates:
|
||||
|
||||
# Maintain dependencies for GitHub Actions
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "daily"
|
||||
|
||||
# Maintain dependencies for Golang
|
||||
- package-ecosystem: "gomod"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "daily"
|
20
.github/workflows/autoapprove.yml
vendored
Normal file
20
.github/workflows/autoapprove.yml
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
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 }}
|
21
.github/workflows/automerge.yml
vendored
Normal file
21
.github/workflows/automerge.yml
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
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}}
|
47
.github/workflows/build.yml
vendored
Normal file
47
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
name: build
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
jobs:
|
||||
test:
|
||||
name: test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: setup
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.17
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: cache
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: ${{ runner.os }}-go-
|
||||
- name: deps
|
||||
run: go get -v -t -d ./...
|
||||
- name: test
|
||||
env:
|
||||
INTEGRATION_TESTS: yes
|
||||
run: go test -mod readonly -v ./...
|
||||
lint:
|
||||
name: lint
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: lint
|
||||
uses: golangci/golangci-lint-action@v3.4.0
|
||||
continue-on-error: true
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
version: v1.30
|
||||
# Optional: working directory, useful for monorepos
|
||||
# working-directory: somedir
|
||||
# Optional: golangci-lint command line arguments.
|
||||
# args: --issues-exit-code=0
|
||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||
# only-new-issues: true
|
78
.github/workflows/codeql-analysis.yml
vendored
Normal file
78
.github/workflows/codeql-analysis.yml
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
# For most projects, this workflow file will not need changing; you simply need
|
||||
# to commit it to your repository.
|
||||
#
|
||||
# You may wish to alter this file to override the set of languages analyzed,
|
||||
# or to provide custom queries or build logic.
|
||||
#
|
||||
# ******** NOTE ********
|
||||
# We have attempted to detect the languages in your repository. Please check
|
||||
# the `language` matrix defined below to confirm you have the correct set of
|
||||
# supported CodeQL languages.
|
||||
#
|
||||
name: "codeql"
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: ["prbuild"]
|
||||
types:
|
||||
- completed
|
||||
push:
|
||||
branches: [ master, v3 ]
|
||||
pull_request:
|
||||
# The branches below must be a subset of the branches above
|
||||
branches: [ master, v3 ]
|
||||
schedule:
|
||||
- cron: '34 1 * * 0'
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
name: analyze
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
actions: read
|
||||
contents: read
|
||||
security-events: write
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
language: [ 'go' ]
|
||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
||||
# Learn more:
|
||||
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
||||
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: setup
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.17
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: init
|
||||
uses: github/codeql-action/init@v2
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
# By default, queries listed here will override any specified in a config file.
|
||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||
|
||||
# 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)
|
||||
- name: autobuild
|
||||
uses: github/codeql-action/autobuild@v2
|
||||
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
|
||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
||||
# and modify them (or add more) to build your code if your project
|
||||
# uses a compiled language
|
||||
|
||||
#- run: |
|
||||
# make bootstrap
|
||||
# make release
|
||||
|
||||
- name: analyze
|
||||
uses: github/codeql-action/analyze@v2
|
27
.github/workflows/dependabot-automerge.yml
vendored
Normal file
27
.github/workflows/dependabot-automerge.yml
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
name: "dependabot-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 == 'dependabot[bot]'
|
||||
steps:
|
||||
- name: metadata
|
||||
id: metadata
|
||||
uses: dependabot/fetch-metadata@v1.3.6
|
||||
with:
|
||||
github-token: "${{ secrets.TOKEN }}"
|
||||
- name: merge
|
||||
id: merge
|
||||
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
|
||||
run: gh pr merge --auto --merge "$PR_URL"
|
||||
env:
|
||||
PR_URL: ${{github.event.pull_request.html_url}}
|
||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
53
.github/workflows/job_coverage.yml
vendored
53
.github/workflows/job_coverage.yml
vendored
@@ -1,53 +0,0 @@
|
||||
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 }}
|
29
.github/workflows/job_lint.yml
vendored
29
.github/workflows/job_lint.yml
vendored
@@ -1,29 +0,0 @@
|
||||
name: lint
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
branches: [ master, v3, v4 ]
|
||||
paths-ignore:
|
||||
- '.github/**'
|
||||
- '.gitea/**'
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
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: setup deps
|
||||
run: go get -v ./...
|
||||
- name: run lint
|
||||
uses: golangci/golangci-lint-action@v6
|
||||
with:
|
||||
version: 'latest'
|
94
.github/workflows/job_sync.yml
vendored
94
.github/workflows/job_sync.yml
vendored
@@ -1,94 +0,0 @@
|
||||
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
|
31
.github/workflows/job_test.yml
vendored
31
.github/workflows/job_test.yml
vendored
@@ -1,31 +0,0 @@
|
||||
name: test
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
branches: [ master, v3, v4 ]
|
||||
push:
|
||||
branches: [ master, v3, v4 ]
|
||||
paths-ignore:
|
||||
- '.github/**'
|
||||
- '.gitea/**'
|
||||
|
||||
jobs:
|
||||
test:
|
||||
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: setup deps
|
||||
run: go get -v ./...
|
||||
- name: run test
|
||||
env:
|
||||
INTEGRATION_TESTS: yes
|
||||
run: go test -mod readonly -v ./...
|
50
.github/workflows/job_tests.yml
vendored
50
.github/workflows/job_tests.yml
vendored
@@ -1,50 +0,0 @@
|
||||
name: test
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
branches: [ master, v3, v4 ]
|
||||
push:
|
||||
branches: [ master, v3, v4 ]
|
||||
paths-ignore:
|
||||
- '.github/**'
|
||||
- '.gitea/**'
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
filter: 'blob:none'
|
||||
- name: checkout tests
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: master
|
||||
filter: 'blob:none'
|
||||
repository: unistack-org/micro-tests
|
||||
path: micro-tests
|
||||
- name: setup go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
cache-dependency-path: "**/*.sum"
|
||||
go-version: 'stable'
|
||||
- name: setup go work
|
||||
env:
|
||||
GOWORK: ${{ github.workspace }}/go.work
|
||||
run: |
|
||||
go work init
|
||||
go work use .
|
||||
go work use micro-tests
|
||||
- name: setup deps
|
||||
env:
|
||||
GOWORK: ${{ github.workspace }}/go.work
|
||||
run: go get -v ./...
|
||||
- name: run tests
|
||||
env:
|
||||
INTEGRATION_TESTS: yes
|
||||
GOWORK: ${{ github.workspace }}/go.work
|
||||
run: |
|
||||
cd micro-tests
|
||||
go test -mod readonly -v ./... || true
|
47
.github/workflows/pr.yml
vendored
Normal file
47
.github/workflows/pr.yml
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
name: prbuild
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
jobs:
|
||||
test:
|
||||
name: test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: setup
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.17
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: cache
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: ${{ runner.os }}-go-
|
||||
- name: deps
|
||||
run: go get -v -t -d ./...
|
||||
- name: test
|
||||
env:
|
||||
INTEGRATION_TESTS: yes
|
||||
run: go test -mod readonly -v ./...
|
||||
lint:
|
||||
name: lint
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: lint
|
||||
uses: golangci/golangci-lint-action@v3.4.0
|
||||
continue-on-error: true
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
version: v1.30
|
||||
# Optional: working directory, useful for monorepos
|
||||
# working-directory: somedir
|
||||
# Optional: golangci-lint command line arguments.
|
||||
# args: --issues-exit-code=0
|
||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||
# only-new-issues: true
|
@@ -1,5 +0,0 @@
|
||||
run:
|
||||
concurrency: 8
|
||||
timeout: 5m
|
||||
issues-exit-code: 1
|
||||
tests: true
|
@@ -1,2 +0,0 @@
|
||||
# micro-meter-prometheus
|
||||

|
20
go.mod
20
go.mod
@@ -1,21 +1,25 @@
|
||||
module go.unistack.org/micro-meter-prometheus/v4
|
||||
module go.unistack.org/micro-meter-prometheus/v3
|
||||
|
||||
go 1.22.0
|
||||
go 1.21
|
||||
|
||||
toolchain go1.24.0
|
||||
toolchain go1.22.0
|
||||
|
||||
require (
|
||||
github.com/prometheus/client_golang v1.21.0
|
||||
github.com/prometheus/client_golang v1.20.4
|
||||
github.com/prometheus/client_model v0.6.1
|
||||
github.com/prometheus/common v0.62.0
|
||||
go.unistack.org/micro/v4 v4.1.2
|
||||
github.com/prometheus/common v0.59.1
|
||||
go.unistack.org/micro/v3 v3.10.91
|
||||
)
|
||||
|
||||
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
|
||||
golang.org/x/sys v0.30.0 // indirect
|
||||
google.golang.org/protobuf v1.36.5 // 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-20230530153820-e85fd2cbaebc // indirect
|
||||
google.golang.org/grpc v1.57.0 // indirect
|
||||
google.golang.org/protobuf v1.34.2 // indirect
|
||||
)
|
||||
|
50
go.sum
50
go.sum
@@ -2,31 +2,45 @@ 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/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/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
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/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
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/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
|
||||
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
||||
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/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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
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 v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
|
||||
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.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
|
||||
github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I=
|
||||
github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJoX0=
|
||||
github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0=
|
||||
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=
|
||||
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=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
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.90 h1:EE7t4J14yjZC/lW6e0BfiBnU/xt/KqGkqSxumJhI/fI=
|
||||
go.unistack.org/micro/v3 v3.10.90/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg=
|
||||
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.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
|
||||
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
|
||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc h1:XSJ8Vk1SWuNr8S18z1NZSziL0CPIXLCCMDOEFtHBOFc=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
|
||||
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
|
||||
google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo=
|
||||
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
346
prometheus.go
346
prometheus.go
@@ -5,7 +5,6 @@ import (
|
||||
"hash/fnv"
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -13,236 +12,213 @@ import (
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
dto "github.com/prometheus/client_model/go"
|
||||
"github.com/prometheus/common/expfmt"
|
||||
"go.unistack.org/micro/v4/meter"
|
||||
"go.unistack.org/micro/v3/meter"
|
||||
)
|
||||
|
||||
var _ meter.Meter = &prometheusMeter{}
|
||||
var _ meter.Meter = (*prometheusMeter)(nil)
|
||||
|
||||
type prometheusMeter struct {
|
||||
opts meter.Options
|
||||
set prometheus.Registerer
|
||||
counter map[string]*counters
|
||||
floatCounter map[string]*floatCounters
|
||||
gauge map[string]*gauges
|
||||
histogram map[string]*histograms
|
||||
summary map[string]*summaries
|
||||
mu *sync.Mutex
|
||||
counter *sync.Map
|
||||
floatCounter *sync.Map
|
||||
gauge *sync.Map
|
||||
histogram *sync.Map
|
||||
summary *sync.Map
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
type counters struct {
|
||||
cs map[uint64]*prometheusCounter
|
||||
cs *sync.Map
|
||||
}
|
||||
|
||||
type gauges struct {
|
||||
cs map[uint64]*prometheusGauge
|
||||
cs *sync.Map
|
||||
}
|
||||
|
||||
type histograms struct {
|
||||
cs map[uint64]*prometheusHistogram
|
||||
cs *sync.Map
|
||||
}
|
||||
|
||||
type summaries struct {
|
||||
cs map[uint64]*prometheusSummary
|
||||
cs *sync.Map
|
||||
}
|
||||
|
||||
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])
|
||||
cs *sync.Map
|
||||
}
|
||||
|
||||
func NewMeter(opts ...meter.Option) *prometheusMeter {
|
||||
return &prometheusMeter{
|
||||
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{},
|
||||
counter: &sync.Map{},
|
||||
floatCounter: &sync.Map{},
|
||||
gauge: &sync.Map{},
|
||||
histogram: &sync.Map{},
|
||||
summary: &sync.Map{},
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
nm := m.buildMetric(name)
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
labels = append(m.opts.Labels, labels...)
|
||||
cd, ok := m.counter[nm]
|
||||
vcd, ok := m.counter.Load(name)
|
||||
h := newHash(labels)
|
||||
if !ok {
|
||||
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
|
||||
cd := &counters{cs: &sync.Map{}}
|
||||
c := &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.counter.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c, ok := cd.cs[h]
|
||||
cd := vcd.(*counters)
|
||||
vc, ok := cd.cs.Load(h)
|
||||
if !ok {
|
||||
c = &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
||||
cd.cs[h] = c
|
||||
m.counter[nm] = cd
|
||||
c := &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.counter.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c := vc.(*prometheusCounter)
|
||||
return c
|
||||
}
|
||||
|
||||
func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
nm := m.buildMetric(name)
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
labels = append(m.opts.Labels, labels...)
|
||||
cd, ok := m.floatCounter[nm]
|
||||
vcd, ok := m.floatCounter.Load(name)
|
||||
h := newHash(labels)
|
||||
if !ok {
|
||||
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
|
||||
cd := &floatCounters{cs: &sync.Map{}}
|
||||
c := &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.floatCounter.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c, ok := cd.cs[h]
|
||||
cd := vcd.(*floatCounters)
|
||||
vc, ok := cd.cs.Load(h)
|
||||
if !ok {
|
||||
c = &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
||||
cd.cs[h] = c
|
||||
m.floatCounter[nm] = cd
|
||||
c := &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.floatCounter.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c := vc.(*prometheusFloatCounter)
|
||||
return c
|
||||
}
|
||||
|
||||
func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
nm := m.buildMetric(name)
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
labels = append(m.opts.Labels, labels...)
|
||||
cd, ok := m.gauge[nm]
|
||||
vcd, ok := m.gauge.Load(name)
|
||||
h := newHash(labels)
|
||||
if !ok {
|
||||
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
|
||||
cd := &gauges{cs: &sync.Map{}}
|
||||
c := &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.gauge.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c, ok := cd.cs[h]
|
||||
cd := vcd.(*gauges)
|
||||
vc, ok := cd.cs.Load(h)
|
||||
if !ok {
|
||||
c = &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
||||
cd.cs[h] = c
|
||||
m.gauge[nm] = cd
|
||||
c := &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.gauge.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c := vc.(*prometheusGauge)
|
||||
return c
|
||||
}
|
||||
|
||||
func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
nm := m.buildMetric(name)
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
labels = append(m.opts.Labels, labels...)
|
||||
cd, ok := m.histogram[nm]
|
||||
vcd, ok := m.histogram.Load(name)
|
||||
h := newHash(labels)
|
||||
if !ok {
|
||||
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
|
||||
cd := &histograms{cs: &sync.Map{}}
|
||||
c := &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.histogram.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c, ok := cd.cs[h]
|
||||
cd := vcd.(*histograms)
|
||||
vc, ok := cd.cs.Load(h)
|
||||
if !ok {
|
||||
c = &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: nm}), labels: labels}
|
||||
cd.cs[h] = c
|
||||
m.histogram[nm] = cd
|
||||
c := &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.histogram.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c := vc.(*prometheusHistogram)
|
||||
return c
|
||||
}
|
||||
|
||||
func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
nm := m.buildMetric(name)
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
labels = append(m.opts.Labels, labels...)
|
||||
cd, ok := m.summary[nm]
|
||||
vcd, ok := m.summary.Load(name)
|
||||
h := newHash(labels)
|
||||
if !ok {
|
||||
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
|
||||
cd := &summaries{cs: &sync.Map{}}
|
||||
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.summary.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c, ok := cd.cs[h]
|
||||
cd := vcd.(*summaries)
|
||||
vc, ok := cd.cs.Load(h)
|
||||
if !ok {
|
||||
c = &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: nm}), labels: labels}
|
||||
cd.cs[h] = c
|
||||
m.summary[nm] = cd
|
||||
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.summary.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c := vc.(*prometheusSummary)
|
||||
return c
|
||||
}
|
||||
|
||||
func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
nm := m.buildMetric(name)
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
labels = append(m.opts.Labels, labels...)
|
||||
cd, ok := m.summary[nm]
|
||||
vcd, ok := m.summary.Load(name)
|
||||
h := newHash(labels)
|
||||
if !ok {
|
||||
cd = &summaries{cs: make(map[uint64]*prometheusSummary)}
|
||||
cd := &summaries{cs: &sync.Map{}}
|
||||
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
|
||||
Name: nm,
|
||||
Name: name,
|
||||
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
|
||||
cd.cs.Store(h, c)
|
||||
m.summary.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c, ok := cd.cs[h]
|
||||
cd := vcd.(*summaries)
|
||||
vc, ok := cd.cs.Load(h)
|
||||
if !ok {
|
||||
c = &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
|
||||
Name: nm,
|
||||
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
|
||||
Name: name,
|
||||
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
|
||||
cd.cs.Store(h, c)
|
||||
m.summary.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c := vc.(*prometheusSummary)
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -250,6 +226,14 @@ 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
|
||||
}
|
||||
|
||||
@@ -259,13 +243,6 @@ 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)
|
||||
@@ -278,84 +255,95 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
|
||||
|
||||
enc := expfmt.NewEncoder(w, expfmt.NewFormat(expfmt.TypeTextPlain))
|
||||
|
||||
m.mu.Lock()
|
||||
|
||||
for name, metrics := range m.counter {
|
||||
m.counter.Range(func(k, v any) bool {
|
||||
name := k.(string)
|
||||
mf := &dto.MetricFamily{
|
||||
Name: newName(name),
|
||||
Type: dto.MetricType_GAUGE.Enum(),
|
||||
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
|
||||
Name: &name,
|
||||
Type: dto.MetricType_GAUGE.Enum(),
|
||||
}
|
||||
for _, c := range metrics.cs {
|
||||
v.(*counters).cs.Range(func(_, nv any) bool {
|
||||
c := nv.(*prometheusCounter)
|
||||
m := &dto.Metric{}
|
||||
_ = c.c.Write(m)
|
||||
fillMetric(m, c.labels)
|
||||
mf.Metric = append(mf.Metric, m)
|
||||
}
|
||||
return true
|
||||
})
|
||||
mfs = append(mfs, mf)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
for name, metrics := range m.gauge {
|
||||
m.gauge.Range(func(k, v any) bool {
|
||||
name := k.(string)
|
||||
mf := &dto.MetricFamily{
|
||||
Name: newName(name),
|
||||
Type: dto.MetricType_GAUGE.Enum(),
|
||||
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
|
||||
Name: &name,
|
||||
Type: dto.MetricType_GAUGE.Enum(),
|
||||
}
|
||||
for _, c := range metrics.cs {
|
||||
v.(*gauges).cs.Range(func(_, nv any) bool {
|
||||
c := nv.(*prometheusGauge)
|
||||
m := &dto.Metric{}
|
||||
_ = c.c.Write(m)
|
||||
fillMetric(m, c.labels)
|
||||
mf.Metric = append(mf.Metric, m)
|
||||
}
|
||||
return true
|
||||
})
|
||||
mfs = append(mfs, mf)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
for name, metrics := range m.floatCounter {
|
||||
m.floatCounter.Range(func(k, v any) bool {
|
||||
name := k.(string)
|
||||
mf := &dto.MetricFamily{
|
||||
Name: newName(name),
|
||||
Type: dto.MetricType_GAUGE.Enum(),
|
||||
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
|
||||
Name: &name,
|
||||
Type: dto.MetricType_GAUGE.Enum(),
|
||||
}
|
||||
for _, c := range metrics.cs {
|
||||
v.(*floatCounters).cs.Range(func(_, nv any) bool {
|
||||
c := nv.(*prometheusFloatCounter)
|
||||
m := &dto.Metric{}
|
||||
_ = c.c.Write(m)
|
||||
fillMetric(m, c.labels)
|
||||
mf.Metric = append(mf.Metric, m)
|
||||
}
|
||||
return true
|
||||
})
|
||||
mfs = append(mfs, mf)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
for name, metrics := range m.histogram {
|
||||
m.histogram.Range(func(k, v any) bool {
|
||||
name := k.(string)
|
||||
mf := &dto.MetricFamily{
|
||||
Name: newName(name),
|
||||
Type: dto.MetricType_HISTOGRAM.Enum(),
|
||||
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
|
||||
Name: &name,
|
||||
Type: dto.MetricType_HISTOGRAM.Enum(),
|
||||
}
|
||||
for _, c := range metrics.cs {
|
||||
v.(*histograms).cs.Range(func(_, nv any) bool {
|
||||
c := nv.(*prometheusHistogram)
|
||||
m := &dto.Metric{}
|
||||
_ = c.c.Write(m)
|
||||
fillMetric(m, c.labels)
|
||||
mf.Metric = append(mf.Metric, m)
|
||||
}
|
||||
return true
|
||||
})
|
||||
mfs = append(mfs, mf)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
for name, metrics := range m.summary {
|
||||
m.summary.Range(func(k, v any) bool {
|
||||
name := k.(string)
|
||||
mf := &dto.MetricFamily{
|
||||
Name: newName(name),
|
||||
Type: dto.MetricType_SUMMARY.Enum(),
|
||||
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
|
||||
Name: &name,
|
||||
Type: dto.MetricType_SUMMARY.Enum(),
|
||||
}
|
||||
for _, c := range metrics.cs {
|
||||
v.(*summaries).cs.Range(func(_, nv any) bool {
|
||||
c := nv.(*prometheusSummary)
|
||||
m := &dto.Metric{}
|
||||
_ = c.c.Write(m)
|
||||
fillMetric(m, c.labels)
|
||||
mf.Metric = append(mf.Metric, m)
|
||||
}
|
||||
return true
|
||||
})
|
||||
mfs = append(mfs, mf)
|
||||
}
|
||||
|
||||
m.mu.Unlock()
|
||||
return true
|
||||
})
|
||||
|
||||
for _, mf := range mfs {
|
||||
_ = enc.Encode(mf)
|
||||
@@ -375,7 +363,6 @@ func (m *prometheusMeter) Clone(opts ...meter.Option) meter.Meter {
|
||||
}
|
||||
|
||||
return &prometheusMeter{
|
||||
mu: m.mu,
|
||||
set: m.set,
|
||||
opts: options,
|
||||
floatCounter: m.floatCounter,
|
||||
@@ -500,6 +487,7 @@ func (c prometheusSummary) UpdateDuration(n time.Time) {
|
||||
}
|
||||
|
||||
func newHash(labels []string) uint64 {
|
||||
labels = meter.BuildLabels(labels...)
|
||||
h := fnv.New64a()
|
||||
for _, l := range labels {
|
||||
h.Write([]byte(l))
|
||||
@@ -508,12 +496,18 @@ func newHash(labels []string) uint64 {
|
||||
}
|
||||
|
||||
func fillMetric(m *dto.Metric, labels []string) *dto.Metric {
|
||||
var ok bool
|
||||
seen := make(map[string]bool, len(labels)/2)
|
||||
m.Label = make([]*dto.LabelPair, 0, len(labels)/2)
|
||||
for idx := 0; idx < len(labels); idx += 2 {
|
||||
if _, ok = seen[labels[idx]]; ok {
|
||||
continue
|
||||
}
|
||||
m.Label = append(m.Label, &dto.LabelPair{
|
||||
Name: newString(labels[idx]),
|
||||
Value: newString(labels[idx+1]),
|
||||
Name: &(labels[idx]),
|
||||
Value: &(labels[idx+1]),
|
||||
})
|
||||
seen[labels[idx]] = true
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
@@ -2,10 +2,12 @@ package prometheus
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go.unistack.org/micro/v4/meter"
|
||||
"go.unistack.org/micro/v3/client"
|
||||
"go.unistack.org/micro/v3/codec"
|
||||
"go.unistack.org/micro/v3/meter"
|
||||
)
|
||||
|
||||
func TestStd(t *testing.T) {
|
||||
@@ -20,16 +22,24 @@ func TestStd(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
func TestWrapper(t *testing.T) {
|
||||
m := NewMeter() // meter.Labels("test_key", "test_val"))
|
||||
|
||||
cnt := m.Counter("counter", "key", "val")
|
||||
cnt.Inc()
|
||||
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 TestMultiple(t *testing.T) {
|
||||
@@ -50,27 +60,3 @@ func TestMultiple(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user