Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cni/network/plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var version string

// If report write succeeded, mark the report flag state to false.
func markSendReport(reportManager *telemetry.ReportManager) {
if err := reportManager.SetReportState(); err != nil {
if err := reportManager.SetReportState(telemetry.CNITelemetryFile); err != nil {
log.Printf("SetReportState failed due to %v", err)
reflect.ValueOf(reportManager.Report).Elem().FieldByName("ErrorMessage").SetString(err.Error())

Expand Down Expand Up @@ -67,7 +67,7 @@ func main() {
reportManager.GetHostMetadata()
reportManager.Report.(*telemetry.CNIReport).GetReport(pluginName, config.Version, ipamQueryURL)

if !reportManager.GetReportState() {
if !reportManager.GetReportState(telemetry.CNITelemetryFile) {
log.Printf("GetReport state file didn't exist. Setting flag to true")

err = reportManager.SendReport()
Expand Down
115 changes: 12 additions & 103 deletions telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
)

const (
// NPMTelemetryFile Path.
NPMTelemetryFile = platform.NPMRuntimePath + "AzureNPMTelemetry.json"
// CNITelemetryFile Path.
CNITelemetryFile = platform.CNIRuntimePath + "AzureCNITelemetry.json"

metadataURL = "http://169.254.169.254/metadata/instance?api-version=2017-08-01&format=json"
ContentType = "application/json"
)
Expand Down Expand Up @@ -73,24 +78,6 @@ type OrchestratorInfo struct {
ErrorMessage string
}

const (
// CNITelemetryFile Path.
CNITelemetryFile = platform.CNIRuntimePath + "AzureCNITelemetry.json"
// NPMTelemetryFile Path.
NPMTelemetryFile = platform.NPMRuntimePath + "AzureNPMTelemetry.json"
// DNCTelemetryFile Path.
DNCTelemetryFile = platform.DNCRuntimePath + "AzureDNCTelemetry.json"
)

const (
// CNI report type
CNIReportType = "CNI"
// NPM report type
NPMReportType = "NPM"
// DNC report type
DNCReportType = "DNC"
)

// Metadata retrieved from wireserver
type Metadata struct {
Location string `json:"location"`
Expand Down Expand Up @@ -155,18 +142,6 @@ type NPMReport struct {
Metadata Metadata `json:"compute"`
}

// DNCReport structure.
type DNCReport struct {
IsNewInstance bool
CPUUsage string
MemoryUsage string
Processes string
EventMessage string
PartitionKey string
Allocations string
Metadata Metadata `json:"compute"`
}

// ReportManager structure.
type ReportManager struct {
HostNetAgentURL string
Expand Down Expand Up @@ -230,24 +205,18 @@ func (report *NPMReport) GetReport(clusterID, nodeName, npmVersion, kubernetesVe
func (reportMgr *ReportManager) SendReport() error {
log.Printf("[Telemetry] Going to send Telemetry report to hostnetagent %v", reportMgr.HostNetAgentURL)

var body bytes.Buffer
httpc := &http.Client{}
json.NewEncoder(&body).Encode(reportMgr.Report)

switch reportMgr.Report.(type) {
case *CNIReport:
log.Printf("[Telemetry] %+v", reportMgr.Report.(*CNIReport))

case *NPMReport:
log.Printf("[Telemetry] %+v", reportMgr.Report.(*NPMReport))

case *DNCReport:
log.Printf("[Telemetry] %+v", reportMgr.Report.(*DNCReport))

default:
return fmt.Errorf("[Telemetry] failed to resolve report type")
log.Printf("[Telemetry] %+v", reportMgr.Report)
}

httpc := &http.Client{}
var body bytes.Buffer
json.NewEncoder(&body).Encode(reportMgr.Report)
resp, err := httpc.Post(reportMgr.HostNetAgentURL, reportMgr.ContentType, &body)
if err != nil {
return fmt.Errorf("[Telemetry] HTTP Post returned error %v", err)
Expand All @@ -271,29 +240,11 @@ func (reportMgr *ReportManager) SendReport() error {
}

// SetReportState will save the state in file if telemetry report sent successfully.
func (reportMgr *ReportManager) SetReportState() error {
var telemetryFile string
func (reportMgr *ReportManager) SetReportState(telemetryFile string) error {
var reportBytes []byte
var err error

// get bytes from associated report type
switch reportMgr.Report.(type) {
case *CNIReport:
telemetryFile = CNITelemetryFile
reportBytes, err = json.Marshal(reportMgr.Report.(*CNIReport))

case *NPMReport:
telemetryFile = NPMTelemetryFile
reportBytes, err = json.Marshal(reportMgr.Report.(*NPMReport))

case *DNCReport:
telemetryFile = DNCTelemetryFile
reportBytes, err = json.Marshal(reportMgr.Report.(*DNCReport))

default:
return fmt.Errorf("[Telemetry] Invalid report type")
}

reportBytes, err = json.Marshal(reportMgr.Report)
if err != nil {
return fmt.Errorf("[Telemetry] report write failed with err %+v", err)
}
Expand All @@ -319,24 +270,7 @@ func (reportMgr *ReportManager) SetReportState() error {
}

// GetReportState will check if report is sent at least once by checking telemetry file.
func (reportMgr *ReportManager) GetReportState() bool {
var telemetryFile string

switch reportMgr.Report.(type) {
case *CNIReport:
telemetryFile = CNITelemetryFile

case *NPMReport:
telemetryFile = NPMTelemetryFile

case *DNCReport:
telemetryFile = DNCTelemetryFile

default:
log.Printf("[Telemetry] Invalid report type")
return false
}

func (reportMgr *ReportManager) GetReportState(telemetryFile string) bool {
// try to set IsNewInstance in report
if _, err := os.Stat(telemetryFile); os.IsNotExist(err) {
log.Printf("[Telemetry] File not exist %v", telemetryFile)
Expand Down Expand Up @@ -518,28 +452,3 @@ func (reportMgr *ReportManager) GetHostMetadata() error {

return err
}

// GetSystemDetails - retrieve system details like cpu usage, mem usage, and number or running processes
func (reportMgr *ReportManager) GetSystemDetails() error {
// hostStat, err := host.Info()
// if err != nil {
// return err
// }

// reflect.ValueOf(reportMgr.Report).Elem().FieldByName("Processes").SetString(strconv.FormatUint(hostStat.Procs, 10))

// cpuStat, err := cpu.Percent(0, false)
// if err != nil {
// return err
// }

// reflect.ValueOf(reportMgr.Report).Elem().FieldByName("CPUUsage").SetString(strconv.FormatFloat(cpuStat[0], 'f', 2, 64))

// memStat, err := mem.VirtualMemory()
// if err != nil {
// return err
// }

// reflect.ValueOf(reportMgr.Report).Elem().FieldByName("MemoryUsage").SetString(strconv.FormatFloat(memStat.UsedPercent, 'f', 2, 64))
return nil
}
8 changes: 4 additions & 4 deletions telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,20 @@ func TestGetOSDetails(t *testing.T) {
}
}
func TestGetSystemDetails(t *testing.T) {
reportManager.GetSystemDetails()
reportManager.Report.(*CNIReport).GetSystemDetails()
if reportManager.Report.(*CNIReport).ErrorMessage != "" {
t.Errorf("GetSystemDetails failed due to %v", reportManager.Report.(*CNIReport).ErrorMessage)
}
}
func TestGetInterfaceDetails(t *testing.T) {
reportManager.GetSystemDetails()
reportManager.Report.(*CNIReport).GetSystemDetails()
if reportManager.Report.(*CNIReport).ErrorMessage != "" {
t.Errorf("GetInterfaceDetails failed due to %v", reportManager.Report.(*CNIReport).ErrorMessage)
}
}

func TestGetReportState(t *testing.T) {
state := reportManager.GetReportState()
state := reportManager.GetReportState(CNITelemetryFile)
if state != false {
t.Errorf("Wrong state in getreport state")
}
Expand All @@ -128,7 +128,7 @@ func TestSendTelemetry(t *testing.T) {
}

func TestSetReportState(t *testing.T) {
err := reportManager.SetReportState()
err := reportManager.SetReportState(CNITelemetryFile)
if err != nil {
t.Errorf("SetReportState failed due to %v", err)
}
Expand Down