Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(routing/http/client): allow custom User-Agent #31

Merged
merged 2 commits into from
Jan 17, 2023

Conversation

lidel
Copy link
Member

@lidel lidel commented Jan 14, 2023

This PR Closes #17

  • Adds WithUserAgent which allows Kubo (or any other user of routing/http/client) to set own version
  • Uses module version if reported via ReadBuildInfo() when no override is present and module is imported from github.com/ipfs/go-libipfs/routing/http/client – allows us to quickly tell if a request was made by stable official release or some hacky fork.
  • Does nothing otherwise. This sends User-Agent: Go-http-client/1.1 (current state)

@welcome

This comment was marked as resolved.

lidel added a commit to ipfs/kubo that referenced this pull request Jan 14, 2023
@lidel lidel marked this pull request as ready for review January 14, 2023 22:07
lidel added a commit to ipfs/kubo that referenced this pull request Jan 14, 2023
routing/http/client/transport.go Outdated Show resolved Hide resolved

// moduleVersion returns a useful user agent version string allowing us to
// identify requests coming from officialxl releases of this module.
func moduleVersion() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test for this? Really I'm interested in making sure a test fails if this code is moved and ImportPath is not updated (or use reflection or something to get the import path dynamically).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guseggert simplified and added test in 4e11e8b

Comment on lines +47 to +79
// ImportPath is the canonical import path that allows us to identify
// official client builds vs modified forks, and use that info in User-Agent header.
var ImportPath = importPath()

// importPath returns the path that library consumers would have in go.mod
func importPath() string {
p := reflect.ValueOf(ResponseBodyLimitedTransport{}).Type().PkgPath()
// we have monorepo, so stripping the remainder
return strings.TrimSuffix(p, "/routing/http/client")
}

// moduleVersion returns a useful user agent version string allowing us to
// identify requests coming from official releases of this module vs forks.
func moduleVersion() (ua string) {
ua = ImportPath
var module *debug.Module
if bi, ok := debug.ReadBuildInfo(); ok {
// If debug.ReadBuildInfo was successful, we can read Version by finding
// this client in the dependency list of the app that has it in go.mod
for _, dep := range bi.Deps {
if dep.Path == ImportPath {
module = dep
break
}
}
if module != nil {
ua += "@" + module.Version
return
}
ua += "@unknown"
}
return
}
Copy link
Member

@ajnavarro ajnavarro Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a bit complex. Can we just have a default string header as a simpler default? something like router/http/go

Edit: Actually, I don't think the actual default header User-Agent: Go-http-client/1.1 is bad, isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaulting to User-Agent: Go-http-client/1.1 is not bad, but does not tell us when someone used our library, and when they wrote their own client.

Having dynamic default set to package name will tell us what version of lib is in the use, how fast people upgrade, and if they fork, we will see the repo name and be able to inspect what changes were made.

Overall, less guesswork for IPFS stewards, more visibility into ecosystem.

@lidel
Copy link
Member Author

lidel commented Jan 17, 2023

As per verbal from today's standup, merging to include this in Kubo 0.18, continued in ipfs/kubo#9550

@lidel lidel merged commit 442a269 into main Jan 17, 2023
@lidel lidel deleted the feat/support-user-agent branch January 17, 2023 18:02
@lidel lidel mentioned this pull request Jan 17, 2023
lidel added a commit to ipfs/kubo that referenced this pull request Jan 17, 2023
lidel added a commit to ipfs/kubo that referenced this pull request Jan 18, 2023
lidel added a commit to ipfs/kubo that referenced this pull request Jan 18, 2023
lidel added a commit to ipfs/kubo that referenced this pull request Jan 18, 2023
galargh pushed a commit to ipfs/kubo that referenced this pull request Jan 23, 2023
hannahhoward pushed a commit to filecoin-project/kubo-api-client that referenced this pull request Jun 19, 2023
hannahhoward pushed a commit to filecoin-project/kubo-api-client that referenced this pull request Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

routing/http/client: allow custom User-Agent
3 participants