Skip to content

Commit

Permalink
feat: improve the metrics handling
Browse files Browse the repository at this point in the history
  • Loading branch information
1995parham committed Oct 23, 2024
1 parent 629ce81 commit 0b339d4
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 195 deletions.
6 changes: 3 additions & 3 deletions internal/api/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (a API) ACLv2(c *fiber.Ctx) error {
zap.String("username", request.Username),
zap.String("password", request.Password),
)
authenticator.IncrementWithErrorACLCounter("unknown_company_before_parse_body", err)
a.Metrics.ACLFailed("unknown_company_before_parse_body", err)

return c.Status(http.StatusOK).JSON(ACLResponse{
Result: "deny",
Expand Down Expand Up @@ -85,7 +85,7 @@ func (a API) ACLv2(c *fiber.Ctx) error {
span.RecordError(err)
}

authenticator.IncrementWithErrorACLCounter(vendor, err)
a.Metrics.ACLFailed(vendor, err)

var tnaErr authenticator.TopicNotAllowedError

Expand All @@ -106,7 +106,7 @@ func (a API) ACLv2(c *fiber.Ctx) error {

logger.
Info("acl ok")
authenticator.IncrementACLCounter(vendor)
a.Metrics.ACLSuccess(vendor)

return c.Status(http.StatusOK).JSON(ACLResponse{
Result: "allow",
Expand Down
4 changes: 3 additions & 1 deletion internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/snapp-incubator/soteria/internal/authenticator"
"github.com/snapp-incubator/soteria/internal/clientid"
"github.com/snapp-incubator/soteria/internal/metric"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)
Expand All @@ -20,6 +21,7 @@ type API struct {
Tracer trace.Tracer
Parser *clientid.Parser
Logger *zap.Logger
Metrics *metric.APIMetrics
}

// MetricLogSkipper check if route is equal "metric" disable log.
Expand All @@ -39,7 +41,7 @@ func (a API) ReSTServer() *fiber.App {
Logger: a.Logger.Named("fiber"),
}))

prometheus := fiberprometheus.NewWith("http", "dispatching", "soteria")
prometheus := fiberprometheus.NewWith("http", "platform", "soteria")
prometheus.RegisterAt(app, "/metrics")
app.Use(prometheus.Middleware)

Expand Down
2 changes: 2 additions & 0 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/snapp-incubator/soteria/internal/authenticator"
"github.com/snapp-incubator/soteria/internal/clientid"
"github.com/snapp-incubator/soteria/internal/config"
"github.com/snapp-incubator/soteria/internal/metric"
"github.com/stretchr/testify/suite"
"go.opentelemetry.io/otel/trace/noop"
"go.uber.org/zap"
Expand Down Expand Up @@ -138,6 +139,7 @@ func (suite *APITestSuite) SetupSuite() {
DefaultVendor: "snapp",
Tracer: noop.NewTracerProvider().Tracer(""),
Logger: zap.NewExample(),
Metrics: metric.NewAPIMetrics(),
Parser: clientid.NewParser(clientid.Config{
Patterns: map[string]string{},
}),
Expand Down
7 changes: 3 additions & 4 deletions internal/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v5"
"github.com/snapp-incubator/soteria/internal/authenticator"
"go.opentelemetry.io/otel/attribute"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -42,7 +41,7 @@ func (a API) Authv2(c *fiber.Ctx) error {
Warn("bad request",
zap.Error(err),
)
authenticator.IncrementWithErrorAuthCounter("unknown_company_before_parse_body", "-", err)
a.Metrics.AuthFailed("unknown_company_before_parse_body", "-", err)

return c.Status(http.StatusOK).JSON(AuthResponse{
Result: "deny",
Expand Down Expand Up @@ -74,7 +73,7 @@ func (a API) Authv2(c *fiber.Ctx) error {

if err := auth.Auth(ctx, token); err != nil {
span.RecordError(err)
authenticator.IncrementWithErrorAuthCounter(vendor, source, err)
a.Metrics.AuthFailed(vendor, source, err)

if !errors.Is(err, jwt.ErrTokenExpired) {
logger.
Expand All @@ -92,7 +91,7 @@ func (a API) Authv2(c *fiber.Ctx) error {

logger.
Info("auth ok")
authenticator.IncrementAuthCounter(vendor, source)
a.Metrics.AuthSuccess(vendor, source)

return c.Status(http.StatusOK).JSON(AuthResponse{
Result: "allow",
Expand Down
109 changes: 0 additions & 109 deletions internal/authenticator/metrics.go

This file was deleted.

78 changes: 0 additions & 78 deletions internal/authenticator/metrics_test.go

This file was deleted.

2 changes: 2 additions & 0 deletions internal/cmd/serve/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/snapp-incubator/soteria/internal/authenticator"
"github.com/snapp-incubator/soteria/internal/clientid"
"github.com/snapp-incubator/soteria/internal/config"
"github.com/snapp-incubator/soteria/internal/metric"
"github.com/spf13/cobra"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
Expand Down Expand Up @@ -39,6 +40,7 @@ func (s Serve) main() {
Tracer: s.Tracer,
Logger: s.Logger.Named("api"),
Parser: clientid.NewParser(s.Cfg.Parser),
Metrics: metric.NewAPIMetrics(),
}

if _, ok := api.Authenticators[s.Cfg.DefaultVendor]; !ok {
Expand Down
Loading

0 comments on commit 0b339d4

Please sign in to comment.