Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit bfbd09e

Browse files
committed
fix/http: add logging middleware to default exported external http clients
1 parent b5d7a4f commit bfbd09e

File tree

139 files changed

+912
-653
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+912
-653
lines changed

cmd/frontend/graphqlbackend/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ go_test(
510510
"//internal/gitserver/gitdomain",
511511
"//internal/gitserver/protocol",
512512
"//internal/gqlutil",
513+
"//internal/httpcli",
513514
"//internal/observation",
514515
"//internal/oobmigration",
515516
"//internal/ratelimit",

cmd/frontend/graphqlbackend/cody_gateway_rate_limit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ func (r *siteResolver) CodyGatewayRateLimitStatus(ctx context.Context) (*[]RateL
2424
return nil, err
2525
}
2626

27-
cgc, ok := codygateway.NewClientFromSiteConfig(httpcli.ExternalDoer)
27+
logger := r.logger.Scoped("CodyGatewayRateLimitStatus")
28+
cgc, ok := codygateway.NewClientFromSiteConfig(httpcli.ExternalDoer(logger))
2829
if !ok {
2930
// Not configured for chat/autocomplete.
3031
return nil, nil

cmd/frontend/graphqlbackend/external_accounts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (r *schemaResolver) AddExternalAccount(ctx context.Context, args *struct {
173173

174174
switch args.ServiceType {
175175
case extsvc.TypeGerrit:
176-
err := gext.AddGerritExternalAccount(ctx, r.db, a.UID, args.ServiceID, args.AccountDetails)
176+
err := gext.AddGerritExternalAccount(ctx, r.db, r.logger, a.UID, args.ServiceID, args.AccountDetails)
177177
if err != nil {
178178
return nil, err
179179
}

cmd/frontend/graphqlbackend/external_accounts_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func TestExternalAccounts_AddExternalAccount(t *testing.T) {
180180
}
181181
}
182182

183-
gerrit.MockVerifyAccount = func(_ context.Context, _ *url.URL, _ *gerrit.AccountCredentials) (*gerrit.Account, error) {
183+
gerrit.MockVerifyAccount = func(_ context.Context, _ log.Logger, _ *url.URL, _ *gerrit.AccountCredentials) (*gerrit.Account, error) {
184184
return &gerrit.Account{
185185
ID: 1234,
186186
Username: "alice",

cmd/frontend/graphqlbackend/external_service.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,14 @@ func (r *externalServiceResolver) CheckConnection(ctx context.Context) (*externa
311311
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
312312
defer cancel()
313313

314+
logger := r.logger.Scoped("externalServiceResolver.CheckConnection")
315+
314316
source, err := repos.NewSource(
315317
ctx,
316-
log.Scoped("externalServiceResolver.CheckConnection"),
318+
logger,
317319
r.db,
318320
r.externalService,
319-
httpcli.ExternalClientFactory,
321+
httpcli.ExternalClientFactory(logger.Scoped("httpclient")),
320322
gitserver.NewClient("graphql.check-connection"),
321323
)
322324
if err != nil {

cmd/frontend/graphqlbackend/guardrails_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/graph-gophers/graphql-go"
12+
"github.com/sourcegraph/log"
1213
"github.com/stretchr/testify/require"
1314

1415
"github.com/sourcegraph/sourcegraph/cmd/frontend/enterprise"
@@ -20,6 +21,7 @@ import (
2021
"github.com/sourcegraph/sourcegraph/internal/database"
2122
"github.com/sourcegraph/sourcegraph/internal/database/dbmocks"
2223
"github.com/sourcegraph/sourcegraph/internal/gitserver"
24+
"github.com/sourcegraph/sourcegraph/internal/httpcli"
2325
"github.com/sourcegraph/sourcegraph/internal/observation"
2426
"github.com/sourcegraph/sourcegraph/lib/errors"
2527
"github.com/sourcegraph/sourcegraph/lib/pointers"
@@ -194,7 +196,9 @@ func makeGatewayEndpoint(t *testing.T) string {
194196

195197
func TestSnippetAttributionReactsToSiteConfigChanges(t *testing.T) {
196198
// Use a regular HTTP client as default external doer cannot hit localhost.
197-
guardrails.MockHttpClient = http.DefaultClient
199+
guardrails.MockHttpClient = func(_ log.Logger) httpcli.Doer {
200+
return http.DefaultClient
201+
}
198202
t.Cleanup(func() { guardrails.MockHttpClient = nil })
199203
// Starting attribution configuration has no endpoints to use.
200204
noAttributionConfigured := schema.SiteConfiguration{

cmd/frontend/graphqlbackend/survey_response.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (r *schemaResolver) SubmitSurvey(ctx context.Context, args *struct {
117117
}
118118

119119
// Submit form to HubSpot
120-
if err := hubspotutil.Client().SubmitForm(hubspotutil.SurveyFormID, &surveySubmissionForHubSpot{
120+
if err := hubspotutil.Client(r.logger).SubmitForm(hubspotutil.SurveyFormID, &surveySubmissionForHubSpot{
121121
Email: email,
122122
Score: args.Input.Score,
123123
OtherUseCase: args.Input.OtherUseCase,
@@ -181,7 +181,7 @@ func (r *schemaResolver) SubmitHappinessFeedback(ctx context.Context, args *stru
181181
}
182182

183183
// Submit form to HubSpot
184-
if err := hubspotutil.Client().SubmitForm(hubspotutil.HappinessFeedbackFormID, &data); err != nil {
184+
if err := hubspotutil.Client(r.logger).SubmitForm(hubspotutil.HappinessFeedbackFormID, &data); err != nil {
185185
// Log an error, but don't return one if the only failure was in submitting feedback results to HubSpot.
186186
log15.Error("Unable to submit happiness feedback results to Sourcegraph remote", "error", err)
187187
}

cmd/frontend/graphqlbackend/user_usage_stats.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ func (r *schemaResolver) LogEvents(ctx context.Context, args *EventBatch) (*Empt
169169
}
170170
}
171171

172-
hubspotutil.SyncUser(userPrimaryEmail, hubspotutil.CodyClientInstalledEventID, &hubspot.ContactProperties{
172+
hubspotutil.SyncUser(r.logger, userPrimaryEmail, hubspotutil.CodyClientInstalledEventID, &hubspot.ContactProperties{
173173
DatabaseID: userID,
174174
})
175175

176-
hubspotutil.SyncUserWithV3Event(userPrimaryEmail, hubspotutil.CodyClientInstalledV3EventID,
176+
hubspotutil.SyncUserWithV3Event(r.logger, userPrimaryEmail, hubspotutil.CodyClientInstalledV3EventID,
177177
&hubspot.ContactProperties{
178178
DatabaseID: userID,
179179
},

cmd/frontend/hubspot/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go_library(
1414
"//internal/httpcli",
1515
"//lib/errors",
1616
"@com_github_google_go_querystring//query",
17+
"@com_github_sourcegraph_log//:log",
1718
"@org_uber_go_atomic//:atomic",
1819
],
1920
)

cmd/frontend/hubspot/hubspot.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/google/go-querystring/query"
15+
"github.com/sourcegraph/log"
1516
"go.uber.org/atomic"
1617

1718
"github.com/sourcegraph/sourcegraph/internal/httpcli"
@@ -25,13 +26,15 @@ type Client struct {
2526

2627
lastPing atomic.Time
2728
lastPingResult atomic.Error
29+
logger log.Logger
2830
}
2931

3032
// New returns a new HubSpot client using the given Portal ID.
31-
func New(portalID, accessToken string) *Client {
33+
func New(portalID, accessToken string, logger log.Logger) *Client {
3234
return &Client{
3335
portalID: portalID,
3436
accessToken: accessToken,
37+
logger: logger.Scoped("HubSpotClient"),
3538
}
3639
}
3740

@@ -61,7 +64,7 @@ func (c *Client) postForm(methodName string, baseURL *url.URL, suffix string, bo
6164
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
6265
setAccessTokenAuthorizationHeader(req, c.accessToken)
6366

64-
resp, err := httpcli.ExternalDoer.Do(req)
67+
resp, err := httpcli.ExternalDoer(c.logger).Do(req)
6568
if err != nil {
6669
return wrapError(methodName, err)
6770
}
@@ -95,7 +98,7 @@ func (c *Client) postJSON(methodName string, baseURL *url.URL, reqPayload, respP
9598
req.Header.Set("Content-Type", "application/json")
9699
setAccessTokenAuthorizationHeader(req, c.accessToken)
97100

98-
resp, err := httpcli.ExternalDoer.Do(req.WithContext(ctx))
101+
resp, err := httpcli.ExternalDoer(c.logger).Do(req.WithContext(ctx))
99102
if err != nil {
100103
return wrapError(methodName, err)
101104
}
@@ -129,7 +132,7 @@ func (c *Client) get(ctx context.Context, methodName string, baseURL *url.URL, s
129132
ctx, cancel := context.WithTimeout(req.Context(), time.Minute)
130133
defer cancel()
131134

132-
resp, err := httpcli.ExternalDoer.Do(req.WithContext(ctx))
135+
resp, err := httpcli.ExternalDoer(c.logger).Do(req.WithContext(ctx))
133136
if err != nil {
134137
return wrapError(methodName, err)
135138
}

0 commit comments

Comments
 (0)