Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Add logger, api stats (#347)
Browse files Browse the repository at this point in the history
* Add logger, api stats
  • Loading branch information
millken authored Apr 29, 2022
1 parent ded978c commit 227d8e5
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
9 changes: 9 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,12 @@ voteWeightCalConsts:
durationLg: 1.2
autoStake: 1
selfStake: 1.06
zap:
development: false
level: info
encoding: json
disableCaller: false
disableStacktrace: false
outputPaths: ["analytics.log"]
errorOutputPaths: ["stderr"]

1 change: 1 addition & 0 deletions indexservice/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type Config struct {
VoteWeightCalConsts indexprotocol.VoteWeightCalConsts `yaml:"voteWeightCalConsts"`
RewardPortionCfg indexprotocol.RewardPortionCfg `yaml:"rewardPortionCfg"`
ReadOnly bool `yaml:"readOnly"`
Zap *zap.Config `json:"zap" yaml:"zap"`
}

// NewIndexer creates a new indexer
Expand Down
46 changes: 46 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package main

import (
"bytes"
"context"
"io/ioutil"
"net/http"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/iotexproject/iotex-election/pb/api"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -78,6 +80,23 @@ func main() {
if err := yaml.Unmarshal(data, &cfg); err != nil {
log.L().Fatal("failed to unmarshal config", zap.Error(err))
}

if cfg.Zap == nil {
zapCfg := zap.NewProductionConfig()
cfg.Zap = &zapCfg
} else {
if cfg.Zap.Development {
cfg.Zap.EncoderConfig = zap.NewDevelopmentEncoderConfig()
} else {
cfg.Zap.EncoderConfig = zap.NewProductionEncoderConfig()
}
}
cfg.Zap.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
cfg.Zap.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
logger, err := cfg.Zap.Build()
if err == nil {
zap.ReplaceGlobals(logger)
}
readOnly := os.Getenv("READ_ONLY")
if readOnly != "" {
cfg.ReadOnly = readOnly == "true"
Expand Down Expand Up @@ -156,6 +175,33 @@ func graphqlHandler(playgroundHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
if r.Method == "POST" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.L().Error("Failed to read request body", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
// clone body
r.Body = ioutil.NopCloser(bytes.NewReader(body))
clientIP, clientID := getIPID(r)
log.L().Info("request stat",
zap.String("clientIP", clientIP),
zap.String("clientID", clientID),
zap.ByteString("body", body))
}
playgroundHandler.ServeHTTP(w, r)
})
}

func getIPID(r *http.Request) (ip, id string) {
ip = r.Header.Get("X-Forwarded-For")
if ip == "" {
ip = r.RemoteAddr
}
id = r.Header.Get("x-iotex-client-id")
if id == "" {
id = "unknown"
}
return
}
16 changes: 8 additions & 8 deletions testutil/blockbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func BuildCompleteBlock(height uint64, nextEpochHeight uint64) (*block.Block, er
Nonce: 101,
},
SenderPubKey: PubKey1.Bytes(),
Signature: SigPlaceholder,
Signature: SigPlaceholder,
},
{
Core: &iotextypes.ActionCore{
Expand All @@ -77,7 +77,7 @@ func BuildCompleteBlock(height uint64, nextEpochHeight uint64) (*block.Block, er
Nonce: 102,
},
SenderPubKey: PubKey1.Bytes(),
Signature: SigPlaceholder,
Signature: SigPlaceholder,
},
{
Core: &iotextypes.ActionCore{
Expand All @@ -88,7 +88,7 @@ func BuildCompleteBlock(height uint64, nextEpochHeight uint64) (*block.Block, er
Nonce: 103,
},
SenderPubKey: PubKey1.Bytes(),
Signature: SigPlaceholder,
Signature: SigPlaceholder,
},
{
Core: &iotextypes.ActionCore{
Expand All @@ -115,7 +115,7 @@ func BuildCompleteBlock(height uint64, nextEpochHeight uint64) (*block.Block, er
Nonce: 104,
},
SenderPubKey: PubKey1.Bytes(),
Signature: SigPlaceholder,
Signature: SigPlaceholder,
},
{
Core: &iotextypes.ActionCore{
Expand All @@ -129,7 +129,7 @@ func BuildCompleteBlock(height uint64, nextEpochHeight uint64) (*block.Block, er
Nonce: 105,
},
SenderPubKey: PubKey1.Bytes(),
Signature: SigPlaceholder,
Signature: SigPlaceholder,
},
{
Core: &iotextypes.ActionCore{
Expand All @@ -143,7 +143,7 @@ func BuildCompleteBlock(height uint64, nextEpochHeight uint64) (*block.Block, er
Nonce: 106,
},
SenderPubKey: PubKey1.Bytes(),
Signature: SigPlaceholder,
Signature: SigPlaceholder,
},
{
Core: &iotextypes.ActionCore{
Expand All @@ -154,7 +154,7 @@ func BuildCompleteBlock(height uint64, nextEpochHeight uint64) (*block.Block, er
Nonce: 107,
},
SenderPubKey: PubKey1.Bytes(),
Signature: SigPlaceholder,
Signature: SigPlaceholder,
},
{
Core: &iotextypes.ActionCore{
Expand All @@ -165,7 +165,7 @@ func BuildCompleteBlock(height uint64, nextEpochHeight uint64) (*block.Block, er
Nonce: 108,
},
SenderPubKey: PubKey1.Bytes(),
Signature: SigPlaceholder,
Signature: SigPlaceholder,
},
},
},
Expand Down

0 comments on commit 227d8e5

Please sign in to comment.