-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Fallback to reference rpcs and record error metric
- Loading branch information
Showing
11 changed files
with
431 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package metrics | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
|
||
"golang.org/x/exp/slog" | ||
) | ||
|
||
type FallbackClient struct { | ||
hosts []url.URL | ||
httpDo func(req *http.Request) (*http.Response, error) | ||
log *slog.Logger | ||
metrics ClientMetrics | ||
rpcType string | ||
} | ||
|
||
type ClientMetrics interface { | ||
IncClientError(rpcType string, host url.URL, reason string) | ||
// TODO(nix): Metrics for request counts. Latency histogram. | ||
} | ||
|
||
func NewFallbackClient(client *http.Client, metrics ClientMetrics, rpcType string, hosts []url.URL) *FallbackClient { | ||
if len(hosts) == 0 { | ||
panic("no hosts provided") | ||
} | ||
return &FallbackClient{ | ||
hosts: hosts, | ||
httpDo: client.Do, | ||
log: slog.Default(), | ||
metrics: metrics, | ||
rpcType: rpcType, | ||
} | ||
} | ||
|
||
const unknownErrReason = "unknown" | ||
|
||
func (c FallbackClient) Get(ctx context.Context, path string) (*http.Response, error) { | ||
doGet := func(host url.URL) (*http.Response, error) { | ||
log := c.log.With("host", host.Hostname(), "path", path, "rpc", c.rpcType) | ||
host.Path = path | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, host.String(), nil) | ||
if err != nil { | ||
log.Error("Failed to create request", "error", err) | ||
c.recordErrMetric(host, err) | ||
return nil, err | ||
} | ||
resp, err := c.httpDo(req) | ||
if err != nil { | ||
log.Error("Failed to send request", "error", err) | ||
c.recordErrMetric(host, err) | ||
return nil, err | ||
} | ||
if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
_ = resp.Body.Close() | ||
log.Error("Response returned bad status code", "status", resp.StatusCode) | ||
c.metrics.IncClientError(c.rpcType, host, strconv.Itoa(resp.StatusCode)) | ||
return nil, fmt.Errorf("%s: bad status code %d", req.URL, resp.StatusCode) | ||
} | ||
return resp, nil | ||
} | ||
|
||
var lastErr error | ||
for _, host := range c.hosts { | ||
resp, err := doGet(host) | ||
if err != nil { | ||
lastErr = err | ||
continue | ||
} | ||
return resp, nil | ||
} | ||
return nil, lastErr | ||
} | ||
|
||
func (c FallbackClient) recordErrMetric(host url.URL, err error) { | ||
reason := unknownErrReason | ||
switch { | ||
case errors.Is(err, context.DeadlineExceeded): | ||
reason = "timeout" | ||
case errors.Is(err, context.Canceled): | ||
// Do not record when the process is shutting down. | ||
return | ||
} | ||
c.metrics.IncClientError(c.rpcType, host, reason) | ||
} |
Oops, something went wrong.