Skip to content

Commit eeb7ef0

Browse files
author
Jon Mullen
authored
Merge pull request #122 from pinpt/shared-metrics
Shared metrics
2 parents 0c02111 + 3ad9743 commit eeb7ef0

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/nlopes/slack v0.6.0
1414
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
1515
github.com/pinpt/httpclient v0.0.0-20190815022759-09e0028c9067
16+
github.com/prometheus/client_golang v1.3.0
1617
github.com/spf13/cobra v1.0.0
1718
github.com/spf13/pflag v1.0.5 // indirect
1819
github.com/streadway/amqp v0.0.0-20200108173154-1c71cc93ed71

go.sum

+9
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN
2424
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
2525
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
2626
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
27+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
2728
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
2829
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
2930
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
3031
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
3132
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
3233
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
3334
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
35+
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
3436
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
3537
github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE=
3638
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
@@ -90,6 +92,7 @@ github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4er
9092
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
9193
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
9294
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
95+
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
9396
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
9497
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
9598
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
@@ -173,6 +176,7 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA
173176
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
174177
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
175178
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
179+
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
176180
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
177181
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
178182
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
@@ -237,21 +241,26 @@ github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP
237241
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
238242
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
239243
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
244+
github.com/prometheus/client_golang v1.3.0 h1:miYCvYqFXtl/J9FIy8eNpBfYthAEFg+Ys0XyUVEcDsc=
240245
github.com/prometheus/client_golang v1.3.0/go.mod h1:hJaj2vgQTGQmVCsAACORcieXFeDPbaTKGT+JTgUa3og=
246+
github.com/prometheus/client_golang v1.8.0 h1:zvJNkoCFAnYFNC24FV8nW4JdRJ3GIFcLbg65lL/JDcw=
241247
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
242248
github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
243249
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
244250
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
251+
github.com/prometheus/client_model v0.1.0 h1:ElTg5tNp4DqfV7UQjDqv2+RJlNzsDtvNAWccbItceIE=
245252
github.com/prometheus/client_model v0.1.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
246253
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
247254
github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
248255
github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
249256
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
257+
github.com/prometheus/common v0.7.0 h1:L+1lyG48J1zAQXA3RBX/nG/B3gjlHq0zTt2tlbJLyCY=
250258
github.com/prometheus/common v0.7.0/go.mod h1:DjGbpBbp5NYNiECxcL/VnbXCCaQpKd3tt26CguLLsqA=
251259
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
252260
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
253261
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
254262
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
263+
github.com/prometheus/procfs v0.0.8 h1:+fpWZdT24pJBiqJdAwYBjPSk+5YmQzYNPYzQsdzLkt8=
255264
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
256265
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
257266
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=

metrics/metrics.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package metrics
2+
3+
import (
4+
"github.com/prometheus/client_golang/prometheus"
5+
)
6+
7+
var (
8+
// RequestDurationMilliseconds is the metric
9+
RequestDurationMilliseconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
10+
Name: "pinpoint_request_duration_milliseconds",
11+
Help: "latency of a given service - operation",
12+
Buckets: []float64{5, 10, 25, 50, 75, 100, 200, 500, 800, 1000, 3000, 5000},
13+
}, []string{"service", "operation"})
14+
15+
// RequestsTotal is the metric
16+
RequestsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
17+
Name: "pinpoint_requests_total",
18+
Help: "total number of requests by service - operation",
19+
}, []string{"service", "operation", "response_code"})
20+
21+
// WebsocketConnections is the metric
22+
WebsocketConnections = prometheus.NewGaugeVec(prometheus.GaugeOpts{
23+
Name: "pinpoint_websocket_connections",
24+
Help: "number of websocket connections for a request by service",
25+
}, []string{"service"})
26+
27+
// RequestBytesSum is the metric
28+
RequestBytesSum = prometheus.NewGaugeVec(prometheus.GaugeOpts{
29+
Name: "pinpoint_request_bytes_sum",
30+
Help: "sum of bytes for a request by service - operation",
31+
}, []string{"service", "operation"})
32+
33+
// ResponseBytesSum is the metric
34+
ResponseBytesSum = prometheus.NewGaugeVec(prometheus.GaugeOpts{
35+
Name: "pinpoint_response_bytes_sum",
36+
Help: "sum of bytes for a response by service - operation",
37+
}, []string{"service", "operation"})
38+
)
39+
40+
func init() {
41+
prometheus.MustRegister(RequestDurationMilliseconds)
42+
prometheus.MustRegister(RequestsTotal)
43+
prometheus.MustRegister(RequestBytesSum)
44+
prometheus.MustRegister(ResponseBytesSum)
45+
prometheus.MustRegister(prometheus.NewBuildInfoCollector())
46+
}

metrics/server.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package metrics
2+
3+
import (
4+
"fmt"
5+
"context"
6+
"net/http"
7+
8+
"github.com/pinpt/go-common/v10/log"
9+
pos "github.com/pinpt/go-common/v10/os"
10+
"github.com/prometheus/client_golang/prometheus/promhttp"
11+
)
12+
13+
// StartServer starts the metricserver
14+
func StartServer(ctx context.Context, logger log.Logger, port string) {
15+
// run webserver
16+
http.Handle("/metrics", promhttp.Handler())
17+
18+
// expose a `/ready` endpoint so we can standardize readiness checks & liveness checks
19+
http.HandleFunc("/ready", func(w http.ResponseWriter, req *http.Request) {
20+
w.WriteHeader(http.StatusOK)
21+
w.Write([]byte("OK"))
22+
})
23+
24+
// clients will be expected to implement custom /health checks based on a service's requirements
25+
26+
server := &http.Server{
27+
Addr: ":" + port,
28+
}
29+
go func() {
30+
log.Debug(logger, "starting /metrics endpoint", "port", port)
31+
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
32+
select {
33+
case <-ctx.Done():
34+
default:
35+
log.Fatal(logger, "error starting Metrics server", "err", err)
36+
}
37+
}
38+
}()
39+
40+
pprofPort := pos.Getenv("PP_PROFILE_PORT", "")
41+
if len(pprofPort) > 0 {
42+
log.Debug(logger, "pprof listening on port ", "pprofPort", pprofPort)
43+
44+
go func() {
45+
fmt.Println(http.ListenAndServe(":"+pprofPort, nil))
46+
}()
47+
}
48+
}

0 commit comments

Comments
 (0)