mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-28 13:48:49 +08:00

* Use Normalizer as a lib To normalize messages on the consumer side, Normalizer is moved to the internal pkgs. Writers being message consumers are modified to do message normalization instead of subscribing to normalized messages subject. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix logging middleware for readers and writers Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove normalizer interface Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Use Normalizer in writers As we agreed on #919, we'll use normalizer as an interface and provide the default SenML implementation. Because of that, Normalizer is removed from `internal` and we'll use the project structure proposed in the aforementioned issue. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Fix tests Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove unused batch settings from influxDB reader Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update docs Move Normalizer service to `addons`. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Rename channels input topic Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update Noramlizer docs Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove commented code Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update readers logging Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update addons docker-compose files Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update topcis explanations Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
170 lines
4.2 KiB
Go
170 lines
4.2 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/logger"
|
|
"github.com/mainflux/mainflux/normalizer"
|
|
"github.com/mainflux/mainflux/writers"
|
|
"github.com/mainflux/mainflux/writers/api"
|
|
"github.com/mainflux/mainflux/writers/mongodb"
|
|
nats "github.com/nats-io/go-nats"
|
|
stdprometheus "github.com/prometheus/client_golang/prometheus"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
const (
|
|
svcName = "mongodb-writer"
|
|
|
|
defNatsURL = nats.DefaultURL
|
|
defLogLevel = "error"
|
|
defPort = "8180"
|
|
defDBName = "mainflux"
|
|
defDBHost = "localhost"
|
|
defDBPort = "27017"
|
|
defChanCfgPath = "/config/channels.toml"
|
|
|
|
envNatsURL = "MF_NATS_URL"
|
|
envLogLevel = "MF_MONGO_WRITER_LOG_LEVEL"
|
|
envPort = "MF_MONGO_WRITER_PORT"
|
|
envDBName = "MF_MONGO_WRITER_DB_NAME"
|
|
envDBHost = "MF_MONGO_WRITER_DB_HOST"
|
|
envDBPort = "MF_MONGO_WRITER_DB_PORT"
|
|
envChanCfgPath = "MF_MONGO_WRITER_CHANNELS_CONFIG"
|
|
)
|
|
|
|
type config struct {
|
|
natsURL string
|
|
logLevel string
|
|
port string
|
|
dbName string
|
|
dbHost string
|
|
dbPort string
|
|
channels map[string]bool
|
|
}
|
|
|
|
func main() {
|
|
cfg := loadConfigs()
|
|
|
|
logger, err := logger.New(os.Stdout, cfg.logLevel)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
nc, err := nats.Connect(cfg.natsURL)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Failed to connect to NATS: %s", err))
|
|
os.Exit(1)
|
|
}
|
|
defer nc.Close()
|
|
|
|
addr := fmt.Sprintf("mongodb://%s:%s", cfg.dbHost, cfg.dbPort)
|
|
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(addr))
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Failed to connect to database: %s", err))
|
|
os.Exit(1)
|
|
}
|
|
|
|
db := client.Database(cfg.dbName)
|
|
repo := mongodb.New(db)
|
|
|
|
counter, latency := makeMetrics()
|
|
repo = api.LoggingMiddleware(repo, logger)
|
|
repo = api.MetricsMiddleware(repo, counter, latency)
|
|
norm := normalizer.New()
|
|
if err := writers.Start(nc, repo, norm, svcName, cfg.channels, logger); err != nil {
|
|
logger.Error(fmt.Sprintf("Failed to start MongoDB writer: %s", err))
|
|
os.Exit(1)
|
|
}
|
|
|
|
errs := make(chan error, 2)
|
|
go func() {
|
|
c := make(chan os.Signal)
|
|
signal.Notify(c, syscall.SIGINT)
|
|
errs <- fmt.Errorf("%s", <-c)
|
|
}()
|
|
|
|
go startHTTPService(cfg.port, logger, errs)
|
|
|
|
err = <-errs
|
|
logger.Error(fmt.Sprintf("MongoDB writer service terminated: %s", err))
|
|
}
|
|
|
|
func loadConfigs() config {
|
|
chanCfgPath := mainflux.Env(envChanCfgPath, defChanCfgPath)
|
|
return config{
|
|
natsURL: mainflux.Env(envNatsURL, defNatsURL),
|
|
logLevel: mainflux.Env(envLogLevel, defLogLevel),
|
|
port: mainflux.Env(envPort, defPort),
|
|
dbName: mainflux.Env(envDBName, defDBName),
|
|
dbHost: mainflux.Env(envDBHost, defDBHost),
|
|
dbPort: mainflux.Env(envDBPort, defDBPort),
|
|
channels: loadChansConfig(chanCfgPath),
|
|
}
|
|
}
|
|
|
|
type channels struct {
|
|
List []string `toml:"filter"`
|
|
}
|
|
|
|
type chanConfig struct {
|
|
Channels channels `toml:"channels"`
|
|
}
|
|
|
|
func loadChansConfig(chanConfigPath string) map[string]bool {
|
|
data, err := ioutil.ReadFile(chanConfigPath)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
var chanCfg chanConfig
|
|
if err := toml.Unmarshal(data, &chanCfg); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
chans := map[string]bool{}
|
|
for _, ch := range chanCfg.Channels.List {
|
|
chans[ch] = true
|
|
}
|
|
|
|
return chans
|
|
}
|
|
|
|
func makeMetrics() (*kitprometheus.Counter, *kitprometheus.Summary) {
|
|
counter := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
|
|
Namespace: "mongodb",
|
|
Subsystem: "message_writer",
|
|
Name: "request_count",
|
|
Help: "Number of database inserts.",
|
|
}, []string{"method"})
|
|
|
|
latency := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
|
|
Namespace: "mongodb",
|
|
Subsystem: "message_writer",
|
|
Name: "request_latency_microseconds",
|
|
Help: "Total duration of inserts in microseconds.",
|
|
}, []string{"method"})
|
|
|
|
return counter, latency
|
|
}
|
|
|
|
func startHTTPService(port string, logger logger.Logger, errs chan error) {
|
|
p := fmt.Sprintf(":%s", port)
|
|
logger.Info(fmt.Sprintf("Mongodb writer service started, exposed port %s", p))
|
|
errs <- http.ListenAndServe(p, api.MakeHandler(svcName))
|
|
}
|