mirror of https://github.com/grafana/loki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.7 KiB
121 lines
3.7 KiB
![]()
7 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
![]()
6 years ago
|
"fmt"
|
||
![]()
7 years ago
|
"os"
|
||
![]()
6 years ago
|
"reflect"
|
||
![]()
7 years ago
|
|
||
![]()
6 years ago
|
"k8s.io/klog"
|
||
|
|
||
![]()
5 years ago
|
"github.com/cortexproject/cortex/pkg/util/flagext"
|
||
![]()
4 years ago
|
util_log "github.com/cortexproject/cortex/pkg/util/log"
|
||
![]()
7 years ago
|
"github.com/go-kit/kit/log/level"
|
||
![]()
7 years ago
|
"github.com/prometheus/client_golang/prometheus"
|
||
|
"github.com/prometheus/common/version"
|
||
![]()
6 years ago
|
"github.com/weaveworks/common/logging"
|
||
![]()
7 years ago
|
|
||
![]()
4 years ago
|
// embed time zone data
|
||
|
_ "time/tzdata"
|
||
|
|
||
![]()
4 years ago
|
"github.com/grafana/loki/clients/pkg/logentry/stages"
|
||
|
"github.com/grafana/loki/clients/pkg/promtail"
|
||
|
"github.com/grafana/loki/clients/pkg/promtail/config"
|
||
|
|
||
![]()
5 years ago
|
logutil "github.com/grafana/loki/pkg/util"
|
||
![]()
4 years ago
|
_ "github.com/grafana/loki/pkg/util/build"
|
||
|
"github.com/grafana/loki/pkg/util/cfg"
|
||
![]()
7 years ago
|
)
|
||
|
|
||
![]()
7 years ago
|
func init() {
|
||
|
prometheus.MustRegister(version.NewCollector("promtail"))
|
||
|
}
|
||
|
|
||
![]()
5 years ago
|
type Config struct {
|
||
![]()
5 years ago
|
config.Config `yaml:",inline"`
|
||
|
printVersion bool
|
||
|
printConfig bool
|
||
|
logConfig bool
|
||
|
dryRun bool
|
||
|
configFile string
|
||
|
configExpandEnv bool
|
||
![]()
5 years ago
|
}
|
||
|
|
||
|
func (c *Config) RegisterFlags(f *flag.FlagSet) {
|
||
|
f.BoolVar(&c.printVersion, "version", false, "Print this builds version information")
|
||
|
f.BoolVar(&c.printConfig, "print-config-stderr", false, "Dump the entire Loki config object to stderr")
|
||
|
f.BoolVar(&c.logConfig, "log-config-reverse-order", false, "Dump the entire Loki config object at Info log "+
|
||
![]()
5 years ago
|
"level with the order reversed, reversing the order makes viewing the entries easier in Grafana.")
|
||
![]()
5 years ago
|
f.BoolVar(&c.dryRun, "dry-run", false, "Start Promtail but print entries instead of sending them to Loki.")
|
||
|
f.StringVar(&c.configFile, "config.file", "", "yaml file to load")
|
||
![]()
5 years ago
|
f.BoolVar(&c.configExpandEnv, "config.expand-env", false, "Expands ${var} in config according to the values of the environment variables.")
|
||
![]()
5 years ago
|
c.Config.RegisterFlags(f)
|
||
|
}
|
||
|
|
||
|
// Clone takes advantage of pass-by-value semantics to return a distinct *Config.
|
||
|
// This is primarily used to parse a different flag set without mutating the original *Config.
|
||
|
func (c *Config) Clone() flagext.Registerer {
|
||
|
return func(c Config) *Config {
|
||
|
return &c
|
||
|
}(*c)
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
![]()
6 years ago
|
// Load config, merging config file and CLI flags
|
||
![]()
5 years ago
|
var config Config
|
||
![]()
6 years ago
|
if err := cfg.Parse(&config); err != nil {
|
||
![]()
6 years ago
|
fmt.Println("Unable to parse config:", err)
|
||
![]()
6 years ago
|
os.Exit(1)
|
||
|
}
|
||
![]()
6 years ago
|
|
||
|
// Handle -version CLI flag
|
||
![]()
5 years ago
|
if config.printVersion {
|
||
![]()
6 years ago
|
fmt.Println(version.Print("promtail"))
|
||
![]()
6 years ago
|
os.Exit(0)
|
||
|
}
|
||
|
|
||
![]()
6 years ago
|
// Init the logger which will honor the log level set in cfg.Server
|
||
![]()
6 years ago
|
if reflect.DeepEqual(&config.ServerConfig.Config.LogLevel, &logging.Level{}) {
|
||
![]()
6 years ago
|
fmt.Println("Invalid log level")
|
||
![]()
6 years ago
|
os.Exit(1)
|
||
|
}
|
||
![]()
4 years ago
|
util_log.InitLogger(&config.ServerConfig.Config)
|
||
![]()
6 years ago
|
|
||
![]()
6 years ago
|
// Use Stderr instead of files for the klog.
|
||
|
klog.SetOutput(os.Stderr)
|
||
|
|
||
![]()
6 years ago
|
// Set the global debug variable in the stages package which is used to conditionally log
|
||
|
// debug messages which otherwise cause huge allocations processing log lines for log messages never printed
|
||
|
if config.ServerConfig.Config.LogLevel.String() == "debug" {
|
||
|
stages.Debug = true
|
||
|
}
|
||
|
|
||
![]()
5 years ago
|
if config.printConfig {
|
||
![]()
5 years ago
|
err := logutil.PrintConfig(os.Stderr, &config)
|
||
|
if err != nil {
|
||
![]()
4 years ago
|
level.Error(util_log.Logger).Log("msg", "failed to print config to stderr", "err", err.Error())
|
||
![]()
5 years ago
|
}
|
||
|
}
|
||
|
|
||
![]()
5 years ago
|
if config.logConfig {
|
||
![]()
5 years ago
|
err := logutil.LogConfig(&config)
|
||
|
if err != nil {
|
||
![]()
4 years ago
|
level.Error(util_log.Logger).Log("msg", "failed to log config object", "err", err.Error())
|
||
![]()
5 years ago
|
}
|
||
|
}
|
||
|
|
||
![]()
5 years ago
|
p, err := promtail.New(config.Config, config.dryRun)
|
||
![]()
7 years ago
|
if err != nil {
|
||
![]()
4 years ago
|
level.Error(util_log.Logger).Log("msg", "error creating promtail", "error", err)
|
||
![]()
7 years ago
|
os.Exit(1)
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
4 years ago
|
level.Info(util_log.Logger).Log("msg", "Starting Promtail", "version", version.Info())
|
||
![]()
5 years ago
|
defer p.Shutdown()
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
if err := p.Run(); err != nil {
|
||
![]()
4 years ago
|
level.Error(util_log.Logger).Log("msg", "error starting promtail", "error", err)
|
||
![]()
7 years ago
|
os.Exit(1)
|
||
![]()
7 years ago
|
}
|
||
|
}
|