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

feat(executor): Log verb kind statusCode for executor Kubernetes API requests #4989

Merged
merged 6 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/argoexec/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/argoproj/argo/v3"
"github.com/argoproj/argo/v3/util"
"github.com/argoproj/argo/v3/util/cmd"
"github.com/argoproj/argo/v3/util/logs"
"github.com/argoproj/argo/v3/workflow/common"
"github.com/argoproj/argo/v3/workflow/executor"
"github.com/argoproj/argo/v3/workflow/executor/docker"
Expand Down Expand Up @@ -74,6 +75,8 @@ func initExecutor() *executor.WorkflowExecutor {
config, err := clientConfig.ClientConfig()
checkErr(err)

logs.AddK8SLogTransportWrapper(config) // lets log all request as we should typically do < 5 per pod, so this is will show up problems

namespace, _, err := clientConfig.Namespace()
checkErr(err)

Expand Down
38 changes: 38 additions & 0 deletions util/k8s/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package k8s

import (
"net/http"
"strings"
)

func ParseRequest(r *http.Request) (verb string, kind string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lift-and-shift

i := strings.Index(r.URL.Path, "/v") + 1
path := strings.Split(r.URL.Path[i:], "/")
n := len(path)

verb = map[string]string{
http.MethodGet: "List",
http.MethodPost: "Create",
http.MethodDelete: "Delete",
http.MethodPatch: "Patch",
http.MethodPut: "Update",
}[r.Method]

if r.URL.Query().Get("watch") != "" {
verb = "Watch"
} else if verb == "List" && n%2 == 1 {
verb = "Get"
} else if verb == "Delete" && n%2 == 0 {
verb = "DeleteCollection"
}

kind = "Unknown"
switch verb {
case "List", "Watch", "Create", "DeleteCollection":
kind = path[n-1]
case "Get", "Delete", "Patch", "Update":
kind = path[n-2]
}

return verb, kind
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package metrics
package k8s

import (
"net/http"
Expand All @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
)

func Test_parseRequest(t *testing.T) {
func Test_ParseRequest(t *testing.T) {
for _, tt := range []struct {
name string
method string
Expand All @@ -26,7 +26,7 @@ func Test_parseRequest(t *testing.T) {
} {
t.Run(tt.name, func(t *testing.T) {
x, _ := url.Parse(tt.url)
verb, kind := parseRequest(&http.Request{Method: tt.method, URL: x})
verb, kind := ParseRequest(&http.Request{Method: tt.method, URL: x})
assert.Equal(t, tt.wantVerb, verb)
assert.Equal(t, tt.wantKind, kind)
})
Expand Down
34 changes: 34 additions & 0 deletions util/logs/log-k8s-requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package logs

import (
"net/http"

log "github.com/sirupsen/logrus"
"k8s.io/client-go/rest"

"github.com/argoproj/argo/v3/util/k8s"
)

type k8sLogRoundTripper struct {
roundTripper http.RoundTripper
}

func (m k8sLogRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
x, err := m.roundTripper.RoundTrip(r)
if x != nil {
verb, kind := k8s.ParseRequest(r)
log.Infof("%s %s %d", verb, kind, x.StatusCode)
}
return x, err
}

func AddK8SLogTransportWrapper(config *rest.Config) *rest.Config {
wrap := config.WrapTransport
config.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
if wrap != nil {
rt = wrap(rt)
}
return &k8sLogRoundTripper{roundTripper: rt}
}
return config
}
37 changes: 3 additions & 34 deletions workflow/metrics/k8s_request_total_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package metrics
import (
"net/http"
"strconv"
"strings"

"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/rest"

"github.com/argoproj/argo/v3/util/k8s"
)

var (
Expand All @@ -28,44 +29,12 @@ type metricsRoundTripper struct {
func (m metricsRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
x, err := m.roundTripper.RoundTrip(r)
if x != nil {
verb, kind := parseRequest(r)
verb, kind := k8s.ParseRequest(r)
K8sRequestTotalMetric.WithLabelValues(kind, verb, strconv.Itoa(x.StatusCode)).Inc()
}
return x, err
}

func parseRequest(r *http.Request) (verb string, kind string) {
i := strings.Index(r.URL.Path, "/v") + 1
path := strings.Split(r.URL.Path[i:], "/")
n := len(path)

verb = map[string]string{
http.MethodGet: "List",
http.MethodPost: "Create",
http.MethodDelete: "Delete",
http.MethodPatch: "Patch",
http.MethodPut: "Update",
}[r.Method]

if r.URL.Query().Get("watch") != "" {
verb = "Watch"
} else if verb == "List" && n%2 == 1 {
verb = "Get"
} else if verb == "Delete" && n%2 == 0 {
verb = "DeleteCollection"
}

kind = "Unknown"
switch verb {
case "List", "Watch", "Create", "DeleteCollection":
kind = path[n-1]
case "Get", "Delete", "Patch", "Update":
kind = path[n-2]
}

return verb, kind
}

func AddMetricsTransportWrapper(config *rest.Config) *rest.Config {
wrap := config.WrapTransport
config.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
Expand Down
2 changes: 1 addition & 1 deletion workflow/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func ResumeWorkflow(ctx context.Context, wfIf v1alpha1.WorkflowInterface, hydrat

err = hydrator.Hydrate(wf)
if err != nil {
return false, err
return true, err
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug

}

workflowUpdated := false
Expand Down