Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow disable server #6

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
name: Pull Request Check

on: [pull_request]
on: [ pull_request ]

jobs:
compliant:
runs-on: self-hosted
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.19

- name: Check License Header
uses: apache/skywalking-eyes@main
Expand All @@ -19,12 +24,12 @@ jobs:
staticcheck:
runs-on: self-hosted
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.18
go-version: 1.19

- uses: actions/cache@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Check Source Branch
run: python2 -c "exit(0 if '${{ github.head_ref }}'.startswith('release') or '${{ github.head_ref }}'.startswith('hotfix') else 1)"
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ jobs:
lint-and-ut:
runs-on: self-hosted
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.18
go-version: 1.19

- uses: actions/cache@v3
with:
Expand Down
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ header:
license:
spdx-id: Apache-2.0
copyright-owner: CloudWeGo Authors
copyright-year: "2022"

paths:
- '**/*.go'
Expand Down
40 changes: 36 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
module github.com/hertz-contrib/monitor-prometheus

go 1.16
go 1.19

require (
github.com/cloudwego/hertz v0.4.0
github.com/prometheus/client_golang v1.14.0
github.com/stretchr/testify v1.8.0
github.com/cloudwego/hertz v0.7.3
github.com/prometheus/client_golang v1.17.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/andeya/ameda v1.5.3 // indirect
github.com/andeya/goutil v1.0.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/go-tagexpr/v2 v2.9.11 // indirect
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/cloudwego/netpoll v0.5.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/nyaruka/phonenumbers v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.6.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
562 changes: 85 additions & 477 deletions go.sum

Large diffs are not rendered by default.

24 changes: 21 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package prometheus

import (
prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
)

var defaultBuckets = []float64{5000, 10000, 25000, 50000, 100000, 250000, 500000, 1000000}
Expand All @@ -34,16 +35,19 @@ func (fn option) apply(cfg *config) {
}

type config struct {
buckets []float64
enableGoCollector bool
registry *prom.Registry
buckets []float64
enableGoCollector bool
registry *prom.Registry
runtimeMetricRules []collectors.GoRuntimeMetricsRule
disableServer bool
}

func defaultConfig() *config {
return &config{
buckets: defaultBuckets,
enableGoCollector: false,
registry: prom.NewRegistry(),
disableServer: false,
}
}

Expand All @@ -54,6 +58,20 @@ func WithEnableGoCollector(enable bool) Option {
})
}

// WithGoCollectorRule define your custom go collector rule
func WithGoCollectorRule(rules ...collectors.GoRuntimeMetricsRule) Option {
return option(func(cfg *config) {
cfg.runtimeMetricRules = rules
})
}

// WithDisableServer disable prometheus server
func WithDisableServer(disable bool) Option {
return option(func(cfg *config) {
cfg.disableServer = disable
})
}

// WithHistogramBuckets define your custom histogram buckets base on your biz
func WithHistogramBuckets(buckets []float64) Option {
return option(func(cfg *config) {
Expand Down
21 changes: 12 additions & 9 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import (
"net/http"
"strconv"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/tracer"
"github.com/cloudwego/hertz/pkg/common/tracer/stats"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

const (
Expand Down Expand Up @@ -82,12 +83,14 @@ func NewServerTracer(addr, path string, opts ...Option) tracer.Tracer {
opts.apply(cfg)
}

http.Handle(path, promhttp.HandlerFor(cfg.registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}))
go func() {
if err := http.ListenAndServe(addr, nil); err != nil {
hlog.Fatal("HERTZ: Unable to start a promhttp server, err: " + err.Error())
}
}()
if !cfg.disableServer {
http.Handle(path, promhttp.HandlerFor(cfg.registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}))
go func() {
if err := http.ListenAndServe(addr, nil); err != nil {
hlog.Fatal("HERTZ: Unable to start a promhttp server, err: " + err.Error())
}
}()
}

serverHandledCounter := prom.NewCounterVec(
prom.CounterOpts{
Expand All @@ -109,7 +112,7 @@ func NewServerTracer(addr, path string, opts ...Option) tracer.Tracer {
cfg.registry.MustRegister(serverHandledHistogram)

if cfg.enableGoCollector {
cfg.registry.MustRegister(collectors.NewGoCollector())
cfg.registry.MustRegister(collectors.NewGoCollector(collectors.WithGoCollectorRuntimeMetrics(cfg.runtimeMetricRules...)))
}

return &serverTracer{
Expand Down
Loading