Skip to content

Commit f643a2d

Browse files
authored
map telemetry metadata fields and compose logger
Signed-off-by: Evan Baker <[email protected]>
1 parent 0c7f2ee commit f643a2d

File tree

13 files changed

+115
-33
lines changed

13 files changed

+115
-33
lines changed

aitelemetry/telemetrywrapper.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package aitelemetry
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67
"runtime"
78
"time"
89

@@ -36,6 +37,8 @@ const (
3637
defaultRefreshTimeoutInSecs = 10
3738
)
3839

40+
var MetadataFile = filepath.Join(os.TempDir(), "azuremetadata.json")
41+
3942
type Level = contracts.SeverityLevel
4043

4144
const (
@@ -98,7 +101,7 @@ func getMetadata(th *telemetryHandle) {
98101

99102
// check if metadata in memory otherwise initiate wireserver request
100103
for {
101-
metadata, err = common.GetHostMetadata(metadataFile)
104+
metadata, err = common.GetHostMetadata(MetadataFile)
102105
if err == nil || th.disableMetadataRefreshThread {
103106
break
104107
}
@@ -117,14 +120,14 @@ func getMetadata(th *telemetryHandle) {
117120
th.metadata = metadata
118121
th.rwmutex.Unlock()
119122

120-
lockclient, err := processlock.NewFileLock(metadataFile + store.LockExtension)
123+
lockclient, err := processlock.NewFileLock(MetadataFile + store.LockExtension)
121124
if err != nil {
122125
log.Printf("Error initializing file lock:%v", err)
123126
return
124127
}
125128

126129
// Save metadata retrieved from wireserver to a file
127-
kvs, err := store.NewJsonFileStore(metadataFile, lockclient, nil)
130+
kvs, err := store.NewJsonFileStore(MetadataFile, lockclient, nil)
128131
if err != nil {
129132
debugLog("[AppInsights] Error initializing kvs store: %v", err)
130133
return
@@ -134,7 +137,7 @@ func getMetadata(th *telemetryHandle) {
134137
log.Errorf("getMetadata: Not able to acquire lock:%v", err)
135138
return
136139
}
137-
metadataErr := common.SaveHostMetadata(th.metadata, metadataFile)
140+
metadataErr := common.SaveHostMetadata(th.metadata, MetadataFile)
138141
err = kvs.Unlock()
139142
if err != nil {
140143
log.Errorf("getMetadata: Not able to release lock:%v", err)

aitelemetry/telemetrywrapper_linux.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

aitelemetry/telemetrywrapper_windows.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

cns/logger/v2/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func (c *Config) UnmarshalJSON(data []byte) error {
4242
return nil
4343
}
4444

45-
// Normalize checks the Config for missing or illegal values and sets them
46-
// to defaults if appropriate.
45+
// Normalize checks the Config for missing/default values and sets them
46+
// if appropriate.
4747
func (c *Config) Normalize() {
4848
if c.File != nil {
4949
if c.File.Filepath == "" {

cns/logger/v2/cores/ai.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ func ApplicationInsightsCore(cfg *AppInsightsConfig) (zapcore.Core, func(), erro
6262
core := zapai.NewCore(cfg.level, sink)
6363
core = core.WithFieldMappers(zapai.DefaultMappers)
6464
// add normalized fields for the built-in AI Tags
65-
// TODO(rbtr): move to the caller
65+
6666
return core.With(cfg.Fields), aiclose, nil
6767
}

cns/logger/v2/cores/etw_windows.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
11
package logger
22

33
import (
4+
"encoding/json"
5+
46
"github.com/Azure/azure-container-networking/zapetw"
7+
"github.com/pkg/errors"
58
"go.uber.org/zap"
69
"go.uber.org/zap/zapcore"
710
)
811

912
type ETWConfig struct {
10-
EventName string
11-
Level zapcore.Level
12-
ProviderName string
13+
EventName string `json:"eventname"`
14+
Level string `json:"level"`
15+
level zapcore.Level `json:"-"`
16+
ProviderName string `json:"providername"`
17+
Fields []zapcore.Field `json:"fields"`
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 *ETWConfig) UnmarshalJSON(data []byte) error {
24+
type Alias ETWConfig
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 ETWConfig")
32+
}
33+
lvl, err := zapcore.ParseLevel(cfg.Level)
34+
if err != nil {
35+
return errors.Wrap(err, "failed to parse ETWConfig Level")
36+
}
37+
cfg.level = lvl
38+
return nil
1339
}
1440

1541
// ETWCore builds a zapcore.Core that sends logs to ETW.
@@ -18,5 +44,5 @@ func ETWCore(cfg *ETWConfig) (zapcore.Core, func(), error) {
1844
encoderConfig := zap.NewProductionEncoderConfig()
1945
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
2046
jsonEncoder := zapcore.NewJSONEncoder(encoderConfig)
21-
return zapetw.New(cfg.ProviderName, cfg.EventName, jsonEncoder, cfg.Level) //nolint:wrapcheck // ignore
47+
return zapetw.New(cfg.ProviderName, cfg.EventName, jsonEncoder, cfg.level) //nolint:wrapcheck // ignore
2248
}

cns/logger/v2/cores/file.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import (
1010
)
1111

1212
type FileConfig struct {
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"`
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+
Fields []zapcore.Field `json:"fields"`
1819
}
1920

2021
// UnmarshalJSON implements json.Unmarshaler for the Config.

cns/logger/v2/cores/stdout.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
11
package logger
22

33
import (
4+
"encoding/json"
45
"os"
56

67
logfmt "github.com/jsternberg/zap-logfmt"
8+
"github.com/pkg/errors"
79
"go.uber.org/zap"
810
"go.uber.org/zap/zapcore"
911
)
1012

13+
type StdoutConfig struct {
14+
Level string `json:"level"`
15+
level zapcore.Level `json:"-"`
16+
Fields []zapcore.Field `json:"fields"`
17+
}
18+
19+
// UnmarshalJSON implements json.Unmarshaler for the Config.
20+
// It only differs from the default by parsing the
21+
// Level string into a zapcore.Level and setting the level field.
22+
func (cfg *StdoutConfig) UnmarshalJSON(data []byte) error {
23+
type Alias StdoutConfig
24+
aux := &struct {
25+
*Alias
26+
}{
27+
Alias: (*Alias)(cfg),
28+
}
29+
if err := json.Unmarshal(data, &aux); err != nil {
30+
return errors.Wrap(err, "failed to unmarshal StdoutConfig")
31+
}
32+
lvl, err := zapcore.ParseLevel(cfg.Level)
33+
if err != nil {
34+
return errors.Wrap(err, "failed to parse StdoutConfig Level")
35+
}
36+
cfg.level = lvl
37+
return nil
38+
}
39+
1140
// StdoutCore builds a zapcore.Core that writes to stdout.
1241
func StdoutCore(l zapcore.Level) zapcore.Core {
1342
encoderConfig := zap.NewProductionEncoderConfig()

cns/logger/v2/fields.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package logger
2+
3+
import (
4+
"github.com/Azure/azure-container-networking/common"
5+
"go.uber.org/zap"
6+
)
7+
8+
// MetadataToFields transforms Az IMDS Metadata in to zap.Field for
9+
// attaching to a root zap core or logger instance.
10+
// This uses the nice-names from the zapai.DefaultMappers instead of
11+
// raw AppInsights key names.
12+
func MetadataToFields(meta common.Metadata) []zap.Field {
13+
return []zap.Field{
14+
zap.String("account", meta.SubscriptionID),
15+
zap.String("anonymous_user_id", meta.VMName),
16+
zap.String("location", meta.Location),
17+
zap.String("resource_group", meta.ResourceGroupName),
18+
zap.String("vm_size", meta.VMSize),
19+
zap.String("os_version", meta.OSVersion),
20+
zap.String("vm_id", meta.VMID),
21+
zap.String("session_id", meta.VMID),
22+
}
23+
}

cns/logger/v2/logger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func (c compoundCloser) Close() {
1616
}
1717
}
1818

19+
// New creates a v2 CNS logger built with Zap.
1920
func New(cfg *Config) (*zap.Logger, func(), error) {
2021
cfg.Normalize()
2122
core := cores.StdoutCore(cfg.level)

0 commit comments

Comments
 (0)