Skip to content

Commit

Permalink
zerolog and prometheus metrics added
Browse files Browse the repository at this point in the history
  • Loading branch information
chandanpasunoori committed Jun 6, 2022
1 parent ed1175d commit 77f698d
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 75 deletions.
50 changes: 32 additions & 18 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package cmd
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"

"github.com/chandanpasunoori/event-sync/pkg"
log "github.com/sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
Expand All @@ -25,45 +28,56 @@ var rootCmd = &cobra.Command{
Long: `Built to ease process of syncing data between different storage systems`,
Version: version,
Run: func(cmd *cobra.Command, args []string) {

zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.DebugLevel)

if verbose {
logger.SetLevel(log.DebugLevel)
zerolog.SetGlobalLevel(zerolog.TraceLevel)
}
configBytes, err := ioutil.ReadFile(configDoc)
if err != nil {
logger.WithError(err).Errorf("config file not found at " + configDoc)
log.Error().Err(err).Str("path", configDoc).Msg("config file not found")
os.Exit(1)
}
if err := json.Unmarshal(configBytes, &config); err != nil {
logger.WithError(err).Errorf("error parsing config")
log.Error().Err(err).Msg("error parsing config")
os.Exit(1)
}
logger.Info(
"event-sync " + version + " is ready to sync events",
)
pkg.SyncEvents(config)
log.Info().Str("version", version).Msg("event-sync is ready to sync events")
go pkg.SyncEvents(config)
runServer()
},
}

var logger = log.Logger{
Out: os.Stdout,
Formatter: &log.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05.000",
},
Level: log.InfoLevel,
func runServer() {
http.Handle("/metrics", promhttp.Handler())
healthCheckHandler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("content-type", "application/json")
rw.WriteHeader(200)
json.NewEncoder(rw).Encode(map[string]string{"status": "ok"})
})

http.Handle("/", healthCheckHandler)
http.Handle("/_status/healthz", healthCheckHandler)

err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Warn().Err(err).Msg("http server error")
}
log.Info().Msg("server stopped")
}

func Execute() {
if genDoc := os.Getenv("GEN_DOC"); genDoc == "true" {
err := doc.GenMarkdownTree(rootCmd, "./docs")
if err != nil {
log.Errorf("Failed generating docs: %v", err)
log.Error().Err(err).Msg("failed generating docs")
}
}

if err := rootCmd.Execute(); err != nil {
logger.WithError(err).Errorf("error executing command")
log.Error().Err(err).Msg("error executing command")
os.Exit(1)
}
}
Expand Down
13 changes: 10 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
cloud.google.com/go/pubsub v1.17.1
cloud.google.com/go/storage v1.18.2
github.com/google/uuid v1.3.0
github.com/prometheus/client_golang v1.12.2
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
google.golang.org/grpc v1.42.0
Expand All @@ -15,6 +16,7 @@ require (

require (
cloud.google.com/go v0.97.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4 // indirect
Expand All @@ -31,16 +33,21 @@ require (
github.com/google/gofuzz v1.1.0 // indirect
github.com/googleapis/gax-go/v2 v2.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rs/zerolog v1.26.1
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20211116231205-47ca1ff31462 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
Expand Down
Loading

0 comments on commit 77f698d

Please sign in to comment.