Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit 4fcb885

Browse files
authored
Merge pull request #5 from randlabs/external_server
Added external web server support and improvements.
2 parents 1796ea3 + eec0a09 commit 4fcb885

10 files changed

+204
-181
lines changed

README.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# go-metrics
22

3-
Health and Metrics web server library for Go
3+
Health and Metrics controller and web server library for Go
44

55
## Usage with example
66

@@ -10,23 +10,26 @@ package example
1010
import (
1111
"math/rand"
1212

13-
metrics "github.com/randlabs/go-metrics"
13+
"github.com/randlabs/go-metrics"
1414
)
1515

1616
func main() {
17-
// Create a new health & metrics web server
17+
// Create a new health & metrics controller with a web server
1818
srvOpts := metrics.Options{
19-
Address: "127.0.0.1",
20-
Port: 3000,
21-
HealthCallback: healthCallback, // Setup our health check callback
19+
Address: "127.0.0.1",
20+
Port: 3000,
21+
HealthCallback: healthCallback, // Setup our health check callback
22+
EnableDebugProfiles: true,
23+
IncludeCORS: true,
24+
DisableClientCache: true,
2225
}
23-
mws, err := metrics.CreateMetricsWebServer(srvOpts)
26+
mc, err := metrics.CreateController(srvOpts)
2427
if err != nil {
2528
// handle error
2629
}
2730

2831
// Create a custom prometheus counter
29-
err = mws.CreateCounterWithCallback(
32+
err = mc.CreateCounterWithCallback(
3033
"random_counter", "A random counter",
3134
func() float64 {
3235
// Return the counter value.
@@ -37,15 +40,15 @@ func main() {
3740
)
3841

3942
// Start health & metrics web server
40-
err = mws.Start()
43+
err = mc.Start()
4144
if err != nil {
4245
// handle error
4346
}
4447

4548
// your app code may go here
4649

4750
// Stop health & metrics web server before quitting
48-
mws.Stop()
51+
mc.Destroy()
4952
}
5053

5154
// Health output is in JSON format. Don't forget to add json tags.

go.mod

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@ module github.com/randlabs/go-metrics
33
go 1.17
44

55
require (
6-
github.com/prometheus/client_golang v1.12.1
6+
github.com/prometheus/client_golang v1.12.2
77
github.com/prometheus/client_model v0.2.0
8-
github.com/randlabs/go-webserver v1.1.3
8+
github.com/randlabs/go-webserver v1.1.4
9+
github.com/randlabs/rundown-protection v1.0.5
910
google.golang.org/protobuf v1.28.0
1011
)
1112

1213
require (
1314
github.com/andybalholm/brotli v1.0.4 // indirect
1415
github.com/beorn7/perks v1.0.1 // indirect
1516
github.com/cespare/xxhash/v2 v2.1.2 // indirect
16-
github.com/fasthttp/router v1.4.8 // indirect
17+
github.com/fasthttp/router v1.4.9 // indirect
1718
github.com/golang/protobuf v1.5.2 // indirect
18-
github.com/klauspost/compress v1.15.2 // indirect
19+
github.com/klauspost/compress v1.15.5 // indirect
1920
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
2021
github.com/prometheus/common v0.32.1 // indirect
2122
github.com/prometheus/procfs v0.7.3 // indirect
2223
github.com/savsgio/gotils v0.0.0-20220401102855-e56b59f40436 // indirect
2324
github.com/valyala/bytebufferpool v1.0.0 // indirect
24-
github.com/valyala/fasthttp v1.35.0 // indirect
25+
github.com/valyala/fasthttp v1.37.0 // indirect
2526
github.com/valyala/tcplisten v1.0.0 // indirect
26-
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
27+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
2728
)

go.sum

+15-13
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF
5959
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
6060
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
6161
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
62-
github.com/fasthttp/router v1.4.8 h1:4zj4sAzXibjA6ZW19MdMe3GaYD1SM+TXrMLzHcVMBOI=
63-
github.com/fasthttp/router v1.4.8/go.mod h1:UUtJdXFYlqYRQ32EAtWOvNYIZ1XfyC5JJIknWai6foI=
62+
github.com/fasthttp/router v1.4.9 h1:8s1HEqP+GvsC2B8vPdLAPHJegs4s28z7UsraPuHM1K8=
63+
github.com/fasthttp/router v1.4.9/go.mod h1:oWPrQCi9QOrzxKC+rZuliS1+JhYj2bpR01J6T8vUDUQ=
6464
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
6565
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
6666
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@@ -140,9 +140,8 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
140140
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
141141
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
142142
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
143-
github.com/klauspost/compress v1.15.1/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
144-
github.com/klauspost/compress v1.15.2 h1:3WH+AG7s2+T8o3nrM/8u2rdqUEcQhmga7smjrT41nAw=
145-
github.com/klauspost/compress v1.15.2/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
143+
github.com/klauspost/compress v1.15.5 h1:qyCLMz2JCrKADihKOh9FxnW3houKeNsp2h5OEz0QSEA=
144+
github.com/klauspost/compress v1.15.5/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
146145
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
147146
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
148147
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
@@ -166,8 +165,8 @@ github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP
166165
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
167166
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
168167
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
169-
github.com/prometheus/client_golang v1.12.1 h1:ZiaPsmm9uiBeaSMRznKsCDNtPCS0T3JVDGF+06gjBzk=
170-
github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY=
168+
github.com/prometheus/client_golang v1.12.2 h1:51L9cDoUHVrXx4zWYlcLQIZ+d+VXHgqnYKkIuq4g/34=
169+
github.com/prometheus/client_golang v1.12.2/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY=
171170
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
172171
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
173172
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
@@ -184,8 +183,10 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
184183
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
185184
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
186185
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
187-
github.com/randlabs/go-webserver v1.1.3 h1:TNoCdbebbZ5C9nkSySwzWFs1Bxs3ZjJabq6UIFePq3E=
188-
github.com/randlabs/go-webserver v1.1.3/go.mod h1:3GmB2xj542/o1vp1jbVs/eUSWdc3y7sxDcg4ZRzBdvA=
186+
github.com/randlabs/go-webserver v1.1.4 h1:PGYmiXJedEp8/xzN+d3M6toNTKjW6kehAYzmkH9pYLw=
187+
github.com/randlabs/go-webserver v1.1.4/go.mod h1:LkGZZ6Cm01yQ864kvPP7GbGFVC9LuQPWQ58F1OSG/gU=
188+
github.com/randlabs/rundown-protection v1.0.5 h1:Bq9GMTuWvZI3915VkVLVysayQfSfHmWwuhD6feawhfU=
189+
github.com/randlabs/rundown-protection v1.0.5/go.mod h1:rccYmsidTaGDOvZSAF9eYsLzMVvh9tj043JyTicfQ5o=
189190
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
190191
github.com/savsgio/gotils v0.0.0-20220401102855-e56b59f40436 h1:sfTahD3f2BSjx9U3R4K09PkNuZZWthT7g6vzTIXNWkM=
191192
github.com/savsgio/gotils v0.0.0-20220401102855-e56b59f40436/go.mod h1:Gy+0tqhJvgGlqnTF8CVGP0AaGRjwBtXs/a5PA0Y3+A4=
@@ -199,8 +200,9 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
199200
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
200201
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
201202
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
202-
github.com/valyala/fasthttp v1.35.0 h1:wwkR8mZn2NbigFsaw2Zj5r+xkmzjbrA/lyTmiSlal/Y=
203-
github.com/valyala/fasthttp v1.35.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
203+
github.com/valyala/fasthttp v1.36.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
204+
github.com/valyala/fasthttp v1.37.0 h1:7WHCyI7EAkQMVmrfBhWTCOaeROb1aCBiTopx63LkMbE=
205+
github.com/valyala/fasthttp v1.37.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
204206
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
205207
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
206208
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -333,8 +335,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
333335
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
334336
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
335337
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
336-
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc=
337-
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
338+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
339+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
338340
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
339341
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
340342
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

handlers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// -----------------------------------------------------------------------------
1010

11-
func (mws *MetricsWebServer) getHealthHandler() webserver.HandlerFunc {
11+
func (mws *Controller) getHealthHandler() webserver.HandlerFunc {
1212
return func(req *request.RequestContext) error {
1313
// Get current state from callback
1414
state := mws.healthCallback()
@@ -22,7 +22,7 @@ func (mws *MetricsWebServer) getHealthHandler() webserver.HandlerFunc {
2222
}
2323
}
2424

25-
func (mws *MetricsWebServer) getMetricsHandler() webserver.HandlerFunc {
25+
func (mws *Controller) getMetricsHandler() webserver.HandlerFunc {
2626
return webserver.HandlerFromHttpHandler(promhttp.HandlerFor(
2727
mws.registry,
2828
promhttp.HandlerOpts{

handlers_access.go

-54
This file was deleted.

0 commit comments

Comments
 (0)