11package logger
22
33import (
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}
0 commit comments