Skip to content

Commit

Permalink
feat: add log level config via env var (#189)
Browse files Browse the repository at this point in the history
* feat: add log level config via env var

* redirect to stdout

* add better debug logging for streams

* use correct log method

* fix blob size formatting

Co-authored-by: Emma Haruka Iwao <[email protected]>
  • Loading branch information
hongalex and yuryu authored Mar 4, 2021
1 parent 5c3d824 commit 52e7d16
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
22 changes: 15 additions & 7 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import (
"os"
"strconv"

log "github.com/sirupsen/logrus"

"github.com/googleforgames/open-saves/internal/app/server"
"github.com/googleforgames/open-saves/internal/pkg/cmd"
log "github.com/sirupsen/logrus"
)

func main() {
Expand All @@ -33,13 +32,15 @@ func main() {
defaultBucket := cmd.GetEnvVarString("OPEN_SAVES_BUCKET", "")
defaultProject := cmd.GetEnvVarString("OPEN_SAVES_PROJECT", "")
defaultCache := cmd.GetEnvVarString("OPEN_SAVES_CACHE", "localhost:6379")
defaultLogLevel := cmd.GetEnvVarString("LOG_LEVEL", "info")

var (
port = flag.Uint("port", uint(defaultPort), "The port number to run Open Saves on")
cloud = flag.String("cloud", defaultCloud, "The public cloud provider you wish to run Open Saves on")
bucket = flag.String("bucket", defaultBucket, "The bucket which will hold Open Saves blobs")
project = flag.String("project", defaultProject, "The GCP project ID to use for Datastore")
cache = flag.String("cache", defaultCache, "The address of the cache store instance")
port = flag.Uint("port", uint(defaultPort), "The port number to run Open Saves on")
cloud = flag.String("cloud", defaultCloud, "The public cloud provider you wish to run Open Saves on")
bucket = flag.String("bucket", defaultBucket, "The bucket which will hold Open Saves blobs")
project = flag.String("project", defaultProject, "The GCP project ID to use for Datastore")
cache = flag.String("cache", defaultCache, "The address of the cache store instance")
logLevel = flag.String("log", defaultLogLevel, "The level to log messages at")
)

flag.Parse()
Expand All @@ -56,6 +57,13 @@ func main() {
log.Fatal("missing -cache argument for cache store")
}

ll, err := log.ParseLevel(*logLevel)
if err != nil {
ll = log.InfoLevel
}
log.SetLevel(ll)
log.Infof("Log level is: %s", ll.String())

cfg := &server.Config{
Address: fmt.Sprintf(":%d", *port),
Cloud: *cloud,
Expand Down
5 changes: 5 additions & 0 deletions internal/app/server/open_saves.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ func (s *openSavesServer) QueryRecords(ctx context.Context, stream *pb.QueryReco
}

func (s *openSavesServer) insertInlineBlob(ctx context.Context, stream pb.OpenSaves_CreateBlobServer, meta *pb.BlobMetadata) error {
log.Debugf("Inserting inline blob: %v\n", meta)
// Receive the blob
size := meta.GetSize()
buffer := bytes.NewBuffer(make([]byte, 0, size))
Expand Down Expand Up @@ -291,6 +292,7 @@ func (s *openSavesServer) blobRefFail(ctx context.Context, blobref *metadb.BlobR
}

func (s *openSavesServer) insertExternalBlob(ctx context.Context, stream pb.OpenSaves_CreateBlobServer, meta *pb.BlobMetadata) error {
log.Debugf("Inserting external blob: %v\n", meta)
// Create a blob reference based on the metadata.
blobref := metadb.NewBlobRef(meta.GetSize(), meta.GetStoreKey(), meta.GetRecordKey())
blobref, err := s.metaDB.InsertBlobRef(ctx, blobref)
Expand Down Expand Up @@ -365,6 +367,7 @@ func (s *openSavesServer) insertExternalBlob(ctx context.Context, stream pb.Open
}

func (s *openSavesServer) CreateBlob(stream pb.OpenSaves_CreateBlobServer) error {
log.Debug("Creating blob stream\n")
ctx := stream.Context()

// The first message must be metadata.
Expand All @@ -378,6 +381,8 @@ func (s *openSavesServer) CreateBlob(stream pb.OpenSaves_CreateBlobServer) error
log.Error("CreateBlob: first message was not metadata")
return status.Error(codes.InvalidArgument, "The first message must be metadata.")
}
log.Debugf("Got metadata from stream: store(%s), record(%s), blob size(%d)\n",
meta.GetStoreKey(), meta.GetRecordKey(), meta.GetSize())

// TODO(yuryu): Make the threshold configurable
if meta.GetSize() <= int64(maxInlineBlobSize) {
Expand Down

0 comments on commit 52e7d16

Please sign in to comment.