Skip to content
This repository was archived by the owner on Mar 26, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions glustercli/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/gluster/glusterd2/pkg/restclient"
"github.com/gluster/glusterd2/version"
)

var (
Expand All @@ -20,6 +21,8 @@ var (

func initRESTClient(hostname, user, secret, cacert string, insecure bool) {
client = restclient.New(hostname, user, secret, cacert, insecure)
client.ExtendAgent(
fmt.Sprintf("glustercli/%v", version.GlusterdVersion))
Copy link
Member

Choose a reason for hiding this comment

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

nit: We can have cli version instead of glusterd version. For now it is same, but it can change in future. @prashanthpai your thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

IMO, this should be okay. glustercli and glusterd are almost always built together. We can quickly identify if older/outdated clients connect to more recent glusterd2 by looking at the version if they follow same versioning. A separate version for CLI will makes more sense if it's a separate repo/project.

Copy link
Member

Choose a reason for hiding this comment

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

makes sense. we can update later if that happens.

}

func isConnectionRefusedErr(err error) bool {
Expand Down
4 changes: 4 additions & 0 deletions glustercli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ var RootCmd = &cobra.Command{
}

initRESTClient(flagEndpoints[0], flagUser, secret, flagCacert, flagInsecure)
if len(os.Args) > 1 {
args := []string{"glustercli"}
client.SetOriginArgs(append(args, os.Args[1:]...))
}
},
}

Expand Down
16 changes: 14 additions & 2 deletions glusterd2/middleware/request_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@ import (
// Apache Common Log Format (CLF)
func LogRequest(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
entry := log.WithField("reqid", gdctx.GetReqID(r.Context()).String())
f := log.Fields{
"subsys": "rest",
"reqid": gdctx.GetReqID(r.Context()).String(),
}
if origin := r.Header.Get("X-Gluster-Origin-Args"); origin != "" {
f["origin"] = origin
Copy link
Contributor

Choose a reason for hiding this comment

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

TRIVIAL: IMO origin seems a bit ambiguous, a better variable name can be chosen, something like orgnArgs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good.

}
entry := log.WithFields(f)
entry.WithFields(log.Fields{
"method": r.Method,
"url": r.URL,
"client": r.UserAgent(),
}).Info("HTTP Request")
delete(entry.Data, logging.SourceField)
handlers.LoggingHandler(entry.Writer(), next).ServeHTTP(w, r)
handlers.CombinedLoggingHandler(entry.Writer(), next).ServeHTTP(w, r)
})
}
44 changes: 43 additions & 1 deletion pkg/restclient/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/gluster/glusterd2/pkg/api"
"github.com/gluster/glusterd2/version"

"github.com/dgrijalva/jwt-go"
)
Expand All @@ -29,11 +30,18 @@ type Client struct {
password string
cacert string
insecure bool

// Add to the identifier to further specify the client
// using the api.
agent string
originArgs []string
}

// New creates new instance of Glusterd REST Client
func New(baseURL string, username string, password string, cacert string, insecure bool) *Client {
return &Client{baseURL, username, password, cacert, insecure}
return &Client{baseURL, username, password, cacert, insecure,
fmt.Sprintf("GlusterD2-rest-client/%v", version.GlusterdVersion),
[]string{}}
}

func parseHTTPError(jsonData []byte) string {
Expand Down Expand Up @@ -105,6 +113,7 @@ func (c *Client) do(method string, url string, data interface{}, expectStatusCod
if err != nil {
return err
}
c.setAgent(req)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Close = true
Expand All @@ -114,6 +123,8 @@ func (c *Client) do(method string, url string, data interface{}, expectStatusCod
req.Header.Set("Authorization", "bearer "+getAuthToken(c.username, c.password))
}

c.sendOriginArgs(req)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: this can be named to setOriginArgs

Copy link
Member

Choose a reason for hiding this comment

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

We have a c.SetOriginArgs which does a different job to sendOriginArgs. The suggested rename makes the code more confusing.

I'd suggest that it be renamed to addOriginArgs, but that would take away the consistent naming with the other "set" methods being called. Maybe also rename 'c.setAgenttoc.addAgent`?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps we keep SetOriginArgs and rename this to addOriginArgsToHeaders ?


tr := &http.Transport{
DisableCompression: true,
DisableKeepAlives: true,
Expand Down Expand Up @@ -163,3 +174,34 @@ func (c *Client) do(method string, url string, data interface{}, expectStatusCod
func (c *Client) Ping() error {
return c.get("/ping", nil, http.StatusOK, nil)
}

func (c *Client) setAgent(req *http.Request) {
req.Header.Set("User-Agent",
fmt.Sprintf("%v (Go-http-client/1.1)", c.agent))
}

// ExtendAgent adds the given string to the client's user agent
// by prefixing it to the existing agent information.
// This allows client programs to identify themselves more than
// just something using the rest client api.
func (c *Client) ExtendAgent(a string) {
Copy link
Member

Choose a reason for hiding this comment

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

please rename "a" to something meaningful.

// new additions to the agent identifier go to the
// beginning of the string. It is meant to read like:
// foo (based on) bar (based on) baz, etc...
c.agent = fmt.Sprintf("%v %v", a, c.agent)
}

func (c *Client) sendOriginArgs(req *http.Request) {
if len(c.originArgs) != 0 {
req.Header.Set("X-Gluster-Origin-Args",
Copy link
Member

Choose a reason for hiding this comment

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

This is nice. is this logged? I don't see this detail in output @prashanthpai posted #978 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be, but only if the client sends it. I'm not exactly sure exactly how @prashanthpai got his examples, but when I incorporate the other feedback for these changes I'll include examples of the logging as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

My bad. I think I did make glusterd2 as I usually do. I can see it after I recompiled the CLI.

INFO[2018-07-17 10:03:37.209081] HTTP Request                                  client="glustercli/v4.1.0-97.git987fac1 GlusterD2-rest-client/v4.1.0-97.git987fac1 (Go-http-client/1.1)" method=GET origin="1:\"glustercli peer list\"" reqid=817c7683-37dd-4b04-a589-16136deaf766 source="[request_logging.go:29:middleware.LogRequest.func1]" subsys=rest url=/v1/peers
INFO[2018-07-17 10:03:37.209832] 127.0.0.1 - - [17/Jul/2018:10:03:37 +0530] "GET /v1/peers HTTP/1.1" 401 71 "" "glustercli/v4.1.0-97.git987fac1 GlusterD2-rest-client/v4.1.0-97.git987fac1 (Go-http-client/1.1)"  origin="1:\"glustercli peer list\"" reqid=817c7683-37dd-4b04-a589-16136deaf766 subsys=rest

Copy link
Member

Choose a reason for hiding this comment

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

nice, thanks for the update

fmt.Sprintf("1:%#v", strings.Join(c.originArgs, " ")))
}
}

// SetOriginArgs provides a way for tools using this library to
// inform the server what arguments were provided to generate
// the api call(s). The contents of the array are meant only for
// human interpretation but will generally be command line args.
func (c *Client) SetOriginArgs(a []string) {
Copy link
Member

Choose a reason for hiding this comment

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

same here, please rename "a"

c.originArgs = a
}