Skip to content

Commit

Permalink
check for nil client within trackpanic instead
Browse files Browse the repository at this point in the history
  • Loading branch information
nddq committed Mar 19, 2024
1 parent 1647081 commit 9dc60fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 1 addition & 3 deletions pkg/managers/controllermanager/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ func (m *Controller) Init(ctx context.Context) error {

func (m *Controller) Start(ctx context.Context) {
// Only track panics if telemetry is enabled
if telemetry.Client != nil {
defer telemetry.TrackPanic()
}
defer telemetry.TrackPanic()

var g *errgroup.Group

Expand Down
24 changes: 14 additions & 10 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

var (
Client appinsights.TelemetryClient
client appinsights.TelemetryClient
version string
)

Expand All @@ -32,21 +32,21 @@ type Telemetry interface {
}

func InitAppInsights(appinsightsId, appVersion string) {
if Client != nil {
if client != nil {
fmt.Printf("appinsights client already initialized")
return
}
telemetryConfig := appinsights.NewTelemetryConfiguration(appinsightsId)
telemetryConfig.MaxBatchInterval = 1 * time.Second
Client = appinsights.NewTelemetryClientFromConfig(telemetryConfig)
client = appinsights.NewTelemetryClientFromConfig(telemetryConfig)

// Set the app version
version = appVersion
}

func ShutdownAppInsights() {
select {
case <-Client.Channel().Close(5 * time.Second): //nolint:gomnd // ignore
case <-client.Channel().Close(5 * time.Second): //nolint:gomnd // ignore
// Five second timeout for retries.

// If we got here, then all telemetry was submitted
Expand All @@ -72,7 +72,7 @@ type TelemetryClient struct {
}

func NewAppInsightsTelemetryClient(processName string, additionalproperties map[string]string) *TelemetryClient {
if Client == nil {
if client == nil {
fmt.Println("appinsights client not initialized")
}

Expand All @@ -91,14 +91,18 @@ func NewAppInsightsTelemetryClient(processName string, additionalproperties map[
// TrackPanic function sends the stacktrace and flushes logs only in a goroutine where its call is deferred.
// Panics in other goroutines will not be caught by this recover function.
func TrackPanic() {
// no telemetry means client is not initialized
if client == nil {
return
}
if r := recover(); r != nil {
message := fmt.Sprintf("Panic caused by: %v , Stacktrace %s", r, string(debug.Stack()))
trace := appinsights.NewTraceTelemetry(message, appinsights.Critical)
trace.Properties = GetEnvironmentProperties()
trace.Properties["version"] = version

// Create trace and track it
Client.Track(trace)
client.Track(trace)

// Close zapai and flush logs
if logger := log.Logger(); logger != nil {
Expand Down Expand Up @@ -159,7 +163,7 @@ func (t *TelemetryClient) TrackEvent(name string, properties map[string]string)
t.RUnlock()
}

Client.Track(event)
client.Track(event)
}

func (t *TelemetryClient) TrackMetric(metricname string, value float64, properties map[string]string) {
Expand All @@ -178,7 +182,7 @@ func (t *TelemetryClient) TrackMetric(metricname string, value float64, properti
t.RUnlock()
}

Client.Track(metric)
client.Track(metric)
}

func (t *TelemetryClient) TrackTrace(name string, severity contracts.SeverityLevel, properties map[string]string) {
Expand All @@ -197,7 +201,7 @@ func (t *TelemetryClient) TrackTrace(name string, severity contracts.SeverityLev
t.RUnlock()
}

Client.Track(trace)
client.Track(trace)
}

func (t *TelemetryClient) TrackException(exception *appinsights.ExceptionTelemetry) {
Expand All @@ -211,7 +215,7 @@ func (t *TelemetryClient) TrackException(exception *appinsights.ExceptionTelemet

t.RUnlock()
}
Client.Track(exception)
client.Track(exception)
}

type PerformanceCounter struct {
Expand Down

0 comments on commit 9dc60fb

Please sign in to comment.