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

* Add batch of streams Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Add env variables for batch point setup Fix InfluxDB reader tests. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update Compose and Kubernetes config Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update env variables Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update docs Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Remove unused const Refactor code and docs. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Handle NewBatchPoints error Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Reduce wait time in batch save test Fix typos. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Update tests Use single test with multiple cases to test single point save as well as a batch of points. Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> * Add an explanation for not resetting ticker Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
167 lines
4.4 KiB
Go
167 lines
4.4 KiB
Go
//
|
|
// Copyright (c) 2018
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"strconv"
|
|
"syscall"
|
|
"time"
|
|
|
|
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
|
|
influxdata "github.com/influxdata/influxdb/client/v2"
|
|
"github.com/mainflux/mainflux"
|
|
log "github.com/mainflux/mainflux/logger"
|
|
"github.com/mainflux/mainflux/writers"
|
|
"github.com/mainflux/mainflux/writers/influxdb"
|
|
nats "github.com/nats-io/go-nats"
|
|
stdprometheus "github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
const (
|
|
queue = "influxdb-writer"
|
|
|
|
defNatsURL = nats.DefaultURL
|
|
defPort = "8180"
|
|
defBatchSize = "5000"
|
|
defBatchTimeout = "5"
|
|
defDBName = "mainflux"
|
|
defDBHost = "localhost"
|
|
defDBPort = "8086"
|
|
defDBUser = "mainflux"
|
|
defDBPass = "mainflux"
|
|
|
|
envNatsURL = "MF_NATS_URL"
|
|
envPort = "MF_INFLUX_WRITER_PORT"
|
|
envBatchSize = "MF_INFLUX_WRITER_BATCH_SIZE"
|
|
envBatchTimeout = "MF_INFLUX_WRITER_BATCH_TIMEOUT"
|
|
envDBName = "MF_INFLUX_WRITER_DB_NAME"
|
|
envDBHost = "MF_INFLUX_WRITER_DB_HOST"
|
|
envDBPort = "MF_INFLUX_WRITER_DB_PORT"
|
|
envDBUser = "MF_INFLUX_WRITER_DB_USER"
|
|
envDBPass = "MF_INFLUX_WRITER_DB_PASS"
|
|
)
|
|
|
|
type config struct {
|
|
NatsURL string
|
|
Port string
|
|
BatchSize int
|
|
BatchTimeout int
|
|
DBName string
|
|
DBHost string
|
|
DBPort string
|
|
DBUser string
|
|
DBPass string
|
|
}
|
|
|
|
func main() {
|
|
logger := log.New(os.Stdout)
|
|
cfg, clientCfg := loadConfigs(logger)
|
|
|
|
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()
|
|
|
|
client, err := influxdata.NewHTTPClient(clientCfg)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Failed to create InfluxDB client: %s", err))
|
|
os.Exit(1)
|
|
}
|
|
defer client.Close()
|
|
|
|
timeout := time.Duration(cfg.BatchTimeout) * time.Second
|
|
repo, err := influxdb.New(client, cfg.DBName, cfg.BatchSize, timeout)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Failed to create InfluxDB writer: %s", err))
|
|
os.Exit(1)
|
|
}
|
|
|
|
counter, latency := makeMetrics()
|
|
repo = writers.LoggingMiddleware(repo, logger)
|
|
repo = writers.MetricsMiddleware(repo, counter, latency)
|
|
if err := writers.Start(nc, repo, queue, logger); err != nil {
|
|
logger.Error(fmt.Sprintf("Failed to start message 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("InfluxDB writer service terminated: %s", err))
|
|
}
|
|
|
|
func loadConfigs(logger log.Logger) (config, influxdata.HTTPConfig) {
|
|
cfg := config{
|
|
NatsURL: mainflux.Env(envNatsURL, defNatsURL),
|
|
Port: mainflux.Env(envPort, defPort),
|
|
DBName: mainflux.Env(envDBName, defDBName),
|
|
DBHost: mainflux.Env(envDBHost, defDBHost),
|
|
DBPort: mainflux.Env(envDBPort, defDBPort),
|
|
DBUser: mainflux.Env(envDBUser, defDBUser),
|
|
DBPass: mainflux.Env(envDBPass, defDBPass),
|
|
}
|
|
|
|
var err error
|
|
cfg.BatchSize, err = strconv.Atoi(mainflux.Env(envBatchSize, defBatchSize))
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Invalid value of batch size: %s", err))
|
|
os.Exit(1)
|
|
}
|
|
|
|
cfg.BatchTimeout, err = strconv.Atoi(mainflux.Env(envBatchTimeout, defBatchTimeout))
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Invalid value for batch timeout: %s", err))
|
|
os.Exit(1)
|
|
}
|
|
|
|
clientCfg := influxdata.HTTPConfig{
|
|
Addr: fmt.Sprintf("http://%s:%s", cfg.DBHost, cfg.DBPort),
|
|
Username: cfg.DBUser,
|
|
Password: cfg.DBPass,
|
|
}
|
|
|
|
return cfg, clientCfg
|
|
}
|
|
|
|
func makeMetrics() (*kitprometheus.Counter, *kitprometheus.Summary) {
|
|
counter := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
|
|
Namespace: "influxdb",
|
|
Subsystem: "message_writer",
|
|
Name: "request_count",
|
|
Help: "Number of database inserts.",
|
|
}, []string{"method"})
|
|
|
|
latency := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
|
|
Namespace: "influxdb",
|
|
Subsystem: "message_writer",
|
|
Name: "request_latency_microseconds",
|
|
Help: "Total duration of inserts in microseconds.",
|
|
}, []string{"method"})
|
|
|
|
return counter, latency
|
|
}
|
|
|
|
func startHTTPService(port string, logger log.Logger, errs chan error) {
|
|
p := fmt.Sprintf(":%s", port)
|
|
logger.Info(fmt.Sprintf("InfluxDB writer service started, exposed port %s", p))
|
|
errs <- http.ListenAndServe(p, influxdb.MakeHandler())
|
|
}
|