Skip to content

Commit f4f2b79

Browse files
authored
Merge pull request #32 from Chia-Network/logs
Add logrus + ability to set log-level with flags/env/config
2 parents 7eb7058 + f494c27 commit f4f2b79

File tree

13 files changed

+84
-41
lines changed

13 files changed

+84
-41
lines changed

Makefile

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ $(BIN)/%: | $(BIN) ; $(info $(M) building $(PACKAGE)…)
3535
GOLINT = $(BIN)/golint
3636
$(BIN)/golint: PACKAGE=golang.org/x/lint/golint
3737

38+
STATICCHECK = $(BIN)/staticcheck
39+
$(BIN)/staticcheck: PACKAGE=honnef.co/go/tools/cmd/staticcheck
40+
41+
ERRCHECK = $(BIN)/errcheck
42+
$(BIN)/errcheck: PACKAGE=github.com/kisielk/errcheck
43+
3844
GOCOV = $(BIN)/gocov
3945
$(BIN)/gocov: PACKAGE=github.com/axw/gocov/...
4046

@@ -54,10 +60,10 @@ test-verbose: ARGS=-v ## Run tests in verbose mode with coverage repo
5460
test-race: ARGS=-race ## Run tests with race detector
5561
$(TEST_TARGETS): NAME=$(MAKECMDGOALS:test-%=%)
5662
$(TEST_TARGETS): test
57-
check test tests: fmt lint vet; $(info $(M) running $(NAME:%=% )tests…) @ ## Run tests
63+
check test tests: fmt lint vet staticcheck errcheck; $(info $(M) running $(NAME:%=% )tests…) @ ## Run tests
5864
$Q $(GO) test -timeout $(TIMEOUT)s $(ARGS) $(TESTPKGS)
5965

60-
test-xml: fmt lint vet | $(GO2XUNIT) ; $(info $(M) running xUnit tests…) @ ## Run tests with xUnit output
66+
test-xml: fmt lint vet staticcheck errcheck | $(GO2XUNIT) ; $(info $(M) running xUnit tests…) @ ## Run tests with xUnit output
6167
$Q mkdir -p test
6268
$Q 2>&1 $(GO) test -timeout $(TIMEOUT)s -v $(TESTPKGS) | tee test/tests.output
6369
$(GO2XUNIT) -fail -input test/tests.output -output test/tests.xml
@@ -69,7 +75,7 @@ COVERAGE_HTML = $(COVERAGE_DIR)/index.html
6975
.PHONY: test-coverage test-coverage-tools
7076
test-coverage-tools: | $(GOCOV) $(GOCOVXML)
7177
test-coverage: COVERAGE_DIR := $(CURDIR)/test/coverage
72-
test-coverage: fmt lint vet test-coverage-tools ; $(info $(M) running coverage tests…) @ ## Run coverage tests
78+
test-coverage: fmt lint vet staticcheck errcheck test-coverage-tools ; $(info $(M) running coverage tests…) @ ## Run coverage tests
7379
$Q mkdir -p $(COVERAGE_DIR)
7480
$Q $(GO) test \
7581
-coverpkg=$$($(GO) list -f '{{ join .Deps "\n" }}' $(TESTPKGS) | \
@@ -92,6 +98,14 @@ fmt: ; $(info $(M) running gofmt…) @ ## Run gofmt on all source files
9298
vet: ; $(info $(M) running go vet…) @ ## Run go vet on all source files
9399
$Q $(GO) vet $(PKGS)
94100

101+
.PHONY: staticcheck
102+
staticcheck: | $(STATICCHECK) ; $(info $(M) running staticcheck…) @
103+
$Q $(STATICCHECK) $(PKGS)
104+
105+
.PHONY: errcheck
106+
errcheck: | $(ERRCHECK) ; $(info $(M) running errcheck…) @
107+
$Q $(ERRCHECK) $(PKGS)
108+
95109
# Misc
96110

97111
.PHONY: clean

cmd/root.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"strings"
77

8+
log "github.com/sirupsen/logrus"
89
"github.com/spf13/cobra"
910
"github.com/spf13/viper"
1011
)
@@ -30,16 +31,28 @@ func init() {
3031
var (
3132
metricsPort int
3233
maxmindDBPath string
34+
logLevel string
3335
)
3436

3537
cobra.OnInitialize(initConfig)
3638
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.chia-exporter.yaml)")
3739

3840
rootCmd.PersistentFlags().IntVar(&metricsPort, "metrics-port", 9914, "The port the metrics server binds to")
3941
rootCmd.PersistentFlags().StringVar(&maxmindDBPath, "maxmind-db-path", "", "Path to the maxmind database file")
42+
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "How verbose the logs should be. panic, fatal, error, warn, info, debug, trace")
4043

41-
viper.BindPFlag("metrics-port", rootCmd.PersistentFlags().Lookup("metrics-port"))
42-
viper.BindPFlag("maxmind-db-path", rootCmd.PersistentFlags().Lookup("maxmind-db-path"))
44+
err := viper.BindPFlag("metrics-port", rootCmd.PersistentFlags().Lookup("metrics-port"))
45+
if err != nil {
46+
log.Fatalln(err.Error())
47+
}
48+
err = viper.BindPFlag("maxmind-db-path", rootCmd.PersistentFlags().Lookup("maxmind-db-path"))
49+
if err != nil {
50+
log.Fatalln(err.Error())
51+
}
52+
err = viper.BindPFlag("log-level", rootCmd.PersistentFlags().Lookup("log-level"))
53+
if err != nil {
54+
log.Fatalln(err.Error())
55+
}
4356
}
4457

4558
// initConfig reads in config file and ENV variables if set.

cmd/serve.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package cmd
22

33
import (
4-
"log"
54
"time"
65

6+
log "github.com/sirupsen/logrus"
7+
78
"github.com/spf13/cobra"
89
"github.com/spf13/viper"
910

@@ -15,7 +16,11 @@ var serveCmd = &cobra.Command{
1516
Use: "serve",
1617
Short: "Starts the metrics server",
1718
Run: func(cmd *cobra.Command, args []string) {
18-
m, err := metrics.NewMetrics(uint16(viper.GetInt("metrics-port")))
19+
level, err := log.ParseLevel(viper.GetString("log-level"))
20+
if err != nil {
21+
log.Fatalf("Error parsing log level: %s\n", err.Error())
22+
}
23+
m, err := metrics.NewMetrics(uint16(viper.GetInt("metrics-port")), level)
1924
if err != nil {
2025
log.Fatalln(err.Error())
2126
}
@@ -29,7 +34,7 @@ var serveCmd = &cobra.Command{
2934
log.Println("App is stopping. Cleaning up...")
3035
err := m.CloseWebsocket()
3136
if err != nil {
32-
log.Printf("Error closing websocket connection: %s\n", err.Error())
37+
log.Errorf("Error closing websocket connection: %s\n", err.Error())
3338
}
3439
}(m)
3540

@@ -48,7 +53,7 @@ func startWebsocket(m *metrics.Metrics) {
4853
for {
4954
err := m.OpenWebsocket()
5055
if err != nil {
51-
log.Println(err.Error())
56+
log.Errorln(err.Error())
5257
time.Sleep(5 * time.Second)
5358
continue
5459
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/chia-network/go-chia-libs v0.0.3
77
github.com/oschwald/maxminddb-golang v1.8.0
88
github.com/prometheus/client_golang v1.12.0
9+
github.com/sirupsen/logrus v1.8.1
910
github.com/spf13/cobra v1.3.0
1011
github.com/spf13/viper v1.10.1
1112
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg
348348
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
349349
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
350350
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
351+
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
352+
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
351353
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
352354
github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
353355
github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY=

internal/metrics/crawler.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package metrics
33
import (
44
"encoding/json"
55
"fmt"
6-
"log"
76
"net"
87
"time"
98

9+
log "github.com/sirupsen/logrus"
10+
1011
"github.com/chia-network/go-chia-libs/pkg/rpc"
1112
"github.com/chia-network/go-chia-libs/pkg/types"
1213
"github.com/prometheus/client_golang/prometheus"
@@ -49,7 +50,7 @@ func (s *CrawlerServiceMetrics) InitMetrics() {
4950
err := s.initMaxmindDB()
5051
if err != nil {
5152
// Continue on maxmind error - optional/not critical functionality
52-
log.Printf("Error initializing maxmind DB: %s\n", err.Error())
53+
log.Errorf("Error initializing maxmind DB: %s\n", err.Error())
5354
}
5455
}
5556

@@ -99,7 +100,7 @@ func (s *CrawlerServiceMetrics) GetPeerCounts(resp *types.WebsocketResponse) {
99100
counts := &rpc.GetPeerCountsResponse{}
100101
err := json.Unmarshal(resp.Data, counts)
101102
if err != nil {
102-
log.Printf("Error unmarshalling: %s\n", err.Error())
103+
log.Errorf("Error unmarshalling: %s\n", err.Error())
103104
return
104105
}
105106

@@ -137,7 +138,7 @@ func (s *CrawlerServiceMetrics) StartIPCountryMapping(limit uint) {
137138
Limit: limit,
138139
})
139140
if err != nil {
140-
log.Printf("Error getting IPs: %s\n", err.Error())
141+
log.Errorf("Error getting IPs: %s\n", err.Error())
141142
return
142143
}
143144

@@ -169,7 +170,7 @@ func (s *CrawlerServiceMetrics) GetIPsAfterTimestamp(ips *rpc.GetIPsAfterTimesta
169170
}
170171

171172
countryName := ""
172-
countryName, _ = country.Country.Names["en"]
173+
countryName = country.Country.Names["en"]
173174

174175
if _, ok := countryCounts[country.Country.ISOCode]; !ok {
175176
countryCounts[country.Country.ISOCode] = &countStruct{

internal/metrics/fullnode.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package metrics
33
import (
44
"encoding/json"
55
"fmt"
6-
"log"
6+
7+
log "github.com/sirupsen/logrus"
78

89
"github.com/chia-network/go-chia-libs/pkg/rpc"
910
"github.com/chia-network/go-chia-libs/pkg/types"
@@ -144,12 +145,12 @@ func (s *FullNodeServiceMetrics) GetBlockchainState(resp *types.WebsocketRespons
144145
state := &types.WebsocketBlockchainState{}
145146
err := json.Unmarshal(resp.Data, state)
146147
if err != nil {
147-
log.Printf("Error unmarshalling: %s\n", err.Error())
148+
log.Errorf("Error unmarshalling: %s\n", err.Error())
148149
return
149150
}
150151

151152
if state.BlockchainState.Sync != nil {
152-
if state.BlockchainState.Sync.Synced == true {
153+
if state.BlockchainState.Sync.Synced {
153154
s.nodeSynced.Set(1)
154155
} else {
155156
s.nodeSynced.Set(0)
@@ -185,7 +186,7 @@ func (s *FullNodeServiceMetrics) GetConnections(resp *types.WebsocketResponse) {
185186
connections := &rpc.GetConnectionsResponse{}
186187
err := json.Unmarshal(resp.Data, connections)
187188
if err != nil {
188-
log.Printf("Error unmarshalling: %s\n", err.Error())
189+
log.Errorf("Error unmarshalling: %s\n", err.Error())
189190
return
190191
}
191192

@@ -228,13 +229,13 @@ func (s *FullNodeServiceMetrics) Block(resp *types.WebsocketResponse) {
228229
block := &types.BlockEvent{}
229230
err := json.Unmarshal(resp.Data, block)
230231
if err != nil {
231-
log.Printf("Error unmarshalling: %s\n", err.Error())
232+
log.Errorf("Error unmarshalling: %s\n", err.Error())
232233
return
233234
}
234235

235236
s.kSize.WithLabelValues(fmt.Sprintf("%d", block.KSize)).Inc()
236237

237-
if block.TransactionBlock == true {
238+
if block.TransactionBlock {
238239
s.blockCost.Set(float64(block.BlockCost))
239240
s.blockFees.Set(float64(block.BlockFees))
240241
}
@@ -246,7 +247,7 @@ func (s *FullNodeServiceMetrics) GetBlockCountMetrics(resp *types.WebsocketRespo
246247
blockMetrics := &rpc.GetBlockCountMetricsResponse{}
247248
err := json.Unmarshal(resp.Data, blockMetrics)
248249
if err != nil {
249-
log.Printf("Error unmarshalling: %s\n", err.Error())
250+
log.Errorf("Error unmarshalling: %s\n", err.Error())
250251
return
251252
}
252253

@@ -262,7 +263,7 @@ func (s *FullNodeServiceMetrics) SignagePoint(resp *types.WebsocketResponse) {
262263
signagePoint := &types.SignagePointEvent{}
263264
err := json.Unmarshal(resp.Data, signagePoint)
264265
if err != nil {
265-
log.Printf("Error unmarshalling: %s\n", err.Error())
266+
log.Errorf("Error unmarshalling: %s\n", err.Error())
266267
return
267268
}
268269

internal/metrics/metrics.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package metrics
22

33
import (
44
"fmt"
5-
"log"
65
"net/http"
76

7+
log "github.com/sirupsen/logrus"
8+
89
"github.com/chia-network/go-chia-libs/pkg/rpc"
910
"github.com/chia-network/go-chia-libs/pkg/types"
1011
"github.com/prometheus/client_golang/prometheus"
@@ -57,7 +58,7 @@ type Metrics struct {
5758

5859
// NewMetrics returns a new instance of metrics
5960
// All metrics are registered here
60-
func NewMetrics(port uint16) (*Metrics, error) {
61+
func NewMetrics(port uint16, logLevel log.Level) (*Metrics, error) {
6162
var err error
6263

6364
metrics := &Metrics{
@@ -66,6 +67,8 @@ func NewMetrics(port uint16) (*Metrics, error) {
6667
serviceMetrics: map[chiaService]serviceMetrics{},
6768
}
6869

70+
log.SetLevel(logLevel)
71+
6972
metrics.client, err = rpc.NewClient(rpc.ConnectionModeWebsocket)
7073
if err != nil {
7174
return nil, err
@@ -75,7 +78,7 @@ func NewMetrics(port uint16) (*Metrics, error) {
7578
if err != nil {
7679
// For now, http client is optional
7780
// Sometimes this fails with outdated config.yaml files that don't have the crawler/seeder section present
78-
log.Printf("Error creating http client: %s\n", err.Error())
81+
log.Errorf("Error creating http client: %s\n", err.Error())
7982
}
8083

8184
// Register each service's metrics
@@ -208,11 +211,12 @@ func (m *Metrics) StartServer() error {
208211

209212
func (m *Metrics) websocketReceive(resp *types.WebsocketResponse, err error) {
210213
if err != nil {
211-
log.Printf("Websocket received err: %s\n", err.Error())
214+
log.Errorf("Websocket received err: %s\n", err.Error())
212215
return
213216
}
214217

215218
log.Printf("recv: %s %s\n", resp.Origin, resp.Command)
219+
log.Debugf("origin: %s command: %s destination: %s data: %s\n", resp.Origin, resp.Command, resp.Destination, string(resp.Data))
216220

217221
switch resp.Origin {
218222
case "chia_full_node":
@@ -237,6 +241,6 @@ func healthcheckEndpoint(w http.ResponseWriter, r *http.Request) {
237241
w.WriteHeader(http.StatusOK)
238242
_, err := fmt.Fprintf(w, "Ok")
239243
if err != nil {
240-
log.Printf("Error writing healthcheck response %s\n", err.Error())
244+
log.Errorf("Error writing healthcheck response %s\n", err.Error())
241245
}
242246
}

internal/metrics/timelord.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package metrics
22

33
import (
44
"encoding/json"
5-
"log"
5+
6+
log "github.com/sirupsen/logrus"
67

78
"github.com/chia-network/go-chia-libs/pkg/types"
89
"github.com/prometheus/client_golang/prometheus"
@@ -67,7 +68,7 @@ func (s *TimelordServiceMetrics) FinishedPoT(resp *types.WebsocketResponse) {
6768
potEvent := &types.FinishedPoTEvent{}
6869
err := json.Unmarshal(resp.Data, potEvent)
6970
if err != nil {
70-
log.Printf("Error unmarshalling: %s\n", err.Error())
71+
log.Errorf("Error unmarshalling: %s\n", err.Error())
7172
return
7273
}
7374
s.estimatedIPS.Set(potEvent.EstimatedIPS)
@@ -78,7 +79,7 @@ func (s *TimelordServiceMetrics) NewCompactProof(resp *types.WebsocketResponse)
7879
compactProof := &types.NewCompactProofEvent{}
7980
err := json.Unmarshal(resp.Data, compactProof)
8081
if err != nil {
81-
log.Printf("Error unmarshalling: %s\n", err.Error())
82+
log.Errorf("Error unmarshalling: %s\n", err.Error())
8283
return
8384
}
8485

internal/metrics/wallet.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package metrics
33
import (
44
"encoding/json"
55
"fmt"
6-
"log"
6+
7+
log "github.com/sirupsen/logrus"
78

89
"github.com/chia-network/go-chia-libs/pkg/rpc"
910
"github.com/chia-network/go-chia-libs/pkg/types"
@@ -81,7 +82,7 @@ func (s *WalletServiceMetrics) CoinAdded(resp *types.WebsocketResponse) {
8182
coinAdded := &types.CoinAddedEvent{}
8283
err := json.Unmarshal(resp.Data, coinAdded)
8384
if err != nil {
84-
log.Printf("Error unmarshalling: %s\n", err.Error())
85+
log.Errorf("Error unmarshalling: %s\n", err.Error())
8586
return
8687
}
8788

@@ -100,7 +101,7 @@ func (s *WalletServiceMetrics) GetSyncStatus(resp *types.WebsocketResponse) {
100101
syncStatusResponse := &rpc.GetWalletSyncStatusResponse{}
101102
err := json.Unmarshal(resp.Data, syncStatusResponse)
102103
if err != nil {
103-
log.Printf("Error unmarshalling: %s\n", err.Error())
104+
log.Errorf("Error unmarshalling: %s\n", err.Error())
104105
return
105106
}
106107

@@ -116,7 +117,7 @@ func (s *WalletServiceMetrics) GetWalletBalance(resp *types.WebsocketResponse) {
116117
walletBalance := &rpc.GetWalletBalanceResponse{}
117118
err := json.Unmarshal(resp.Data, walletBalance)
118119
if err != nil {
119-
log.Printf("Error unmarshalling: %s\n", err.Error())
120+
log.Errorf("Error unmarshalling: %s\n", err.Error())
120121
return
121122
}
122123

0 commit comments

Comments
 (0)