From a9a70f448dfca363b604cb4b682679e966ef96c0 Mon Sep 17 00:00:00 2001 From: "taekyu.kang" Date: Tue, 7 May 2024 10:29:53 +0900 Subject: [PATCH] feature. implementation calculating resources for namespace --- internal/delivery/http/app-serve-app.go | 2 +- internal/usecase/dashboard.go | 5 ++++ internal/usecase/project.go | 40 ++++++++++++++++++------- internal/validator/validator.go | 5 ++-- pkg/thanos-client/types.go | 1 + 5 files changed, 40 insertions(+), 13 deletions(-) diff --git a/internal/delivery/http/app-serve-app.go b/internal/delivery/http/app-serve-app.go index ab127e72..70a383ac 100644 --- a/internal/delivery/http/app-serve-app.go +++ b/internal/delivery/http/app-serve-app.go @@ -113,7 +113,7 @@ func (h *AppServeAppHandler) CreateAppServeApp(w http.ResponseWriter, r *http.Re appReq := domain.CreateAppServeAppRequest{} err := UnmarshalRequestInput(r, &appReq) if err != nil { - ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("Error while unmarshalling request"), "C_INTERNAL_ERROR", "")) + ErrorJSON(w, r, err) return } diff --git a/internal/usecase/dashboard.go b/internal/usecase/dashboard.go index 31eb3ef6..a47f3fae 100644 --- a/internal/usecase/dashboard.go +++ b/internal/usecase/dashboard.go @@ -1141,6 +1141,11 @@ func (u *DashboardUsecase) GetThanosClient(ctx context.Context, organizationId s return nil, httpErrors.NewInternalServerError(err, "D_INVALID_PRIMARY_STACK", "") } address, port := helper.SplitAddress(ctx, thanosUrl) + + // [TEST] + //address = "http://a93c60de70c794ef39b495976588c989-d7cd29ca75def693.elb.ap-northeast-2.amazonaws.com" + //port = 9090 + client, err := thanos.New(address, port, false, "") if err != nil { return nil, errors.Wrap(err, "failed to create thanos client") diff --git a/internal/usecase/project.go b/internal/usecase/project.go index 80775111..2ad792a4 100644 --- a/internal/usecase/project.go +++ b/internal/usecase/project.go @@ -911,19 +911,39 @@ func (u *ProjectUsecase) GetResourcesUsage(ctx context.Context, thanosClient tha if err != nil { return out, errors.Wrap(err, fmt.Sprintf("Failed to get cluster : stackId %s", stackId)) } - /* - query := "sum(rate(container_cpu_usage_seconds_total{image!=\"\"}[10m]) ) by (taco_cluster, namespace)" - result, err := thanosClient.Get(ctx, query) - if err != nil { - return out, err + + // sum(rate(container_cpu_usage_seconds_total{taco_cluster=\"$taco_cluster\",image!=\"\"}[$__rate_interval])) by (namespace) + query := fmt.Sprintf("sum(rate(container_cpu_usage_seconds_total{image!=\"\", namespace=\"%s\"}[10m]) ) by (taco_cluster, namespace)", namespace) + result, err := thanosClient.Get(ctx, query) + if err != nil { + return out, err + } + for _, val := range result.Data.Result { + if val.Metric.TacoCluster == stackId.String() { + if val.Metric.Namespace == namespace { + if s, err := strconv.ParseFloat(val.Value[1].(string), 32); err == nil { + out.Cpu = fmt.Sprintf("%0.2f %%", s*100) + } + } } - log.Info(ctx, helper.ModelToJson(result)) + } - */ + // sum(container_memory_working_set_bytes{taco_cluster=\"$taco_cluster\",image!=\"\"}) by (namespace) + query = fmt.Sprintf("sum(container_memory_working_set_bytes{image!=\"\", namespace=\"%s\"}) by (taco_cluster, namespace)", namespace) + result, err = thanosClient.Get(ctx, query) + if err != nil { + return out, err + } + for _, val := range result.Data.Result { + if val.Metric.TacoCluster == stackId.String() { + if val.Metric.Namespace == namespace { + memory, _ := strconv.Atoi(val.Value[1].(string)) + out.Memory = fmt.Sprintf("%d MiB", memory/1024/1024) + } + } + } - out.Cpu = "1.0 %" - out.Memory = "2.0 %" - out.Storage = "3.0 %" + out.Storage = "" return } diff --git a/internal/validator/validator.go b/internal/validator/validator.go index 5d9fd3f7..3a161738 100644 --- a/internal/validator/validator.go +++ b/internal/validator/validator.go @@ -1,6 +1,7 @@ package validator import ( + "context" "regexp" "strings" "unicode/utf8" @@ -10,7 +11,7 @@ import ( validator "github.com/go-playground/validator/v10" en_translations "github.com/go-playground/validator/v10/translations/en" "github.com/openinfradev/tks-api/pkg/domain" - "github.com/opentracing/opentracing-go/log" + "github.com/openinfradev/tks-api/pkg/log" ) const ( @@ -30,7 +31,7 @@ func NewValidator() (*validator.Validate, *ut.UniversalTranslator) { v := validator.New() err := en_translations.RegisterDefaultTranslations(v, trans) if err != nil { - log.Error(err) + log.Error(context.TODO(), err) } // register custom validator diff --git a/pkg/thanos-client/types.go b/pkg/thanos-client/types.go index cffb437a..3d6c7c80 100644 --- a/pkg/thanos-client/types.go +++ b/pkg/thanos-client/types.go @@ -18,6 +18,7 @@ type MetricDataResult struct { type MetricDataResultMetric struct { Name string `json:"__name__"` + Namespace string `json:"namespace,omitempty"` TacoCluster string `json:"taco_cluster"` }