Skip to content

Commit

Permalink
feat(metrics): integrate Prometheus for HTTP request metrics
Browse files Browse the repository at this point in the history
- Added `PROMETHEUS_ADDR` to `.env` for Prometheus configuration
- Updated `compose.yml` for asynchronous Fluentd connection and Prometheus service
- Introduced `MetricsMiddleware` to track HTTP requests
- Renamed several files in `configfx`, `jsonparser`, `openapi`, `healthcheck` modules for consistency
- Created `metricsfx` package for Prometheus metrics integration
- Updated `UserRepository` and `Context` methods to adhere to best practices
- Refactored code to include HTTP metrics tracking and Prometheus client updates in `go.mod` and `go.sum`
  • Loading branch information
eser committed Sep 22, 2024
1 parent d7b7ab2 commit 9e439a4
Show file tree
Hide file tree
Showing 24 changed files with 168 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# APP_NAME=go-service
# LOG_TARGET=stdout
# LOG__TARGET=stdout
# LOG__LEVEL=info
# JWT_SIGNATURE=
PROMETHEUS_ADDR=localhost:9090
PORT=8080
# CORS_ORIGIN=
# CORS_STRICT_HEADERS=
Expand Down
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ linters:
- stylecheck
- varnamelen
- execinquery
- exportloopref

linters-settings:
depguard:
Expand All @@ -34,6 +35,7 @@ linters-settings:
- github.com/go-faker/faker/v4
- github.com/getkin/kin-openapi/openapi3
- github.com/golang-jwt/jwt/v5
- github.com/prometheus/client_golang/prometheus
revive:
# enable-all-rules: true
ignore-generated-header: true
Expand Down
2 changes: 2 additions & 0 deletions deployments/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ services:
driver: fluentd
options:
fluentd-address: localhost:24224
fluentd-async-connect: "true"
tag: go-service

postgres:
Expand All @@ -60,6 +61,7 @@ services:
driver: fluentd
options:
fluentd-address: localhost:24224
fluentd-async-connect: "true"
tag: postgres

prometheus:
Expand Down
15 changes: 14 additions & 1 deletion deployments/resources/fluentd/fluentd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,32 @@
port 24224
</source>

<filter **>
@type parser
key_name log
<parse>
@type json
</parse>
</filter>

<match **>
@type loki
url http://loki:3100
# url http://loki:3100/loki/api/v1/push
# insecure_tls true
# tenant ${$.kubernetes.labels.tenant}

extra_labels {"env":"dev"}
extra_labels {"agent":"fluentd"}
# extract_kubernetes_labels true
# remove_keys kubernetes
# <format>
# @type json
# </format>

<label>
# worker fluentd_worker
# container $.kubernetes.container
# container_id ${record["container_id"]}
</label>

<buffer>
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ require (
golang.org/x/net v0.28.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // 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
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
Expand Down Expand Up @@ -44,6 +53,7 @@ require (
github.com/pingcap/log v1.1.0 // indirect
github.com/pingcap/tidb/pkg/parser v0.0.0-20231103154709-4f00ece106b1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.20.4
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/riza-io/grpc-go v0.2.0 // indirect
github.com/spf13/cobra v1.8.1 // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
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/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cubicdaiya/gonp v1.0.4 h1:ky2uIAJh81WiLcGKBVD5R7KsM/36W6IqqTy6Bo6rGws=
github.com/cubicdaiya/gonp v1.0.4/go.mod h1:iWGuP/7+JVTn02OWhRemVbMmG1DOUnmrGTYYACpOI0I=
Expand Down Expand Up @@ -65,6 +69,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
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/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
Expand All @@ -88,6 +94,14 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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.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/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/riza-io/grpc-go v0.2.0 h1:2HxQKFVE7VuYstcJ8zqpN84VnAoJ4dCL6YFhJewNcHQ=
Expand Down
3 changes: 2 additions & 1 deletion pkg/app/mod.go → pkg/app/mod-fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ func LoadConfig(cl configfx.ConfigLoader) (*config.AppConfig, error) {
return appConfig, nil
}

func RegisterMiddlewares(routes httpfx.Router, appConfig *config.AppConfig) {
func RegisterMiddlewares(routes httpfx.Router, httpMetrics *httpfx.Metrics, appConfig *config.AppConfig) {
routes.Use(middlewares.ErrorHandlerMiddleware())
routes.Use(middlewares.ResolveAddressMiddleware())
routes.Use(middlewares.ResponseTimeMiddleware())
routes.Use(middlewares.CorrelationIdMiddleware())
routes.Use(middlewares.CorsMiddleware())
routes.Use(middlewares.MetricsMiddleware(httpMetrics))
}
2 changes: 1 addition & 1 deletion pkg/app/storage/repositories/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewUserRepository(tx datafx.DbExecutor) UserRepository {
}
}

func (r UserRepository) DbExecutor() datafx.DbExecutor { //nolint:ireturn
func (r UserRepository) DbExecutor() datafx.DbExecutor {
return r.tx
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/bliss/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ type BaseConfig struct {
Http httpfx.Config `conf:"HTTP"`

// AppName string `conf:"APP_NAME" default:"go-service"`
// LogTarget string `conf:"LOG_TARGET" default:"stdout"`
// Port string `conf:"PORT" default:"8080"`
// JwtSignature string `conf:"JWT_SIGNATURE"`
// CorsOrigin string `conf:"CORS_ORIGIN"`
// CorsStrictHeaders bool `conf:"CORS_STRICT_HEADERS"`
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/bliss/httpfx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Context struct {
// isAborted bool
}

func (c *Context) Next() Result {
func (c *Context) Next() Result { //nolint:ireturn
c.index++

for c.index < len(c.handlers) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/bliss/httpfx/http-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ type HttpService interface {
}

type HttpServiceImpl struct {
InnerServer *http.Server
InnerRouter Router
InnerServer *http.Server
InnerRouter Router
InnerMetrics *Metrics

Config *Config
}
Expand All @@ -41,8 +42,8 @@ func NewHttpService(config *Config, router Router) *HttpServiceImpl {

return &HttpServiceImpl{
InnerServer: server,
Config: config,
InnerRouter: router,
Config: config,
}
}

Expand Down
29 changes: 29 additions & 0 deletions pkg/bliss/httpfx/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package httpfx

import (
"github.com/eser/go-service/pkg/bliss/metricsfx"
"github.com/prometheus/client_golang/prometheus"
)

type Metrics struct {
mp metricsfx.MetricsProvider

RequestsTotal *prometheus.CounterVec
}

func NewMetrics(mp metricsfx.MetricsProvider) *Metrics {
requestsTotal := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Total number of HTTP requests",
},
[]string{"method", "endpoint", "status"},
)

mp.GetRegistry().MustRegister(requestsTotal)

return &Metrics{
mp: mp,
RequestsTotal: requestsTotal,
}
}
19 changes: 19 additions & 0 deletions pkg/bliss/httpfx/middlewares/metrics-middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package middlewares

import (
"strconv"

"github.com/eser/go-service/pkg/bliss/httpfx"
)

func MetricsMiddleware(httpMetrics *httpfx.Metrics) httpfx.Handler {
return func(ctx *httpfx.Context) httpfx.Result {
result := ctx.Next()

httpMetrics.RequestsTotal.WithLabelValues(
ctx.Request.Method,
ctx.Request.URL.Path, strconv.Itoa(result.StatusCode())).Inc()

return result
}
}
6 changes: 5 additions & 1 deletion pkg/bliss/httpfx/mod-fx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package httpfx

import (
"github.com/eser/go-service/pkg/bliss/metricsfx"
"go.uber.org/fx"
)

Expand All @@ -19,17 +20,20 @@ type FxResult struct {

HttpService HttpService
Routes Router
Metrics *Metrics
}

func FxNew(config *Config) FxResult {
func FxNew(config *Config, mp metricsfx.MetricsProvider) FxResult {
routes := NewRouter("/")
httpService := NewHttpService(config, routes)
metrics := NewMetrics(mp)

return FxResult{
Out: fx.Out{},

HttpService: httpService,
Routes: routes,
Metrics: metrics,
}
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions pkg/bliss/metricsfx/mod-fx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package metricsfx

import (
"go.uber.org/fx"
)

var FxModule = fx.Module( //nolint:gochecknoglobals
"metrics",
fx.Provide(
FxNew,
),
)

type FxResult struct {
fx.Out

MetricsProvider MetricsProvider
}

func FxNew() FxResult {
return FxResult{
Out: fx.Out{},

MetricsProvider: NewMetricsProvider(),
}
}
34 changes: 34 additions & 0 deletions pkg/bliss/metricsfx/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package metricsfx

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

type MetricsProvider interface {
GetRegistry() *prometheus.Registry
}

type MetricsProviderImpl struct {
registry *prometheus.Registry
}

var _ MetricsProvider = (*MetricsProviderImpl)(nil)

func NewMetricsProvider() *MetricsProviderImpl {
registry := prometheus.NewRegistry()

// Register the Go collector (which collects runtime metrics)
registry.MustRegister(collectors.NewGoCollector())

// Register the process collector (which collects process-level metrics)
registry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))

return &MetricsProviderImpl{
registry: registry,
}
}

func (mp *MetricsProviderImpl) GetRegistry() *prometheus.Registry {
return mp.registry
}
2 changes: 2 additions & 0 deletions pkg/bliss/mod.go → pkg/bliss/mod-fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"github.com/eser/go-service/pkg/bliss/datafx"
"github.com/eser/go-service/pkg/bliss/httpfx"
"github.com/eser/go-service/pkg/bliss/logfx"
"github.com/eser/go-service/pkg/bliss/metricsfx"
"go.uber.org/fx"
)

var FxModule = fx.Module( //nolint:gochecknoglobals
"bliss",
logfx.FxModule,
configfx.FxModule,
metricsfx.FxModule,
httpfx.FxModule,
datafx.FxModule,
)

0 comments on commit 9e439a4

Please sign in to comment.