...
 
Commits (5)
......@@ -2,6 +2,7 @@ package app
import (
"git.oolsa.net/jooola/jetstats/app/mailjet"
"git.oolsa.net/jooola/jetstats/log"
"git.oolsa.net/jooola/jetstats/sender"
"git.oolsa.net/jooola/jetstats/store"
)
......@@ -17,18 +18,26 @@ type App struct {
// NewApp creates a new app and initiate its components
func NewApp(config *Config) *App {
a := &App{}
log.Debug("app: creating new application")
a := &App{}
a.config = config
a.mailjet = mailjet.New()
a.store = store.New("boltdb")
log.Info("app: store created")
a.sender = sender.New("influxdb")
log.Info("app: sender created")
log.Info("app: application created")
return a
}
// Close closes all application components
func (a *App) Close() {
log.Info("app: closing application")
a.store.Close()
a.sender.Close()
}
package app
import (
"log"
"net/url"
"git.oolsa.net/jooola/jetstats/log"
env "github.com/caarlos0/env/v6"
)
......@@ -14,9 +15,6 @@ type Config struct {
PublicURL string `env:"PUBLIC_URL,required"`
publicURL *url.URL
Verbose bool `env:"VERBOSE" envDefault:"false"`
Debug bool `env:"DEBUG" envDefault:"false"`
WebhookUser string `env:"WEBHOOK_USER,required"`
WebhookPass string `env:"WEBHOOK_PASS,required"`
}
......@@ -25,11 +23,11 @@ type Config struct {
func NewConfig() *Config {
c := Config{}
if err := env.Parse(&c); err != nil {
log.Fatalln(err)
log.Fatal(err)
}
if err := c.Validate(); err != nil {
log.Fatalln(err)
log.Fatal(err)
}
return &c
......
package app
import (
"log"
"net/http"
"strconv"
"git.oolsa.net/jooola/jetstats/events"
"git.oolsa.net/jooola/jetstats/log"
)
// WriteHeader write the response code and increment metrics counter
......@@ -18,14 +18,14 @@ func (a *App) webhookHandler(w http.ResponseWriter, r *http.Request, eventType s
event, err := events.Parse(eventType, r.Body)
if err != nil {
WriteHeader(w, http.StatusBadRequest)
log.Fatal(err)
log.Error(err)
return
}
if err := a.store.Save(event); err != nil {
WriteHeader(w, http.StatusInternalServerError)
log.Fatal(err)
log.Error(err)
return
}
......
package mailjet
import (
"log"
"git.oolsa.net/jooola/jetstats/log"
env "github.com/caarlos0/env/v6"
mailjet "github.com/mailjet/mailjet-apiv3-go"
......
......@@ -2,12 +2,13 @@ package app
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"time"
"git.oolsa.net/jooola/jetstats/log"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
......@@ -17,6 +18,8 @@ const ServerTimeout = 15
// Serve create the servers and start listening for calls
func (a *App) Serve() {
log.Debug("app: starting server")
publicSrv := a.NewPublicServer()
metricsSrv := a.NewMetricServer()
......@@ -32,23 +35,27 @@ func (a *App) Serve() {
}
}()
log.Info("app: server started")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
log.Debug("app: interrupt signal received, stopping server")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*ServerTimeout)
defer cancel()
if err := publicSrv.Shutdown(ctx); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
log.Error(err)
}
if err := metricsSrv.Shutdown(ctx); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
log.Error(err)
}
}
func newServer(addr string, router *mux.Router) *http.Server {
func newServer(addr string, router http.Handler) *http.Server {
return &http.Server{
Addr: addr,
WriteTimeout: time.Second * ServerTimeout,
......
......@@ -46,7 +46,7 @@ func (a *App) WebhooksStatus() ([]resources.Eventcallbackurl, error) {
}
if len(validWebhooks) != len(events.Types) {
return validWebhooks, errors.New("mailjet webhooks are not set or not valid")
return validWebhooks, errors.New("app: mailjet webhooks are not set or not valid")
}
return validWebhooks, nil
......
package cmd
import (
"log"
"git.oolsa.net/jooola/jetstats/log"
"git.oolsa.net/jooola/jetstats/store/boltdb"
"github.com/spf13/cobra"
)
......@@ -26,7 +26,7 @@ var storeImportCmd = &cobra.Command{
Short: "Import store file",
Run: func(cmd *cobra.Command, args []string) {
if storeImportPath == "" {
log.Fatalf("cmd: a path is required")
log.Fatal("a path is required")
}
from := boltdb.NewWithConfig(&boltdb.Config{
......
......@@ -2,9 +2,9 @@ package cmd
import (
"fmt"
"log"
"git.oolsa.net/jooola/jetstats/app"
"git.oolsa.net/jooola/jetstats/log"
"github.com/spf13/cobra"
)
......@@ -31,7 +31,7 @@ var webhooksStatusCmd = &cobra.Command{
fmt.Printf("%s: %s\tbackup: %t\turl: %s\n", webhook.EventType, webhook.Status, webhook.IsBackup, webhook.URL)
}
if err != nil {
log.Fatalln(err)
log.Fatal(err)
}
defer a.Close()
......@@ -45,7 +45,7 @@ var webhooksSetupCmd = &cobra.Command{
a := app.NewApp(app.NewConfig())
if err := a.WebhooksSetup(); err != nil {
log.Fatalln(err)
log.Fatal(err)
}
defer a.Close()
......@@ -59,7 +59,7 @@ var webhooksDeleteCmd = &cobra.Command{
a := app.NewApp(app.NewConfig())
if err := a.WebhooksDelete(); err != nil {
log.Fatalln(err)
log.Fatal(err)
}
defer a.Close()
......
......@@ -9,6 +9,8 @@ import (
// Base define a base for all other events, should not be used
type Base struct {
cachedString string
UUID string `json:"uuid"`
Event string `json:"event"` // the event type
......@@ -49,7 +51,11 @@ func (e *Base) GetType() string {
// String convert an event to a string
func (e *Base) String() string {
return fmt.Sprintf("uuid=%s timestamp=%d id=%d event=%s ", e.UUID, e.Timestamp, e.MailjetMessageID, e.Event)
if e.cachedString == "" {
e.cachedString = fmt.Sprintf("uuid=%s timestamp=%d id=%d event=%s ", e.UUID, e.Timestamp, e.MailjetMessageID, e.Event)
}
return e.cachedString
}
// Bytes convert the event to a json body
......
package events
import (
"log"
"os"
"strings"
"testing"
......@@ -10,7 +9,7 @@ import (
func TestBase(t *testing.T) {
file, err := os.Open("fixtures/click.json")
if err != nil {
log.Fatalln(err)
t.Fatal(err)
}
event, err := Parse(ClickType, file)
......
......@@ -4,8 +4,9 @@ import (
"bytes"
"encoding/json"
"io"
"log"
"time"
"git.oolsa.net/jooola/jetstats/log"
)
// Event type enum
......@@ -43,6 +44,8 @@ type Event interface {
// Parse an event based on its type
func Parse(t string, data io.Reader) (Event, error) {
log.Debug("events: parsing event")
var event Event
switch t {
......@@ -71,6 +74,8 @@ func Parse(t string, data io.Reader) (Event, error) {
event.SetUUID()
log.Debugf("events: event parsed '%s'", event.String())
return event, nil
}
......@@ -80,7 +85,7 @@ func Bytes(e interface{}) []byte {
encoder := json.NewEncoder(buf)
if err := encoder.Encode(e); err != nil {
log.Fatalln(err)
log.Error(err)
}
return buf.Bytes()
......
......@@ -3,7 +3,6 @@ package events
import (
"bytes"
"fmt"
"log"
"os"
"testing"
)
......@@ -23,7 +22,7 @@ func TestParse(t *testing.T) {
t.Run(name, func(t *testing.T) {
file, err := os.Open(fmt.Sprintf("fixtures/%s.json", tc))
if err != nil {
log.Fatalln(err)
t.Fatal(err)
}
defer file.Close()
......
......@@ -7,7 +7,6 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
......@@ -26,7 +25,6 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deepmap/oapi-codegen v1.3.6 h1:Wj44p9A0V0PJ+AUg0BWdyGcsS1LY18U+0rCuPQgK0+o=
github.com/deepmap/oapi-codegen v1.3.6/go.mod h1:aBozjEveG+33xPiP55Iw/XbVkhtZHEGLq3nxlX0+hfU=
......@@ -47,7 +45,6 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
......@@ -61,7 +58,6 @@ github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
......@@ -87,10 +83,8 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/labstack/echo/v4 v4.1.11 h1:z0BZoArY4FqdpUEl+wlHp4hnr/oSR6MTmQmv8OHSoww=
github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g=
......@@ -122,27 +116,22 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3 h1:9iH4JKXLzFbOAdtqv/a+j8aewx2Y8lAjAydhbaScPF8=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.6.0 h1:YVPodQOcK15POxhgARIvnDRVpLcuK8mglnMrWfyrw6A=
github.com/prometheus/client_golang v1.6.0/go.mod h1:ZLOG9ck3JLRdB5MgO8f+lLTe83AXG6ro35rLTxvnIl4=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/common v0.4.0 h1:7etb9YClo3a6HjLzfl6rIQaU+FDfi0VSX39io3aQ+DM=
github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.9.1 h1:KOMtN28tlbam3/7ZKEYKHhKoJZYYj3gMH4uc62x7X7U=
github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084 h1:sofwID9zm4tzrgykg80hfFph1mryUeLRsUfoocVVmRY=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.11 h1:DhHlBtkHWPYi8O2y31JkK0TF+DGM+51OopZjH/Ia5qI=
......@@ -169,7 +158,6 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
......@@ -237,7 +225,6 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
......@@ -251,9 +238,7 @@ google.golang.org/protobuf v1.21.0 h1:qdOKuR/EIArgaWNjetjgTzgVTAZ+S/WXVrq9HW9zim
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
......@@ -261,6 +246,5 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
package log
import (
stdlog "log"
"os"
"strings"
)
var (
std = stdlog.New(os.Stderr, "", stdlog.LstdFlags)
level = info
)
const (
debug = iota
info
error
fatal
)
var levels = []string{
debug: "debug",
info: "info",
error: "error",
fatal: "fatal",
}
func init() {
if l := os.Getenv("VERBOSITY"); l != "" {
for k, v := range levels {
if strings.Compare(strings.ToLower(l), v) == 0 {
level = k
return
}
}
Fatalf("log: invalid logging level '%s'", l)
}
}
// Debug forward the call to standard logger
func Debug(v ...interface{}) {
if level <= debug {
std.Print(v...)
}
}
// Debugf forward the call to standard logger
func Debugf(format string, v ...interface{}) {
if level <= debug {
std.Printf(format, v...)
}
}
// Info forward the call to standard logger
func Info(v ...interface{}) {
if level <= info {
std.Print(v...)
}
}
// Infof forward the call to standard logger
func Infof(format string, v ...interface{}) {
if level <= info {
std.Printf(format, v...)
}
}
// Error forward the call to standard logger
func Error(v ...interface{}) {
if level <= error {
std.Print(v...)
}
}
// Errorf forward the call to standard logger
func Errorf(format string, v ...interface{}) {
if level <= error {
std.Printf(format, v...)
}
}
// Fatal forward the call to standard logger
func Fatal(v ...interface{}) {
if level <= fatal {
std.Fatal(v...)
}
}
// Fatalf forward the call to standard logger
func Fatalf(format string, v ...interface{}) {
if level <= fatal {
std.Fatalf(format, v...)
}
}
package main
import (
"log"
"git.oolsa.net/jooola/jetstats/cmd"
"git.oolsa.net/jooola/jetstats/log"
"git.oolsa.net/jooola/jetstats/x"
)
......
......@@ -4,6 +4,7 @@ import (
"strconv"
"git.oolsa.net/jooola/jetstats/events"
influx "github.com/influxdata/influxdb-client-go"
write "github.com/influxdata/influxdb-client-go/api/write"
)
......
package influxdb
import (
"log"
"time"
"git.oolsa.net/jooola/jetstats/events"
"git.oolsa.net/jooola/jetstats/log"
env "github.com/caarlos0/env/v6"
influxdb "github.com/influxdata/influxdb-client-go"
......@@ -68,9 +68,12 @@ func NewWithConfig(config *Config) *Sender {
// Send saved data to influxdb
func (s *Sender) Send(event events.Event) {
log.Debugf("influxdb: sending event '%s'", event.String())
s.writer.WritePoint(ToPoint(event))
if time.Since(s.flushed) > time.Second*10 {
log.Debug("influxdb: flushing writer")
s.writer.Flush()
s.flushed = time.Now()
}
......@@ -83,6 +86,7 @@ func (s *Sender) Errors() <-chan error {
// Close the sender
func (s *Sender) Close() {
log.Debug("influxdb: closing sender")
s.writer.Close()
s.client.Close()
close(s.errors)
......
package sender
import (
"log"
"git.oolsa.net/jooola/jetstats/events"
"git.oolsa.net/jooola/jetstats/log"
"git.oolsa.net/jooola/jetstats/sender/influxdb"
"git.oolsa.net/jooola/jetstats/sender/mock"
)
......@@ -23,13 +22,20 @@ const (
// New creates a sender based on type
func New(t string) Sender {
log.Debug("sender: creating new sender")
var s Sender
switch t {
case MockSender:
return mock.New()
s = mock.New()
case InfluxDBSender:
return influxdb.New()
s = influxdb.New()
default:
log.Fatalf("sender: unknown sender type '%s'", t)
return nil
}
log.Debugf("sender: '%s' sender created", t)
return s
}
......@@ -16,8 +16,8 @@ typeset -r container_port="9999"
typeset -r org="org"
typeset -r bucket="jetstats"
# Don't create new environement if .env file is exist
if [[ -f .env ]]; then
# Don't create new environement if .env file is exist and container is running
if [[ -f .env ]] && docker ps --format '{{.Names}}' | grep -q "$container_name"; then
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && return || exit
fi
......
package boltdb
import (
"log"
"time"
"git.oolsa.net/jooola/jetstats/events"
"git.oolsa.net/jooola/jetstats/log"
env "github.com/caarlos0/env/v6"
bolt "go.etcd.io/bbolt"
......@@ -63,11 +63,14 @@ func (s *Store) Save(event events.Event) error {
return s.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(eventsBucketKey)
log.Debugf("boltdb: saving events %s", event.String())
return bucket.Put(FormatKey(event), event.Bytes())
})
}
// Close the store
func (s *Store) Close() error {
log.Debug("boltdb: closing store")
return s.DB.Close()
}
......@@ -21,12 +21,10 @@ func (s *Store) Import(from *Store) error {
_, err := events.Parse(eventType, bytes.NewReader(value))
if err != nil {
tx.Rollback()
return err
}
if err := bucket.Put(key, value); err != nil {
tx.Rollback()
return err
}
}
......
......@@ -6,6 +6,7 @@ import (
"git.oolsa.net/jooola/jetstats/events"
"git.oolsa.net/jooola/jetstats/x"
bolt "go.etcd.io/bbolt"
)
......@@ -22,7 +23,9 @@ func TestImport(t *testing.T) {
event := x.RandomEvent()
from.Save(event)
if err := from.Save(event); err != nil {
t.Fatal(err)
}
if err := to.Import(from); err != nil {
t.Fatal(err)
......
package store
import (
"log"
"git.oolsa.net/jooola/jetstats/events"
"git.oolsa.net/jooola/jetstats/log"
"git.oolsa.net/jooola/jetstats/store/boltdb"
"git.oolsa.net/jooola/jetstats/store/mock"
)
......@@ -22,13 +21,20 @@ const (
// New creates a store based on type
func New(t string) Store {
log.Debug("store: creating new store")
var s Store
switch t {
case MockStore:
return mock.New()
s = mock.New()
case BoltDBStore:
return boltdb.New()
s = boltdb.New()
default:
log.Fatalf("store: unknown store type '%s'", t)
return nil
}
log.Debugf("store: '%s' store created", t)
return s
}
......@@ -10,5 +10,6 @@ func LoadEnv() error {
if FileExist(filename) {
return gotenv.Load(filename)
}
return nil
}
......@@ -10,5 +10,6 @@ func FileExist(path string) bool {
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
......@@ -45,5 +45,6 @@ func RandomTime() time.Time {
min := now.AddDate(0, -1, 0).Unix()
delta := max - min
return time.Unix(rand.Int63n(delta)+min, 0)
}
......@@ -2,14 +2,13 @@ package x
import (
"io/ioutil"
"log"
)
// TempFile return a tempfile path
func TempFile() string {
f, err := ioutil.TempFile("", "jetstats.*.bin")
if err != nil {
log.Fatal(err)
panic(err)
}
defer f.Close()
......