Skip to content
New issue

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

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

Already on GitHub? # to your account

AWSPrometheusRemoteWriteExporter - Add SDK and system information to User-Agent header #3317

Merged
Merged
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions exporter/awsprometheusremotewriteexporter/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"io/ioutil"
"net/http"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -57,6 +59,15 @@ func (si *signingRoundTripper) RoundTrip(req *http.Request) (*http.Response, err

// Clone request to ensure thread safety.
req2 := cloneRequest(req)

// Add the sdk and system information to the User-Agent header of the request
extras := []string{runtime.Version(), runtime.GOOS, runtime.GOARCH}
Aneurysm9 marked this conversation as resolved.
Show resolved Hide resolved
if v := os.Getenv("AWS_EXECUTION_ENV"); v != "" {
extras = append(extras, v)
}
addToUserAgent(req2, aws.SDKName, aws.SDKVersion, extras...)

// Sign the request
_, err = si.signer.Sign(req2, body, si.service, si.region, time.Now())
if err != nil {
return nil, err
Expand Down Expand Up @@ -147,3 +158,20 @@ func cloneRequest(r *http.Request) *http.Request {
}
return r2
}

// addToUserAgent will format the provided name and version to the name/version format.
// If the extra parameters are provided they will be added as metadata to the
// name/version pair resulting in the following format.
// "name/version (extra0; extra1; ...)"
// It then adds the string to the end of the request's current User-Agent.
func addToUserAgent(req *http.Request, name, version string, extra ...string) {
ua := fmt.Sprintf("%s/%s", name, version)
Aneurysm9 marked this conversation as resolved.
Show resolved Hide resolved
if len(extra) > 0 {
ua = fmt.Sprintf("%s (%s)", ua, strings.Join(extra, "; "))
}
curUA := req.Header.Get("User-Agent")
if len(curUA) > 0 {
ua = curUA + " " + ua
}
req.Header.Set("User-Agent", ua)
}