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

refactor: use salt mux server and update logging to support google cloud logging #124

Merged
merged 8 commits into from
Sep 13, 2022
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: 2 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/spf13/cobra"
)

const serviceName = "siren"

func New(ctx context.Context) *cobra.Command {
rootCmd := &cobra.Command{
Use: "siren <command> <subcommand> [flags]",
Expand Down
50 changes: 39 additions & 11 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import (
"github.com/odpf/siren/core/rule"
"github.com/odpf/siren/core/subscription"
"github.com/odpf/siren/core/template"
"github.com/odpf/siren/internal/api"
"github.com/odpf/siren/internal/server"
"github.com/odpf/siren/internal/store/postgres"
"github.com/odpf/siren/pkg/cortex"
"github.com/odpf/siren/pkg/errors"
"github.com/odpf/siren/pkg/secret"
"github.com/odpf/siren/pkg/slack"
"github.com/odpf/siren/pkg/telemetry"
"github.com/odpf/siren/pkg/zaputil"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -133,7 +135,7 @@ func runServer(cfg config.Config) error {
return err
}

logger := initLogger(cfg.Log.Level)
logger := initLogger(cfg.Log)

dbClient, err := db.New(cfg.DB)
if err != nil {
Expand Down Expand Up @@ -200,23 +202,49 @@ func runServer(cfg config.Config) error {
subscriptionRepository := postgres.NewSubscriptionRepository(pgClient)
subscriptionService := subscription.NewService(subscriptionRepository, providerService, namespaceService, receiverService, cortexClient)

apiDeps := &api.Deps{
TemplateService: templateService,
RuleService: ruleService,
AlertService: alertHistoryService,
ProviderService: providerService,
NamespaceService: namespaceService,
ReceiverService: receiverService,
SubscriptionService: subscriptionService,
}
return server.RunServer(
cfg.Service,
logger,
nr,
templateService,
ruleService,
alertHistoryService,
providerService,
namespaceService,
receiverService,
subscriptionService)
apiDeps,
)
}

func initLogger(logLevel string) log.Logger {
func initLogger(cfg config.Log) log.Logger {
defaultConfig := zap.NewProductionConfig()
defaultConfig.Level = zap.NewAtomicLevelAt(getZapLogLevelFromString(logLevel))
return log.NewZap(log.ZapWithConfig(defaultConfig, zap.AddCaller(), zap.AddStacktrace(zap.DPanicLevel)))
defaultConfig.Level = zap.NewAtomicLevelAt(getZapLogLevelFromString(cfg.Level))

if cfg.GCPCompatible {
defaultConfig = zap.Config{
Level: zap.NewAtomicLevelAt(getZapLogLevelFromString(cfg.Level)),
Encoding: "json",
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
EncoderConfig: zaputil.EncoderConfig,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
}

return log.NewZap(log.ZapWithConfig(
defaultConfig,
zap.Fields(zaputil.ServiceContext(serviceName)),
zap.AddCaller(),
zap.AddStacktrace(zap.DPanicLevel),
))

}

// getZapLogLevelFromString helps to set logLevel from string
Expand Down
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func Load(configFile string) (Config, error) {
return cfg, nil
}

type LogConfig struct {
Level string `yaml:"level" mapstructure:"level" default:"info"`
type Log struct {
Level string `yaml:"level" mapstructure:"level" default:"info"`
GCPCompatible bool `yaml:"gcp_compatible" mapstructure:"gcp_compatible" default:"true"`
}

type SlackApp struct {
Expand All @@ -51,7 +52,7 @@ type Config struct {
Cortex cortex.Config `mapstructure:"cortex"`
NewRelic telemetry.NewRelicConfig `mapstructure:"newrelic"`
Service server.Config `mapstructure:"service"`
Log LogConfig `mapstructure:"log"`
Log Log `mapstructure:"log"`
SlackApp SlackApp `mapstructure:"slack_app"`
EncryptionKey string `mapstructure:"encryption_key"`
}
6 changes: 0 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,24 @@ require (
github.com/jackc/pgx/v4 v4.16.1
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.2
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-sqlite3 v1.14.12 // indirect
github.com/mcuadros/go-defaults v1.2.0
github.com/muesli/termenv v0.12.0 // indirect
github.com/newrelic/go-agent/v3 v3.12.0
github.com/newrelic/go-agent/v3/integrations/nrgrpc v1.3.1
github.com/odpf/salt v0.1.1-0.20220912101358-d28f61d005ca
github.com/ory/dockertest/v3 v3.9.1
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/prometheus/alertmanager v0.21.1-0.20200911160112-1fdff6b3f939
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/prometheus v1.8.2-0.20201014093524-73e2ce1bd643
github.com/schollz/progressbar/v3 v3.9.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/slack-go/slack v0.11.0
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.12.0 // indirect
github.com/stretchr/testify v1.8.0
github.com/subosito/gotenv v1.4.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.22.0
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e
golang.org/x/tools v0.1.11 // indirect
google.golang.org/genproto v0.0.0-20220805133916-01dd62135a58
google.golang.org/grpc v1.48.0
Expand Down
Loading