Skip to content

Commit d3bbfc8

Browse files
authored
update loggerv2 configs, add custom Unmarshalling
Signed-off-by: Evan Baker <[email protected]>
1 parent 3762c0d commit d3bbfc8

File tree

7 files changed

+125
-43
lines changed

7 files changed

+125
-43
lines changed

cns/logger/log.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
)
88

99
var (
10-
Log *CNSLogger
11-
aiMetadata string // this var is set at build time.
10+
Log *CNSLogger
11+
aiMetadata string // this var is set at build time.
12+
AppInsightsIKey = aiMetadata
1213
)
1314

1415
// todo: the functions below should be removed. CNSLogger should be injected where needed and not used from package level scope.

cns/logger/v2/config.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package logger
2+
3+
import (
4+
"encoding/json"
5+
"time"
6+
7+
loggerv1 "github.com/Azure/azure-container-networking/cns/logger"
8+
cores "github.com/Azure/azure-container-networking/cns/logger/v2/cores"
9+
"github.com/pkg/errors"
10+
"go.uber.org/zap/zapcore"
11+
)
12+
13+
//nolint:unused // will be used
14+
const (
15+
defaultMaxBackups = 10
16+
defaultMaxSize = 10 // MB
17+
defaultMaxBatchInterval = 30 * time.Second
18+
defaultMaxBatchSize = 32000
19+
defaultGracePeriod = 30 * time.Second
20+
)
21+
22+
//nolint:unused // will be used
23+
var defaultIKey = loggerv1.AppInsightsIKey
24+
25+
type Config struct {
26+
// Level is the general logging Level. If cores have more specific config it will override this.
27+
Level string `json:"level"`
28+
level zapcore.Level `json:"-"`
29+
AppInsights cores.AppInsightsConfig `json:"appInsights"`
30+
File cores.FileConfig `json:"file"`
31+
}
32+
33+
// UnmarshalJSON implements json.Unmarshaler for the Config.
34+
// It only differs from the default by parsing the
35+
// Level string into a zapcore.Level and setting the level field.
36+
func (c *Config) UnmarshalJSON(data []byte) error {
37+
type Alias Config
38+
aux := &struct {
39+
*Alias
40+
}{
41+
Alias: (*Alias)(c),
42+
}
43+
if err := json.Unmarshal(data, &aux); err != nil {
44+
return errors.Wrap(err, "failed to unmarshal Config")
45+
}
46+
if l, err := zapcore.ParseLevel(c.Level); err == nil {
47+
c.level = l
48+
}
49+
return nil
50+
}

cns/logger/v2/cores/ai.go

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package logger
22

33
import (
4-
"runtime"
4+
"encoding/json"
55
"time"
66

77
"github.com/Azure/azure-container-networking/zapai"
@@ -11,17 +11,40 @@ import (
1111
"go.uber.org/zap/zapcore"
1212
)
1313

14-
type AIConfig struct {
15-
GracePeriod time.Duration
16-
IKey string
17-
Level zapcore.Level
18-
MaxBatchInterval time.Duration
19-
MaxBatchSize int
14+
type AppInsightsConfig struct {
15+
GracePeriod time.Duration `json:"grace_period"`
16+
IKey string `json:"ikey"`
17+
Level string `json:"level"`
18+
level zapcore.Level `json:"-"` // Zero value is default Info level.
19+
MaxBatchInterval time.Duration `json:"max_batch_interval"`
20+
MaxBatchSize int `json:"max_batch_size"`
21+
Fields []zapcore.Field `json:"fields"`
22+
}
23+
24+
// UnmarshalJSON implements json.Unmarshaler for the Config.
25+
// It only differs from the default by parsing the
26+
// Level string into a zapcore.Level and setting the level field.
27+
func (c *AppInsightsConfig) UnmarshalJSON(data []byte) error {
28+
type Alias AppInsightsConfig
29+
aux := &struct {
30+
*Alias
31+
}{
32+
Alias: (*Alias)(c),
33+
}
34+
if err := json.Unmarshal(data, &aux); err != nil {
35+
return errors.Wrap(err, "failed to unmarshal AppInsightsConfig")
36+
}
37+
lvl, err := zapcore.ParseLevel(c.Level)
38+
if err != nil {
39+
return errors.Wrap(err, "failed to parse AppInsightsConfig Level")
40+
}
41+
c.level = lvl
42+
return nil
2043
}
2144

2245
// ApplicationInsightsCore builds a zapcore.Core that sends logs to Application Insights.
2346
// The first return is the core, the second is a function to close the sink.
24-
func ApplicationInsightsCore(cfg *AIConfig) (zapcore.Core, func(), error) {
47+
func ApplicationInsightsCore(cfg *AppInsightsConfig) (zapcore.Core, func(), error) {
2548
// build the AI config
2649
aicfg := *appinsights.NewTelemetryConfiguration(cfg.IKey)
2750
aicfg.MaxBatchSize = cfg.MaxBatchSize
@@ -36,23 +59,9 @@ func ApplicationInsightsCore(cfg *AIConfig) (zapcore.Core, func(), error) {
3659
return nil, aiclose, errors.Wrap(err, "failed to open AI sink")
3760
}
3861
// build the AI core
39-
core := zapai.NewCore(cfg.Level, sink)
62+
core := zapai.NewCore(cfg.level, sink)
4063
core = core.WithFieldMappers(zapai.DefaultMappers)
4164
// add normalized fields for the built-in AI Tags
4265
// TODO(rbtr): move to the caller
43-
return core.With([]zapcore.Field{
44-
zap.String("user_id", runtime.GOOS),
45-
zap.String("operation_id", ""),
46-
zap.String("parent_id", "v0.0.0"),
47-
zap.String("version", "v0.0.0"),
48-
zap.String("account", "SubscriptionID"),
49-
zap.String("anonymous_user_id", "VMName"),
50-
zap.String("session_id", "VMID"),
51-
zap.String("AppName", "name"),
52-
zap.String("Region", "Location"),
53-
zap.String("ResourceGroup", "ResourceGroupName"),
54-
zap.String("VMSize", "VMSize"),
55-
zap.String("OSVersion", "OSVersion"),
56-
zap.String("VMID", "VMID"),
57-
}), aiclose, nil
66+
return core.With(cfg.Fields), aiclose, nil
5867
}

cns/logger/v2/cores/file.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
11
package logger
22

33
import (
4+
"encoding/json"
5+
6+
"github.com/pkg/errors"
47
"go.uber.org/zap"
58
"go.uber.org/zap/zapcore"
69
"gopkg.in/natefinch/lumberjack.v2"
710
)
811

912
type FileConfig struct {
10-
Filepath string
11-
Level zapcore.Level
12-
MaxBackups int
13-
MaxSize int
13+
Filepath string `json:"filepath"`
14+
Level string `json:"level"`
15+
level zapcore.Level `json:"-"`
16+
MaxBackups int `json:"maxBackups"`
17+
MaxSize int `json:"maxSize"`
18+
}
19+
20+
// UnmarshalJSON implements json.Unmarshaler for the Config.
21+
// It only differs from the default by parsing the
22+
// Level string into a zapcore.Level and setting the level field.
23+
func (cfg *FileConfig) UnmarshalJSON(data []byte) error {
24+
type Alias FileConfig
25+
aux := &struct {
26+
*Alias
27+
}{
28+
Alias: (*Alias)(cfg),
29+
}
30+
if err := json.Unmarshal(data, &aux); err != nil {
31+
return errors.Wrap(err, "failed to unmarshal FileConfig")
32+
}
33+
lvl, err := zapcore.ParseLevel(cfg.Level)
34+
if err != nil {
35+
return errors.Wrap(err, "failed to parse FileConfig Level")
36+
}
37+
cfg.level = lvl
38+
return nil
1439
}
1540

1641
// FileCore builds a zapcore.Core that writes to a file.
1742
// The first return is the core, the second is a function to close the file.
1843
func FileCore(cfg *FileConfig) (zapcore.Core, func(), error) {
1944
filesink := &lumberjack.Logger{
2045
Filename: cfg.Filepath,
21-
MaxSize: cfg.MaxSize, // MegaBytes
46+
MaxSize: cfg.MaxSize, // MB
2247
MaxBackups: cfg.MaxBackups,
2348
}
2449
encoderConfig := zap.NewProductionEncoderConfig()
2550
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
2651
jsonEncoder := zapcore.NewJSONEncoder(encoderConfig)
27-
return zapcore.NewCore(jsonEncoder, zapcore.AddSync(filesink), cfg.Level), func() { _ = filesink.Close() }, nil
52+
return zapcore.NewCore(jsonEncoder, zapcore.AddSync(filesink), cfg.level), func() { _ = filesink.Close() }, nil
2853
}

cns/logger/v2/logger.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ import (
66
"go.uber.org/zap/zapcore"
77
)
88

9-
type Config struct {
10-
// Level is the general logging Level. If cores have more specific config it will override this.
11-
Level zapcore.Level
12-
AIConfig cores.AIConfig
13-
FileConfig cores.FileConfig
14-
}
15-
169
type compoundCloser []func()
1710

1811
func (c compoundCloser) Close() {
@@ -22,14 +15,14 @@ func (c compoundCloser) Close() {
2215
}
2316

2417
func New(cfg *Config) (*zap.Logger, func(), error) {
25-
stdoutCore := cores.StdoutCore(cfg.Level)
18+
stdoutCore := cores.StdoutCore(cfg.level)
2619
closer := compoundCloser{}
27-
fileCore, fileCloser, err := cores.FileCore(&cfg.FileConfig)
20+
fileCore, fileCloser, err := cores.FileCore(&cfg.File)
2821
closer = append(closer, fileCloser)
2922
if err != nil {
3023
return nil, closer.Close, err //nolint:wrapcheck // it's an internal pkg
3124
}
32-
aiCore, aiCloser, err := cores.ApplicationInsightsCore(&cfg.AIConfig)
25+
aiCore, aiCloser, err := cores.ApplicationInsightsCore(&cfg.AppInsights)
3326
closer = append(closer, aiCloser)
3427
if err != nil {
3528
return nil, closer.Close, err //nolint:wrapcheck // it's an internal pkg

cns/logger/v2/logger_linux.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"go.uber.org/zap/zapcore"
55
)
66

7+
const defaultFilePath = "/var/log/azure-cns.log"
8+
79
// platformCore returns a no-op core for Linux.
810
func platformCore(*Config) (zapcore.Core, func(), error) {
911
return zapcore.NewNopCore(), func() {}, nil

cns/logger/v2/logger_windows.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"go.uber.org/zap/zapcore"
66
)
77

8+
const defaultFilePath = "/k/azurecns/azure-cns.log"
9+
810
// platformCore returns a zapcore.Core that sends logs to ETW.
911
func platformCore(cfg *Config) (zapcore.Core, func(), error) {
1012
return cores.ETWCore(&cores.ETWConfig{ //nolint:wrapcheck // ignore
1113
EventName: "AzureCNS",
12-
Level: cfg.Level,
14+
Level: cfg.level,
1315
ProviderName: "ACN-Monitoring",
1416
})
1517
}

0 commit comments

Comments
 (0)