elastic / elastic-transport-go

Transport struct and utilities shared among Go Elastic client libraries
Apache License 2.0
13 stars 10 forks source link

fix: add globalheader to client#getNodesInfo() #24

Open AkisAya opened 2 months ago

AkisAya commented 2 months ago

i use go-elasticsearch to visit elasticsearch servers, and due to the restrictions of my company, i have to add a custom header to the request. go-elasticsearch config and underlying transport client config provides a global header field to do this thing, like the bode below

c, err := es8.NewTypedClient(es8.Config{
    Addresses:            url,
    Username:             name,
    Password:             pwd,
    DiscoverNodesOnStart: true,
    Header:               globalHeader,
})

it works when i just perform a search, because the underlying transport client set the global header before performing a request like this

func (c *Client) Perform(req *http.Request) (*http.Response, error) {
    var (
        res *http.Response
        err error
    )
        ...
    // Update request
    c.setReqUserAgent(req)
    c.setReqGlobalHeader(req)
        ...
}

c.setReqGlobalHeader(req) is used to set the global header.

but when it comes to discoverNodes(), the global header is not set, leading to discoverNodes failure in my case

func (c *Client) getNodesInfo() ([]nodeInfo, error) {
    var (
        out    []nodeInfo
        scheme = c.urls[0].Scheme
    )

    req, err := http.NewRequest("GET", "/_nodes/http", nil)
    ...

    c.setReqURL(conn.URL, req)
    c.setReqAuth(conn.URL, req)
    c.setReqUserAgent(req)

    res, err := c.transport.RoundTrip(req)
        ...
}

I think this is a mistake, and this pr add c.setReqGlobalHeader(req) to getNodesInfo() to set global header

AkisAya commented 2 months ago

Hi @Anaethelion , would you mind reviewing this PR whenever you are available?