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

🗒️ add func to send slow query info upstream #5107

Merged
merged 1 commit into from
Jan 21, 2025
Merged
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
60 changes: 58 additions & 2 deletions providers-sdk/v1/upstream/health/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"runtime/debug"
"time"

"github.com/rs/zerolog/log"
"go.mondoo.com/cnquery/v11/cli/config"
Expand Down Expand Up @@ -64,6 +65,63 @@ func sendPanic(product, version, build string, r any, stacktrace []byte) {
},
}

// 3. send error to mondoo platform
sendErrorToMondooPlatform(serviceAccount, event)

log.Info().Msg("reported panic to Mondoo Platform")
}

type SlowQueryInfo struct {
CodeID string
Query string
Duration time.Duration
}

func ReportSlowQuery(product, version, build string, q SlowQueryInfo) {
sendSlowQuery(product, version, build, q)
}

// sendSlowQuery sends queries that have been deemed excessively slow to
// the platform for futher analysis.
func sendSlowQuery(product, version, build string, q SlowQueryInfo) {
// 1. read config
opts, err := config.Read()
if err != nil {
log.Error().Err(err).Msg("failed to read config")
return
}

serviceAccount := opts.GetServiceCredential()
if serviceAccount == nil {
log.Error().Msg("no service account configured")
return
}

msg := "slow query: " + fmt.Sprintf("%s took %d", q.CodeID, q.Duration)
if q.Query != "" {
msg = "slow query: " + fmt.Sprintf("%s (%s) took %d", q.Query, q.CodeID, q.Duration)
}
// 2. create local support bundle
event := &SendErrorReq{
ServiceAccountMrn: opts.ServiceAccountMrn,
AgentMrn: opts.AgentMrn,
Product: &ProductInfo{
Name: product,
Version: version,
Build: build,
},
Error: &ErrorInfo{
Message: msg,
},
}

// 3. send error to mondoo platform
sendErrorToMondooPlatform(serviceAccount, event)

log.Debug().Msg("reported slow query to Mondoo Platform")
}

func sendErrorToMondooPlatform(serviceAccount *upstream.ServiceAccountCredentials, event *SendErrorReq) {
// 3. send error to mondoo platform
proxy, err := config.GetAPIProxy()
if err != nil {
Expand All @@ -90,6 +148,4 @@ func sendPanic(product, version, build string, r any, stacktrace []byte) {
log.Error().Err(err).Msg("failed to send error to mondoo platform")
return
}

log.Info().Msg("reported panic to Mondoo Platform")
}
Loading