-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NOISSUE - Switch to Consumers interface (#1316)
* Replace Writer with Consumer Signed-off-by: dusanb94 <[email protected]> * Add Notifications package Signed-off-by: dusanb94 <[email protected]> * Update Consumer Start Signed-off-by: dusanb94 <[email protected]> * Fix Readers Signed-off-by: dusanb94 <[email protected]> * Fix Consumer naming Signed-off-by: dusanb94 <[email protected]> * Add repo to Notify Signed-off-by: dusanb94 <[email protected]> * Remove notify Signed-off-by: dusanb94 <[email protected]> * Rename consumer field in middlewares Signed-off-by: dusanb94 <[email protected]> * Fix remarks and add Readme Signed-off-by: dusanb94 <[email protected]>
- Loading branch information
1 parent
973ca17
commit 6b7dc54
Showing
45 changed files
with
187 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Consumers | ||
|
||
Consumers provide an abstraction of various `Mainflux consumers`. | ||
Mainflux consumer is a generic service that can handle received messages - consume them. | ||
The message is not necessarily a Mainflux message - before consuming, Mainflux message can | ||
be transformed into any valid format that specific consumer can understand. For example, | ||
writers are consumers that can take a SenML or JSON message and store it. | ||
|
||
Consumers are optional services and are treated as plugins. In order to | ||
run consumer services, core services must be up and running. | ||
|
||
For an in-depth explanation of the usage of `consumers`, as well as thorough | ||
understanding of Mainflux, please check out the [official documentation][doc]. | ||
|
||
[doc]: http://mainflux.readthedocs.io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) Mainflux | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// +build !test | ||
|
||
package api | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/mainflux/mainflux/consumers" | ||
log "github.com/mainflux/mainflux/logger" | ||
) | ||
|
||
var _ consumers.Consumer = (*loggingMiddleware)(nil) | ||
|
||
type loggingMiddleware struct { | ||
logger log.Logger | ||
consumer consumers.Consumer | ||
} | ||
|
||
// LoggingMiddleware adds logging facilities to the adapter. | ||
func LoggingMiddleware(consumer consumers.Consumer, logger log.Logger) consumers.Consumer { | ||
return &loggingMiddleware{ | ||
logger: logger, | ||
consumer: consumer, | ||
} | ||
} | ||
|
||
func (lm *loggingMiddleware) Consume(msgs interface{}) (err error) { | ||
defer func(begin time.Time) { | ||
message := fmt.Sprintf("Method consume took %s to complete", time.Since(begin)) | ||
if err != nil { | ||
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) | ||
return | ||
} | ||
lm.logger.Info(fmt.Sprintf("%s without errors.", message)) | ||
}(time.Now()) | ||
|
||
return lm.consumer.Consume(msgs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) Mainflux | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package api | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/go-kit/kit/metrics" | ||
"github.com/mainflux/mainflux/consumers" | ||
) | ||
|
||
var _ consumers.Consumer = (*metricsMiddleware)(nil) | ||
|
||
type metricsMiddleware struct { | ||
counter metrics.Counter | ||
latency metrics.Histogram | ||
consumer consumers.Consumer | ||
} | ||
|
||
// MetricsMiddleware returns new message repository | ||
// with Save method wrapped to expose metrics. | ||
func MetricsMiddleware(consumer consumers.Consumer, counter metrics.Counter, latency metrics.Histogram) consumers.Consumer { | ||
return &metricsMiddleware{ | ||
counter: counter, | ||
latency: latency, | ||
consumer: consumer, | ||
} | ||
} | ||
|
||
func (mm *metricsMiddleware) Consume(msgs interface{}) error { | ||
defer func(begin time.Time) { | ||
mm.counter.With("method", "consume").Add(1) | ||
mm.latency.With("method", "consume").Observe(time.Since(begin).Seconds()) | ||
}(time.Now()) | ||
return mm.consumer.Consume(msgs) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Mainflux | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package consumers | ||
|
||
// Consumer specifies message consuming API. | ||
type Consumer interface { | ||
// Consume method is used to consumed received messages. | ||
// A non-nil error is returned to indicate operation failure. | ||
Consume(messages interface{}) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) Mainflux | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package consumers contain the domain concept definitions needed to | ||
// support Mainflux consumer services functionality. | ||
package consumers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.