Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
update middleware
Browse files Browse the repository at this point in the history
Signed-off-by: Musilah <[email protected]>
  • Loading branch information
Musilah committed Jan 15, 2024
1 parent 077c0f1 commit c6782cd
Show file tree
Hide file tree
Showing 14 changed files with 1,490 additions and 316 deletions.
220 changes: 181 additions & 39 deletions bootstrap/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@ func LoggingMiddleware(svc bootstrap.Service, logger slog.Logger) bootstrap.Serv
// If the request fails, it logs the error.
func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstrap.Config) (saved bootstrap.Config, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method add using token %s with thing %s took %s to complete", token, saved.ThingID, time.Since(begin))
message := "Method add completed"
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
lm.logger.Warn(
fmt.Sprintf("%s with error.", message),
slog.String("method", "add"),
slog.String("error", err.Error()),
slog.String("duration", time.Since(begin).String()),
)
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
lm.logger.Info(
fmt.Sprintf("%s without errors.", message),
slog.String("method", "add"),
slog.String("thing_id", saved.ThingID),
slog.String("token", token),
slog.String("duration", time.Since(begin).String()),
)
}(time.Now())

return lm.svc.Add(ctx, token, cfg)
Expand All @@ -45,12 +56,23 @@ func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstra
// If the request fails, it logs the error.
func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved bootstrap.Config, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method view using token %s with thing %s took %s to complete", token, saved.ThingID, time.Since(begin))
message := "Method view completed"
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
lm.logger.Warn(
fmt.Sprintf("%s with error.", message),
slog.String("method", "view"),
slog.String("error", err.Error()),
slog.String("duration", time.Since(begin).String()),
)
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
lm.logger.Info(
fmt.Sprintf("%s without errors.", message),
slog.String("method", "view"),
slog.String("thing_id", saved.ThingID),
slog.String("token", token),
slog.String("duration", time.Since(begin).String()),
)
}(time.Now())

return lm.svc.View(ctx, token, id)
Expand All @@ -60,12 +82,23 @@ func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved
// If the request fails, it logs the error.
func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg bootstrap.Config) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method update using token %s with thing %s took %s to complete", token, cfg.ThingID, time.Since(begin))
message := "Method update completed"
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
lm.logger.Warn(
fmt.Sprintf("%s with error: %s.", message, err),
slog.String("method", "update"),
slog.String("error", err.Error()),
slog.String("duration", time.Since(begin).String()),
)
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
lm.logger.Info(
fmt.Sprintf("%s without errors.", message),
slog.String("method", "update"),
slog.String("thing_id", cfg.ThingID),
slog.String("token", token),
slog.String("duration", time.Since(begin).String()),
)
}(time.Now())

return lm.svc.Update(ctx, token, cfg)
Expand All @@ -75,12 +108,23 @@ func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg boots
// If the request fails, it logs the error.
func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, clientCert, clientKey, caCert string) (cfg bootstrap.Config, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method update_cert using token %s with thing id %s took %s to complete", token, thingID, time.Since(begin))
message := "Method update_cert completed"
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
lm.logger.Warn(
fmt.Sprintf("%s with error.", message),
slog.String("method", "update_cert"),
slog.String("error", err.Error()),
slog.String("duration", time.Since(begin).String()),
)
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
lm.logger.Info(
fmt.Sprintf("%s without errors.", message),
slog.String("method", "update_cert"),
slog.String("thing_id", thingID),
slog.String("token", token),
slog.String("duration", time.Since(begin).String()),
)
}(time.Now())

return lm.svc.UpdateCert(ctx, token, thingID, clientCert, clientKey, caCert)
Expand All @@ -90,12 +134,23 @@ func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, cli
// If the request fails, it logs the error.
func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id string, connections []string) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method update_connections using token %s with thing %s took %s to complete", token, id, time.Since(begin))
message := "Method update_connections completed"
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
lm.logger.Warn(
fmt.Sprintf("%s with error: %s.", message, err),
slog.String("method", "update_connections"),
slog.String("error", err.Error()),
slog.String("duration", time.Since(begin).String()),
)
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
lm.logger.Info(
fmt.Sprintf("%s without errors.", message),
slog.String("method", "update_connections"),
slog.String("thing_id", id),
slog.String("token", token),
slog.String("duration", time.Since(begin).String()),
)
}(time.Now())

return lm.svc.UpdateConnections(ctx, token, id, connections)
Expand All @@ -105,12 +160,24 @@ func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id st
// If the request fails, it logs the error.
func (lm *loggingMiddleware) List(ctx context.Context, token string, filter bootstrap.Filter, offset, limit uint64) (res bootstrap.ConfigsPage, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method list using token %s with offset %d and limit %d took %s to complete", token, offset, limit, time.Since(begin))
message := "Method list completed"
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
lm.logger.Warn(
fmt.Sprintf("%s with error.", message),
slog.String("method", "list"),
slog.String("error", err.Error()),
slog.String("duration", time.Since(begin).String()),
)
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
lm.logger.Info(
fmt.Sprintf("%s without errors.", message),
slog.String("method", "list"),
slog.String("token", token),
slog.Uint64("offset", offset),
slog.Uint64("limit", limit),
slog.String("duration", time.Since(begin).String()),
)
}(time.Now())

return lm.svc.List(ctx, token, filter, offset, limit)
Expand All @@ -120,90 +187,165 @@ func (lm *loggingMiddleware) List(ctx context.Context, token string, filter boot
// If the request fails, it logs the error.
func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method remove using token %s with thing %s took %s to complete", token, id, time.Since(begin))
message := "Method remove completed"
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
lm.logger.Warn(
fmt.Sprintf("%s with error: %s.", message, err),
slog.String("method", "remove"),
slog.String("error", err.Error()),
slog.String("duration", time.Since(begin).String()),
)
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
lm.logger.Info(
fmt.Sprintf("%s without errors.", message),
slog.String("method", "remove"),
slog.String("thing_id", id),
slog.String("token", token),
slog.String("duration", time.Since(begin).String()),
)
}(time.Now())

return lm.svc.Remove(ctx, token, id)
}

func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externalID string, secure bool) (cfg bootstrap.Config, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method bootstrap for thing with external id %s took %s to complete", externalID, time.Since(begin))
message := "Method bootstrap completed"
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
lm.logger.Warn(
fmt.Sprintf("%s with error.", message),
slog.String("method", "bootstrap"),
slog.String("error", err.Error()),
slog.String("duration", time.Since(begin).String()),
)
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
lm.logger.Info(
fmt.Sprintf("%s without errors.", message),
slog.String("method", "bootstrap"),
slog.String("external_id", externalID),
slog.String("external_key", externalKey),
slog.String("duration", time.Since(begin).String()),
)
}(time.Now())

return lm.svc.Bootstrap(ctx, externalKey, externalID, secure)
}

func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, state bootstrap.State) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method change_state for token %s and thing %s took %s to complete", token, id, time.Since(begin))
message := "Method change_state completed"
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
lm.logger.Warn(
fmt.Sprintf("%s with error.", message),
slog.String("method", "change_state"),
slog.String("error", err.Error()),
slog.String("duration", time.Since(begin).String()),
)
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
lm.logger.Info(
fmt.Sprintf("%s without errors.", message),
slog.String("method", "change_state"),
slog.String("thing_id", id),
slog.String("token", token),
slog.String("state", state.String()),
slog.String("duration", time.Since(begin).String()),
)
}(time.Now())

return lm.svc.ChangeState(ctx, token, id, state)
}

func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel bootstrap.Channel) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method update_channel_handler for channel %s took %s to complete", channel.ID, time.Since(begin))
message := "Method update_channel_handler completed"
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
lm.logger.Warn(
fmt.Sprintf("%s with error.", message),
slog.String("method", "update_channel_handler"),
slog.String("error", err.Error()),
slog.String("duration", time.Since(begin).String()),
)
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
lm.logger.Info(
fmt.Sprintf("%s without errors.", message),
slog.String("method", "update_channel_handler"),
slog.String("channel_id", channel.ID),
slog.String("duration", time.Since(begin).String()),
)
}(time.Now())

return lm.svc.UpdateChannelHandler(ctx, channel)
}

func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method remove_config_handler for config %s took %s to complete", id, time.Since(begin))
message := "Method remove_config_handler completed"
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
lm.logger.Warn(
fmt.Sprintf("%s with error: %s.", message, err),
slog.String("method", "remove_config_handler"),
slog.String("error", err.Error()),
slog.String("duration", time.Since(begin).String()),
)
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
lm.logger.Info(
fmt.Sprintf("%s without errors.", message),
slog.String("method", "remove_config_handler"),
slog.String("config_id", id),
slog.String("duration", time.Since(begin).String()),
)
}(time.Now())

return lm.svc.RemoveConfigHandler(ctx, id)
}

func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method remove_channel_handler for channel %s took %s to complete", id, time.Since(begin))
message := "Method remove_channel_handler completed"
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
lm.logger.Warn(
fmt.Sprintf("%s with error.", message),
slog.String("method", "remove_channel_handler"),
slog.String("error", err.Error()),
slog.String("duration", time.Since(begin).String()),
)
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
lm.logger.Info(
fmt.Sprintf("%s without errors.", message),
slog.String("method", "remove_channel_handler"),
slog.String("channel_id", id),
slog.String("duration", time.Since(begin).String()),
)
}(time.Now())

return lm.svc.RemoveChannelHandler(ctx, id)
}

func (lm *loggingMiddleware) DisconnectThingHandler(ctx context.Context, channelID, thingID string) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method disconnect_thing_handler for channel %s and thing %s took %s to complete", channelID, thingID, time.Since(begin))
message := "Method disconnect_thing_handler completed"
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
lm.logger.Warn(
fmt.Sprintf("%s with error.", message),
slog.String("method", "disconnect_thing_handler"),
slog.String("error", err.Error()),
slog.String("duration", time.Since(begin).String()),
)
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
lm.logger.Info(
fmt.Sprintf("%s without errors.", message),
slog.String("method", "disconnect_thing_handler"),
slog.String("channel_id", channelID),
slog.String("thing_id", thingID),
slog.String("duration", time.Since(begin).String()),
)
}(time.Now())

return lm.svc.DisconnectThingHandler(ctx, channelID, thingID)
Expand Down
Loading

0 comments on commit c6782cd

Please sign in to comment.