Skip to content

Commit

Permalink
Merge pull request #7 from ElisaOyj/feature/detectmode
Browse files Browse the repository at this point in the history
support sum_rate and sum_irate recording rules
  • Loading branch information
zetaab authored Sep 17, 2021
2 parents 3b6f02f + 79b7351 commit 6c71199
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
7 changes: 7 additions & 0 deletions pkg/advisor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ func Run(o *Options) (*Response, error) {
if err != nil {
return nil, err
}

ctx := context.Background()

o.mode, err = o.detectMode(ctx)
if err != nil {
return nil, err
}

if o.NamespaceSelector != "" {
namespaces, err := o.Client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{
LabelSelector: o.NamespaceSelector,
Expand All @@ -65,6 +71,7 @@ func Run(o *Options) (*Response, error) {
fmt.Printf("Namespaces: %s\n", o.usedNamespaces)
fmt.Printf("Quantile: %s\n", o.Quantile)
fmt.Printf("Limit margin: %s\n", o.LimitMargin)
fmt.Printf("Using mode: %s\n", o.mode)

data := [][]string{}

Expand Down
1 change: 1 addition & 0 deletions pkg/advisor/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Options struct {
LimitMargin string
promClient *promClient
Client *kubernetes.Clientset
mode string // sum_irate or sum_rate, older prometheusrules uses sum_rate but newest uses sum_irate
}

// Response contains struct to get response from resource-advisor
Expand Down
35 changes: 31 additions & 4 deletions pkg/advisor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (

const (
promOperatorClusterURL = "/api/v1/namespaces/monitoring/services/prometheus-operated:web/proxy/"
podCPURequest = `quantile_over_time(%s, node_namespace_pod_container:container_cpu_usage_seconds_total:sum_rate{pod="%s", container!=""}[1w])`
podCPULimit = `max_over_time(node_namespace_pod_container:container_cpu_usage_seconds_total:sum_rate{pod="%s", container!=""}[1w]) * %s`
podCPURequest = `quantile_over_time(%s, node_namespace_pod_container:container_cpu_usage_seconds_total:%s{pod="%s", container!=""}[1w])`
podCPULimit = `max_over_time(node_namespace_pod_container:container_cpu_usage_seconds_total:%s{pod="%s", container!=""}[1w]) * %s`
podMemoryRequest = `quantile_over_time(%s, container_memory_working_set_bytes{pod="%s", container!=""}[1w]) / 1024 / 1024`
podMemoryLimit = `(max_over_time(container_memory_working_set_bytes{pod="%s", container!=""}[1w]) / 1024 / 1024) * %s`
deploymentRevision = "deployment.kubernetes.io/revision"
Expand Down Expand Up @@ -89,12 +89,12 @@ func (o *Options) queryPrometheusForPod(ctx context.Context, client *promClient,
var err error

output := prometheusMetrics{}
output.RequestCPU, err = queryStatistic(ctx, client, fmt.Sprintf(podCPURequest, o.Quantile, pod.Name), now)
output.RequestCPU, err = queryStatistic(ctx, client, fmt.Sprintf(podCPURequest, o.Quantile, o.mode, pod.Name), now)
if err != nil {
return output, err
}

output.LimitCPU, err = queryStatistic(ctx, client, fmt.Sprintf(podCPULimit, pod.Name, o.LimitMargin), now)
output.LimitCPU, err = queryStatistic(ctx, client, fmt.Sprintf(podCPULimit, o.mode, pod.Name, o.LimitMargin), now)
if err != nil {
return output, err
}
Expand Down Expand Up @@ -181,6 +181,33 @@ func queryPrometheus(ctx context.Context, client *promClient, query string, ts t
return promcli.Query(ctx, query, ts)
}

func (o *Options) detectMode(ctx context.Context) (string, error) {
now := time.Now()

cpuUsage := `node_namespace_pod_container:container_cpu_usage_seconds_total:%s`

request := fmt.Sprintf(cpuUsage, "sum_irate")
response, _, err := queryPrometheus(ctx, o.promClient, request, now)
if err != nil {
return "", fmt.Errorf("Error detecting mode %v", err)
}
asSamples := response.(prommodel.Vector)
if len(asSamples) > 0 {
return "sum_irate", nil
}

request = fmt.Sprintf(cpuUsage, "sum_rate")
response, _, err = queryPrometheus(ctx, o.promClient, request, now)
if err != nil {
return "", fmt.Errorf("Error detecting mode %v", err)
}
asSamples = response.(prommodel.Vector)
if len(asSamples) > 0 {
return "sum_rate", nil
}
return "", fmt.Errorf("Could not find cpu mode")
}

func (c *promClient) URL(ep string, args map[string]string) *url.URL {
p := path.Join(c.endpoint.Path, ep)

Expand Down

0 comments on commit 6c71199

Please sign in to comment.