Skip to content

Commit 10d660f

Browse files
authored
Introduce logging level configuration (#514)
* Introduce logging level configuration Signed-off-by: Pavol Loffay <ploffay@redhat.com> * Fix review comments Signed-off-by: Pavol Loffay <ploffay@redhat.com> * Add return Signed-off-by: Pavol Loffay <ploffay@redhat.com> * Remove debug printf Signed-off-by: Pavol Loffay <ploffay@redhat.com> * Revert empty select in agent Signed-off-by: Pavol Loffay <ploffay@redhat.com>
1 parent e0a74f2 commit 10d660f

File tree

5 files changed

+75
-35
lines changed

5 files changed

+75
-35
lines changed

cmd/agent/main.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package main
1616

1717
import (
18+
"fmt"
19+
"os"
1820
"runtime"
1921

2022
"github.com/pkg/errors"
@@ -30,14 +32,22 @@ import (
3032
)
3133

3234
func main() {
33-
logger, _ := zap.NewProduction()
3435
v := viper.New()
3536
var command = &cobra.Command{
3637
Use: "jaeger-agent",
3738
Short: "Jaeger agent is a local daemon program which collects tracing data.",
3839
Long: `Jaeger agent is a daemon program that runs on every host and receives tracing data submitted by Jaeger client libraries.`,
3940
RunE: func(cmd *cobra.Command, args []string) error {
40-
flags.TryLoadConfigFile(v, logger)
41+
err := flags.TryLoadConfigFile(v)
42+
if err != nil {
43+
return err
44+
}
45+
46+
sFlags := new(flags.SharedFlags).InitFromViper(v)
47+
logger, err := sFlags.NewLogger(zap.NewProductionConfig())
48+
if err != nil {
49+
return err
50+
}
4151

4252
builder := &app.Builder{}
4353
builder.InitFromViper(v)
@@ -70,6 +80,7 @@ func main() {
7080
)
7181

7282
if err := command.Execute(); err != nil {
73-
logger.Fatal("agent command failed", zap.Error(err))
83+
fmt.Println(err.Error())
84+
os.Exit(1)
7485
}
7586
}

cmd/collector/main.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package main
1616

1717
import (
18+
"fmt"
1819
"net"
1920
"net/http"
2021
"os"
@@ -50,7 +51,6 @@ func main() {
5051
var signalsChannel = make(chan os.Signal, 0)
5152
signal.Notify(signalsChannel, os.Interrupt, syscall.SIGTERM)
5253

53-
logger, _ := zap.NewProduction()
5454
serviceName := "jaeger-collector"
5555
casOptions := casFlags.NewOptions("cassandra")
5656
esOptions := esFlags.NewOptions("es")
@@ -61,10 +61,18 @@ func main() {
6161
Short: "Jaeger collector receives and processes traces from Jaeger agents and clients",
6262
Long: `Jaeger collector receives traces from Jaeger agents and agent and runs them through
6363
a processing pipeline.`,
64-
Run: func(cmd *cobra.Command, args []string) {
65-
flags.TryLoadConfigFile(v, logger)
64+
RunE: func(cmd *cobra.Command, args []string) error {
65+
err := flags.TryLoadConfigFile(v)
66+
if err != nil {
67+
return err
68+
}
6669

6770
sFlags := new(flags.SharedFlags).InitFromViper(v)
71+
logger, err := sFlags.NewLogger(zap.NewProductionConfig())
72+
if err != nil {
73+
return err
74+
}
75+
6876
casOptions.InitFromViper(v)
6977
esOptions.InitFromViper(v)
7078

@@ -127,6 +135,7 @@ func main() {
127135
case <-signalsChannel:
128136
logger.Info("Jaeger Collector is finishing")
129137
}
138+
return nil
130139
},
131140
}
132141

@@ -143,7 +152,8 @@ func main() {
143152
)
144153

145154
if error := command.Execute(); error != nil {
146-
logger.Fatal(error.Error())
155+
fmt.Println(error.Error())
156+
os.Exit(1)
147157
}
148158
}
149159

cmd/flags/flags.go

+20-19
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
package flags
1616

1717
import (
18-
"errors"
1918
"flag"
2019
"fmt"
21-
"strings"
2220
"time"
2321

22+
"github.com/pkg/errors"
2423
"github.com/spf13/viper"
2524
"go.uber.org/zap"
25+
"go.uber.org/zap/zapcore"
2626
)
2727

2828
const (
@@ -44,14 +44,26 @@ func AddConfigFileFlag(flagSet *flag.FlagSet) {
4444
}
4545

4646
// TryLoadConfigFile initializes viper with config file specified as flag
47-
func TryLoadConfigFile(v *viper.Viper, logger *zap.Logger) {
47+
func TryLoadConfigFile(v *viper.Viper) error {
4848
if file := v.GetString(configFile); file != "" {
4949
v.SetConfigFile(file)
5050
err := v.ReadInConfig()
5151
if err != nil {
52-
logger.Fatal("Error loading config file", zap.Error(err), zap.String(configFile, file))
52+
return errors.Wrapf(err, "Error loading config file %s", file)
5353
}
5454
}
55+
return nil
56+
}
57+
58+
// NewLogger returns logger based on configuration in SharedFlags
59+
func (flags *SharedFlags) NewLogger(conf zap.Config, options ...zap.Option) (*zap.Logger, error) {
60+
var level zapcore.Level
61+
err := (&level).UnmarshalText([]byte(flags.Logging.Level))
62+
if err != nil {
63+
return nil, err
64+
}
65+
conf.Level = zap.NewAtomicLevelAt(level)
66+
return conf.Build(options...)
5567
}
5668

5769
// SharedFlags holds flags configuration
@@ -60,19 +72,22 @@ type SharedFlags struct {
6072
SpanStorage spanStorage
6173
// DependencyStorage defines common settings for Dependency Storage.
6274
DependencyStorage dependencyStorage
75+
// Logging holds logging configuration
76+
Logging logging
6377
}
6478

6579
// InitFromViper initializes SharedFlags with properties from viper
6680
func (flags *SharedFlags) InitFromViper(v *viper.Viper) *SharedFlags {
6781
flags.SpanStorage.Type = v.GetString(spanStorageType)
6882
flags.DependencyStorage.DataFrequency = v.GetDuration(dependencyStorageDataFrequency)
83+
flags.Logging.Level = v.GetString(logLevel)
6984
return flags
7085
}
7186

7287
// AddFlags adds flags for SharedFlags
7388
func AddFlags(flagSet *flag.FlagSet) {
7489
flagSet.String(spanStorageType, CassandraStorageType, fmt.Sprintf("The type of span storage backend to use, options are currently [%v,%v,%v]", CassandraStorageType, ESStorageType, MemoryStorageType))
75-
flagSet.String(logLevel, "info", "Minimal allowed log level")
90+
flagSet.String(logLevel, "info", "Minimal allowed log Level. For more levels see https://github.com/uber-go/zap")
7691
flagSet.Duration(dependencyStorageDataFrequency, time.Hour*24, "Frequency of service dependency calculations")
7792
}
7893

@@ -88,19 +103,5 @@ type spanStorage struct {
88103
}
89104

90105
type dependencyStorage struct {
91-
Type string
92106
DataFrequency time.Duration
93107
}
94-
95-
type cassandraOptions struct {
96-
ConnectionsPerHost int
97-
MaxRetryAttempts int
98-
QueryTimeout time.Duration
99-
Servers string
100-
Port int
101-
Keyspace string
102-
}
103-
104-
func (co cassandraOptions) ServerList() []string {
105-
return strings.Split(co.Servers, ",")
106-
}

cmd/query/main.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package main
1616

1717
import (
18+
"fmt"
1819
"net/http"
1920
"os"
2021
"os/signal"
@@ -45,7 +46,6 @@ func main() {
4546
var serverChannel = make(chan os.Signal, 0)
4647
signal.Notify(serverChannel, os.Interrupt, syscall.SIGTERM)
4748

48-
logger, _ := zap.NewProduction()
4949
casOptions := casFlags.NewOptions("cassandra", "cassandra.archive")
5050
esOptions := esFlags.NewOptions("es", "es.archive")
5151
v := viper.New()
@@ -54,10 +54,18 @@ func main() {
5454
Use: "jaeger-query",
5555
Short: "Jaeger query is a service to access tracing data",
5656
Long: `Jaeger query is a service to access tracing data and host UI.`,
57-
Run: func(cmd *cobra.Command, args []string) {
58-
flags.TryLoadConfigFile(v, logger)
57+
RunE: func(cmd *cobra.Command, args []string) error {
58+
err := flags.TryLoadConfigFile(v)
59+
if err != nil {
60+
return err
61+
}
5962

6063
sFlags := new(flags.SharedFlags).InitFromViper(v)
64+
logger, err := sFlags.NewLogger(zap.NewProductionConfig())
65+
if err != nil {
66+
return err
67+
}
68+
6169
casOptions.InitFromViper(v)
6270
esOptions.InitFromViper(v)
6371
queryOpts := new(builder.QueryOptions).InitFromViper(v)
@@ -119,6 +127,7 @@ func main() {
119127
case <-serverChannel:
120128
logger.Info("Jaeger Query is finishing")
121129
}
130+
return nil
122131
},
123132
}
124133

@@ -135,7 +144,8 @@ func main() {
135144
)
136145

137146
if error := command.Execute(); error != nil {
138-
logger.Fatal(error.Error())
147+
fmt.Println(error.Error())
148+
os.Exit(1)
139149
}
140150
}
141151

cmd/standalone/main.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"net"
2020
"net/http"
21+
"os"
2122
"runtime"
2223
"strconv"
2324

@@ -51,19 +52,25 @@ import (
5152

5253
// standalone/main is a standalone full-stack jaeger backend, backed by a memory store
5354
func main() {
54-
logger, _ := zap.NewProduction()
5555
v := viper.New()
56-
5756
command := &cobra.Command{
5857
Use: "jaeger-standalone",
5958
Short: "Jaeger all-in-one distribution with agent, collector and query in one process.",
6059
Long: `Jaeger all-in-one distribution with agent, collector and query. Use with caution this version
6160
uses only in-memory database.`,
6261
RunE: func(cmd *cobra.Command, args []string) error {
63-
flags.TryLoadConfigFile(v, logger)
62+
err := flags.TryLoadConfigFile(v)
63+
if err != nil {
64+
return err
65+
}
6466

65-
runtime.GOMAXPROCS(runtime.NumCPU())
6667
sFlags := new(flags.SharedFlags).InitFromViper(v)
68+
logger, err := sFlags.NewLogger(zap.NewProductionConfig())
69+
if err != nil {
70+
return err
71+
}
72+
73+
runtime.GOMAXPROCS(runtime.NumCPU())
6774
cOpts := new(collector.CollectorOptions).InitFromViper(v)
6875
qOpts := new(query.QueryOptions).InitFromViper(v)
6976

@@ -93,7 +100,8 @@ func main() {
93100
)
94101

95102
if err := command.Execute(); err != nil {
96-
logger.Fatal("standalone command failed", zap.Error(err))
103+
fmt.Println(err.Error())
104+
os.Exit(1)
97105
}
98106
}
99107

0 commit comments

Comments
 (0)