Skip to content

Commit

Permalink
refactor(middleware): group service middleware
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Gateru <[email protected]>
  • Loading branch information
felixgateru committed Oct 22, 2024
1 parent 52c0f26 commit bcf9c32
Show file tree
Hide file tree
Showing 54 changed files with 164 additions and 188 deletions.
9 changes: 9 additions & 0 deletions auth/middleware/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

// Package middleware provides tracing, logging and metrics middleware
// for Magistrala Auth service.
//
// For more details about tracing instrumentation for Magistrala messaging refer
// to the documentation at https://docs.magistrala.abstractmachines.fr/tracing/.
package middleware
2 changes: 1 addition & 1 deletion auth/api/logging.go → auth/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//go:build !test

package api
package middleware

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion auth/api/metrics.go → auth/middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//go:build !test

package api
package middleware

import (
"context"
Expand Down
6 changes: 3 additions & 3 deletions auth/tracing/tracing.go → auth/middleware/tracing.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package tracing
package middleware

import (
"context"
Expand All @@ -20,8 +20,8 @@ type tracingMiddleware struct {
svc auth.Service
}

// New returns a new group service with tracing capabilities.
func New(svc auth.Service, tracer trace.Tracer) auth.Service {
// TracingMiddleware returns a new auth service with tracing capabilities.
func TracingMiddleware(svc auth.Service, tracer trace.Tracer) auth.Service {
return &tracingMiddleware{tracer, svc}
}

Expand Down
12 changes: 0 additions & 12 deletions auth/tracing/doc.go

This file was deleted.

9 changes: 9 additions & 0 deletions bootstrap/middleware/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

// Package middleware provides authorization, tracing, logging and metrics middleware
// for Magistrala Boostrap service.
//
// For more details about tracing instrumentation for Magistrala messaging refer
// to the documentation at https://docs.magistrala.abstractmachines.fr/tracing/.
package middleware
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package tracing
package middleware

import (
"context"
Expand All @@ -19,8 +19,8 @@ type tracingMiddleware struct {
svc bootstrap.Service
}

// New returns a new bootstrap service with tracing capabilities.
func New(svc bootstrap.Service, tracer trace.Tracer) bootstrap.Service {
// TracingMiddleware returns a new bootstrap service with tracing capabilities.
func TracingMiddleware(svc bootstrap.Service, tracer trace.Tracer) bootstrap.Service {
return &tracingMiddleware{tracer, svc}
}

Expand Down
12 changes: 0 additions & 12 deletions bootstrap/tracing/doc.go

This file was deleted.

9 changes: 9 additions & 0 deletions certs/middleware/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

// Package middleware provides tracing, logging and metrics middleware
// for Magistrala Auth service.
//
// For more details about tracing instrumentation for Magistrala messaging refer
// to the documentation at https://docs.magistrala.abstractmachines.fr/tracing/.
package middleware
2 changes: 1 addition & 1 deletion certs/api/logging.go → certs/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//go:build !test

package api
package middleware

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion certs/api/metrics.go → certs/middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//go:build !test

package api
package middleware

import (
"context"
Expand Down
6 changes: 3 additions & 3 deletions certs/tracing/tracing.go → certs/middleware/tracing.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package tracing
package middleware

import (
"context"
Expand All @@ -18,8 +18,8 @@ type tracingMiddleware struct {
svc certs.Service
}

// New returns a new certs service with tracing capabilities.
func New(svc certs.Service, tracer trace.Tracer) certs.Service {
// TracingMiddleware returns a new certs service with tracing capabilities.
func TracingMiddleware(svc certs.Service, tracer trace.Tracer) certs.Service {
return &tracingMiddleware{tracer, svc}
}

Expand Down
12 changes: 0 additions & 12 deletions certs/tracing/doc.go

This file was deleted.

9 changes: 4 additions & 5 deletions cmd/auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ import (
chclient "github.com/absmach/callhome/pkg/client"
"github.com/absmach/magistrala"
"github.com/absmach/magistrala/auth"
api "github.com/absmach/magistrala/auth/api"
authgrpcapi "github.com/absmach/magistrala/auth/api/grpc/auth"
domainsgrpcapi "github.com/absmach/magistrala/auth/api/grpc/domains"
tokengrpcapi "github.com/absmach/magistrala/auth/api/grpc/token"
httpapi "github.com/absmach/magistrala/auth/api/http"
"github.com/absmach/magistrala/auth/events"
"github.com/absmach/magistrala/auth/jwt"
"github.com/absmach/magistrala/auth/middleware"
apostgres "github.com/absmach/magistrala/auth/postgres"
"github.com/absmach/magistrala/auth/tracing"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/jaeger"
"github.com/absmach/magistrala/pkg/policies/spicedb"
Expand Down Expand Up @@ -224,10 +223,10 @@ func newService(ctx context.Context, db *sqlx.DB, tracer trace.Tracer, cfg confi
logger.Error(fmt.Sprintf("failed to init event store middleware : %s", err))
return nil
}
svc = api.LoggingMiddleware(svc, logger)
svc = middleware.LoggingMiddleware(svc, logger)
counter, latency := prometheus.MakeMetrics("groups", "api")
svc = api.MetricsMiddleware(svc, counter, latency)
svc = tracing.New(svc, tracer)
svc = middleware.MetricsMiddleware(svc, counter, latency)
svc = middleware.TracingMiddleware(svc, tracer)

return svc
}
3 changes: 1 addition & 2 deletions cmd/bootstrap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/absmach/magistrala/bootstrap/events/producer"
"github.com/absmach/magistrala/bootstrap/middleware"
bootstrappg "github.com/absmach/magistrala/bootstrap/postgres"
"github.com/absmach/magistrala/bootstrap/tracing"
mglog "github.com/absmach/magistrala/logger"
authsvcAuthn "github.com/absmach/magistrala/pkg/authn/authsvc"
mgauthz "github.com/absmach/magistrala/pkg/authz"
Expand Down Expand Up @@ -223,7 +222,7 @@ func newService(ctx context.Context, authz mgauthz.Authorization, policySvc poli
svc = middleware.LoggingMiddleware(svc, logger)
counter, latency := prometheus.MakeMetrics(svcName, "api")
svc = middleware.MetricsMiddleware(svc, counter, latency)
svc = tracing.New(svc, tracer)
svc = middleware.TracingMiddleware(svc, tracer)

return svc, nil
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/certs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/absmach/magistrala"
"github.com/absmach/magistrala/certs"
"github.com/absmach/magistrala/certs/api"
"github.com/absmach/magistrala/certs/middleware"
pki "github.com/absmach/magistrala/certs/pki/amcerts"
"github.com/absmach/magistrala/certs/tracing"
mglog "github.com/absmach/magistrala/logger"
authsvcAuthn "github.com/absmach/magistrala/pkg/authn/authsvc"
"github.com/absmach/magistrala/pkg/grpcclient"
Expand Down Expand Up @@ -159,10 +159,10 @@ func newService(tracer trace.Tracer, logger *slog.Logger, cfg config, pkiAgent p
}
sdk := mgsdk.NewSDK(config)
svc := certs.New(sdk, pkiAgent)
svc = api.LoggingMiddleware(svc, logger)
svc = middleware.LoggingMiddleware(svc, logger)
counter, latency := prometheus.MakeMetrics(svcName, "api")
svc = api.MetricsMiddleware(svc, counter, latency)
svc = tracing.New(svc, tracer)
svc = middleware.MetricsMiddleware(svc, counter, latency)
svc = middleware.TracingMiddleware(svc, tracer)

return svc
}
8 changes: 4 additions & 4 deletions cmd/coap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/absmach/magistrala"
"github.com/absmach/magistrala/coap"
"github.com/absmach/magistrala/coap/api"
"github.com/absmach/magistrala/coap/tracing"
"github.com/absmach/magistrala/coap/middleware"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/grpcclient"
jaegerclient "github.com/absmach/magistrala/pkg/jaeger"
Expand Down Expand Up @@ -128,12 +128,12 @@ func main() {

svc := coap.New(thingsClient, nps)

svc = tracing.New(tracer, svc)
svc = middleware.TracingMiddleware(tracer, svc)

svc = api.LoggingMiddleware(svc, logger)
svc = middleware.LoggingMiddleware(svc, logger)

counter, latency := prometheus.MakeMetrics(svcName, "api")
svc = api.MetricsMiddleware(svc, counter, latency)
svc = middleware.MetricsMiddleware(svc, counter, latency)

hs := httpserver.NewServer(ctx, cancel, svcName, httpServerConfig, api.MakeHandler(cfg.InstanceID), logger)

Expand Down
5 changes: 3 additions & 2 deletions cmd/postgres-reader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/absmach/magistrala/pkg/uuid"
"github.com/absmach/magistrala/readers"
"github.com/absmach/magistrala/readers/api"
"github.com/absmach/magistrala/readers/middleware"
"github.com/absmach/magistrala/readers/postgres"
"github.com/caarlos0/env/v11"
"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -147,9 +148,9 @@ func main() {

func newService(db *sqlx.DB, logger *slog.Logger) readers.MessageRepository {
svc := postgres.New(db)
svc = api.LoggingMiddleware(svc, logger)
svc = middleware.LoggingMiddleware(svc, logger)
counter, latency := prometheus.MakeMetrics("postgres", "message_reader")
svc = api.MetricsMiddleware(svc, counter, latency)
svc = middleware.MetricsMiddleware(svc, counter, latency)

return svc
}
3 changes: 2 additions & 1 deletion cmd/provision/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/absmach/magistrala/pkg/uuid"
"github.com/absmach/magistrala/provision"
"github.com/absmach/magistrala/provision/api"
"github.com/absmach/magistrala/provision/middleware"
"github.com/caarlos0/env/v11"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -84,7 +85,7 @@ func main() {
SDK := mgsdk.NewSDK(SDKCfg)

svc := provision.New(cfg, SDK, logger)
svc = api.NewLoggingMiddleware(svc, logger)
svc = middleware.NewLoggingMiddleware(svc, logger)

httpServerConfig := server.Config{Host: "", Port: cfg.Server.HTTPPort, KeyFile: cfg.Server.ServerKey, CertFile: cfg.Server.ServerCert}
hs := httpserver.NewServer(ctx, cancel, svcName, httpServerConfig, api.MakeHandler(svc, logger, cfg.InstanceID), logger)
Expand Down
6 changes: 2 additions & 4 deletions cmd/things/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
gevents "github.com/absmach/magistrala/internal/groups/events"
gmiddleware "github.com/absmach/magistrala/internal/groups/middleware"
gpostgres "github.com/absmach/magistrala/internal/groups/postgres"
gtracing "github.com/absmach/magistrala/internal/groups/tracing"
mglog "github.com/absmach/magistrala/logger"
authsvcAuthn "github.com/absmach/magistrala/pkg/authn/authsvc"
mgauthz "github.com/absmach/magistrala/pkg/authz"
Expand All @@ -44,7 +43,6 @@ import (
thevents "github.com/absmach/magistrala/things/events"
tmiddleware "github.com/absmach/magistrala/things/middleware"
thingspg "github.com/absmach/magistrala/things/postgres"
ctracing "github.com/absmach/magistrala/things/tracing"
"github.com/authzed/authzed-go/v1"
"github.com/authzed/grpcutil"
"github.com/caarlos0/env/v11"
Expand Down Expand Up @@ -262,12 +260,12 @@ func newService(ctx context.Context, db *sqlx.DB, dbConfig pgclient.Config, auth
csvc = tmiddleware.AuthorizationMiddleware(csvc, authz)
gsvc = gmiddleware.AuthorizationMiddleware(gsvc, authz)

csvc = ctracing.New(csvc, tracer)
csvc = tmiddleware.TracingMiddleware(csvc, tracer)
csvc = tmiddleware.LoggingMiddleware(csvc, logger)
counter, latency := prometheus.MakeMetrics(svcName, "api")
csvc = tmiddleware.MetricsMiddleware(csvc, counter, latency)

gsvc = gtracing.New(gsvc, tracer)
gsvc = gmiddleware.TracingMiddleware(gsvc, tracer)
gsvc = gmiddleware.LoggingMiddleware(gsvc, logger)
counter, latency = prometheus.MakeMetrics(fmt.Sprintf("%s_groups", svcName), "api")
gsvc = gmiddleware.MetricsMiddleware(gsvc, counter, latency)
Expand Down
5 changes: 3 additions & 2 deletions cmd/timescale-reader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/absmach/magistrala/pkg/uuid"
"github.com/absmach/magistrala/readers"
"github.com/absmach/magistrala/readers/api"
"github.com/absmach/magistrala/readers/middleware"
"github.com/absmach/magistrala/readers/timescale"
"github.com/caarlos0/env/v11"
"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -145,9 +146,9 @@ func main() {

func newService(db *sqlx.DB, logger *slog.Logger) readers.MessageRepository {
svc := timescale.New(db)
svc = api.LoggingMiddleware(svc, logger)
svc = middleware.LoggingMiddleware(svc, logger)
counter, latency := prometheus.MakeMetrics("timescale", "message_reader")
svc = api.MetricsMiddleware(svc, counter, latency)
svc = middleware.MetricsMiddleware(svc, counter, latency)

return svc
}
6 changes: 2 additions & 4 deletions cmd/users/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
gevents "github.com/absmach/magistrala/internal/groups/events"
gmiddleware "github.com/absmach/magistrala/internal/groups/middleware"
gpostgres "github.com/absmach/magistrala/internal/groups/postgres"
gtracing "github.com/absmach/magistrala/internal/groups/tracing"
mglog "github.com/absmach/magistrala/logger"
authsvcAuthn "github.com/absmach/magistrala/pkg/authn/authsvc"
mgauthz "github.com/absmach/magistrala/pkg/authz"
Expand All @@ -47,7 +46,6 @@ import (
"github.com/absmach/magistrala/users/hasher"
cmiddleware "github.com/absmach/magistrala/users/middleware"
clientspg "github.com/absmach/magistrala/users/postgres"
ctracing "github.com/absmach/magistrala/users/tracing"
"github.com/authzed/authzed-go/v1"
"github.com/authzed/grpcutil"
"github.com/caarlos0/env/v11"
Expand Down Expand Up @@ -282,12 +280,12 @@ func newService(ctx context.Context, authz mgauthz.Authorization, token magistra
csvc = cmiddleware.AuthorizationMiddleware(csvc, authz, c.SelfRegister)
gsvc = gmiddleware.AuthorizationMiddleware(gsvc, authz)

csvc = ctracing.New(csvc, tracer)
csvc = cmiddleware.TracingMiddleware(csvc, tracer)
csvc = cmiddleware.LoggingMiddleware(csvc, logger)
counter, latency := prometheus.MakeMetrics(svcName, "api")
csvc = cmiddleware.MetricsMiddleware(csvc, counter, latency)

gsvc = gtracing.New(gsvc, tracer)
gsvc = gmiddleware.TracingMiddleware(gsvc, tracer)
gsvc = gmiddleware.LoggingMiddleware(gsvc, logger)
counter, latency = prometheus.MakeMetrics("groups", "api")
gsvc = gmiddleware.MetricsMiddleware(gsvc, counter, latency)
Expand Down
Loading

0 comments on commit bcf9c32

Please sign in to comment.