Skip to content

Commit a151c83

Browse files
AITelemetry support for CNS (#455)
* Added logger package for CNS * added aitelemetry for cns * Added AI Telemetry for CNS * added respective folders in makefile and added config file * added all config fields * addressed review comments * removing repetitive log * logged configpath * reverted to keep old telemetry channel and pulled new ai package * removed unwanted logs..added log file
1 parent df012bc commit a151c83

File tree

20 files changed

+606
-311
lines changed

20 files changed

+606
-311
lines changed

Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ CNSFILES = \
3838
$(wildcard cns/*.go) \
3939
$(wildcard cns/cnsclient/*.go) \
4040
$(wildcard cns/common/*.go) \
41+
$(wildcard cns/configuration/*.go) \
4142
$(wildcard cns/dockerclient/*.go) \
4243
$(wildcard cns/imdsclient/*.go) \
4344
$(wildcard cns/ipamclient/*.go) \
4445
$(wildcard cns/hnsclient/*.go) \
46+
$(wildcard cns/logger/*.go) \
4547
$(wildcard cns/nmagentclient/*.go) \
4648
$(wildcard cns/restserver/*.go) \
4749
$(wildcard cns/routes/*.go) \
@@ -124,7 +126,8 @@ AZURE_VNET_TELEMETRY_IMAGE = containernetworking.azurecr.io/public/containernetw
124126
AZURE_CNS_IMAGE = containernetworking.azurecr.io/public/containernetworking/azure-cns
125127

126128
VERSION ?= $(shell git describe --tags --always --dirty)
127-
129+
CNS_AI_ID = ce672799-8f08-4235-8c12-08563dc2acef
130+
cnsaipath=github.com/Azure/azure-container-networking/cns/logger.aiMetadata
128131
ENSURE_OUTPUT_DIR_EXISTS := $(shell mkdir -p $(OUTPUT_DIR))
129132

130133
# Shorthand target names for convenience.
@@ -176,7 +179,7 @@ $(CNI_BUILD_DIR)/azure-vnet-telemetry$(EXE_EXT): $(CNIFILES)
176179

177180
# Build the Azure CNS Service.
178181
$(CNS_BUILD_DIR)/azure-cns$(EXE_EXT): $(CNSFILES)
179-
go build -v -o $(CNS_BUILD_DIR)/azure-cns$(EXE_EXT) -ldflags "-X main.version=$(VERSION) -s -w" $(CNS_DIR)/*.go
182+
go build -v -o $(CNS_BUILD_DIR)/azure-cns$(EXE_EXT) -ldflags "-X main.version=$(VERSION) -X $(cnsaipath)=$(CNS_AI_ID) -s -w" $(CNS_DIR)/*.go
180183

181184
# Build the Azure NPM plugin.
182185
$(NPM_BUILD_DIR)/azure-npm$(EXE_EXT): $(NPMFILES)
@@ -317,8 +320,9 @@ cnm-archive:
317320
# Create a CNS archive for the target platform.
318321
.PHONY: cns-archive
319322
cns-archive:
323+
cp cns/configuration/cns_config.json $(CNS_BUILD_DIR)/cns_config.json
320324
chmod 0755 $(CNS_BUILD_DIR)/azure-cns$(EXE_EXT)
321-
cd $(CNS_BUILD_DIR) && $(ARCHIVE_CMD) $(CNS_ARCHIVE_NAME) azure-cns$(EXE_EXT)
325+
cd $(CNS_BUILD_DIR) && $(ARCHIVE_CMD) $(CNS_ARCHIVE_NAME) azure-cns$(EXE_EXT) cns_config.json
322326
chown $(BUILD_USER):$(BUILD_USER) $(CNS_BUILD_DIR)/$(CNS_ARCHIVE_NAME)
323327

324328
# Create a NPM archive for the target platform. Only Linux is supported for now.

aitelemetry/telemetrywrapper.go

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,48 @@ import (
1212
)
1313

1414
const (
15-
resourceGroupStr = "ResourceGroup"
16-
vmSizeStr = "VMSize"
17-
osVersionStr = "OSVersion"
18-
locationStr = "Region"
19-
appNameStr = "AppName"
20-
subscriptionIDStr = "SubscriptionID"
21-
vmNameStr = "VMName"
22-
vmIDStr = "VMID"
23-
versionStr = "AppVersion"
24-
azurePublicCloudStr = "AzurePublicCloud"
25-
defaultTimeout = 10
15+
resourceGroupStr = "ResourceGroup"
16+
vmSizeStr = "VMSize"
17+
osVersionStr = "OSVersion"
18+
locationStr = "Region"
19+
appNameStr = "AppName"
20+
subscriptionIDStr = "SubscriptionID"
21+
vmNameStr = "VMName"
22+
vmIDStr = "VMID"
23+
versionStr = "AppVersion"
24+
azurePublicCloudStr = "AzurePublicCloud"
25+
defaultTimeout = 10
26+
defaultBatchIntervalInSecs = 15
27+
defaultBatchSizeInBytes = 32768
28+
defaultGetEnvRetryCount = 5
29+
defaultGetEnvRetryWaitTimeInSecs = 3
30+
defaultRefreshTimeoutInSecs = 10
2631
)
2732

2833
var debugMode bool
2934

35+
func setAIConfigDefaults(config *AIConfig) {
36+
if config.RefreshTimeout == 0 {
37+
config.RefreshTimeout = defaultRefreshTimeoutInSecs
38+
}
39+
40+
if config.BatchInterval == 0 {
41+
config.BatchInterval = defaultBatchIntervalInSecs
42+
}
43+
44+
if config.BatchSize == 0 {
45+
config.BatchSize = defaultBatchSizeInBytes
46+
}
47+
48+
if config.GetEnvRetryCount == 0 {
49+
config.GetEnvRetryCount = defaultGetEnvRetryCount
50+
}
51+
52+
if config.GetEnvRetryWaitTimeInSecs == 0 {
53+
config.GetEnvRetryWaitTimeInSecs = defaultGetEnvRetryWaitTimeInSecs
54+
}
55+
}
56+
3057
func messageListener() appinsights.DiagnosticsMessageListener {
3158
if debugMode {
3259
return appinsights.NewDiagnosticsMessageListener(func(msg string) error {
@@ -124,6 +151,8 @@ func NewAITelemetry(
124151
return nil, fmt.Errorf("AI key is empty")
125152
}
126153

154+
setAIConfigDefaults(&aiConfig)
155+
127156
// check if azure instance is in public cloud
128157
isPublic, err := isPublicEnvironment(azEnvUrl, aiConfig.GetEnvRetryCount, aiConfig.GetEnvRetryWaitTimeInSecs)
129158
if !isPublic {

cns/cnsclient/cnsclient.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"net/http"
88

99
"github.com/Azure/azure-container-networking/cns"
10-
"github.com/Azure/azure-container-networking/log"
10+
"github.com/Azure/azure-container-networking/cns/logger"
1111
)
1212

1313
// CNSClient specifies a client to connect to Ipam Plugin.
@@ -55,42 +55,42 @@ func (cnsClient *CNSClient) GetNetworkConfiguration(orchestratorContext []byte)
5555

5656
httpc := &http.Client{}
5757
url := cnsClient.connectionURL + cns.GetNetworkContainerByOrchestratorContext
58-
log.Printf("GetNetworkConfiguration url %v", url)
58+
logger.Printf("GetNetworkConfiguration url %v", url)
5959

6060
payload := &cns.GetNetworkContainerRequest{
6161
OrchestratorContext: orchestratorContext,
6262
}
6363

6464
err := json.NewEncoder(&body).Encode(payload)
6565
if err != nil {
66-
log.Errorf("encoding json failed with %v", err)
66+
logger.Errorf("encoding json failed with %v", err)
6767
return nil, err
6868
}
6969

7070
res, err := httpc.Post(url, "application/json", &body)
7171
if err != nil {
72-
log.Errorf("[Azure CNSClient] HTTP Post returned error %v", err.Error())
72+
logger.Errorf("[Azure CNSClient] HTTP Post returned error %v", err.Error())
7373
return nil, err
7474
}
7575

7676
defer res.Body.Close()
7777

7878
if res.StatusCode != http.StatusOK {
7979
errMsg := fmt.Sprintf("[Azure CNSClient] GetNetworkConfiguration invalid http status code: %v", res.StatusCode)
80-
log.Errorf(errMsg)
80+
logger.Errorf(errMsg)
8181
return nil, fmt.Errorf(errMsg)
8282
}
8383

8484
var resp cns.GetNetworkContainerResponse
8585

8686
err = json.NewDecoder(res.Body).Decode(&resp)
8787
if err != nil {
88-
log.Errorf("[Azure CNSClient] Error received while parsing GetNetworkConfiguration response resp:%v err:%v", res.Body, err.Error())
88+
logger.Errorf("[Azure CNSClient] Error received while parsing GetNetworkConfiguration response resp:%v err:%v", res.Body, err.Error())
8989
return nil, err
9090
}
9191

9292
if resp.Response.ReturnCode != 0 {
93-
log.Errorf("[Azure CNSClient] GetNetworkConfiguration received error response :%v", resp.Response.Message)
93+
logger.Errorf("[Azure CNSClient] GetNetworkConfiguration received error response :%v", resp.Response.Message)
9494
return nil, fmt.Errorf(resp.Response.Message)
9595
}
9696

@@ -107,20 +107,20 @@ func (cnsClient *CNSClient) CreateHostNCApipaEndpoint(
107107

108108
httpc := &http.Client{}
109109
url := cnsClient.connectionURL + cns.CreateHostNCApipaEndpointPath
110-
log.Printf("CreateHostNCApipaEndpoint url: %v for NC: %s", url, networkContainerID)
110+
logger.Printf("CreateHostNCApipaEndpoint url: %v for NC: %s", url, networkContainerID)
111111

112112
payload := &cns.CreateHostNCApipaEndpointRequest{
113113
NetworkContainerID: networkContainerID,
114114
}
115115

116116
if err = json.NewEncoder(&body).Encode(payload); err != nil {
117-
log.Errorf("encoding json failed with %v", err)
117+
logger.Errorf("encoding json failed with %v", err)
118118
return "", err
119119
}
120120

121121
res, err := httpc.Post(url, "application/json", &body)
122122
if err != nil {
123-
log.Errorf("[Azure CNSClient] HTTP Post returned error %v", err.Error())
123+
logger.Errorf("[Azure CNSClient] HTTP Post returned error %v", err.Error())
124124
return "", err
125125
}
126126

@@ -129,20 +129,20 @@ func (cnsClient *CNSClient) CreateHostNCApipaEndpoint(
129129
if res.StatusCode != http.StatusOK {
130130
errMsg := fmt.Sprintf("[Azure CNSClient] CreateHostNCApipaEndpoint: Invalid http status code: %v",
131131
res.StatusCode)
132-
log.Errorf(errMsg)
132+
logger.Errorf(errMsg)
133133
return "", fmt.Errorf(errMsg)
134134
}
135135

136136
var resp cns.CreateHostNCApipaEndpointResponse
137137

138138
if err = json.NewDecoder(res.Body).Decode(&resp); err != nil {
139-
log.Errorf("[Azure CNSClient] Error parsing CreateHostNCApipaEndpoint response resp: %v err: %v",
139+
logger.Errorf("[Azure CNSClient] Error parsing CreateHostNCApipaEndpoint response resp: %v err: %v",
140140
res.Body, err.Error())
141141
return "", err
142142
}
143143

144144
if resp.Response.ReturnCode != 0 {
145-
log.Errorf("[Azure CNSClient] CreateHostNCApipaEndpoint received error response :%v", resp.Response.Message)
145+
logger.Errorf("[Azure CNSClient] CreateHostNCApipaEndpoint received error response :%v", resp.Response.Message)
146146
return "", fmt.Errorf(resp.Response.Message)
147147
}
148148

@@ -155,21 +155,21 @@ func (cnsClient *CNSClient) DeleteHostNCApipaEndpoint(networkContainerID string)
155155

156156
httpc := &http.Client{}
157157
url := cnsClient.connectionURL + cns.DeleteHostNCApipaEndpointPath
158-
log.Printf("DeleteHostNCApipaEndpoint url: %v for NC: %s", url, networkContainerID)
158+
logger.Printf("DeleteHostNCApipaEndpoint url: %v for NC: %s", url, networkContainerID)
159159

160160
payload := &cns.DeleteHostNCApipaEndpointRequest{
161161
NetworkContainerID: networkContainerID,
162162
}
163163

164164
err := json.NewEncoder(&body).Encode(payload)
165165
if err != nil {
166-
log.Errorf("encoding json failed with %v", err)
166+
logger.Errorf("encoding json failed with %v", err)
167167
return err
168168
}
169169

170170
res, err := httpc.Post(url, "application/json", &body)
171171
if err != nil {
172-
log.Errorf("[Azure CNSClient] HTTP Post returned error %v", err.Error())
172+
logger.Errorf("[Azure CNSClient] HTTP Post returned error %v", err.Error())
173173
return err
174174
}
175175

@@ -178,21 +178,21 @@ func (cnsClient *CNSClient) DeleteHostNCApipaEndpoint(networkContainerID string)
178178
if res.StatusCode != http.StatusOK {
179179
errMsg := fmt.Sprintf("[Azure CNSClient] DeleteHostNCApipaEndpoint: Invalid http status code: %v",
180180
res.StatusCode)
181-
log.Errorf(errMsg)
181+
logger.Errorf(errMsg)
182182
return fmt.Errorf(errMsg)
183183
}
184184

185185
var resp cns.DeleteHostNCApipaEndpointResponse
186186

187187
err = json.NewDecoder(res.Body).Decode(&resp)
188188
if err != nil {
189-
log.Errorf("[Azure CNSClient] Error parsing DeleteHostNCApipaEndpoint response resp: %v err: %v",
189+
logger.Errorf("[Azure CNSClient] Error parsing DeleteHostNCApipaEndpoint response resp: %v err: %v",
190190
res.Body, err.Error())
191191
return err
192192
}
193193

194194
if resp.Response.ReturnCode != 0 {
195-
log.Errorf("[Azure CNSClient] DeleteHostNCApipaEndpoint received error response :%v", resp.Response.Message)
195+
logger.Errorf("[Azure CNSClient] DeleteHostNCApipaEndpoint received error response :%v", resp.Response.Message)
196196
return fmt.Errorf(resp.Response.Message)
197197
}
198198

cns/common/service.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ package common
66
import (
77
"errors"
88

9+
"github.com/Azure/azure-container-networking/cns/logger"
910
acn "github.com/Azure/azure-container-networking/common"
10-
"github.com/Azure/azure-container-networking/log"
1111
"github.com/Azure/azure-container-networking/store"
1212
)
1313

@@ -39,7 +39,7 @@ type ServiceConfig struct {
3939

4040
// NewService creates a new Service object.
4141
func NewService(name, version string, store store.KeyValueStore) (*Service, error) {
42-
log.Debugf("[Azure CNS] Going to create a service object with name: %v. version: %v.", name, version)
42+
logger.Debugf("[Azure CNS] Going to create a service object with name: %v. version: %v.", name, version)
4343

4444
svc := &Service{
4545
Name: name,
@@ -48,25 +48,25 @@ func NewService(name, version string, store store.KeyValueStore) (*Service, erro
4848
Store: store,
4949
}
5050

51-
log.Debugf("[Azure CNS] Finished creating service object with name: %v. version: %v.", name, version)
51+
logger.Debugf("[Azure CNS] Finished creating service object with name: %v. version: %v.", name, version)
5252
return svc, nil
5353
}
5454

5555
// Initialize initializes the service.
5656
func (service *Service) Initialize(config *ServiceConfig) error {
5757
if config == nil {
5858
err := "[Azure CNS Errror] Initialize called with nil ServiceConfig."
59-
log.Printf(err)
59+
logger.Errorf(err)
6060
return errors.New(err)
6161
}
6262

63-
log.Debugf("[Azure CNS] Going to initialize the service: %+v with config: %+v.", service, config)
63+
logger.Debugf("[Azure CNS] Going to initialize the service: %+v with config: %+v.", service, config)
6464

6565
service.ErrChan = config.ErrChan
6666
service.Store = config.Store
6767
service.Version = config.Version
6868

69-
log.Debugf("[Azure CNS] nitialized service: %+v with config: %+v.", service, config)
69+
logger.Debugf("[Azure CNS] nitialized service: %+v with config: %+v.", service, config)
7070

7171
return nil
7272
}

cns/configuration/cns_config.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"TelemetrySettings": {
3+
"TelemetryBatchSizeBytes": 16384,
4+
"TelemetryBatchIntervalInSecs": 15,
5+
"RefreshIntervalInSecs": 15,
6+
"DisableAll": false,
7+
"HeartBeatIntervalInMins": 30,
8+
"DebugMode": false
9+
}
10+
}

0 commit comments

Comments
 (0)