Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch logging library from log to go-kit/log #59

Merged
merged 2 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ linters:

linters-settings:
errcheck:
ignore: fmt:.*,io:Close
ignore: fmt:.*,io:Close,github.com/sacloud/autoscaler/logging:.*
58 changes: 58 additions & 0 deletions commands/flags/global_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2021 The sacloud Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package flags

import (
"fmt"
"os"

"github.com/sacloud/autoscaler/log"

"github.com/sacloud/autoscaler/validate"
"github.com/spf13/cobra"
)

type flags struct {
LogLevel string `name:"log-level" validate:"required,oneof=error warn info debug"`
LogFormat string `name:"log-format" validate:"required,oneof=logfmt json"`
}

var global = &flags{
LogLevel: "info",
LogFormat: "logfmt",
}

func SetFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&global.LogLevel, "log-level", "", global.LogLevel, "Level of logging to be output. options: [ error | warn | info | debug ]")
cmd.PersistentFlags().StringVarP(&global.LogFormat, "log-format", "", global.LogFormat, "Format of logging to be output. options: [ logfmt | json ]")
}

func ValidateFlags(cmd *cobra.Command, args []string) error {
err := validate.Struct(global)
if err != nil {
fmt.Println(err)
}
return err
}

func NewLogger() *log.Logger {
return log.NewLogger(&log.LoggerOption{
Writer: os.Stderr,
JSON: global.LogFormat == "json",
TimeStamp: true,
Caller: false,
Level: log.Level(global.LogLevel),
})
}
12 changes: 9 additions & 3 deletions commands/inputs/alertmanager/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package alertmanager

import (
"github.com/sacloud/autoscaler/commands/flags"
"github.com/sacloud/autoscaler/defaults"
"github.com/sacloud/autoscaler/inputs"
"github.com/sacloud/autoscaler/inputs/alertmanager"
"github.com/spf13/cobra"
Expand All @@ -26,12 +28,16 @@ var Command = &cobra.Command{
RunE: run,
}

var server = &alertmanager.Input{}
var (
dest string
address string
)

func init() {
inputs.Init(Command, server)
Command.Flags().StringVarP(&dest, "dest", "", defaults.CoreSocketAddr, "URL of gRPC endpoint of AutoScaler Core")
Command.Flags().StringVarP(&address, "addr", "", ":3001", "the TCP address for the server to listen on")
}

func run(cmd *cobra.Command, args []string) error {
return inputs.Serve(server)
return inputs.Serve(alertmanager.NewInput(dest, address, flags.NewLogger()))
}
1 change: 1 addition & 0 deletions commands/inputs/direct/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func run(cmd *cobra.Command, args []string) error {
return err
}

// 単発の出力のためlog(標準エラー)ではなく標準出力に書く
fmt.Printf("status: %s, job-id: %s", res.Status, res.ScalingJobId)
if res.Message != "" {
fmt.Printf(", message: %s", res.Message)
Expand Down
12 changes: 9 additions & 3 deletions commands/inputs/grafana/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package grafana

import (
"github.com/sacloud/autoscaler/commands/flags"
"github.com/sacloud/autoscaler/defaults"
"github.com/sacloud/autoscaler/inputs"
"github.com/sacloud/autoscaler/inputs/grafana"
"github.com/spf13/cobra"
Expand All @@ -26,12 +28,16 @@ var Command = &cobra.Command{
RunE: run,
}

var server = &grafana.Input{}
var (
dest string
address string
)

func init() {
inputs.Init(Command, server)
Command.Flags().StringVarP(&dest, "dest", "", defaults.CoreSocketAddr, "URL of gRPC endpoint of AutoScaler Core")
Command.Flags().StringVarP(&address, "addr", "", ":3001", "the TCP address for the server to listen on")
}

func run(cmd *cobra.Command, args []string) error {
return inputs.Serve(server)
return inputs.Serve(grafana.NewInput(dest, address, flags.NewLogger()))
}
11 changes: 7 additions & 4 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ import (
"os"

"github.com/sacloud/autoscaler/commands/completion"
"github.com/sacloud/autoscaler/commands/flags"
"github.com/sacloud/autoscaler/commands/inputs"
"github.com/sacloud/autoscaler/commands/server"
"github.com/sacloud/autoscaler/commands/version"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "autoscaler",
Short: "autoscaler is a tool for managing the scale of resources on SAKURA cloud",
SilenceUsage: true,
SilenceErrors: false,
Use: "autoscaler",
Short: "autoscaler is a tool for managing the scale of resources on SAKURA cloud",
PersistentPreRunE: flags.ValidateFlags,
SilenceUsage: true,
SilenceErrors: false,
}

var subCommands = []*cobra.Command{
Expand All @@ -39,6 +41,7 @@ var subCommands = []*cobra.Command{
}

func init() {
flags.SetFlags(rootCmd)
rootCmd.AddCommand(subCommands...)
}

Expand Down
3 changes: 2 additions & 1 deletion commands/server/start/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os/signal"
"syscall"

"github.com/sacloud/autoscaler/commands/flags"
"github.com/sacloud/autoscaler/core"
"github.com/sacloud/autoscaler/defaults"
"github.com/spf13/cobra"
Expand All @@ -40,6 +41,6 @@ var Command = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
return core.Start(ctx, configPath)
return core.Start(ctx, configPath, flags.NewLogger())
},
}
4 changes: 2 additions & 2 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (c *Config) APIClient() sacloud.APICaller {

// TODO Validateの実装

func (c *Config) Handlers() Handlers {
return append(BuiltinHandlers, c.CustomHandlers...)
func (c *Config) Handlers(ctx *Context) Handlers {
return append(BuiltinHandlers(ctx), c.CustomHandlers...)
}

// AutoScalerConfig オートスケーラー自体の動作設定
Expand Down
16 changes: 13 additions & 3 deletions core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,24 @@ package core
import (
"context"
"time"

"github.com/sacloud/autoscaler/log"
)

// Context 1リクエストのスコープに対応するコンテキスト、context.Contextを実装し、リクエスト情報や現在のジョブの情報を保持する
type Context struct {
ctx context.Context
request *requestInfo
job *JobStatus
logger *log.Logger
}

func NewContext(parent context.Context, request *requestInfo) *Context {
func NewContext(parent context.Context, request *requestInfo, logger *log.Logger) *Context {
logger = logger.With("request", request.String())
return &Context{
ctx: parent,
request: request,
logger: logger,
}
}

Expand All @@ -43,7 +48,7 @@ func (c *Context) WithJobStatus(job *JobStatus) *Context {
action: c.request.action,
resourceGroupName: c.request.resourceGroupName,
desiredStateName: c.request.desiredStateName,
})
}, c.logger)
ctx.job = job
return ctx
}
Expand All @@ -59,14 +64,19 @@ func (c *Context) ForRefresh() *Context {
resourceGroupName: c.request.resourceGroupName,
desiredStateName: c.request.desiredStateName,
refresh: true,
})
}, c.logger)
}

// Request 現在のコンテキストで受けたリクエストの情報を返す
func (c *Context) Request() *requestInfo {
return c.request
}

// Logger 現在のコンテキストのロガーを返す
func (c *Context) Logger() *log.Logger {
return c.logger
}

// JobID 現在のコンテキストでのJobのIDを返す
//
// まだJobの実行決定が行われていない場合でも値を返す
Expand Down
22 changes: 13 additions & 9 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ package core
import (
"context"
"fmt"
"log"
"net"
"os"
"strings"

"github.com/sacloud/autoscaler/defaults"
"github.com/sacloud/autoscaler/log"
"github.com/sacloud/autoscaler/request"
"google.golang.org/grpc"
)
Expand All @@ -31,23 +31,25 @@ import (
type Core struct {
config *Config
jobs map[string]*JobStatus
logger *log.Logger
}

func newCoreInstance(c *Config) (*Core, error) {
func newCoreInstance(c *Config, logger *log.Logger) (*Core, error) {
// TODO バリデーションの実装
return &Core{
config: c,
jobs: make(map[string]*JobStatus),
logger: logger.With("from", "autoscaler-core-server"),
}, nil
}

func Start(ctx context.Context, configPath string) error {
func Start(ctx context.Context, configPath string, logger *log.Logger) error {
config, err := NewConfigFromPath(configPath)
if err != nil {
return err
}

instance, err := newCoreInstance(config)
instance, err := newCoreInstance(config, logger)
if err != nil {
return err
}
Expand All @@ -74,13 +76,15 @@ func (c *Core) run(ctx context.Context) error {
lis.Close() // ignore error
if _, err := os.Stat(filename); err == nil {
if err := os.RemoveAll(filename); err != nil {
log.Printf("cleanup failed: %s\n", err)
c.logger.Error("error", "cleanup failed: %s", err) // nolint
}
}
}()

go func() {
log.Printf("autoscaler started with: %s\n", lis.Addr().String())
if err := c.logger.Info("message", "autoscaler started", "address", lis.Addr().String()); err != nil {
errCh <- err
}
if err := server.Serve(lis); err != nil {
errCh <- err
}
Expand All @@ -90,7 +94,7 @@ func (c *Core) run(ctx context.Context) error {
case err := <-errCh:
return fmt.Errorf("core service failed: %s", err)
case <-ctx.Done():
log.Println("shutting down with:", ctx.Err())
c.logger.Info("message", "shutting down", "error", ctx.Err()) // nolint
}
return ctx.Err()
}
Expand Down Expand Up @@ -129,12 +133,12 @@ func (c *Core) handle(ctx *Context) (*JobStatus, string, error) {
}

// TODO バリデーションは起動時に行えるので移動すべき
if err := rg.ValidateActions(ctx.Request().action, c.config.Handlers()); err != nil {
if err := rg.ValidateActions(ctx.Request().action, c.config.Handlers(ctx)); err != nil {
job.SetStatus(request.ScalingJobStatus_JOB_CANCELED) // まだ実行前のためCANCELEDを返す
return job, "", err
}

go rg.HandleAll(ctx, c.config.APIClient(), c.config.Handlers())
go rg.HandleAll(ctx, c.config.APIClient(), c.config.Handlers(ctx))

job.SetStatus(request.ScalingJobStatus_JOB_ACCEPTED)
return job, "", nil
Expand Down
9 changes: 9 additions & 0 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package core
import (
"os"

"github.com/sacloud/autoscaler/log"

"github.com/sacloud/libsacloud/v2/helper/api"
)

Expand All @@ -30,4 +32,11 @@ var (
TraceHTTP: os.Getenv("SAKURACLOUD_TRACE") != "",
FakeMode: true,
})
testLogger = log.NewLogger(&log.LoggerOption{
Writer: os.Stderr,
JSON: false,
TimeStamp: true,
Caller: true,
Level: log.LevelDebug,
})
)
Loading