NOISSUE - Group service middleware into single folder (#2472)
Continuous Delivery / Build and Push (push) Has been cancelled
Check the consistency of generated files / check-generated-files (push) Has been cancelled
Check License Header / check-license (push) Has been cancelled
Deploy GitHub Pages / swagger-ui (push) Has been cancelled

Signed-off-by: Felix Gateru <felix.gateru@gmail.com>
This commit is contained in:
Felix Gateru
2025-09-26 14:55:00 +03:00
committed by GitHub
parent 0465ceb7a3
commit b031dc08b8
80 changed files with 302 additions and 326 deletions
+2 -2
View File
@@ -24,7 +24,7 @@ tags:
description: CRUD operations for your domains
externalDocs:
description: Find out more about domains
url: https://docs.magistrala.abstractmachines.fr/
url: https://docs.supermq.abstractmachines.fr/
- name: Roles
description: All operations involving roles for domains
externalDocs:
@@ -39,7 +39,7 @@ tags:
description: Service health check endpoint.
externalDocs:
description: Find out more about health check
url: https://docs.magistrala.abstractmachines.fr/
url: https://docs.supermq.abstractmachines.fr/
paths:
/domains:
+9
View File
@@ -0,0 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package middleware provides logging metrics and tracing middleware
// for SuperMQ Auth service.
//
// For more details about tracing instrumentation for SuperMQ refer to the
// documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package middleware
@@ -3,7 +3,7 @@
//go:build !test
package api
package middleware
import (
"context"
@@ -21,8 +21,8 @@ type loggingMiddleware struct {
svc auth.Service
}
// LoggingMiddleware adds logging facilities to the core service.
func LoggingMiddleware(svc auth.Service, logger *slog.Logger) auth.Service {
// NewLogging adds logging facilities to the core service.
func NewLogging(svc auth.Service, logger *slog.Logger) auth.Service {
return &loggingMiddleware{logger, svc}
}
@@ -3,7 +3,7 @@
//go:build !test
package api
package middleware
import (
"context"
@@ -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 {
// NewMetrics instruments core service by tracking request count and latency.
func NewMetrics(svc auth.Service, counter metrics.Counter, latency metrics.Histogram) auth.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package tracing
package middleware
import (
"context"
@@ -21,8 +21,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 {
// NewTracing returns a new auth service with tracing capabilities.
func NewTracing(svc auth.Service, tracer trace.Tracer) auth.Service {
return &tracingMiddleware{tracer, svc}
}
-12
View File
@@ -1,12 +0,0 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package tracing provides tracing instrumentation for SuperMQ Users service.
//
// This package provides tracing middleware for SuperMQ Users service.
// It can be used to trace incoming requests and add tracing capabilities to
// SuperMQ Users service.
//
// For more details about tracing instrumentation for SuperMQ messaging refer
// to the documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package tracing
+9
View File
@@ -0,0 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package middleware provides logging, metrics and tracing middleware
// for SuperMQ Certs service.
//
// For more details about tracing instrumentation for SuperMQ refer to the
// documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package middleware
@@ -3,7 +3,7 @@
//go:build !test
package api
package middleware
import (
"context"
@@ -21,8 +21,8 @@ type loggingMiddleware struct {
svc certs.Service
}
// LoggingMiddleware adds logging facilities to the certs service.
func LoggingMiddleware(svc certs.Service, logger *slog.Logger) certs.Service {
// NewLogging adds logging facilities to the certs service.
func NewLogging(svc certs.Service, logger *slog.Logger) certs.Service {
return &loggingMiddleware{logger, svc}
}
@@ -3,7 +3,7 @@
//go:build !test
package api
package middleware
import (
"context"
@@ -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 {
// NewMetrics instruments core service by tracking request count and latency.
func NewMetrics(svc certs.Service, counter metrics.Counter, latency metrics.Histogram) certs.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package tracing
package middleware
import (
"context"
@@ -19,8 +19,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 {
// NewTracing returns a new certs service with tracing capabilities.
func NewTracing(svc certs.Service, tracer trace.Tracer) certs.Service {
return &tracingMiddleware{tracer, svc}
}
-12
View File
@@ -1,12 +0,0 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package tracing provides tracing instrumentation for SuperMQ Users Groups service.
//
// This package provides tracing middleware for SuperMQ Users Groups service.
// It can be used to trace incoming requests and add tracing capabilities to
// SuperMQ Users Groups service.
//
// For more details about tracing instrumentation for SuperMQ messaging refer
// to the documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package tracing
+1 -1
View File
@@ -2,5 +2,5 @@
// SPDX-License-Identifier: Apache-2.0
// Package cache contains the domain concept definitions needed to
// support Magistrala Channels cache service functionality.
// support SuperMQ Channels cache service functionality.
package cache
+5 -5
View File
@@ -19,7 +19,7 @@ import (
svcerr "github.com/absmach/supermq/pkg/errors/service"
"github.com/absmach/supermq/pkg/policies"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/absmach/supermq/pkg/svcutil"
)
@@ -51,11 +51,11 @@ type authorizationMiddleware struct {
opp svcutil.OperationPerm
extOpp svcutil.ExternalOperationPerm
callout callout.Callout
rmMW.RoleManagerAuthorizationMiddleware
rolemw.RoleManagerAuthorizationMiddleware
}
// AuthorizationMiddleware adds authorization to the channels service.
func AuthorizationMiddleware(
// NewAuthorization adds authorization to the channels service.
func NewAuthorization(
svc channels.Service,
repo channels.Repository,
authz smqauthz.Authorization,
@@ -78,7 +78,7 @@ func AuthorizationMiddleware(
if err := extOpp.Validate(); err != nil {
return nil, err
}
ram, err := rmMW.NewRoleManagerAuthorizationMiddleware(policies.ChannelType, svc, authz, rolesOpPerm, callout)
ram, err := rolemw.NewAuthorization(policies.ChannelType, svc, authz, rolesOpPerm, callout)
if err != nil {
return nil, err
}
+9
View File
@@ -0,0 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package middleware provides authorization, logging, metrics and tracing middleware
// for SuperMQ Channels Service.
//
// For more details about tracing instrumentation for SuperMQ refer to the
// documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package middleware
+5 -4
View File
@@ -13,7 +13,7 @@ import (
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/connections"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/go-chi/chi/v5/middleware"
)
@@ -22,11 +22,12 @@ var _ channels.Service = (*loggingMiddleware)(nil)
type loggingMiddleware struct {
logger *slog.Logger
svc channels.Service
rmMW.RoleManagerLoggingMiddleware
rolemw.RoleManagerLoggingMiddleware
}
func LoggingMiddleware(svc channels.Service, logger *slog.Logger) channels.Service {
return &loggingMiddleware{logger, svc, rmMW.NewRoleManagerLoggingMiddleware("channels", svc, logger)}
// NewLogging adds logging facilities to the channels service.
func NewLogging(svc channels.Service, logger *slog.Logger) channels.Service {
return &loggingMiddleware{logger, svc, rolemw.NewLogging("channels", svc, logger)}
}
func (lm *loggingMiddleware) CreateChannels(ctx context.Context, session authn.Session, clients ...channels.Channel) (cs []channels.Channel, rps []roles.RoleProvision, err error) {
+5 -5
View File
@@ -11,7 +11,7 @@ import (
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/connections"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/go-kit/kit/metrics"
)
@@ -21,16 +21,16 @@ type metricsMiddleware struct {
counter metrics.Counter
latency metrics.Histogram
svc channels.Service
rmMW.RoleManagerMetricsMiddleware
rolemw.RoleManagerMetricsMiddleware
}
// MetricsMiddleware returns a new metrics middleware wrapper.
func MetricsMiddleware(svc channels.Service, counter metrics.Counter, latency metrics.Histogram) channels.Service {
// NewMetrics returns a new metrics middleware wrapper.
func NewMetrics(svc channels.Service, counter metrics.Counter, latency metrics.Histogram) channels.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
svc: svc,
RoleManagerMetricsMiddleware: rmMW.NewRoleManagerMetricsMiddleware("channels", svc, counter, latency),
RoleManagerMetricsMiddleware: rolemw.NewMetrics("channels", svc, counter, latency),
}
}
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package tracing
package middleware
import (
"context"
@@ -10,7 +10,7 @@ import (
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/connections"
"github.com/absmach/supermq/pkg/roles"
rmTrace "github.com/absmach/supermq/pkg/roles/rolemanager/tracing"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/absmach/supermq/pkg/tracing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
@@ -21,12 +21,12 @@ var _ channels.Service = (*tracingMiddleware)(nil)
type tracingMiddleware struct {
tracer trace.Tracer
svc channels.Service
rmTrace.RoleManagerTracing
rolemw.RoleManagerTracing
}
// New returns a new group service with tracing capabilities.
func New(svc channels.Service, tracer trace.Tracer) channels.Service {
return &tracingMiddleware{tracer, svc, rmTrace.NewRoleManagerTracing("channels", svc, tracer)}
// NewTracing returns a new channels service with tracing capabilities.
func NewTracing(svc channels.Service, tracer trace.Tracer) channels.Service {
return &tracingMiddleware{tracer, svc, rolemw.NewTracing("channels", svc, tracer)}
}
// CreateChannels traces the "CreateChannels" operation of the wrapped policies.Service.
-12
View File
@@ -1,12 +0,0 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package tracing provides tracing instrumentation for SuperMQ channels service.
//
// This package provides tracing middleware for SuperMQ channels service.
// It can be used to trace incoming requests and add tracing capabilities to
// SuperMQ channels service.
//
// For more details about tracing instrumentation for SuperMQ messaging refer
// to the documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package tracing
+5 -5
View File
@@ -17,7 +17,7 @@ import (
svcerr "github.com/absmach/supermq/pkg/errors/service"
"github.com/absmach/supermq/pkg/policies"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/absmach/supermq/pkg/svcutil"
)
@@ -45,11 +45,11 @@ type authorizationMiddleware struct {
opp svcutil.OperationPerm
extOpp svcutil.ExternalOperationPerm
callout callout.Callout
rmMW.RoleManagerAuthorizationMiddleware
rolemw.RoleManagerAuthorizationMiddleware
}
// AuthorizationMiddleware adds authorization to the clients service.
func AuthorizationMiddleware(
// NewAuthorization adds authorization to the clients service.
func NewAuthorization(
entityType string,
svc clients.Service,
authz smqauthz.Authorization,
@@ -65,7 +65,7 @@ func AuthorizationMiddleware(
if err := opp.Validate(); err != nil {
return nil, err
}
ram, err := rmMW.NewRoleManagerAuthorizationMiddleware(policies.ClientType, svc, authz, rolesOpPerm, callout)
ram, err := rolemw.NewAuthorization(policies.ClientType, svc, authz, rolesOpPerm, callout)
if err != nil {
return nil, err
}
+5 -1
View File
@@ -1,5 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package middleware provides middleware for SuperMQ Clients service.
// Package middleware provides authorization, logging, metrics and tracing middleware
// for SuperMQ Clients Service.
//
// For more details about tracing instrumentation for SuperMQ refer to the
// documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package middleware
+5 -4
View File
@@ -12,7 +12,7 @@ import (
"github.com/absmach/supermq/clients"
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/go-chi/chi/v5/middleware"
)
@@ -21,14 +21,15 @@ var _ clients.Service = (*loggingMiddleware)(nil)
type loggingMiddleware struct {
logger *slog.Logger
svc clients.Service
rmMW.RoleManagerLoggingMiddleware
rolemw.RoleManagerLoggingMiddleware
}
func LoggingMiddleware(svc clients.Service, logger *slog.Logger) clients.Service {
// NewLogging adds logging facilities to the core service.
func NewLogging(svc clients.Service, logger *slog.Logger) clients.Service {
return &loggingMiddleware{
logger: logger,
svc: svc,
RoleManagerLoggingMiddleware: rmMW.NewRoleManagerLoggingMiddleware("clients", svc, logger),
RoleManagerLoggingMiddleware: rolemw.NewLogging("clients", svc, logger),
}
}
+5 -5
View File
@@ -10,7 +10,7 @@ import (
"github.com/absmach/supermq/clients"
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/go-kit/kit/metrics"
)
@@ -20,16 +20,16 @@ type metricsMiddleware struct {
counter metrics.Counter
latency metrics.Histogram
svc clients.Service
rmMW.RoleManagerMetricsMiddleware
rolemw.RoleManagerMetricsMiddleware
}
// MetricsMiddleware returns a new metrics middleware wrapper.
func MetricsMiddleware(svc clients.Service, counter metrics.Counter, latency metrics.Histogram) clients.Service {
// NewMetrics returns a new metrics middleware wrapper.
func NewMetrics(svc clients.Service, counter metrics.Counter, latency metrics.Histogram) clients.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
svc: svc,
RoleManagerMetricsMiddleware: rmMW.NewRoleManagerMetricsMiddleware("clients", svc, counter, latency),
RoleManagerMetricsMiddleware: rolemw.NewMetrics("clients", svc, counter, latency),
}
}
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package tracing
package middleware
import (
"context"
@@ -9,7 +9,7 @@ import (
"github.com/absmach/supermq/clients"
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/roles"
rmTrace "github.com/absmach/supermq/pkg/roles/rolemanager/tracing"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/absmach/supermq/pkg/tracing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
@@ -20,15 +20,15 @@ var _ clients.Service = (*tracingMiddleware)(nil)
type tracingMiddleware struct {
tracer trace.Tracer
svc clients.Service
rmTrace.RoleManagerTracing
rolemw.RoleManagerTracing
}
// New returns a new group service with tracing capabilities.
func New(svc clients.Service, tracer trace.Tracer) clients.Service {
// NewTracing returns a new clients service with tracing capabilities.
func NewTracing(svc clients.Service, tracer trace.Tracer) clients.Service {
return &tracingMiddleware{
tracer: tracer,
svc: svc,
RoleManagerTracing: rmTrace.NewRoleManagerTracing("group", svc, tracer),
RoleManagerTracing: rolemw.NewTracing("group", svc, tracer),
}
}
-12
View File
@@ -1,12 +0,0 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package tracing provides tracing instrumentation for SuperMQ clients service.
//
// This package provides tracing middleware for SuperMQ clients service.
// It can be used to trace incoming requests and add tracing capabilities to
// SuperMQ clients service.
//
// For more details about tracing instrumentation for SuperMQ messaging refer
// to the documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package tracing
+4 -5
View File
@@ -17,15 +17,14 @@ import (
grpcAuthV1 "github.com/absmach/supermq/api/grpc/auth/v1"
grpcTokenV1 "github.com/absmach/supermq/api/grpc/token/v1"
"github.com/absmach/supermq/auth"
api "github.com/absmach/supermq/auth/api"
authgrpcapi "github.com/absmach/supermq/auth/api/grpc/auth"
tokengrpcapi "github.com/absmach/supermq/auth/api/grpc/token"
httpapi "github.com/absmach/supermq/auth/api/http"
"github.com/absmach/supermq/auth/cache"
"github.com/absmach/supermq/auth/hasher"
"github.com/absmach/supermq/auth/jwt"
"github.com/absmach/supermq/auth/middleware"
apostgres "github.com/absmach/supermq/auth/postgres"
"github.com/absmach/supermq/auth/tracing"
redisclient "github.com/absmach/supermq/internal/clients/redis"
smqlog "github.com/absmach/supermq/logger"
"github.com/absmach/supermq/pkg/jaeger"
@@ -245,10 +244,10 @@ func newService(db *sqlx.DB, tracer trace.Tracer, cfg config, dbConfig pgclient.
t := jwt.New([]byte(cfg.SecretKey))
svc := auth.New(keysRepo, patsRepo, nil, hasher, idProvider, t, pEvaluator, pService, cfg.AccessDuration, cfg.RefreshDuration, cfg.InvitationDuration)
svc = api.LoggingMiddleware(svc, logger)
svc = middleware.NewLogging(svc, logger)
counter, latency := prometheus.MakeMetrics("auth", "api")
svc = api.MetricsMiddleware(svc, counter, latency)
svc = tracing.New(svc, tracer)
svc = middleware.NewMetrics(svc, counter, latency)
svc = middleware.NewTracing(svc, tracer)
return svc, nil
}
+4 -4
View File
@@ -16,9 +16,9 @@ import (
"github.com/absmach/supermq"
"github.com/absmach/supermq/certs"
httpapi "github.com/absmach/supermq/certs/api"
"github.com/absmach/supermq/certs/middleware"
pki "github.com/absmach/supermq/certs/pki/openbao"
"github.com/absmach/supermq/certs/postgres"
"github.com/absmach/supermq/certs/tracing"
smqlog "github.com/absmach/supermq/logger"
smqauthn "github.com/absmach/supermq/pkg/authn"
authsvcAuthn "github.com/absmach/supermq/pkg/authn/authsvc"
@@ -193,10 +193,10 @@ func newService(db *sqlx.DB, dbConfig pgclient.Config, tracer trace.Tracer, logg
sdk := mgsdk.NewSDK(config)
repo := postgres.NewRepository(database)
svc := certs.New(sdk, repo, pkiAgent)
svc = httpapi.LoggingMiddleware(svc, logger)
svc = middleware.NewLogging(svc, logger)
counter, latency := prometheus.MakeMetrics(svcName, "api")
svc = httpapi.MetricsMiddleware(svc, counter, latency)
svc = tracing.New(svc, tracer)
svc = middleware.NewMetrics(svc, counter, latency)
svc = middleware.NewTracing(svc, tracer)
return svc
}
+4 -5
View File
@@ -26,7 +26,6 @@ import (
"github.com/absmach/supermq/channels/middleware"
"github.com/absmach/supermq/channels/postgres"
pChannels "github.com/absmach/supermq/channels/private"
"github.com/absmach/supermq/channels/tracing"
dpostgres "github.com/absmach/supermq/domains/postgres"
gpostgres "github.com/absmach/supermq/groups/postgres"
redisclient "github.com/absmach/supermq/internal/clients/redis"
@@ -359,16 +358,16 @@ func newService(ctx context.Context, db *sqlx.DB, dbConfig pgclient.Config, cach
return nil, nil, err
}
svc = tracing.New(svc, tracer)
svc = middleware.NewTracing(svc, tracer)
counter, latency := prometheus.MakeMetrics("channels", "api")
svc = middleware.MetricsMiddleware(svc, counter, latency)
svc = middleware.NewMetrics(svc, counter, latency)
svc, err = middleware.AuthorizationMiddleware(svc, repo, authz, channels.NewOperationPermissionMap(), channels.NewRolesOperationPermissionMap(), channels.NewExternalOperationPermissionMap(), callout)
svc, err = middleware.NewAuthorization(svc, repo, authz, channels.NewOperationPermissionMap(), channels.NewRolesOperationPermissionMap(), channels.NewExternalOperationPermissionMap(), callout)
if err != nil {
return nil, nil, err
}
svc = middleware.LoggingMiddleware(svc, logger)
svc = middleware.NewLogging(svc, logger)
psvc := pChannels.New(repo, cache, pe, ps, da)
return svc, psvc, err
+4 -6
View File
@@ -26,7 +26,6 @@ import (
"github.com/absmach/supermq/clients/middleware"
"github.com/absmach/supermq/clients/postgres"
pClients "github.com/absmach/supermq/clients/private"
"github.com/absmach/supermq/clients/tracing"
dpostgres "github.com/absmach/supermq/domains/postgres"
gpostgres "github.com/absmach/supermq/groups/postgres"
redisclient "github.com/absmach/supermq/internal/clients/redis"
@@ -360,17 +359,16 @@ func newService(ctx context.Context, db *sqlx.DB, dbConfig pgclient.Config, auth
return nil, nil, err
}
csvc = tracing.New(csvc, tracer)
csvc = middleware.NewTracing(csvc, tracer)
counter, latency := prometheus.MakeMetrics(svcName, "api")
csvc = middleware.MetricsMiddleware(csvc, counter, latency)
csvc = middleware.MetricsMiddleware(csvc, counter, latency)
csvc = middleware.NewMetrics(csvc, counter, latency)
csvc, err = middleware.AuthorizationMiddleware(policies.ClientType, csvc, authz, repo, clients.NewOperationPermissionMap(), clients.NewRolesOperationPermissionMap(), clients.NewExternalOperationPermissionMap(), callout)
csvc, err = middleware.NewAuthorization(policies.ClientType, csvc, authz, repo, clients.NewOperationPermissionMap(), clients.NewRolesOperationPermissionMap(), clients.NewExternalOperationPermissionMap(), callout)
if err != nil {
return nil, nil, err
}
csvc = middleware.LoggingMiddleware(csvc, logger)
csvc = middleware.NewLogging(csvc, logger)
isvc := pClients.New(repo, cache, pe, ps)
+4 -4
View File
@@ -15,7 +15,7 @@ import (
"github.com/absmach/supermq"
"github.com/absmach/supermq/coap"
httpapi "github.com/absmach/supermq/coap/api"
"github.com/absmach/supermq/coap/tracing"
"github.com/absmach/supermq/coap/middleware"
smqlog "github.com/absmach/supermq/logger"
domainsAuthz "github.com/absmach/supermq/pkg/domains/grpcclient"
"github.com/absmach/supermq/pkg/grpcclient"
@@ -181,12 +181,12 @@ func main() {
svc := coap.New(clientsClient, channelsClient, nps)
svc = tracing.New(tracer, svc)
svc = middleware.NewTracing(tracer, svc)
svc = httpapi.LoggingMiddleware(svc, logger)
svc = middleware.NewLogging(svc, logger)
counter, latency := prometheus.MakeMetrics(svcName, "api")
svc = httpapi.MetricsMiddleware(svc, counter, latency)
svc = middleware.NewMetrics(svc, counter, latency)
hs := httpserver.NewServer(ctx, cancel, svcName, httpServerConfig, httpapi.MakeHandler(cfg.InstanceID), logger)
+4 -5
View File
@@ -24,7 +24,6 @@ import (
dmw "github.com/absmach/supermq/domains/middleware"
dpostgres "github.com/absmach/supermq/domains/postgres"
"github.com/absmach/supermq/domains/private"
dtracing "github.com/absmach/supermq/domains/tracing"
redisclient "github.com/absmach/supermq/internal/clients/redis"
smqlog "github.com/absmach/supermq/logger"
smqauthn "github.com/absmach/supermq/pkg/authn"
@@ -282,17 +281,17 @@ func newDomainService(ctx context.Context, domainsRepo domainsSvc.Repository, ca
return nil, fmt.Errorf("failed to init domain event store middleware: %w", err)
}
svc, err = dmw.AuthorizationMiddleware(policies.DomainType, svc, authz, domains.NewOperationPermissionMap(), domains.NewRolesOperationPermissionMap(), callout)
svc, err = dmw.NewAuthorization(policies.DomainType, svc, authz, domains.NewOperationPermissionMap(), domains.NewRolesOperationPermissionMap(), callout)
if err != nil {
return nil, err
}
counter, latency := prometheus.MakeMetrics("domains", "api")
svc = dmw.MetricsMiddleware(svc, counter, latency)
svc = dmw.NewMetrics(svc, counter, latency)
svc = dmw.LoggingMiddleware(svc, logger)
svc = dmw.NewLogging(svc, logger)
svc = dtracing.New(svc, tracer)
svc = dmw.NewTracing(svc, tracer)
return svc, nil
}
+4 -5
View File
@@ -26,7 +26,6 @@ import (
"github.com/absmach/supermq/groups/middleware"
"github.com/absmach/supermq/groups/postgres"
pgroups "github.com/absmach/supermq/groups/private"
"github.com/absmach/supermq/groups/tracing"
smqlog "github.com/absmach/supermq/logger"
smqauthn "github.com/absmach/supermq/pkg/authn"
authsvcAuthn "github.com/absmach/supermq/pkg/authn/authsvc"
@@ -329,16 +328,16 @@ func newService(ctx context.Context, authz smqauthz.Authorization, policy polici
return nil, nil, err
}
svc, err = middleware.AuthorizationMiddleware(policies.GroupType, svc, repo, authz, groups.NewOperationPermissionMap(), groups.NewRolesOperationPermissionMap(),
svc, err = middleware.NewAuthorization(policies.GroupType, svc, repo, authz, groups.NewOperationPermissionMap(), groups.NewRolesOperationPermissionMap(),
groups.NewExternalOperationPermissionMap(), callout)
if err != nil {
return nil, nil, err
}
svc = tracing.New(svc, tracer)
svc = middleware.LoggingMiddleware(svc, logger)
svc = middleware.NewTracing(svc, tracer)
svc = middleware.NewLogging(svc, logger)
counter, latency := prometheus.MakeMetrics("groups", "api")
svc = middleware.MetricsMiddleware(svc, counter, latency)
svc = middleware.NewMetrics(svc, counter, latency)
psvc := pgroups.New(repo)
return svc, psvc, err
+2 -2
View File
@@ -240,9 +240,9 @@ func newService(pub messaging.Publisher, authn smqauthn.Authentication, cacheCfg
}
svc := adapter.NewHandler(pub, authn, clients, channels, parser, logger)
svc = handler.NewTracing(tracer, svc)
svc = handler.LoggingMiddleware(svc, logger)
svc = handler.NewLogging(svc, logger)
counter, latency := prometheus.MakeMetrics(svcName, "api")
svc = handler.MetricsMiddleware(svc, counter, latency)
svc = handler.NewMetrics(svc, counter, latency)
return svc, nil
}
+4 -4
View File
@@ -201,11 +201,11 @@ func newService(db *sqlx.DB, dbConfig pgclient.Config, authz smqauthz.Authorizat
idp := uuid.New()
svc := journal.NewService(idp, repo)
svc = middleware.AuthorizationMiddleware(svc, authz)
svc = middleware.LoggingMiddleware(svc, logger)
svc = middleware.NewAuthorization(svc, authz)
svc = middleware.NewLogging(svc, logger)
counter, latency := prometheus.MakeMetrics("journal", "journal_writer")
svc = middleware.MetricsMiddleware(svc, counter, latency)
svc = middleware.Tracing(svc, tracer)
svc = middleware.NewMetrics(svc, counter, latency)
svc = middleware.NewTracing(svc, tracer)
return svc
}
+4 -5
View File
@@ -44,7 +44,6 @@ import (
"github.com/absmach/supermq/users/hasher"
"github.com/absmach/supermq/users/middleware"
"github.com/absmach/supermq/users/postgres"
"github.com/absmach/supermq/users/tracing"
"github.com/authzed/authzed-go/v1"
"github.com/authzed/grpcutil"
"github.com/caarlos0/env/v11"
@@ -297,12 +296,12 @@ func newService(ctx context.Context, authz smqauthz.Authorization, token grpcTok
if err != nil {
return nil, err
}
svc = middleware.AuthorizationMiddleware(svc, authz, c.SelfRegister)
svc = middleware.NewAuthorization(svc, authz, c.SelfRegister)
svc = tracing.New(svc, tracer)
svc = middleware.LoggingMiddleware(svc, logger)
svc = middleware.NewTracing(svc, tracer)
svc = middleware.NewLogging(svc, logger)
counter, latency := prometheus.MakeMetrics(svcName, "api")
svc = middleware.MetricsMiddleware(svc, counter, latency)
svc = middleware.NewMetrics(svc, counter, latency)
userID, err := createAdmin(ctx, c, repo, hsr, svc)
if err != nil {
+4 -4
View File
@@ -35,7 +35,7 @@ import (
"github.com/absmach/supermq/pkg/uuid"
"github.com/absmach/supermq/ws"
httpapi "github.com/absmach/supermq/ws/api"
"github.com/absmach/supermq/ws/tracing"
"github.com/absmach/supermq/ws/middleware"
"github.com/caarlos0/env/v11"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
@@ -239,10 +239,10 @@ func main() {
func newService(clientsClient grpcClientsV1.ClientsServiceClient, channels grpcChannelsV1.ChannelsServiceClient, authn authn.Authentication, nps messaging.PubSub, logger *slog.Logger, tracer trace.Tracer) ws.Service {
svc := ws.New(clientsClient, channels, authn, nps)
svc = tracing.New(tracer, svc)
svc = httpapi.LoggingMiddleware(svc, logger)
svc = middleware.NewTracing(tracer, svc)
svc = middleware.NewLogging(svc, logger)
counter, latency := prometheus.MakeMetrics("ws_adapter", "api")
svc = httpapi.MetricsMiddleware(svc, counter, latency)
svc = middleware.NewMetrics(svc, counter, latency)
return svc
}
+2 -2
View File
@@ -28,7 +28,7 @@ The service is configured using the environment variables presented in the follo
| SMQ_MESSAGE_BROKER_URL | Message broker instance URL | <amqp://guest:guest@rabbitmq:5672/> |
| SMQ_JAEGER_URL | Jaeger server URL | <http://localhost:4318/v1/traces> |
| SMQ_JAEGER_TRACE_RATIO | Jaeger sampling ratio | 1.0 |
| SMQ_SEND_TELEMETRY | Send telemetry to magistrala call home server | true |
| SMQ_SEND_TELEMETRY | Send telemetry to supermq call home server | true |
| SMQ_COAP_ADAPTER_INSTANCE_ID | CoAP adapter instance ID | "" |
## Deployment
@@ -42,7 +42,7 @@ To start the service outside of the container, execute the following shell scrip
# download the latest version of the service
git clone https://github.com/absmach/supermq
cd magistrala
cd supermq
# compile the http
make coap
+9
View File
@@ -0,0 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package middleware provides tracing, logging and metrics middleware
// for SuperMQ COAP service.
//
// For more details about tracing instrumentation for SuperMQ messaging refer
// to the documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package middleware
@@ -3,7 +3,7 @@
//go:build !test
package api
package middleware
import (
"context"
@@ -21,8 +21,8 @@ type loggingMiddleware struct {
svc coap.Service
}
// LoggingMiddleware adds logging facilities to the adapter.
func LoggingMiddleware(svc coap.Service, logger *slog.Logger) coap.Service {
// NewLogging adds logging facilities to the adapter.
func NewLogging(svc coap.Service, logger *slog.Logger) coap.Service {
return &loggingMiddleware{logger, svc}
}
@@ -3,7 +3,7 @@
//go:build !test
package api
package middleware
import (
"context"
@@ -22,8 +22,8 @@ type metricsMiddleware struct {
svc coap.Service
}
// MetricsMiddleware instruments adapter by tracking request count and latency.
func MetricsMiddleware(svc coap.Service, counter metrics.Counter, latency metrics.Histogram) coap.Service {
// NewMetrics instruments adapter by tracking request count and latency.
func NewMetrics(svc coap.Service, counter metrics.Counter, latency metrics.Histogram) coap.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package tracing
package middleware
import (
"context"
@@ -28,8 +28,8 @@ type tracingServiceMiddleware struct {
svc coap.Service
}
// New creates a new instance of TracingServiceMiddleware that wraps an existing CoAP service with tracing capabilities.
func New(tracer trace.Tracer, svc coap.Service) coap.Service {
// NewTracing creates a new instance of TracingServiceMiddleware that wraps an existing CoAP service with tracing capabilities.
func NewTracing(tracer trace.Tracer, svc coap.Service) coap.Service {
return &tracingServiceMiddleware{
tracer: tracer,
svc: svc,
-12
View File
@@ -1,12 +0,0 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package tracing provides tracing instrumentation for SuperMQ WebSocket adapter service.
//
// This package provides tracing middleware for SuperMQ WebSocket adapter service.
// It can be used to trace incoming requests and add tracing capabilities to
// SuperMQ WebSocket adapter service.
//
// For more details about tracing instrumentation for SuperMQ messaging refer
// to the documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package tracing
+1 -1
View File
@@ -2,5 +2,5 @@
// SPDX-License-Identifier: Apache-2.0
// Package cache contains the domain concept definitions needed to
// support Magistrala Domains cache service functionality.
// support SuperMQ Domains cache service functionality.
package cache
+5 -5
View File
@@ -18,7 +18,7 @@ import (
svcerr "github.com/absmach/supermq/pkg/errors/service"
"github.com/absmach/supermq/pkg/policies"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/absmach/supermq/pkg/svcutil"
)
@@ -32,11 +32,11 @@ type authorizationMiddleware struct {
authz smqauthz.Authorization
opp svcutil.OperationPerm
callout callout.Callout
rmMW.RoleManagerAuthorizationMiddleware
rolemw.RoleManagerAuthorizationMiddleware
}
// AuthorizationMiddleware adds authorization to the clients service.
func AuthorizationMiddleware(entityType string, svc domains.Service, authz smqauthz.Authorization, domainsOpPerm, rolesOpPerm map[svcutil.Operation]svcutil.Permission, callout callout.Callout) (domains.Service, error) {
// NewAuthorization adds authorization to the domains service.
func NewAuthorization(entityType string, svc domains.Service, authz smqauthz.Authorization, domainsOpPerm, rolesOpPerm map[svcutil.Operation]svcutil.Permission, callout callout.Callout) (domains.Service, error) {
opp := domains.NewOperationPerm()
if err := opp.AddOperationPermissionMap(domainsOpPerm); err != nil {
return nil, err
@@ -45,7 +45,7 @@ func AuthorizationMiddleware(entityType string, svc domains.Service, authz smqau
return nil, err
}
ram, err := rmMW.NewRoleManagerAuthorizationMiddleware(entityType, svc, authz, rolesOpPerm, callout)
ram, err := rolemw.NewAuthorization(entityType, svc, authz, rolesOpPerm, callout)
if err != nil {
return nil, err
}
+9
View File
@@ -0,0 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package middleware provides authorization, logging, metrics and tracing middleware
// for SuperMQ Domains service.
//
// For more details about tracing instrumentation for SuperMQ refer to the
// documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package middleware
+5 -5
View File
@@ -13,7 +13,7 @@ import (
"github.com/absmach/supermq/domains"
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/go-chi/chi/v5/middleware"
)
@@ -22,12 +22,12 @@ var _ domains.Service = (*loggingMiddleware)(nil)
type loggingMiddleware struct {
logger *slog.Logger
svc domains.Service
rmMW.RoleManagerLoggingMiddleware
rolemw.RoleManagerLoggingMiddleware
}
// LoggingMiddleware adds logging facilities to the core service.
func LoggingMiddleware(svc domains.Service, logger *slog.Logger) domains.Service {
rmlm := rmMW.NewRoleManagerLoggingMiddleware("domains", svc, logger)
// NewLogging adds logging facilities to the core service.
func NewLogging(svc domains.Service, logger *slog.Logger) domains.Service {
rmlm := rolemw.NewLogging("domains", svc, logger)
return &loggingMiddleware{
logger: logger,
svc: svc,
+5 -5
View File
@@ -12,7 +12,7 @@ import (
"github.com/absmach/supermq/domains"
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/go-kit/kit/metrics"
)
@@ -22,12 +22,12 @@ type metricsMiddleware struct {
counter metrics.Counter
latency metrics.Histogram
svc domains.Service
rmMW.RoleManagerMetricsMiddleware
rolemw.RoleManagerMetricsMiddleware
}
// MetricsMiddleware instruments core service by tracking request count and latency.
func MetricsMiddleware(svc domains.Service, counter metrics.Counter, latency metrics.Histogram) domains.Service {
rmmw := rmMW.NewRoleManagerMetricsMiddleware("domains", svc, counter, latency)
// NewMetrics instruments core service by tracking request count and latency.
func NewMetrics(svc domains.Service, counter metrics.Counter, latency metrics.Histogram) domains.Service {
rmmw := rolemw.NewMetrics("domains", svc, counter, latency)
return &metricsMiddleware{
counter: counter,
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package tracing
package middleware
import (
"context"
@@ -9,7 +9,7 @@ import (
"github.com/absmach/supermq/domains"
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/roles"
rmTrace "github.com/absmach/supermq/pkg/roles/rolemanager/tracing"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/absmach/supermq/pkg/tracing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
@@ -20,12 +20,12 @@ var _ domains.Service = (*tracingMiddleware)(nil)
type tracingMiddleware struct {
tracer trace.Tracer
svc domains.Service
rmTrace.RoleManagerTracing
rolemw.RoleManagerTracing
}
// New returns a new group service with tracing capabilities.
func New(svc domains.Service, tracer trace.Tracer) domains.Service {
return &tracingMiddleware{tracer, svc, rmTrace.NewRoleManagerTracing("domain", svc, tracer)}
// NewTracing returns a new domains service with tracing capabilities.
func NewTracing(svc domains.Service, tracer trace.Tracer) domains.Service {
return &tracingMiddleware{tracer, svc, rolemw.NewTracing("domain", svc, tracer)}
}
func (tm *tracingMiddleware) CreateDomain(ctx context.Context, session authn.Session, d domains.Domain) (domains.Domain, []roles.RoleProvision, error) {
+12 -12
View File
@@ -48,8 +48,8 @@ func TestSaveDomain(t *testing.T) {
Metadata: map[string]any{
"test": "test",
},
CreatedAt: time.Now().UTC().Truncate(time.Millisecond),
UpdatedAt: time.Now().UTC().Truncate(time.Millisecond),
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
UpdatedAt: time.Now().UTC().Truncate(time.Microsecond),
CreatedBy: userID,
UpdatedBy: userID,
Status: domains.EnabledStatus,
@@ -66,8 +66,8 @@ func TestSaveDomain(t *testing.T) {
Metadata: map[string]any{
"test": "test",
},
CreatedAt: time.Now().UTC().Truncate(time.Millisecond),
UpdatedAt: time.Now().UTC().Truncate(time.Millisecond),
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
UpdatedAt: time.Now().UTC().Truncate(time.Microsecond),
CreatedBy: userID,
UpdatedBy: userID,
Status: domains.EnabledStatus,
@@ -84,8 +84,8 @@ func TestSaveDomain(t *testing.T) {
Metadata: map[string]any{
"test": "test",
},
CreatedAt: time.Now().UTC().Truncate(time.Millisecond),
UpdatedAt: time.Now().UTC().Truncate(time.Millisecond),
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
UpdatedAt: time.Now().UTC().Truncate(time.Microsecond),
CreatedBy: userID,
UpdatedBy: userID,
Status: domains.EnabledStatus,
@@ -159,8 +159,8 @@ func TestRetrieveByID(t *testing.T) {
},
CreatedBy: userID,
UpdatedBy: userID,
CreatedAt: time.Now().UTC().Truncate(time.Millisecond),
UpdatedAt: time.Now().UTC().Truncate(time.Millisecond),
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
UpdatedAt: time.Now().UTC().Truncate(time.Microsecond),
Status: domains.EnabledStatus,
}
@@ -221,8 +221,8 @@ func TestRetrieveByRoute(t *testing.T) {
},
CreatedBy: userID,
UpdatedBy: userID,
CreatedAt: time.Now().UTC().Truncate(time.Millisecond),
UpdatedAt: time.Now().UTC().Truncate(time.Millisecond),
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
UpdatedAt: time.Now().UTC().Truncate(time.Microsecond),
Status: domains.EnabledStatus,
}
@@ -285,7 +285,7 @@ func TestRetrieveAllByIDs(t *testing.T) {
CreatedBy: userID,
UpdatedBy: userID,
Status: domains.EnabledStatus,
CreatedAt: time.Now().UTC().Truncate(time.Millisecond),
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
}
if i%5 == 0 {
domain.Status = domains.DisabledStatus
@@ -709,7 +709,7 @@ func TestListDomains(t *testing.T) {
CreatedBy: userID,
UpdatedBy: userID,
Status: domains.EnabledStatus,
CreatedAt: time.Now().UTC().Truncate(time.Millisecond),
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
}
if i%5 == 0 {
domain.Status = domains.DisabledStatus
+5 -5
View File
@@ -18,7 +18,7 @@ import (
svcerr "github.com/absmach/supermq/pkg/errors/service"
"github.com/absmach/supermq/pkg/policies"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/absmach/supermq/pkg/svcutil"
)
@@ -51,11 +51,11 @@ type authorizationMiddleware struct {
opp svcutil.OperationPerm
extOpp svcutil.ExternalOperationPerm
callout callout.Callout
rmMW.RoleManagerAuthorizationMiddleware
rolemw.RoleManagerAuthorizationMiddleware
}
// AuthorizationMiddleware adds authorization to the clients service.
func AuthorizationMiddleware(entityType string,
// NewAuthorization adds authorization to the groups service.
func NewAuthorization(entityType string,
svc groups.Service,
repo groups.Repository,
authz smqauthz.Authorization,
@@ -79,7 +79,7 @@ func AuthorizationMiddleware(entityType string,
return nil, err
}
ram, err := rmMW.NewRoleManagerAuthorizationMiddleware(entityType, svc, authz, rolesOpPerm, callout)
ram, err := rolemw.NewAuthorization(entityType, svc, authz, rolesOpPerm, callout)
if err != nil {
return nil, err
}
+5 -1
View File
@@ -1,5 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package middleware provides middleware for SuperMQ Groups service.
// Package middleware provides authorization, logging, metrics and tracing middleware
// for SuperMQ Domains service.
//
// For more details about tracing instrumentation for SuperMQ refer to the
// documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package middleware
+5 -5
View File
@@ -11,7 +11,7 @@ import (
"github.com/absmach/supermq/groups"
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/go-chi/chi/v5/middleware"
)
@@ -20,12 +20,12 @@ var _ groups.Service = (*loggingMiddleware)(nil)
type loggingMiddleware struct {
logger *slog.Logger
svc groups.Service
rmMW.RoleManagerLoggingMiddleware
rolemw.RoleManagerLoggingMiddleware
}
// LoggingMiddleware adds logging facilities to the groups service.
func LoggingMiddleware(svc groups.Service, logger *slog.Logger) groups.Service {
return &loggingMiddleware{logger, svc, rmMW.NewRoleManagerLoggingMiddleware("groups", svc, logger)}
// NewLogging adds logging facilities to the groups service.
func NewLogging(svc groups.Service, logger *slog.Logger) groups.Service {
return &loggingMiddleware{logger, svc, rolemw.NewLogging("groups", svc, logger)}
}
// CreateGroup logs the create_group request. It logs the group name, id and token and the time it took to complete the request.
+5 -5
View File
@@ -10,7 +10,7 @@ import (
"github.com/absmach/supermq/groups"
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/go-kit/kit/metrics"
)
@@ -20,12 +20,12 @@ type metricsMiddleware struct {
counter metrics.Counter
latency metrics.Histogram
svc groups.Service
rmMW.RoleManagerMetricsMiddleware
rolemw.RoleManagerMetricsMiddleware
}
// MetricsMiddleware instruments policies service by tracking request count and latency.
func MetricsMiddleware(svc groups.Service, counter metrics.Counter, latency metrics.Histogram) groups.Service {
rmm := rmMW.NewRoleManagerMetricsMiddleware("group", svc, counter, latency)
// NewMetrics instruments policies service by tracking request count and latency.
func NewMetrics(svc groups.Service, counter metrics.Counter, latency metrics.Histogram) groups.Service {
rmm := rolemw.NewMetrics("group", svc, counter, latency)
return &metricsMiddleware{
counter: counter,
latency: latency,
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package tracing
package middleware
import (
"context"
@@ -10,7 +10,7 @@ import (
"github.com/absmach/supermq/groups"
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/roles"
rmTrace "github.com/absmach/supermq/pkg/roles/rolemanager/tracing"
rolemw "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
"github.com/absmach/supermq/pkg/tracing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
@@ -21,12 +21,12 @@ var _ groups.Service = (*tracingMiddleware)(nil)
type tracingMiddleware struct {
tracer trace.Tracer
svc groups.Service
rmTrace.RoleManagerTracing
rolemw.RoleManagerTracing
}
// New returns a new group service with tracing capabilities.
func New(svc groups.Service, tracer trace.Tracer) groups.Service {
return &tracingMiddleware{tracer, svc, rmTrace.NewRoleManagerTracing("group", svc, tracer)}
// NewTracing returns a new groups service with tracing capabilities.
func NewTracing(svc groups.Service, tracer trace.Tracer) groups.Service {
return &tracingMiddleware{tracer, svc, rolemw.NewTracing("group", svc, tracer)}
}
// CreateGroup traces the "CreateGroup" operation of the wrapped groups.Service.
-12
View File
@@ -1,12 +0,0 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package tracing provides tracing instrumentation for SuperMQ Users Groups service.
//
// This package provides tracing middleware for SuperMQ Users Groups service.
// It can be used to trace incoming requests and add tracing capabilities to
// SuperMQ Users Groups service.
//
// For more details about tracing instrumentation for SuperMQ messaging refer
// to the documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package tracing
+2 -2
View File
@@ -23,8 +23,8 @@ type authorizationMiddleware struct {
authz smqauthz.Authorization
}
// AuthorizationMiddleware adds authorization to the journal service.
func AuthorizationMiddleware(svc journal.Service, authz smqauthz.Authorization) journal.Service {
// NewAuthorization adds authorization to the journal service.
func NewAuthorization(svc journal.Service, authz smqauthz.Authorization) journal.Service {
return &authorizationMiddleware{
svc: svc,
authz: authz,
+5 -2
View File
@@ -1,6 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package middleware provides middleware for the journal service.
// This is logging, metrics, and tracing middleware.
// Package middleware provides tracing, logging and metrics middleware
// for SuperMQ Journal service.
//
// For more details about tracing instrumentation for SuperMQ refer to the
// documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package middleware
+2 -2
View File
@@ -20,8 +20,8 @@ type loggingMiddleware struct {
service journal.Service
}
// LoggingMiddleware adds logging facilities to the adapter.
func LoggingMiddleware(service journal.Service, logger *slog.Logger) journal.Service {
// NewLogging adds logging facilities to the adapter.
func NewLogging(service journal.Service, logger *slog.Logger) journal.Service {
return &loggingMiddleware{
logger: logger,
service: service,
+2 -3
View File
@@ -20,9 +20,8 @@ type metricsMiddleware struct {
service journal.Service
}
// MetricsMiddleware returns new message repository
// with Save method wrapped to expose metrics.
func MetricsMiddleware(service journal.Service, counter metrics.Counter, latency metrics.Histogram) journal.Service {
// NewMetrics returns new message repository with Save method wrapped to expose metrics.
func NewMetrics(service journal.Service, counter metrics.Counter, latency metrics.Histogram) journal.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
+2 -1
View File
@@ -20,7 +20,8 @@ type tracing struct {
svc journal.Service
}
func Tracing(svc journal.Service, tracer trace.Tracer) journal.Service {
// NewTracing returns a new journal service with tracing capabilities.
func NewTracing(svc journal.Service, tracer trace.Tracer) journal.Service {
return &tracing{tracer, svc}
}
+2 -2
View File
@@ -21,8 +21,8 @@ type loggingMiddleware struct {
svc session.Handler
}
// LoggingMiddleware adds logging facilities to the adapter.
func LoggingMiddleware(svc session.Handler, logger *slog.Logger) session.Handler {
// NewLogging adds logging facilities to the adapter.
func NewLogging(svc session.Handler, logger *slog.Logger) session.Handler {
return &loggingMiddleware{logger, svc}
}
+2 -2
View File
@@ -21,8 +21,8 @@ type metricsMiddleware struct {
svc session.Handler
}
// MetricsMiddleware instruments adapter by tracking request count and latency.
func MetricsMiddleware(svc session.Handler, counter metrics.Counter, latency metrics.Histogram) session.Handler {
// NewMetrics instruments adapter by tracking request count and latency.
func NewMetrics(svc session.Handler, counter metrics.Counter, latency metrics.Histogram) session.Handler {
return &metricsMiddleware{
counter: counter,
latency: latency,
+1 -1
View File
@@ -29,7 +29,7 @@ type handlerMiddleware struct {
tracer trace.Tracer
}
// NewHandler creates a new session.Handler middleware with tracing.
// NewTracing creates a new session.Handler middleware with tracing.
func NewTracing(tracer trace.Tracer, handler session.Handler) session.Handler {
return &handlerMiddleware{
tracer: tracer,
-4
View File
@@ -1,4 +0,0 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package rolemanager
@@ -27,8 +27,8 @@ type RoleManagerAuthorizationMiddleware struct {
opp svcutil.OperationPerm
}
// AuthorizationMiddleware adds authorization to the clients service.
func NewRoleManagerAuthorizationMiddleware(entityType string, svc roles.RoleManager, authz smqauthz.Authorization, opPerm map[svcutil.Operation]svcutil.Permission, callout callout.Callout) (RoleManagerAuthorizationMiddleware, error) {
// NewAuthorization adds authorization for role related methods to the core service.
func NewAuthorization(entityType string, svc roles.RoleManager, authz smqauthz.Authorization, opPerm map[svcutil.Operation]svcutil.Permission, callout callout.Callout) (RoleManagerAuthorizationMiddleware, error) {
opp := roles.NewOperationPerm()
if err := opp.AddOperationPermissionMap(opPerm); err != nil {
return RoleManagerAuthorizationMiddleware{}, err
+9
View File
@@ -0,0 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package middleware provides authorization, logging, metrics and tracing middleware
// for SuperMQ RoleManager service.
//
// For more details about tracing instrumentation for SuperMQ refer to the
// documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package middleware
+2 -1
View File
@@ -23,7 +23,8 @@ type RoleManagerLoggingMiddleware struct {
logger *slog.Logger
}
func NewRoleManagerLoggingMiddleware(svcName string, svc roles.RoleManager, logger *slog.Logger) RoleManagerLoggingMiddleware {
// NewLogging adds logging facilities to the core service.
func NewLogging(svcName string, svc roles.RoleManager, logger *slog.Logger) RoleManagerLoggingMiddleware {
return RoleManagerLoggingMiddleware{
svcName: svcName,
svc: svc,
+2 -1
View File
@@ -22,7 +22,8 @@ type RoleManagerMetricsMiddleware struct {
latency metrics.Histogram
}
func NewRoleManagerMetricsMiddleware(svcName string, svc roles.RoleManager, counter metrics.Counter, latency metrics.Histogram) RoleManagerMetricsMiddleware {
// NewMetrics instruments core service by tracking request count and latency.
func NewMetrics(svcName string, svc roles.RoleManager, counter metrics.Counter, latency metrics.Histogram) RoleManagerMetricsMiddleware {
return RoleManagerMetricsMiddleware{
svcName: svcName,
svc: svc,
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package tracing
package middleware
import (
"context"
@@ -19,7 +19,8 @@ type RoleManagerTracing struct {
tracer trace.Tracer
}
func NewRoleManagerTracing(svcName string, svc roles.RoleManager, tracer trace.Tracer) RoleManagerTracing {
// NewTracing adds tracing facilities to the core service.
func NewTracing(svcName string, svc roles.RoleManager, tracer trace.Tracer) RoleManagerTracing {
return RoleManagerTracing{svcName, svc, tracer}
}
+2 -2
View File
@@ -24,8 +24,8 @@ type authorizationMiddleware struct {
selfRegister bool
}
// AuthorizationMiddleware adds authorization to the clients service.
func AuthorizationMiddleware(svc users.Service, authz smqauthz.Authorization, selfRegister bool) users.Service {
// NewAuthorization adds authorization to the users service.
func NewAuthorization(svc users.Service, authz smqauthz.Authorization, selfRegister bool) users.Service {
return &authorizationMiddleware{svc: svc, authz: authz, selfRegister: selfRegister}
}
+5 -1
View File
@@ -1,5 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package middleware provides middleware for SuperMQ Users service.
// Package middleware provides authorization, logging, metrics and tracing middleware
// for SuperMQ Users Service.
//
// For more details about tracing instrumentation for SuperMQ refer to the
// documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package middleware
+2 -2
View File
@@ -21,8 +21,8 @@ type loggingMiddleware struct {
svc users.Service
}
// LoggingMiddleware adds logging facilities to the users service.
func LoggingMiddleware(svc users.Service, logger *slog.Logger) users.Service {
// NewLogging adds logging facilities to the users service.
func NewLogging(svc users.Service, logger *slog.Logger) users.Service {
return &loggingMiddleware{logger, svc}
}
+2 -2
View File
@@ -21,8 +21,8 @@ type metricsMiddleware struct {
svc users.Service
}
// MetricsMiddleware instruments policies service by tracking request count and latency.
func MetricsMiddleware(svc users.Service, counter metrics.Counter, latency metrics.Histogram) users.Service {
// NewMetrics instruments policies service by tracking request count and latency.
func NewMetrics(svc users.Service, counter metrics.Counter, latency metrics.Histogram) users.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package tracing
package middleware
import (
"context"
@@ -21,8 +21,8 @@ type tracingMiddleware struct {
svc users.Service
}
// New returns a new group service with tracing capabilities.
func New(svc users.Service, tracer trace.Tracer) users.Service {
// NewTracing returns a new users service with tracing capabilities.
func NewTracing(svc users.Service, tracer trace.Tracer) users.Service {
return &tracingMiddleware{tracer, svc}
}
+8 -8
View File
@@ -323,7 +323,7 @@ func TestRetrieveAll(t *testing.T) {
Metadata: users.Metadata{},
Status: users.EnabledStatus,
Tags: []string{"tag1"},
CreatedAt: time.Now().UTC().Truncate(time.Millisecond),
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
}
if i%50 == 0 {
user.Metadata = map[string]any{
@@ -1127,7 +1127,7 @@ func TestUpdateRole(t *testing.T) {
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
updatedAt := time.Now().UTC().Truncate(time.Millisecond)
updatedAt := time.Now().UTC().Truncate(time.Microsecond)
updatedBy := testsutil.GenerateUUID(t)
c.userReq.UpdatedAt = updatedAt
c.userReq.UpdatedBy = updatedBy
@@ -1199,7 +1199,7 @@ func TestUpdateEmail(t *testing.T) {
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
updatedAt := time.Now().UTC().Truncate(time.Millisecond)
updatedAt := time.Now().UTC().Truncate(time.Microsecond)
updatedBy := testsutil.GenerateUUID(t)
c.userReq.UpdatedAt = updatedAt
c.userReq.UpdatedBy = updatedBy
@@ -1453,7 +1453,7 @@ func TestUpdate(t *testing.T) {
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
updatedAt := time.Now().UTC().Truncate(time.Millisecond)
updatedAt := time.Now().UTC().Truncate(time.Microsecond)
updatedBy := testsutil.GenerateUUID(t)
c.userReq.UpdatedAt = &updatedAt
c.userReq.UpdatedBy = &updatedBy
@@ -1548,7 +1548,7 @@ func TestUpdateUsername(t *testing.T) {
}
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
c.user.UpdatedAt = time.Now().UTC().Truncate(time.Millisecond)
c.user.UpdatedAt = time.Now().UTC().Truncate(time.Microsecond)
c.user.UpdatedBy = testsutil.GenerateUUID(t)
expected, err := repo.UpdateUsername(context.Background(), c.user)
assert.True(t, errors.Contains(err, c.err), fmt.Sprintf("expected %s to contain %s\n", err, c.err))
@@ -1614,7 +1614,7 @@ func TestUpdateSecret(t *testing.T) {
}
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
c.user.UpdatedAt = time.Now().UTC().Truncate(time.Millisecond)
c.user.UpdatedAt = time.Now().UTC().Truncate(time.Microsecond)
c.user.UpdatedBy = testsutil.GenerateUUID(t)
_, err := repo.UpdateSecret(context.Background(), c.user)
assert.True(t, errors.Contains(err, c.err), fmt.Sprintf("expected %s to contain %s\n", err, c.err))
@@ -1677,7 +1677,7 @@ func TestChangeStatus(t *testing.T) {
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
c.user.UpdatedAt = time.Now().UTC().Truncate(time.Millisecond)
c.user.UpdatedAt = time.Now().UTC().Truncate(time.Microsecond)
c.user.UpdatedBy = testsutil.GenerateUUID(t)
expected, err := repo.ChangeStatus(context.Background(), c.user)
assert.True(t, errors.Contains(err, c.err), fmt.Sprintf("expected %s to contain %s\n", err, c.err))
@@ -2086,7 +2086,7 @@ func generateUser(t *testing.T, status users.Status, repo users.Repository) user
"name": namesgen.Generate(),
},
Status: status,
CreatedAt: time.Now().UTC().Truncate(time.Millisecond),
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
}
user, err := repo.Save(context.Background(), usr)
require.Nil(t, err, fmt.Sprintf("add new user: expected nil got %s\n", err))
-12
View File
@@ -1,12 +0,0 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package tracing provides tracing instrumentation for SuperMQ Users service.
//
// This package provides tracing middleware for SuperMQ Users service.
// It can be used to trace incoming requests and add tracing capabilities to
// SuperMQ Users service.
//
// For more details about tracing instrumentation for SuperMQ messaging refer
// to the documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package tracing
+9
View File
@@ -0,0 +1,9 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package middleware provides logging, metrics and tracing middleware
// for SuperMQ WebSockets service.
//
// For more details about tracing instrumentation for SuperMQ refer to the
// documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package middleware
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package api
package middleware
import (
"context"
@@ -19,8 +19,8 @@ type loggingMiddleware struct {
svc ws.Service
}
// LoggingMiddleware adds logging facilities to the websocket service.
func LoggingMiddleware(svc ws.Service, logger *slog.Logger) ws.Service {
// NewLogging adds logging facilities to the websocket service.
func NewLogging(svc ws.Service, logger *slog.Logger) ws.Service {
return &loggingMiddleware{logger, svc}
}
@@ -3,7 +3,7 @@
//go:build !test
package api
package middleware
import (
"context"
@@ -22,8 +22,8 @@ type metricsMiddleware struct {
svc ws.Service
}
// MetricsMiddleware instruments adapter by tracking request count and latency.
func MetricsMiddleware(svc ws.Service, counter metrics.Counter, latency metrics.Histogram) ws.Service {
// NewMetrics instruments adapter by tracking request count and latency.
func NewMetrics(svc ws.Service, counter metrics.Counter, latency metrics.Histogram) ws.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
@@ -1,7 +1,7 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package tracing
package middleware
import (
"context"
@@ -23,8 +23,8 @@ type tracingMiddleware struct {
svc ws.Service
}
// New returns a new websocket service with tracing capabilities.
func New(tracer trace.Tracer, svc ws.Service) ws.Service {
// NewTracing returns a new websocket service with tracing capabilities.
func NewTracing(tracer trace.Tracer, svc ws.Service) ws.Service {
return &tracingMiddleware{
tracer: tracer,
svc: svc,
-12
View File
@@ -1,12 +0,0 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
// Package tracing provides tracing instrumentation for SuperMQ WebSocket adapter service.
//
// This package provides tracing middleware for SuperMQ WebSocket adapter service.
// It can be used to trace incoming requests and add tracing capabilities to
// SuperMQ WebSocket adapter service.
//
// For more details about tracing instrumentation for SuperMQ messaging refer
// to the documentation at https://docs.supermq.abstractmachines.fr/tracing/.
package tracing