Skip to content

Commit

Permalink
Merge branch 'prometheus'
Browse files Browse the repository at this point in the history
  • Loading branch information
lil5 committed Dec 17, 2024
2 parents 10575d4 + 0c9f5a8 commit b1a60f8
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .example.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ TB_ADDRESSES=3033
# BUFFER_SIZE=20
# BUFFER_DELAY=100ms
# BUFFER_CLUSTER=4

PROMETHEUS_ADDR=:9323
18 changes: 13 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ go 1.23
toolchain go1.23.4

require (
github.com/charithe/timedbuf/v2 v2.0.0-20241209145701-0faa62e2b61c
github.com/gin-gonic/gin v1.10.0
github.com/joho/godotenv v1.5.1
github.com/prometheus/client_golang v1.20.5
github.com/samber/lo v1.47.0
github.com/stretchr/testify v1.10.0
github.com/tidwall/gjson v1.18.0
Expand All @@ -15,12 +17,13 @@ require (
google.golang.org/protobuf v1.35.2
)

require google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
require github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.12.5 // indirect
github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/charithe/timedbuf/v2 v2.0.0-20241209145701-0faa62e2b61c
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
Expand All @@ -30,16 +33,21 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.23.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
Expand All @@ -49,6 +57,6 @@ require (
golang.org/x/net v0.32.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
27 changes: 22 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
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/bytedance/sonic v1.12.5 h1:hoZxY8uW+mT+OpkcUWw4k0fDINtOcVavEsGfzwzFU/w=
github.com/bytedance/sonic v1.12.5/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/bytedance/sonic/loader v0.2.1 h1:1GgorWTqf12TA8mma4DDSbaQigE2wOgQo7iCjjJv3+E=
github.com/bytedance/sonic/loader v0.2.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
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/charithe/timedbuf/v2 v2.0.0-20241209145701-0faa62e2b61c h1:lciXn4srfPElwcpDhcg+A1O7/kW+BXpCtNd/EePkNNw=
github.com/charithe/timedbuf/v2 v2.0.0-20241209145701-0faa62e2b61c/go.mod h1:BnJAc1OkYgsAVeY/HdMw5dCIp4MNAhYiTprthu9e9gY=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
Expand Down Expand Up @@ -35,21 +39,26 @@ github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6
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/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 h1:qnpSQwGEnkcRpTqNOIR6bJbR0gAorgP9CSALpRcKoAA=
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1/go.mod h1:lXGCsh6c22WGtjr+qGHj1otzZpV/1kwTMAqkwZsnWRU=
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 h1:pRhl55Yx1eC7BZ1N+BBWwnKaMyD8uC+34TLdndZMAKk=
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0/go.mod h1:XKMd7iuf/RGPSMJ/U4HP0zS2Z9Fh8Ps9a+6X26m/tmI=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY=
github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
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/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
Expand All @@ -59,13 +68,21 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
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/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
github.com/prometheus/client_golang v1.20.5/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.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
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/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
Expand Down
9 changes: 9 additions & 0 deletions grpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type config struct {
BufferSize int
BufferDelay time.Duration
BufferCluster int

PrometheusAddr string
}

func NewConfig() (ok bool) {
Expand Down Expand Up @@ -75,6 +77,11 @@ func NewConfig() (ok bool) {
}
}

prometheusAddr := os.Getenv("PROMETHEUS_ADDR")
if prometheusAddr == "" {
prometheusAddr = ":9323"
}

Config = config{
Host: os.Getenv("HOST"),
Port: os.Getenv("PORT"),
Expand All @@ -93,6 +100,8 @@ func NewConfig() (ok bool) {
BufferSize: bufferSize,
BufferDelay: bufferDelay,
BufferCluster: bufferCluster,

PrometheusAddr: prometheusAddr,
}
return true
}
13 changes: 9 additions & 4 deletions grpc/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"

"github.com/charithe/timedbuf/v2"
"github.com/lil5/tigerbeetle_api/metrics"
"github.com/lil5/tigerbeetle_api/proto"
"github.com/samber/lo"
tigerbeetle_go "github.com/tigerbeetle/tigerbeetle-go"
Expand Down Expand Up @@ -63,16 +64,20 @@ func NewApp() *App {
var tbuf *timedbuf.TimedBuf[TimedPayload]
var tbufs []*timedbuf.TimedBuf[TimedPayload]
if Config.IsBuffered {
bufWarnLog := int(float32(Config.BufferSize) * 0.8)
tbufs = make([]*timedbuf.TimedBuf[TimedPayload], Config.BufferCluster)

lenMaxBuf := float64(Config.BufferSize)
flushFunc := func(payloads []TimedPayload) {
transfers := []types.Transfer{}
if lenPayloads := len(payloads); lenPayloads < bufWarnLog {
slog.Warn("Flushing Buffer", "max buffer", Config.BufferSize, "buffer size collected", lenPayloads)
}
lenPayloads := float64(len(payloads))
for _, payload := range payloads {
transfers = append(transfers, payload.Transfers...)
}
metrics.TotalBufferCount.Inc()
metrics.TotalBufferContents.Add(lenPayloads)
metrics.TotalBufferMax.Add(lenMaxBuf)
metrics.TotalCreateTransferTx.Add(float64(len(transfers)))

results, err := tigerbeetle_go.CreateTransfers(transfers)
res := TimedPayloadResponse{
Results: results,
Expand Down
14 changes: 14 additions & 0 deletions grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import (
"net"
"os"

"github.com/lil5/tigerbeetle_api/metrics"
"github.com/lil5/tigerbeetle_api/proto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"

grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
)

func NewServer() {
Expand Down Expand Up @@ -38,6 +42,16 @@ func NewServer() {
reflection.Register(s)
}

prometheusDeferClose := metrics.Register(Config.PrometheusAddr, promhttp.HandlerFor(metrics.Reg, promhttp.HandlerOpts{
EnableOpenMetrics: true,
}))
defer prometheusDeferClose()
srvMetrics := grpcprom.NewServerMetrics(grpcprom.WithServerHandlingTimeHistogram(
grpcprom.WithHistogramBuckets([]float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120}),
))
metrics.Reg.MustRegister(srvMetrics)
srvMetrics.InitializeMetrics(s)

slog.Info("GRPC server listening at", "address", lis.Addr())
if err := s.Serve(lis); err != nil {
slog.Error("Failed to serve:", "error", err)
Expand Down
30 changes: 30 additions & 0 deletions metrics/counters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
Reg = prometheus.NewRegistry()

TotalBufferContents = promauto.With(Reg).NewCounter(prometheus.CounterOpts{
Name: "tigerbeetleapi_buffer_contents_total",
Help: "Tigerbeetle requests buffered filled size sum",
})

TotalBufferMax = promauto.With(Reg).NewCounter(prometheus.CounterOpts{
Name: "tigerbeetleapi_buffer_max_total",
Help: "Tigerbeetle requests buffer max size sum",
})

TotalBufferCount = promauto.With(Reg).NewCounter(prometheus.CounterOpts{
Name: "tigerbeetleapi_buffer_count_total",
Help: "Tigerbeetle requests total buffers",
})

TotalCreateTransferTx = promauto.With(Reg).NewCounter(prometheus.CounterOpts{
Name: "tigerbeetleapi_create_transfers_tx_total",
Help: "Created transfer transactions",
})
)
15 changes: 15 additions & 0 deletions metrics/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package metrics

import (
"context"
"log/slog"
"net/http"
)

func Register(addr string, h http.Handler) func() {
http.Handle("/metrics", h)
server := &http.Server{Addr: addr, Handler: nil}
slog.Info("Prometheus server listening at", "address", addr, "path", "/metrics")
go server.ListenAndServe()
return func() { server.Shutdown(context.TODO()) }
}
4 changes: 4 additions & 0 deletions rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/lil5/tigerbeetle_api/grpc"
"github.com/lil5/tigerbeetle_api/metrics"
)

func NewServer() {
Expand All @@ -21,6 +22,9 @@ func NewServer() {
slog.Info("Rest server listening at", "host", grpc.Config.Host, "port", grpc.Config.Port)
defer slog.Info("Server exiting")

prometheusDeferClose := metrics.Register(grpc.Config.PrometheusAddr, nil)
defer prometheusDeferClose()

addr := fmt.Sprintf("%s:%s", grpc.Config.Host, grpc.Config.Port)
if grpc.Config.OnlyIpv4 {
server := &http.Server{Handler: r}
Expand Down

0 comments on commit b1a60f8

Please sign in to comment.