Skip to content

Commit

Permalink
fix: start telemetry async (#4189)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsukhin committed Jul 18, 2023
1 parent 2822b7c commit 60ab21d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
7 changes: 5 additions & 2 deletions cmd/api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,11 @@ func main() {
}

// telemetry based functions
api.SendTelemetryStartEvent(ctx)
api.StartTelemetryHeartbeats(ctx)
telemetryCh := make(chan struct{})
defer close(telemetryCh)

api.SendTelemetryStartEvent(ctx, telemetryCh)
api.StartTelemetryHeartbeats(ctx, telemetryCh)

log.DefaultLogger.Infow(
"starting Testkube API server",
Expand Down
39 changes: 23 additions & 16 deletions internal/app/api/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,28 @@ type oauthParams struct {
}

// SendTelemetryStartEvent sends anonymous start event to telemetry trackers
func (s TestkubeAPI) SendTelemetryStartEvent(ctx context.Context) {
telemetryEnabled, err := s.ConfigMap.GetTelemetryEnabled(ctx)
if err != nil {
s.Log.Errorw("error getting config map", "error", err)
}
func (s TestkubeAPI) SendTelemetryStartEvent(ctx context.Context, ch chan struct{}) {
go func() {
defer func() {
ch <- struct{}{}
}()

if !telemetryEnabled {
return
}
telemetryEnabled, err := s.ConfigMap.GetTelemetryEnabled(ctx)
if err != nil {
s.Log.Errorw("error getting config map", "error", err)
}

out, err := telemetry.SendServerStartEvent(s.Config.ClusterID, version.Version)
if err != nil {
s.Log.Debug("telemetry send error", "error", err.Error())
} else {
s.Log.Debugw("sending telemetry server start event", "output", out)
}
if !telemetryEnabled {
return
}

out, err := telemetry.SendServerStartEvent(s.Config.ClusterID, version.Version)
if err != nil {
s.Log.Debug("telemetry send error", "error", err.Error())
} else {
s.Log.Debugw("sending telemetry server start event", "output", out)
}
}()
}

// InitEnvs initializes api server settings
Expand Down Expand Up @@ -436,9 +442,10 @@ func (s *TestkubeAPI) InitRoutes() {
})
}

func (s TestkubeAPI) StartTelemetryHeartbeats(ctx context.Context) {

func (s TestkubeAPI) StartTelemetryHeartbeats(ctx context.Context, ch chan struct{}) {
go func() {
<-ch

ticker := time.NewTicker(HeartbeatInterval)
for {
telemetryEnabled, err := s.ConfigMap.GetTelemetryEnabled(ctx)
Expand Down

0 comments on commit 60ab21d

Please sign in to comment.