Skip to content

Commit dd402c9

Browse files
authored
chore: Add tracing instrumentation to source code integration (#4607)
This will allow us to understand better what requests a fired and where they might go wrong.
1 parent cc926a7 commit dd402c9

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
lines changed

pkg/frontend/vcs/client/github.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"connectrpc.com/connect"
1111
"github.com/google/go-github/v58/github"
12+
"github.com/opentracing/opentracing-go"
1213
"golang.org/x/oauth2"
1314

1415
vcsv1 "github.com/grafana/pyroscope/api/gen/proto/go/vcs/v1"
@@ -32,6 +33,12 @@ type githubClient struct {
3233
}
3334

3435
func (gh *githubClient) GetCommit(ctx context.Context, owner, repo, ref string) (*vcsv1.CommitInfo, error) {
36+
sp, ctx := opentracing.StartSpanFromContext(ctx, "githubClient.GetCommit")
37+
defer sp.Finish()
38+
sp.SetTag("owner", owner)
39+
sp.SetTag("repo", repo)
40+
sp.SetTag("ref", ref)
41+
3542
commit, _, err := gh.repoService.GetCommit(ctx, owner, repo, ref, nil)
3643
if err != nil {
3744
var githubErr *github.ErrorResponse
@@ -67,6 +74,13 @@ func (gh *githubClient) GetCommit(ctx context.Context, owner, repo, ref string)
6774
}
6875

6976
func (gh *githubClient) GetFile(ctx context.Context, req FileRequest) (File, error) {
77+
sp, ctx := opentracing.StartSpanFromContext(ctx, "githubClient.GetFile")
78+
defer sp.Finish()
79+
sp.SetTag("owner", req.Owner)
80+
sp.SetTag("repo", req.Repo)
81+
sp.SetTag("path", req.Path)
82+
sp.SetTag("ref", req.Ref)
83+
7084
// We could abstract away git provider using git protocol
7185
// git clone https://x-access-token:<token>@github.com/owner/repo.git
7286
// For now we use the github client.

pkg/frontend/vcs/commit.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/opentracing/opentracing-go"
78
"golang.org/x/sync/errgroup"
89

910
vcsv1 "github.com/grafana/pyroscope/api/gen/proto/go/vcs/v1"
@@ -23,6 +24,12 @@ type gitHubCommitGetter interface {
2324
// This function provides partial success behavior, returning any commits
2425
// that were successfully fetched along with errors for those that failed.
2526
func getCommits(ctx context.Context, client gitHubCommitGetter, owner, repo string, refs []string) ([]*vcsv1.CommitInfo, []error, error) {
27+
sp, ctx := opentracing.StartSpanFromContext(ctx, "getCommits")
28+
defer sp.Finish()
29+
sp.SetTag("owner", owner)
30+
sp.SetTag("repo", repo)
31+
sp.SetTag("ref_count", len(refs))
32+
2633
type result struct {
2734
commit *vcsv1.CommitInfo
2835
err error
@@ -71,6 +78,12 @@ func getCommits(ctx context.Context, client gitHubCommitGetter, owner, repo stri
7178
// tryGetCommit attempts to retrieve a commit using different ref formats (commit hash, branch, tag).
7279
// It tries each format in order and returns the first successful result.
7380
func tryGetCommit(ctx context.Context, client gitHubCommitGetter, owner, repo, ref string) (*vcsv1.CommitInfo, error) {
81+
sp, ctx := opentracing.StartSpanFromContext(ctx, "tryGetCommit")
82+
defer sp.Finish()
83+
sp.SetTag("owner", owner)
84+
sp.SetTag("repo", repo)
85+
sp.SetTag("ref", ref)
86+
7487
refFormats := []string{
7588
ref, // Try as a commit hash
7689
"heads/" + ref, // Try as a branch

pkg/frontend/vcs/service.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/go-kit/log"
1212
giturl "github.com/kubescape/go-git-url"
1313
"github.com/kubescape/go-git-url/apis"
14+
"github.com/opentracing/opentracing-go"
1415
"github.com/prometheus/client_golang/prometheus"
1516
"golang.org/x/oauth2"
1617

@@ -37,6 +38,9 @@ func New(logger log.Logger, reg prometheus.Registerer) *Service {
3738
}
3839

3940
func (q *Service) GithubApp(ctx context.Context, req *connect.Request[vcsv1.GithubAppRequest]) (*connect.Response[vcsv1.GithubAppResponse], error) {
41+
sp, _ := opentracing.StartSpanFromContext(ctx, "GithubApp")
42+
defer sp.Finish()
43+
4044
err := isGitHubIntegrationConfigured()
4145
if err != nil {
4246
q.logger.Log("err", err, "msg", "GitHub integration is not configured")
@@ -50,6 +54,9 @@ func (q *Service) GithubApp(ctx context.Context, req *connect.Request[vcsv1.Gith
5054
}
5155

5256
func (q *Service) GithubLogin(ctx context.Context, req *connect.Request[vcsv1.GithubLoginRequest]) (*connect.Response[vcsv1.GithubLoginResponse], error) {
57+
sp, ctx := opentracing.StartSpanFromContext(ctx, "GithubLogin")
58+
defer sp.Finish()
59+
5360
cfg, err := githubOAuthConfig()
5461
if err != nil {
5562
q.logger.Log("err", err, "msg", "failed to get GitHub OAuth config")
@@ -90,6 +97,9 @@ func (q *Service) GithubLogin(ctx context.Context, req *connect.Request[vcsv1.Gi
9097
}
9198

9299
func (q *Service) GithubRefresh(ctx context.Context, req *connect.Request[vcsv1.GithubRefreshRequest]) (*connect.Response[vcsv1.GithubRefreshResponse], error) {
100+
sp, ctx := opentracing.StartSpanFromContext(ctx, "GithubRefresh")
101+
defer sp.Finish()
102+
93103
token, err := tokenFromRequest(ctx, req)
94104
if err != nil {
95105
q.logger.Log("err", err, "msg", "failed to extract token from request")
@@ -138,6 +148,13 @@ func (q *Service) GithubRefresh(ctx context.Context, req *connect.Request[vcsv1.
138148
}
139149

140150
func (q *Service) GetFile(ctx context.Context, req *connect.Request[vcsv1.GetFileRequest]) (*connect.Response[vcsv1.GetFileResponse], error) {
151+
sp, ctx := opentracing.StartSpanFromContext(ctx, "GetFile")
152+
defer sp.Finish()
153+
sp.SetTag("repository_url", req.Msg.RepositoryURL)
154+
sp.SetTag("local_path", req.Msg.LocalPath)
155+
sp.SetTag("root_path", req.Msg.RootPath)
156+
sp.SetTag("ref", req.Msg.Ref)
157+
141158
token, err := tokenFromRequest(ctx, req)
142159
if err != nil {
143160
q.logger.Log("err", err, "msg", "failed to extract token from request")
@@ -184,6 +201,11 @@ func (q *Service) GetFile(ctx context.Context, req *connect.Request[vcsv1.GetFil
184201
}
185202

186203
func (q *Service) GetCommit(ctx context.Context, req *connect.Request[vcsv1.GetCommitRequest]) (*connect.Response[vcsv1.GetCommitResponse], error) {
204+
sp, ctx := opentracing.StartSpanFromContext(ctx, "GetCommit")
205+
defer sp.Finish()
206+
sp.SetTag("repository_url", req.Msg.RepositoryURL)
207+
sp.SetTag("ref", req.Msg.Ref)
208+
187209
token, err := tokenFromRequest(ctx, req)
188210
if err != nil {
189211
q.logger.Log("err", err, "msg", "failed to extract token from request")
@@ -228,6 +250,9 @@ func (q *Service) GetCommit(ctx context.Context, req *connect.Request[vcsv1.GetC
228250
}
229251

230252
func (q *Service) GetCommits(ctx context.Context, req *connect.Request[vcsv1.GetCommitsRequest]) (*connect.Response[vcsv1.GetCommitsResponse], error) {
253+
sp, ctx := opentracing.StartSpanFromContext(ctx, "GetCommits")
254+
defer sp.Finish()
255+
231256
token, err := tokenFromRequest(ctx, req)
232257
if err != nil {
233258
q.logger.Log("err", err, "msg", "failed to extract token from request")

pkg/frontend/vcs/source/find_go.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/go-kit/log/level"
12+
"github.com/opentracing/opentracing-go"
1213
"golang.org/x/mod/modfile"
1314
"golang.org/x/mod/module"
1415

@@ -23,6 +24,10 @@ const (
2324

2425
// findGoFile finds a go file in a vcs repository.
2526
func (ff FileFinder) findGoFile(ctx context.Context) (*vcsv1.GetFileResponse, error) {
27+
sp, ctx := opentracing.StartSpanFromContext(ctx, "findGoFile")
28+
defer sp.Finish()
29+
sp.SetTag("path", ff.path)
30+
2631
if url, ok := golang.StandardLibraryURL(ff.path); ok {
2732
return ff.fetchURL(ctx, url, false)
2833
}
@@ -50,6 +55,12 @@ func (ff FileFinder) findGoFile(ctx context.Context) (*vcsv1.GetFileResponse, er
5055
}
5156

5257
func (ff FileFinder) fetchGoMod(ctx context.Context) (*modfile.File, error) {
58+
sp, ctx := opentracing.StartSpanFromContext(ctx, "fetchGoMod")
59+
defer sp.Finish()
60+
sp.SetTag("owner", ff.repo.GetOwnerName())
61+
sp.SetTag("repo", ff.repo.GetRepoName())
62+
sp.SetTag("ref", ff.ref)
63+
5364
content, err := ff.client.GetFile(ctx, client.FileRequest{
5465
Owner: ff.repo.GetOwnerName(),
5566
Repo: ff.repo.GetRepoName(),
@@ -63,6 +74,10 @@ func (ff FileFinder) fetchGoMod(ctx context.Context) (*modfile.File, error) {
6374
}
6475

6576
func (ff FileFinder) fetchGoDependencyFile(ctx context.Context, module golang.Module) (*vcsv1.GetFileResponse, error) {
77+
sp, ctx := opentracing.StartSpanFromContext(ctx, "fetchGoDependencyFile")
78+
defer sp.Finish()
79+
sp.SetTag("module_path", module.Path)
80+
6681
switch {
6782
case module.IsGitHub():
6883
return ff.fetchGithubModuleFile(ctx, module)
@@ -73,12 +88,21 @@ func (ff FileFinder) fetchGoDependencyFile(ctx context.Context, module golang.Mo
7388
}
7489

7590
func (ff FileFinder) fetchGithubModuleFile(ctx context.Context, mod golang.Module) (*vcsv1.GetFileResponse, error) {
91+
sp, ctx := opentracing.StartSpanFromContext(ctx, "fetchGithubModuleFile")
92+
defer sp.Finish()
93+
sp.SetTag("module_path", mod.Path)
94+
7695
// todo: what if this is not a github repo?
7796
// VSClient should support querying multiple repo providers.
7897
githubFile, err := mod.GithubFile()
7998
if err != nil {
8099
return nil, err
81100
}
101+
sp.SetTag("owner", githubFile.Owner)
102+
sp.SetTag("repo", githubFile.Repo)
103+
sp.SetTag("path", githubFile.Path)
104+
sp.SetTag("ref", githubFile.Ref)
105+
82106
content, err := ff.client.GetFile(ctx, client.FileRequest{
83107
Owner: githubFile.Owner,
84108
Repo: githubFile.Repo,
@@ -92,10 +116,15 @@ func (ff FileFinder) fetchGithubModuleFile(ctx context.Context, mod golang.Modul
92116
}
93117

94118
func (ff FileFinder) fetchGoogleSourceDependencyFile(ctx context.Context, mod golang.Module) (*vcsv1.GetFileResponse, error) {
119+
sp, ctx := opentracing.StartSpanFromContext(ctx, "fetchGoogleSourceDependencyFile")
120+
defer sp.Finish()
121+
sp.SetTag("module_path", mod.Path)
122+
95123
url, err := mod.GoogleSourceURL()
96124
if err != nil {
97125
return nil, err
98126
}
127+
sp.SetTag("url", url)
99128
return ff.fetchURL(ctx, url, true)
100129
}
101130

pkg/frontend/vcs/source/find_go_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ func Test_tryFindGoFile(t *testing.T) {
8989
}
9090
for _, tt := range tests {
9191
t.Run(tt.name, func(t *testing.T) {
92-
ctxMock := context.Context(nil)
92+
ctx := context.Background()
9393
sut := FileFinder{
9494
path: tt.searchedPath,
9595
rootPath: tt.rootPath,
9696
repo: tt.repo,
9797
client: tt.clientMock,
9898
}
99-
_, err := sut.tryFindGoFile(ctxMock, tt.attempts)
99+
_, err := sut.tryFindGoFile(ctx, tt.attempts)
100100
assert.Equal(t, tt.expectedSearchedPaths, (*tt.clientMock).searchedSequence)
101101
assert.Equal(t, tt.expectedError, err)
102102
})

0 commit comments

Comments
 (0)