Skip to content

Commit 6499f85

Browse files
committed
sunlight: include library version in client requests
1 parent 500d704 commit 6499f85

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

client.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"iter"
1818
"log/slog"
1919
"net/http"
20+
"runtime/debug"
2021
"strings"
2122
"time"
2223

@@ -56,6 +57,8 @@ type ClientConfig struct {
5657

5758
// UserAgent is the User-Agent string used for HTTP requests. It must be
5859
// set, and it must include an email address and/or an HTTPS URL.
60+
//
61+
// The library version will be appended to the User-Agent string.
5962
UserAgent string
6063

6164
// Timeout is how long the Entries iterator can take to yield an entry.
@@ -87,7 +90,7 @@ func NewClient(config *ClientConfig) (*Client, error) {
8790
fetcher, err := torchwood.NewTileFetcher(config.MonitoringPrefix,
8891
torchwood.WithTilePath(TilePath),
8992
torchwood.WithHTTPClient(config.HTTPClient),
90-
torchwood.WithUserAgent(config.UserAgent),
93+
torchwood.WithUserAgent(config.UserAgent+" sunlight/"+libraryVersion()),
9194
torchwood.WithConcurrencyLimit(config.ConcurrencyLimit),
9295
torchwood.WithTileFetcherLogger(config.Logger))
9396
if err != nil {
@@ -382,3 +385,19 @@ func (c *Client) UnauthenticatedTrimmedEntries(ctx context.Context, start, end i
382385
}
383386
}
384387
}
388+
389+
func libraryVersion() string {
390+
info, ok := debug.ReadBuildInfo()
391+
if !ok {
392+
return "unknown"
393+
}
394+
for _, dep := range info.Deps {
395+
if dep.Path == "filippo.io/sunlight" {
396+
if dep.Replace != nil {
397+
return dep.Version + "!"
398+
}
399+
return dep.Version
400+
}
401+
}
402+
return "unknown"
403+
}

0 commit comments

Comments
 (0)