Skip to content

Don't return 500's for KV CAS calls if the CAS returned an HTTP status code. #1798

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

Merged
merged 1 commit into from
Nov 10, 2019
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
15 changes: 14 additions & 1 deletion pkg/ring/kv/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package kv

import (
"context"
"strconv"

"github.com/prometheus/client_golang/prometheus"
"github.com/weaveworks/common/httpgrpc"
"github.com/weaveworks/common/instrument"
)

Expand All @@ -18,6 +20,17 @@ func init() {
requestDuration.Register()
}

// errorCode converts an error into an HTTP status code, modified from weaveworks/common/instrument
func errorCode(err error) string {
if err == nil {
return "200"
}
if resp, ok := httpgrpc.HTTPResponseFromError(err); ok {
Copy link
Contributor

@pstibrany pstibrany Nov 8, 2019

Choose a reason for hiding this comment

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

This only works for errors from GRPC layer. I don't think etcd or consul use GRPC. Update: etcd v3 does!

return strconv.Itoa(int(resp.GetCode()))
}
return "500"
}

type metrics struct {
c Client
}
Expand All @@ -33,7 +46,7 @@ func (m metrics) Get(ctx context.Context, key string) (interface{}, error) {
}

func (m metrics) CAS(ctx context.Context, key string, f func(in interface{}) (out interface{}, retry bool, err error)) error {
return instrument.CollectedRequest(ctx, "CAS", requestDuration, instrument.ErrorCode, func(ctx context.Context) error {
return instrument.CollectedRequest(ctx, "CAS", requestDuration, errorCode, func(ctx context.Context) error {
return m.c.CAS(ctx, key, f)
})
}
Expand Down