fix: [Telemetry] Prevent telemetry connect if no binary path (#1883)

* Prevent telemetry connect if no binary path

* agnostic calls, addressing comments

* agnostic calls, addressing comments

* Correcting flow, lint

* Restoring aiwrapper

* lint error 113 cleanup

* addressing comments

* cleaning up telemetry folder error calls

* addressing comments

* addressing comments

* addressing comments

---------

Co-authored-by: jpayne3506 <johnpayne@microsoft.com>
Co-authored-by: tamilmani1989 <tamanoha@microsoft.com>
This commit is contained in:
John Payne 2023-04-07 09:30:54 -07:00 коммит произвёл GitHub
Родитель ab8858b101
Коммит 7546aacf69
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 21 добавлений и 18 удалений

Просмотреть файл

@ -2,16 +2,18 @@
package telemetry
import (
"fmt"
"errors"
"github.com/Azure/azure-container-networking/aitelemetry"
"github.com/Azure/azure-container-networking/log"
)
var (
aiMetadata string
th aitelemetry.TelemetryHandle
gDisableTrace bool
gDisableMetric bool
aiMetadata string
th aitelemetry.TelemetryHandle
gDisableTrace bool
gDisableMetric bool
ErrTelemetryDisabled = errors.New("telemetry is disabled")
)
const (
@ -24,7 +26,7 @@ func CreateAITelemetryHandle(aiConfig aitelemetry.AIConfig, disableAll, disableM
if disableAll {
log.Printf("Telemetry is disabled")
return fmt.Errorf("Telmetry disabled")
return ErrTelemetryDisabled
}
th, err = aitelemetry.NewAITelemetry("", aiMetadata, aiConfig)

Просмотреть файл

@ -4,13 +4,13 @@
package telemetry
import (
"fmt"
"os/exec"
"runtime"
"strings"
"syscall"
"github.com/Azure/azure-container-networking/platform"
"github.com/pkg/errors"
)
// Memory Info structure.
@ -36,7 +36,7 @@ func getMemInfo() (*MemInfo, error) {
err := syscall.Sysinfo(info)
if err != nil {
return nil, fmt.Errorf("Sysinfo failed due to %v", err)
return nil, errors.Wrapf(err, "Sysinfo failed due to ")
}
unit := uint64(info.Unit) * MB // MB
@ -51,7 +51,7 @@ func getDiskInfo(path string) (*DiskInfo, error) {
err := syscall.Statfs(path, &fs)
if err != nil {
return nil, fmt.Errorf("Statfs call failed with error %v", err)
return nil, errors.Wrapf(err, "Statfs call failed with error ")
}
total := fs.Blocks * uint64(fs.Bsize) / MB

Просмотреть файл

@ -7,7 +7,6 @@ import (
"bufio"
"context"
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
@ -305,9 +304,14 @@ func ReadConfigFile(filePath string) (TelemetryConfig, error) {
func (tb *TelemetryBuffer) ConnectToTelemetryService(telemetryNumRetries, telemetryWaitTimeInMilliseconds int) {
path, dir := getTelemetryServiceDirectory()
args := []string{"-d", dir}
for attempt := 0; attempt < 2; attempt++ {
if err := tb.Connect(); err != nil {
log.Logf("Connection to telemetry socket failed: %v", err)
if _, exists := os.Stat(path); exists != nil {
log.Logf("Skip starting telemetry service as file didn't exist")
return
}
tb.Cleanup(FdName)
StartTelemetryService(path, args)
WaitForTelemetrySocket(telemetryNumRetries, time.Duration(telemetryWaitTimeInMilliseconds))
@ -319,20 +323,17 @@ func (tb *TelemetryBuffer) ConnectToTelemetryService(telemetryNumRetries, teleme
}
}
// getTelemetryServiceDirectory - check CNI install directory and Executable location for telemetry binary
func getTelemetryServiceDirectory() (path string, dir string) {
path = fmt.Sprintf("%v/%v", CniInstallDir, TelemetryServiceProcessName)
if exists, _ := platform.CheckIfFileExists(path); !exists {
path = filepath.Join(CniInstallDir, TelemetryServiceProcessName)
if _, exists := os.Stat(path); exists != nil {
ex, _ := os.Executable()
exDir := filepath.Dir(ex)
path = fmt.Sprintf("%v/%v", exDir, TelemetryServiceProcessName)
if exists, _ = platform.CheckIfFileExists(path); !exists {
log.Logf("Skip starting telemetry service as file didn't exist")
return
}
path = filepath.Join(exDir, TelemetryServiceProcessName)
dir = exDir
} else {
dir = CniInstallDir
}
return
}