-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Track metrics around usage of the GitHub API (#3273)
- Loading branch information
1 parent
75bb639
commit 2063655
Showing
12 changed files
with
222 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
|
||
"github.com/grafana/pyroscope/pkg/util" | ||
) | ||
|
||
var ( | ||
githubRouteMatchers = map[string]*regexp.Regexp{ | ||
// Get repository contents. | ||
// https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#get-repository-content | ||
"/repos/{owner}/{repo}/contents/{path}": regexp.MustCompile(`^\/repos\/\S+\/\S+\/contents\/\S+$`), | ||
|
||
// Get a commit. | ||
// https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#get-a-commit | ||
"/repos/{owner}/{repo}/commits/{ref}": regexp.MustCompile(`^\/repos\/\S+\/\S+\/commits\/\S+$`), | ||
|
||
// Refresh auth token. | ||
// https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/refreshing-user-access-tokens#refreshing-a-user-access-token-with-a-refresh-token | ||
"/login/oauth/access_token": regexp.MustCompile(`^\/login\/oauth\/access_token$`), | ||
} | ||
) | ||
|
||
func InstrumentedHTTPClient(logger log.Logger, reg prometheus.Registerer) *http.Client { | ||
apiDuration := promauto.With(reg).NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Namespace: "pyroscope", | ||
Name: "vcs_github_request_duration", | ||
Help: "Duration of GitHub API requests in seconds", | ||
Buckets: prometheus.ExponentialBucketsRange(0.1, 10, 8), | ||
}, | ||
[]string{"method", "route", "status_code"}, | ||
) | ||
|
||
defaultClient := &http.Client{ | ||
Timeout: 10 * time.Second, | ||
Transport: http.DefaultTransport, | ||
} | ||
client := util.InstrumentedHTTPClient(defaultClient, withGitHubMetricsTransport(logger, apiDuration)) | ||
return client | ||
} | ||
|
||
// withGitHubMetricsTransport wraps a transport with a client to track GitHub | ||
// API usage. | ||
func withGitHubMetricsTransport(logger log.Logger, hv *prometheus.HistogramVec) util.RoundTripperInstrumentFunc { | ||
return func(next http.RoundTripper) http.RoundTripper { | ||
return util.RoundTripperFunc(func(req *http.Request) (*http.Response, error) { | ||
route := matchGitHubAPIRoute(req.URL.Path) | ||
statusCode := "" | ||
start := time.Now() | ||
|
||
res, err := next.RoundTrip(req) | ||
if err == nil { | ||
statusCode = fmt.Sprintf("%d", res.StatusCode) | ||
} | ||
|
||
if route == "unknown_route" { | ||
level.Warn(logger).Log("path", req.URL.Path, "msg", "unknown GitHub API route") | ||
} | ||
hv.WithLabelValues(req.Method, route, statusCode).Observe(time.Since(start).Seconds()) | ||
|
||
return res, err | ||
}) | ||
} | ||
} | ||
|
||
func matchGitHubAPIRoute(path string) string { | ||
for route, regex := range githubRouteMatchers { | ||
if regex.MatchString(path) { | ||
return route | ||
} | ||
} | ||
|
||
return "unknown_route" | ||
} |
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,78 @@ | ||
package client | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_matchGitHubAPIRoute(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
Path string | ||
Want string | ||
}{ | ||
{ | ||
Name: "GetContents", | ||
Path: "/repos/grafana/pyroscope/contents/pkg/querier/querier.go", | ||
Want: "/repos/{owner}/{repo}/contents/{path}", | ||
}, | ||
{ | ||
Name: "GetContents with dash", | ||
Path: "/repos/connectrpc/connect-go/contents/protocol.go", | ||
Want: "/repos/{owner}/{repo}/contents/{path}", | ||
}, | ||
{ | ||
Name: "GetContents without path", | ||
Path: "/repos/grafana/pyroscope/contents/", | ||
Want: "unknown_route", | ||
}, | ||
{ | ||
Name: "GetContents with whitespace in path", | ||
Path: "/repos/grafana/pyroscope/contents/path with spaces", | ||
Want: "unknown_route", | ||
}, | ||
{ | ||
Name: "GetCommit", | ||
Path: "/repos/grafana/pyroscope/commits/abcdef1234567890", | ||
Want: "/repos/{owner}/{repo}/commits/{ref}", | ||
}, | ||
{ | ||
Name: "GetCommit with lowercase and uppercase ref", | ||
Path: "/repos/grafana/pyroscope/commits/abcdefABCDEF1234567890", | ||
Want: "/repos/{owner}/{repo}/commits/{ref}", | ||
}, | ||
{ | ||
Name: "GetCommit with non-hexadecimal ref", | ||
Path: "/repos/grafana/pyroscope/commits/HEAD", | ||
Want: "/repos/{owner}/{repo}/commits/{ref}", | ||
}, | ||
{ | ||
Name: "GetCommit without commit", | ||
Path: "/repos/grafana/pyroscope/commits/", | ||
Want: "unknown_route", | ||
}, | ||
{ | ||
Name: "Refresh", | ||
Path: "/login/oauth/access_token", | ||
Want: "/login/oauth/access_token", | ||
}, | ||
{ | ||
Name: "empty path", | ||
Path: "", | ||
Want: "unknown_route", | ||
}, | ||
{ | ||
Name: "unmapped path", | ||
Path: "/some/random/path", | ||
Want: "unknown_route", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.Name, func(t *testing.T) { | ||
got := matchGitHubAPIRoute(tt.Path) | ||
require.Equal(t, tt.Want, got) | ||
}) | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.