From 53f4613655f99ad24d81ca7d2889ba8020f6b783 Mon Sep 17 00:00:00 2001 From: Volodymyr Stoiko Date: Sat, 14 Sep 2024 11:02:33 +0300 Subject: [PATCH] Adjust sentry context (#94) * Enrich sentry context * fix * fix * upd * fix * upd * Delete sentry internal --- go.mod | 9 +++-- go.sum | 10 ++++- main.go | 25 +++++++++--- pkg/kubernetes/utils.go | 15 +++++++ pkg/sentry/sentry.go | 88 ----------------------------------------- 5 files changed, 48 insertions(+), 99 deletions(-) delete mode 100644 pkg/sentry/sentry.go diff --git a/go.mod b/go.mod index c0e74c5..d98da70 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,8 @@ module github.com/kubeshark/tracer -go 1.22.0 +go 1.22.2 -toolchain go1.22.5 +toolchain go1.22.7 require ( github.com/Masterminds/semver v1.5.0 @@ -10,7 +10,7 @@ require ( github.com/cilium/ebpf v0.12.3 github.com/dlclark/regexp2 v1.11.0 github.com/florianl/go-tc v0.4.3 - github.com/getsentry/sentry-go v0.28.1 + github.com/getsentry/sentry-go v0.29.0 github.com/gin-contrib/pprof v1.4.0 github.com/gin-gonic/gin v1.9.1 github.com/go-errors/errors v1.4.2 @@ -19,9 +19,10 @@ require ( github.com/hashicorp/golang-lru/v2 v2.0.2 github.com/jinzhu/copier v0.4.0 github.com/knightsc/gapstone v0.0.0-20191231144527-6fa5afaf11a9 - github.com/kubeshark/api v1.1.16 + github.com/kubeshark/api v1.1.20 github.com/kubeshark/gopacket v1.1.30 github.com/kubeshark/tracerproto v1.0.3-0.20240730073449-de3a99a3719c + github.com/kubeshark/utils v0.0.0-20240912210808-416dca610f11 github.com/moby/moby v25.0.4+incompatible github.com/rs/zerolog v1.32.0 github.com/struCoder/pidusage v0.2.1 diff --git a/go.sum b/go.sum index 32140e0..88099bc 100644 --- a/go.sum +++ b/go.sum @@ -38,8 +38,8 @@ github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx github.com/frankban/quicktest v1.14.5/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= -github.com/getsentry/sentry-go v0.28.1 h1:zzaSm/vHmGllRM6Tpx1492r0YDzauArdBfkJRtY6P5k= -github.com/getsentry/sentry-go v0.28.1/go.mod h1:1fQZ+7l7eeJ3wYi82q5Hg8GqAPgefRq+FP/QhafYVgg= +github.com/getsentry/sentry-go v0.29.0 h1:YtWluuCFg9OfcqnaujpY918N/AhCCwarIDWOYSBAjCA= +github.com/getsentry/sentry-go v0.29.0/go.mod h1:jhPesDAL0Q0W2+2YEuVOvdWmVtdsr1+jtBrlDEVWwLY= github.com/gin-contrib/pprof v1.4.0 h1:XxiBSf5jWZ5i16lNOPbMTVdgHBdhfGRD5PZ1LWazzvg= github.com/gin-contrib/pprof v1.4.0/go.mod h1:RrehPJasUVBPK6yTUwOl8/NP6i0vbUgmxtis+Z5KE90= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -150,11 +150,17 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kubeshark/api v1.1.16 h1:E9MlBpc4zpCE2847+/myEBmug+q6zn4y1nMSUMCKYwI= github.com/kubeshark/api v1.1.16/go.mod h1:13xpBdys1s9gozDtv29njdT3Rx3xl2ZICgdsFXwqk40= +github.com/kubeshark/api v1.1.20 h1:ZE8dBHalyc8OriBOLQ7Olpzjox6Tka/Z0xohIJmGiB4= +github.com/kubeshark/api v1.1.20/go.mod h1:13xpBdys1s9gozDtv29njdT3Rx3xl2ZICgdsFXwqk40= github.com/kubeshark/gopacket v1.1.30 h1:Dz6eo7b6+NdVCrgiyKxlGEVTm0L6PwgbVvSomsuwIyU= github.com/kubeshark/gopacket v1.1.30/go.mod h1:Qo8/i/tdT74CCT7/pjO0L55Pktv5dQfj7M/Arv8MKm8= github.com/kubeshark/tracerproto v1.0.0/go.mod h1:+efDYkwXxwakmHRpxHVEekyXNtg/aFx0uSo/I0lGV9k= github.com/kubeshark/tracerproto v1.0.3-0.20240730073449-de3a99a3719c h1:Km6Wm+AwCRO/6aK60PqGpW6bVuXNw+bbZRjX837N/rU= github.com/kubeshark/tracerproto v1.0.3-0.20240730073449-de3a99a3719c/go.mod h1:+efDYkwXxwakmHRpxHVEekyXNtg/aFx0uSo/I0lGV9k= +github.com/kubeshark/utils v0.0.0-20240912114101-271ed42ebd29 h1:O/t6Nd12MyX6flppOojsAh0gS1pjb0VdsdyxmIEFbjQ= +github.com/kubeshark/utils v0.0.0-20240912114101-271ed42ebd29/go.mod h1:vrL5XTDuLYGhVv/UGEsx+fwbAHcvTUJoP8ApHG11JcQ= +github.com/kubeshark/utils v0.0.0-20240912210808-416dca610f11 h1:g+f7OCI1onHRqEqE5dMlhLfwfdxFI9wmeOvlUaxNzPQ= +github.com/kubeshark/utils v0.0.0-20240912210808-416dca610f11/go.mod h1:vrL5XTDuLYGhVv/UGEsx+fwbAHcvTUJoP8ApHG11JcQ= github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= diff --git a/main.go b/main.go index fa2919f..6e9c75e 100644 --- a/main.go +++ b/main.go @@ -17,14 +17,13 @@ import ( "github.com/kubeshark/tracer/pkg/kubernetes" "github.com/kubeshark/tracer/pkg/version" "github.com/kubeshark/tracer/server" + sentrypkg "github.com/kubeshark/utils/sentry" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/rest" "github.com/kubeshark/tracer/pkg/health" - - sentrypkg "github.com/kubeshark/tracer/pkg/sentry" ) const ( @@ -60,9 +59,9 @@ var tracer *Tracer func main() { var sentryDSN string if sentrypkg.IsSentryEnabled() { - sentryDSN, error := sentrypkg.GetDSN(context.Background()) - if error != nil { - log.Error().Err(error).Msg("Failed to get Sentry DSN") + sentryDSN, err := sentrypkg.GetDSN(context.Background(), "tracer", version.Ver) + if err != nil { + log.Error().Err(err).Msg("Failed to get Sentry DSN") } // To initialize Sentry's handler, you need to initialize Sentry itself beforehand @@ -74,6 +73,7 @@ func main() { // We recommend adjusting this value in production, TracesSampleRate: 1.0, Release: version.Ver, + Environment: sentrypkg.Environment(), }); err != nil { log.Error().Err(err).Msg("Sentry initialization failed:") } else { @@ -141,6 +141,8 @@ func run() { go health.DumpHealthEvery10Seconds(nodeName) + enrichSentryContext(watcher) + if clusterMode { misc.SetDataDir(fmt.Sprintf("/app/data/%s", nodeName)) } @@ -197,3 +199,16 @@ func createTracer() (err error) { return } + +func enrichSentryContext(watcher *kubernetes.Watcher) { + clusterId, err := kubernetes.GetClusterID(watcher) + if err != nil { + log.Error().Err(err).Msg("Failed to get cluster ID for Sentry tag") + } + + tags := map[string]string{ + "clusterID": clusterId, + } + + sentrypkg.AddTags(tags) +} diff --git a/pkg/kubernetes/utils.go b/pkg/kubernetes/utils.go index eb76f47..99c26c6 100644 --- a/pkg/kubernetes/utils.go +++ b/pkg/kubernetes/utils.go @@ -1,8 +1,11 @@ package kubernetes import ( + "context" "os" "strings" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) const ( @@ -36,3 +39,15 @@ func GetSelfPodName() string { } return "" } + +// GetClusterID returns the UID field of the `kube-system` namespace object +// This is used as a unique identifier for the cluster +func GetClusterID(watcher *Watcher) (string, error) { + + namespaceObj, err := watcher.clientSet.CoreV1().Namespaces().Get(context.Background(), "kube-system", metav1.GetOptions{}) + if err != nil { + return "", err + } + + return string(namespaceObj.GetUID()), nil +} diff --git a/pkg/sentry/sentry.go b/pkg/sentry/sentry.go deleted file mode 100644 index 9c26f8f..0000000 --- a/pkg/sentry/sentry.go +++ /dev/null @@ -1,88 +0,0 @@ -package sentry - -import ( - "bytes" - "context" - "encoding/json" - "fmt" - "io" - "net/http" - "os" - - "github.com/hashicorp/go-retryablehttp" - "github.com/kubeshark/tracer/pkg/version" - "github.com/rs/zerolog/log" -) - -type Request struct { - Service string `json:"service"` - Version string `json:"version"` -} - -type DSNResponse struct { - DSN string `json:"dsn"` -} - -func IsSentryEnabled() bool { - return os.Getenv("SENTRY_ENABLED") == "true" -} - -func GetDSN(ctx context.Context) (string, error) { - - retryClient := retryablehttp.NewClient() - retryClient.RetryMax = 3 // Max retry attempts - - client := retryClient.StandardClient() - - endpoint := getDSNEndpoint() - - reqBody := Request{ - Service: "tracer", - Version: version.Ver, - } - - jsonData, err := json.Marshal(reqBody) - if err != nil { - return "", fmt.Errorf("error marshalling request body: %v", err) - } - - req, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(jsonData)) - if err != nil { - return "", fmt.Errorf("error creating POST request: %v", err) - } - - req.Header.Set("Content-Type", "application/json") - - resp, err := client.Do(req) - if err != nil { - return "", fmt.Errorf("error making POST request: %v", err) - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - return "", nil // Return empty string if not 200 - } - - body, err := io.ReadAll(resp.Body) - if err != nil { - return "", fmt.Errorf("error reading response body: %v", err) - } - - var dsnResp DSNResponse - err = json.Unmarshal(body, &dsnResp) - if err != nil { - return "", fmt.Errorf("error unmarshalling response body: %v", err) - } - - return dsnResp.DSN, nil -} - -func getDSNEndpoint() string { - apiUrl, ok := os.LookupEnv("KUBESHARK_CLOUD_API_URL") - if !ok { - log.Info().Msg("KUBESHARK_CLOUD_API_URL wasn't found. Defaulting to https://api.kubeshark.co") - apiUrl = "https://api.kubeshark.co" - } - - return fmt.Sprintf("%s/sentry", apiUrl) -}