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

Add logging to NGINX API http requests #605

Merged
merged 2 commits into from
Mar 22, 2024
Merged
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
17 changes: 14 additions & 3 deletions sdk/config_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
stubStatusAPIDirective = "stub_status"
apiFormat = "http://%s%s"
predefinedAccessLogFormat = "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\""
httpClientTimeout = 1 * time.Second
)

type DirectoryMap struct {
Expand Down Expand Up @@ -856,18 +857,21 @@ func nginxPlusApiCallback(parent *crossplane.Directive, current *crossplane.Dire
}

func pingStubStatusApiEndpoint(statusAPI string) bool {
client := http.Client{Timeout: 50 * time.Millisecond}
client := http.Client{Timeout: httpClientTimeout}
resp, err := client.Get(statusAPI)
if err != nil {
log.Warningf("Unable to perform Stub Status API GET request: %v", err)
return false
}

if resp.StatusCode != 200 {
log.Debugf("Stub Status API responded with a %d status code", resp.StatusCode)
oliveromahony marked this conversation as resolved.
Show resolved Hide resolved
return false
}

bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Warningf("Unable to read Stub Status API response body: %v", err)
return false
}

Expand All @@ -882,27 +886,34 @@ func pingStubStatusApiEndpoint(statusAPI string) bool {
}

func pingNginxPlusApiEndpoint(statusAPI string) bool {
client := http.Client{Timeout: 50 * time.Millisecond}
client := http.Client{Timeout: httpClientTimeout}
resp, err := client.Get(statusAPI)
if err != nil {
log.Warningf("Unable to perform NGINX Plus API GET request: %v", err)
return false
}

if resp.StatusCode != 200 {
log.Debugf("NGINX Plus API responded with a %d status code", resp.StatusCode)
return false
}

bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Warningf("Unable to read NGINX Plus API response body: %v", err)
return false
}

// Expecting API to return the api versions in an array of positive integers
// subset example: [ ... 6,7,8,9 ...]
var responseBody []int
err = json.Unmarshal(bodyBytes, &responseBody)
if err != nil {
log.Debugf("Unable to unmarshal NGINX Plus API response body: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a warning?

return false
}

return err == nil
return true
}

func getUrlsForLocationDirective(parent *crossplane.Directive, current *crossplane.Directive, locationDirectiveName string) []string {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading