Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add clarity to telemetry enablement #69

Merged
merged 4 commits into from
Mar 19, 2024
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
31 changes: 21 additions & 10 deletions captureworkload/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,43 @@ import (
)

var (
applicationInsightsID string //nolint // aiMetadata is set in Makefile
// applicationInsightsID is the instrumentation key for Azure Application Insights
// It is set during the build process using the -ldflags flag
// If it is set, the application will send telemetry to the corresponding Application Insights resource.
applicationInsightsID string //nolint
version string //nolint
)

func main() {
lOpts := log.GetDefaultLogOpts()
lOpts.ApplicationInsightsID = applicationInsightsID
// Set Azure application insights ID if it is provided
if applicationInsightsID != "" {
lOpts.ApplicationInsightsID = applicationInsightsID
}

log.SetupZapLogger(lOpts)
l := log.Logger().Named("captureworkload")
l.Info("Start to capture network traffic")
l.Info("Version: ", zap.String("version", version))
l.Info("Start telemetry with App Insights ID: ", zap.String("applicationInsightsID", applicationInsightsID))

telemetry.InitAppInsights(applicationInsightsID, version)
defer telemetry.ShutdownAppInsights()
var tel telemetry.Telemetry
if applicationInsightsID != "" {
l.Info("telemetry enabled", zap.String("applicationInsightsID", applicationInsightsID))
telemetry.InitAppInsights(applicationInsightsID, version)
defer telemetry.ShutdownAppInsights()
tel = telemetry.NewAppInsightsTelemetryClient("retina-capture", map[string]string{
"version": version,
telemetry.PropertyApiserver: os.Getenv(captureConstants.ApiserverEnvKey),
})
} else {
tel = telemetry.NewNoopTelemetry()
}

// Create channel to listen for signals.
sigChan := make(chan os.Signal, 1)
// Notify sigChan for SIGTERM.
signal.Notify(sigChan, syscall.SIGTERM)

tel := telemetry.NewAppInsightsTelemetryClient("retina-capture", map[string]string{
"version": version,
telemetry.PropertyApiserver: os.Getenv(captureConstants.ApiserverEnvKey),
})

cm := capture.NewCaptureManager(l, tel)

defer func() {
Expand Down
18 changes: 11 additions & 7 deletions controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ const (
var (
scheme = k8sruntime.NewScheme()

applicationInsightsID string //nolint // aiMetadata is set in Makefile
// applicationInsightsID is the instrumentation key for Azure Application Insights
// It is set during the build process using the -ldflags flag
// If it is set, the application will send telemetry to the corresponding Application Insights resource.
applicationInsightsID string
version string

cfgFile string
Expand All @@ -76,10 +79,11 @@ func main() {
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")

// initialize Application Insights
telemetry.InitAppInsights(applicationInsightsID, version)

defer telemetry.TrackPanic()
if applicationInsightsID != "" {
telemetry.InitAppInsights(applicationInsightsID, version)
defer telemetry.ShutdownAppInsights()
defer telemetry.TrackPanic()
}

flag.StringVar(&cfgFile, "config", configFileName, "config file")
flag.Parse()
Expand Down Expand Up @@ -118,8 +122,8 @@ func main() {
mainLogger = log.Logger().Named("main").Sugar()

var tel telemetry.Telemetry
if config.EnableTelemetry {
mainLogger.Infof("telemetry enabled, using Application Insights ID: %s", applicationInsightsID)
if config.EnableTelemetry && applicationInsightsID != "" {
mainLogger.Info("telemetry enabled", zap.String("applicationInsightsID", applicationInsightsID))
tel = telemetry.NewAppInsightsTelemetryClient("retina-agent", map[string]string{
"version": version,
"apiserver": cfg.Host,
Expand Down
2 changes: 1 addition & 1 deletion deploy/manifests/controller/helm/retina/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ logLevel: debug
enabledPlugin_linux: '["dropreason","packetforward","linuxutil","dns"]'
enabledPlugin_win: '["hnsstats"]'

enableTelemetry: true
enableTelemetry: false

# Interval, in seconds, to scrape/publish metrics.
metricsInterval: 10
Expand Down
2 changes: 1 addition & 1 deletion docs/troubleshooting/basic-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ data:
logLevel: info
enabledPlugin: ["dropreason","packetforward","linuxutil"]
metricsInterval: 10
enableTelemetry: true
enableTelemetry: false
kind: ConfigMap
metadata:
annotations:
Expand Down
21 changes: 14 additions & 7 deletions init/retina/main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@ import (
)

var (
applicationInsightsID string //nolint // aiMetadata is set in Makefile
// applicationInsightsID is the instrumentation key for Azure Application Insights
// It is set during the build process using the -ldflags flag
// If it is set, the application will send telemetry to the corresponding Application Insights resource.
applicationInsightsID string
version string
)

func main() {
// Initialize application insights
telemetry.InitAppInsights(applicationInsightsID, version)
defer telemetry.TrackPanic()

// Initialize logger
opts := log.GetDefaultLogOpts()
opts.ApplicationInsightsID = applicationInsightsID
opts.EnableTelemetry = true

// Enable telemetry if applicationInsightsID is provided
if applicationInsightsID != "" {
opts.EnableTelemetry = true
opts.ApplicationInsightsID = applicationInsightsID
// Initialize application insights
telemetry.InitAppInsights(applicationInsightsID, version)
defer telemetry.ShutdownAppInsights()
defer telemetry.TrackPanic()
}

log.SetupZapLogger(opts)
log.Logger().AddFields(zap.String("version", version))
Expand Down
11 changes: 7 additions & 4 deletions operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ var (

version = "undefined"

applicationInsightsID string //nolint // aiMetadata is set in Makefile
// applicationInsightsID is the instrumentation key for Azure Application Insights
// It is set during the build process using the -ldflags flag
// If it is set, the application will send telemetry to the corresponding Application Insights resource.
applicationInsightsID string
)

func init() {
Expand Down Expand Up @@ -160,15 +163,15 @@ func main() {
}

var tel telemetry.Telemetry
if oconfig.EnableTelemetry {
if oconfig.EnableTelemetry && applicationInsightsID != "" {
mainLogger.Info("telemetry enabled", zap.String("applicationInsightsID", applicationInsightsID))
properties := map[string]string{
"version": version,
telemetry.PropertyApiserver: apiserverURL,
}
tel = telemetry.NewAppInsightsTelemetryClient("retina-agent", properties)
tel = telemetry.NewAppInsightsTelemetryClient("retina-operator", properties)
} else {
mainLogger.Info("telemetry disabled for:", zap.String("apiserver", apiserverURL))
mainLogger.Info("telemetry disabled", zap.String("apiserver", apiserverURL))
tel = telemetry.NewNoopTelemetry()
}

Expand Down
1 change: 1 addition & 0 deletions pkg/capture/capture_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (cm *CaptureManager) CaptureNetwork(sigChan <-chan os.Signal) (string, erro
}
}

// no-op telemetry client will not send any telemetry
cm.tel.TrackEvent("capturenetwork", map[string]string{
"captureName": cm.captureName(),
"nodeName": cm.captureNodeHostName(),
Expand Down
1 change: 1 addition & 0 deletions pkg/managers/controllermanager/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (m *Controller) Init(ctx context.Context) error {
}

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

var g *errgroup.Group
Expand Down
6 changes: 5 additions & 1 deletion pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func InitAppInsights(appinsightsId, appVersion string) {

func ShutdownAppInsights() {
select {
case <-client.Channel().Close(5 * time.Second):
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 Down Expand Up @@ -91,6 +91,10 @@ 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)
Expand Down
Loading