Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOISSUE - Group service middleware into single folder #2472

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 logging metrics and tracing 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
6 changes: 3 additions & 3 deletions 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 All @@ -21,9 +21,9 @@
svc auth.Service
}

// LoggingMiddleware adds logging facilities to the core service.
func LoggingMiddleware(svc auth.Service, logger *slog.Logger) auth.Service {
// Logging adds logging facilities to the core service.
func Logging(svc auth.Service, logger *slog.Logger) auth.Service {
return &loggingMiddleware{logger, svc}

Check warning on line 26 in auth/middleware/logging.go

View check run for this annotation

Codecov / codecov/patch

auth/middleware/logging.go#L25-L26

Added lines #L25 - L26 were not covered by tests
}

func (lm *loggingMiddleware) Issue(ctx context.Context, token string, key auth.Key) (tkn auth.Token, err error) {
Expand Down
6 changes: 3 additions & 3 deletions 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 All @@ -22,8 +22,8 @@ type metricsMiddleware struct {
svc auth.Service
}

// MetricsMiddleware instruments core service by tracking request count and latency.
func MetricsMiddleware(svc auth.Service, counter metrics.Counter, latency metrics.Histogram) auth.Service {
// Metrics instruments core service by tracking request count and latency.
func Metrics(svc auth.Service, counter metrics.Counter, latency metrics.Histogram) auth.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
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 @@
svc auth.Service
}

// New returns a new group service with tracing capabilities.
func New(svc auth.Service, tracer trace.Tracer) auth.Service {
// Tracing returns a new auth service with tracing capabilities.
func Tracing(svc auth.Service, tracer trace.Tracer) auth.Service {

Check warning on line 24 in auth/middleware/tracing.go

View check run for this annotation

Codecov / codecov/patch

auth/middleware/tracing.go#L24

Added line #L24 was not covered by tests
return &tracingMiddleware{tracer, svc}
}

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

This file was deleted.

4 changes: 2 additions & 2 deletions bootstrap/middleware/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
authz mgauthz.Authorization
}

// AuthorizationMiddleware adds authorization to the clients service.
func AuthorizationMiddleware(svc bootstrap.Service, authz mgauthz.Authorization) bootstrap.Service {
// Authorization adds authorization to the clients service.
func Authorization(svc bootstrap.Service, authz mgauthz.Authorization) bootstrap.Service {
return &authorizationMiddleware{
svc: svc,

Check warning on line 26 in bootstrap/middleware/authorization.go

View check run for this annotation

Codecov / codecov/patch

bootstrap/middleware/authorization.go#L24-L26

Added lines #L24 - L26 were not covered by tests
authz: authz,
}
}
Expand Down
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
4 changes: 2 additions & 2 deletions bootstrap/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type loggingMiddleware struct {
svc bootstrap.Service
}

// LoggingMiddleware adds logging facilities to the bootstrap service.
func LoggingMiddleware(svc bootstrap.Service, logger *slog.Logger) bootstrap.Service {
// Logging adds logging facilities to the bootstrap service.
func Logging(svc bootstrap.Service, logger *slog.Logger) bootstrap.Service {
return &loggingMiddleware{logger, svc}
}

Expand Down
4 changes: 2 additions & 2 deletions bootstrap/middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type metricsMiddleware struct {
svc bootstrap.Service
}

// MetricsMiddleware instruments core service by tracking request count and latency.
func MetricsMiddleware(svc bootstrap.Service, counter metrics.Counter, latency metrics.Histogram) bootstrap.Service {
// Metrics instruments core service by tracking request count and latency.
func Metrics(svc bootstrap.Service, counter metrics.Counter, latency metrics.Histogram) bootstrap.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
Expand Down
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,10 +19,10 @@
svc bootstrap.Service
}

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

Check warning on line 25 in bootstrap/middleware/tracing.go

View check run for this annotation

Codecov / codecov/patch

bootstrap/middleware/tracing.go#L23-L25

Added lines #L23 - L25 were not covered by tests

// Add traces the "Add" operation of the wrapped bootstrap.Service.
func (tm *tracingMiddleware) Add(ctx context.Context, session mgauthn.Session, token string, cfg bootstrap.Config) (bootstrap.Config, error) {
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 logging, metrics and tracing middleware
// for Magistrala Certs service.
//
// For more details about tracing instrumentation for Magistrala messaging refer
// to the documentation at https://docs.magistrala.abstractmachines.fr/tracing/.
package middleware
6 changes: 3 additions & 3 deletions 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 All @@ -20,8 +20,8 @@ type loggingMiddleware struct {
svc certs.Service
}

// LoggingMiddleware adds logging facilities to the bootstrap service.
func LoggingMiddleware(svc certs.Service, logger *slog.Logger) certs.Service {
// Logging adds logging facilities to the bootstrap service.
func Logging(svc certs.Service, logger *slog.Logger) certs.Service {
return &loggingMiddleware{logger, svc}
}

Expand Down
6 changes: 3 additions & 3 deletions 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 All @@ -21,8 +21,8 @@ type metricsMiddleware struct {
svc certs.Service
}

// MetricsMiddleware instruments core service by tracking request count and latency.
func MetricsMiddleware(svc certs.Service, counter metrics.Counter, latency metrics.Histogram) certs.Service {
// Metrics instruments core service by tracking request count and latency.
func Metrics(svc certs.Service, counter metrics.Counter, latency metrics.Histogram) certs.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
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 @@
svc certs.Service
}

// New returns a new certs service with tracing capabilities.
func New(svc certs.Service, tracer trace.Tracer) certs.Service {
// Tracing returns a new certs service with tracing capabilities.
func Tracing(svc certs.Service, tracer trace.Tracer) certs.Service {

Check warning on line 22 in certs/middleware/tracing.go

View check run for this annotation

Codecov / codecov/patch

certs/middleware/tracing.go#L22

Added line #L22 was not covered by tests
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.Logging(svc, logger)
counter, latency := prometheus.MakeMetrics("groups", "api")
svc = api.MetricsMiddleware(svc, counter, latency)
svc = tracing.New(svc, tracer)
svc = middleware.Metrics(svc, counter, latency)
svc = middleware.Tracing(svc, tracer)

return svc
}
9 changes: 4 additions & 5 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 @@ -218,12 +217,12 @@ func newService(ctx context.Context, authz mgauthz.Authorization, policySvc poli
return nil, err
}

svc = middleware.AuthorizationMiddleware(svc, authz)
svc = middleware.Authorization(svc, authz)
svc = producer.NewEventStoreMiddleware(svc, publisher)
svc = middleware.LoggingMiddleware(svc, logger)
svc = middleware.Logging(svc, logger)
counter, latency := prometheus.MakeMetrics(svcName, "api")
svc = middleware.MetricsMiddleware(svc, counter, latency)
svc = tracing.New(svc, tracer)
svc = middleware.Metrics(svc, counter, latency)
svc = middleware.Tracing(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.Logging(svc, logger)
counter, latency := prometheus.MakeMetrics(svcName, "api")
svc = api.MetricsMiddleware(svc, counter, latency)
svc = tracing.New(svc, tracer)
svc = middleware.Metrics(svc, counter, latency)
svc = middleware.Tracing(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.Tracing(tracer, svc)

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion cmd/invitations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func newService(db *sqlx.DB, dbConfig clientspg.Config, authz mgauthz.Authorizat
sdk := mgsdk.NewSDK(config)

svc := invitations.NewService(token, repo, sdk)
svc = middleware.AuthorizationMiddleware(authz, svc)
svc = middleware.Authorization(authz, svc)
svc = middleware.Tracing(svc, tracer)
svc = middleware.Logging(logger, svc)
counter, latency := prometheus.MakeMetrics(svcName, "api")
Expand Down
4 changes: 2 additions & 2 deletions cmd/journal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ func newService(db *sqlx.DB, dbConfig pgclient.Config, authn mgauthn.Authenticat
idp := uuid.New()

svc := journal.NewService(authn, authz, idp, repo)
svc = middleware.LoggingMiddleware(svc, logger)
svc = middleware.Logging(svc, logger)
counter, latency := prometheus.MakeMetrics("journal", "journal_writer")
svc = middleware.MetricsMiddleware(svc, counter, latency)
svc = middleware.Metrics(svc, counter, latency)
svc = middleware.Tracing(svc, tracer)

return svc
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.Logging(svc, logger)
counter, latency := prometheus.MakeMetrics("postgres", "message_reader")
svc = api.MetricsMiddleware(svc, counter, latency)
svc = middleware.Metrics(svc, counter, latency)

return svc
}
Loading
Loading