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
Remote Sampling - XRay (Part 1) (Fetching Sampling Rules) #1536
Changes from 1 commit
dcc01de
1dd54d9
03f6fb4
5919be5
ebd45b4
e1ff7d0
041d9b8
e12a4b1
e906d2a
86c04d1
047f5d0
21db8fa
5204d27
c9c1bca
c956d4b
12c6c74
e042a6f
1f6b745
ada0137
9c6a430
79d6d15
e21f9a6
2e0f29a
7aedf46
741a285
839cff9
ab3d7b0
17d24f3
cdc58fa
d5902d9
36fa11f
c4ed209
f423e01
5224286
9fe987d
70eb25f
8e1f48f
c20df37
fdbebb0
09475dc
eec96d0
43a2ae7
bd59636
d094d58
01a8995
7b1987e
bb2fb94
92740aa
86c5aec
3dea789
30e6c79
ee415a7
c923616
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -15,65 +15,52 @@ | |||||
package xray | ||||||
|
||||||
import ( | ||||||
"bytes" | ||||||
"context" | ||||||
"encoding/json" | ||||||
"fmt" | ||||||
"net/http" | ||||||
"net/url" | ||||||
) | ||||||
|
||||||
type xrayClient struct { | ||||||
// http client for sending unsigned proxied requests to the collector | ||||||
httpClient *http.Client | ||||||
|
||||||
endpoint string | ||||||
endpoint *url.URL | ||||||
} | ||||||
|
||||||
// newClient returns a http client with proxy endpoint | ||||||
// newClient returns an HTTP client with proxy endpoint | ||||||
func newClient(d string) *xrayClient { | ||||||
bhautikpip marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
endpoint := "http://" + d | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably check if the address already has a scheme. If that should be strictly forbidden, then there should be some validation closer to the user I guess. Note that while not the first version, eventually this sampler needs to be able to support more configuration, for example TLS or auth headers, as users don't necessarily run collectors as plaintext sidecars. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I think we strictly expect string like |
||||||
|
||||||
endpointURL, err := url.Parse(endpoint) | ||||||
if err != nil { | ||||||
globalLogger.Error(err, "unable to parse endpoint from string") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
} | ||||||
|
||||||
return &xrayClient{ | ||||||
httpClient: &http.Client{}, | ||||||
endpoint: endpoint, | ||||||
endpoint: endpointURL, | ||||||
} | ||||||
} | ||||||
|
||||||
// getSamplingRules calls the collector(aws proxy enabled) for sampling rules | ||||||
func (p *xrayClient) getSamplingRules(ctx context.Context) (*getSamplingRulesOutput, error) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit |
||||||
rulesInput := getSamplingRulesInput{} | ||||||
|
||||||
statisticsByte, _ := json.Marshal(rulesInput) | ||||||
body := bytes.NewReader(statisticsByte) | ||||||
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, p.endpoint+"/GetSamplingRules", body) | ||||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, p.endpoint.String()+"/GetSamplingRules", nil) | ||||||
if err != nil { | ||||||
globalLogger.Error(err, "failed to create http request") | ||||||
return nil, fmt.Errorf("xray client: failed to create http request: %w", err) | ||||||
} | ||||||
|
||||||
output, err := p.httpClient.Do(req) | ||||||
if err != nil { | ||||||
return nil, fmt.Errorf("xray client: unable to retrieve sampling settings: %w", err) | ||||||
} | ||||||
defer output.Body.Close() | ||||||
|
||||||
buf := new(bytes.Buffer) | ||||||
_, err = buf.ReadFrom(output.Body) | ||||||
if err != nil { | ||||||
return nil, fmt.Errorf("xray client: unable to read response body: %w", err) | ||||||
} | ||||||
|
||||||
// Unmarshalling json data to populate getSamplingTargetsOutput struct | ||||||
var samplingRulesOutput getSamplingRulesOutput | ||||||
err = json.Unmarshal(buf.Bytes(), &samplingRulesOutput) | ||||||
if err != nil { | ||||||
var samplingRulesOutput *getSamplingRulesOutput | ||||||
if err := json.NewDecoder(output.Body).Decode(&samplingRulesOutput); err != nil { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be passing a pointer to a pointer - I'm surprised this seems to actually work , but think you don't need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried removing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe json.Unmarshal() handles this correctly, but it would be nicer to see
i.e., drop the |
||||||
return nil, fmt.Errorf("xray client: unable to unmarshal the response body: %w", err) | ||||||
} | ||||||
|
||||||
err = output.Body.Close() | ||||||
if err != nil { | ||||||
globalLogger.Error(err, "failed to close http response body") | ||||||
} | ||||||
|
||||||
return &samplingRulesOutput, nil | ||||||
return samplingRulesOutput, nil | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.