This commit is contained in:
QxBytes 2024-11-19 12:16:04 -08:00
Родитель 27832a148e
Коммит 846ed20f81
5 изменённых файлов: 29 добавлений и 28 удалений

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

@ -40,7 +40,7 @@ import (
// matches if the string fully consists of zero or more alphanumeric, dots, dashes, parentheses, spaces, or underscores
var (
allowedInput = regexp.MustCompile(`^[a-zA-Z0-9._\-\(\) ]*$`)
telemetryClient = telemetry.Client
telemetryClient = telemetry.AIClient
)
const (

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

@ -95,19 +95,19 @@ func rootExecute() error {
// Start telemetry process if not already started. This should be done inside lock, otherwise multiple process
// end up creating/killing telemetry process results in undesired state.
telemetry.Client.StartAndConnectTelemetry(logger)
defer telemetry.Client.DisconnectTelemetry()
telemetry.Client.SetSettings(cniReport)
telemetry.AIClient.StartAndConnectTelemetry(logger)
defer telemetry.AIClient.DisconnectTelemetry()
telemetry.AIClient.SetSettings(cniReport)
// CNI Acquires lock
if err = netPlugin.Plugin.InitializeKeyValueStore(&config); err != nil {
network.PrintCNIError(fmt.Sprintf("Failed to initialize key-value store of network plugin: %v", err))
if telemetry.Client.IsConnected() {
if telemetry.AIClient.IsConnected() {
logger.Error("Not connected to telemetry service")
return errors.Wrap(err, "lock acquire error")
}
telemetry.Client.SendError(err)
telemetry.AIClient.SendError(err)
if errors.Is(err, store.ErrTimeoutLockingStore) {
var cniMetric telemetry.AIMetric
@ -116,7 +116,7 @@ func rootExecute() error {
Value: 1.0,
CustomDimensions: make(map[string]string),
}
telemetry.Client.SendMetric(&cniMetric)
telemetry.AIClient.SendMetric(&cniMetric)
}
return errors.Wrap(err, "lock acquire error")
}
@ -136,7 +136,7 @@ func rootExecute() error {
if err = netPlugin.Start(&config); err != nil {
network.PrintCNIError(fmt.Sprintf("Failed to start network plugin, err:%v.\n", err))
telemetry.Client.SendError(err)
telemetry.AIClient.SendError(err)
panic("network plugin start fatal error")
}

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

@ -105,16 +105,16 @@ func rootExecute() error {
}()
// Connect to the telemetry process. Does not start the telemetry service if it is not running.
telemetry.Client.ConnectTelemetry(logger)
defer telemetry.Client.DisconnectTelemetry()
telemetry.Client.SetSettings(cniReport)
telemetry.AIClient.ConnectTelemetry(logger)
defer telemetry.AIClient.DisconnectTelemetry()
telemetry.AIClient.SetSettings(cniReport)
t := time.Now()
cniReport.Timestamp = t.Format("2006-01-02 15:04:05")
if err = netPlugin.Start(&config); err != nil {
network.PrintCNIError(fmt.Sprintf("Failed to start network plugin, err:%v.\n", err))
telemetry.Client.SendError(err)
telemetry.AIClient.SendError(err)
panic("network plugin start fatal error")
}
}

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

@ -13,55 +13,56 @@ const (
telemetryWaitTimeInMilliseconds = 200
)
type TelemetryClient struct {
type Client struct {
cniReportSettings *CNIReport
tb *TelemetryBuffer
logger *zap.Logger
lock sync.Mutex
}
var Client = NewTelemetryClient()
// package level variable for application insights telemetry
var AIClient = NewClient()
func NewTelemetryClient() *TelemetryClient {
return &TelemetryClient{
func NewClient() *Client {
return &Client{
cniReportSettings: &CNIReport{},
}
}
// Settings gets a pointer to the cni report struct, used to modify individual fields
func (c *TelemetryClient) Settings() *CNIReport {
func (c *Client) Settings() *CNIReport {
return c.cniReportSettings
}
// SetSettings REPLACES the pointer to the cni report struct and should only be used on startup
func (c *TelemetryClient) SetSettings(settings *CNIReport) {
func (c *Client) SetSettings(settings *CNIReport) {
c.cniReportSettings = settings
}
func (c *TelemetryClient) IsConnected() bool {
func (c *Client) IsConnected() bool {
return c.tb != nil && c.tb.Connected
}
func (c *TelemetryClient) ConnectTelemetry(logger *zap.Logger) {
func (c *Client) ConnectTelemetry(logger *zap.Logger) {
c.tb = NewTelemetryBuffer(logger)
c.tb.ConnectToTelemetry()
c.logger = logger
}
func (c *TelemetryClient) StartAndConnectTelemetry(logger *zap.Logger) {
func (c *Client) StartAndConnectTelemetry(logger *zap.Logger) {
c.tb = NewTelemetryBuffer(logger)
c.tb.ConnectToTelemetryService(telemetryNumberRetries, telemetryWaitTimeInMilliseconds)
c.logger = logger
}
func (c *TelemetryClient) DisconnectTelemetry() {
func (c *Client) DisconnectTelemetry() {
if c.tb == nil {
return
}
c.tb.Close()
}
func (c *TelemetryClient) sendEvent(msg string) {
func (c *Client) sendEvent(msg string) {
if c.tb == nil {
return
}
@ -72,18 +73,18 @@ func (c *TelemetryClient) sendEvent(msg string) {
SendCNIEvent(c.tb, c.cniReportSettings)
}
func (c *TelemetryClient) sendLog(msg string) {
func (c *Client) sendLog(msg string) {
if c.logger == nil {
return
}
c.logger.Info("Telemetry Event", zap.String("message", msg))
}
func (c *TelemetryClient) SendEvent(msg string) {
func (c *Client) SendEvent(msg string) {
c.sendEvent(msg)
}
func (c *TelemetryClient) SendError(err error) {
func (c *Client) SendError(err error) {
if err == nil {
return
}
@ -93,7 +94,7 @@ func (c *TelemetryClient) SendError(err error) {
c.sendEvent(err.Error())
}
func (c *TelemetryClient) SendMetric(cniMetric *AIMetric) {
func (c *Client) SendMetric(cniMetric *AIMetric) {
if c.tb == nil || cniMetric == nil {
return
}

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

@ -15,7 +15,7 @@ func TestClient(t *testing.T) {
allowedErrorMsg := regexp.MustCompile(`^\[\d+\] mock telemetry client error`)
allowedEventMsg := regexp.MustCompile(`^\[\d+\] telemetry event`)
emptyClient := NewTelemetryClient()
emptyClient := NewClient()
// an empty client should not cause panics
require.NotPanics(t, func() { emptyClient.SendEvent("no errors") })