-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): integrate Prometheus for HTTP request metrics
- 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
Showing
24 changed files
with
168 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters