Skip to content

Commit fd0003a

Browse files
turan18sparr
authored andcommitted
Add custom soci-snapshotter User-Agent when fetching spans
Signed-off-by: Yasin Turan <turyasin@amazon.com>
1 parent 86c249e commit fd0003a

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

fs/remote/resolver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ func (f *httpFetcher) fetch(ctx context.Context, rs []region, retry bool) (multi
429429
}
430430
req.Header.Add("Range", fmt.Sprintf("bytes=%s", ranges[:len(ranges)-1]))
431431
req.Header.Add("Accept-Encoding", "identity")
432+
req.Header.Add("User-Agent", socihttp.UserAgent)
432433
req.Close = false
433434

434435
// Recording the roundtrip latency for remote registry GET operation.

fs/remote/resolver_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func TestRetry(t *testing.T) {
296296
}
297297

298298
if tr.retryCount != 4 {
299-
t.Fatalf("unxpected retryCount; expected=4 got=%d", tr.retryCount)
299+
t.Fatalf("unexpected retryCount; expected=4 got=%d", tr.retryCount)
300300
}
301301
}
302302

@@ -335,3 +335,37 @@ func (r *retryRoundTripper) RoundTrip(req *http.Request) (res *http.Response, er
335335
}
336336
return
337337
}
338+
339+
func TestCustomUserAgent(t *testing.T) {
340+
rt := &userAgentRoundTripper{expectedUserAgent: socihttp.UserAgent}
341+
f := &httpFetcher{
342+
url: "dummyregistry",
343+
tr: rt,
344+
}
345+
regions := []region{{b: 0, e: 1}}
346+
_, err := f.fetch(context.Background(), regions, true)
347+
if err != nil {
348+
t.Fatalf("unexpected error = %v", err)
349+
}
350+
if rt.roundTripUserAgent != rt.expectedUserAgent {
351+
t.Fatalf("unexpected User-Agent; expected %s; got %s", rt.expectedUserAgent, rt.roundTripUserAgent)
352+
}
353+
}
354+
355+
type userAgentRoundTripper struct {
356+
expectedUserAgent string
357+
roundTripUserAgent string
358+
}
359+
360+
func (u *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
361+
u.roundTripUserAgent = req.UserAgent()
362+
363+
header := make(http.Header)
364+
header.Add("Content-Length", "4")
365+
return &http.Response{
366+
StatusCode: http.StatusOK,
367+
Request: req,
368+
Header: header,
369+
Body: io.NopCloser(bytes.NewReader([]byte("test"))),
370+
}, nil
371+
}

util/http/retry.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,23 @@ package http
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"math/rand"
2223
"net"
2324
"net/http"
2425
"time"
2526

2627
"github.com/awslabs/soci-snapshotter/config"
28+
"github.com/awslabs/soci-snapshotter/version"
2729
"github.com/containerd/containerd/log"
2830
rhttp "github.com/hashicorp/go-retryablehttp"
2931
"github.com/sirupsen/logrus"
3032
)
3133

34+
var (
35+
UserAgent = fmt.Sprintf("soci-snapshotter/%s", version.Version)
36+
)
37+
3238
// NewRetryableClient creates a go http.Client which will automatically
3339
// retry on non-fatal errors
3440
func NewRetryableClient(config config.RetryableHTTPClientConfig) *http.Client {

0 commit comments

Comments
 (0)