Skip to content

Commit

Permalink
Analytics + Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ShardulNalegave committed Feb 20, 2024
1 parent 860b78d commit 6b11c02
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
36 changes: 36 additions & 0 deletions analytics/analytics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package analytics

import (
"log"
"os"
"path"
)

const (
ANALYTICS_DIR string = ".freighter"
LOG_FILE string = "logs.txt"
)

type Analytics struct {
LogFile *os.File
Backend map[string]BackendAnalytics
}

func NewAnalytics() *Analytics {
if _, err := os.Stat(ANALYTICS_DIR); os.IsNotExist(err) {
err := os.MkdirAll(ANALYTICS_DIR, 0755)
if err != nil {
log.Fatalf("Could not create directory: %s", ANALYTICS_DIR)
}
}

logFile, err := os.OpenFile(path.Join(ANALYTICS_DIR, LOG_FILE), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("Could not open log-file: %s", LOG_FILE)
}

return &Analytics{
LogFile: logFile,
Backend: make(map[string]BackendAnalytics),
}
}
5 changes: 5 additions & 0 deletions analytics/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package analytics

type BackendAnalytics struct {
//
}
12 changes: 12 additions & 0 deletions freighter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"sync"
"time"

"github.com/ShardulNalegave/freighter/analytics"
"github.com/ShardulNalegave/freighter/pool"
"github.com/ShardulNalegave/freighter/strategy"
"github.com/ShardulNalegave/freighter/utils"
"github.com/rs/zerolog"
)

type Options struct {
Expand All @@ -19,14 +22,18 @@ type Options struct {

type Freighter struct {
URL *url.URL
a *analytics.Analytics
pl *pool.ServerPool
logger zerolog.Logger
Strategy strategy.Strategy
healthCheckInterval time.Duration
}

func (f *Freighter) ListenAndServe(wg *sync.WaitGroup) {
defer wg.Done()
go HealthCheck(f.pl, f.healthCheckInterval)

f.logger.Info().Str("HOST", f.URL.Host).Msg("Listening...")
http.ListenAndServe(f.URL.Host, http.HandlerFunc(f.Handle))
}

Expand All @@ -39,13 +46,18 @@ func (f *Freighter) Handle(w http.ResponseWriter, r *http.Request) {
}

func NewFreighter(opts *Options) *Freighter {
a := analytics.NewAnalytics()
logger := utils.NewLogger(a)

pl := &pool.ServerPool{
Backends: opts.Backends,
}

return &Freighter{
URL: opts.URL,
Strategy: opts.Strategy,
a: a,
logger: logger,
pl: pl,
healthCheckInterval: opts.HealthCheckInterval,
}
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ module github.com/ShardulNalegave/freighter
go 1.21.6

require github.com/google/uuid v1.6.0

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/rs/zerolog v1.32.0 // indirect
golang.org/x/sys v0.12.0 // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1 change: 1 addition & 0 deletions test/.freighter/logs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"level":"info","HOST":":5000","message":"Listening..."}
10 changes: 10 additions & 0 deletions utils/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package utils

import (
"github.com/ShardulNalegave/freighter/analytics"
"github.com/rs/zerolog"
)

func NewLogger(a *analytics.Analytics) zerolog.Logger {
return zerolog.New(a.LogFile)
}

0 comments on commit 6b11c02

Please sign in to comment.