Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Metrics: Ensure Stratos Metrics metadata file is optional (#4801)
Browse files Browse the repository at this point in the history
* Fix link for metrics endpoints

* Ensure metrics works with plain Prometheus

* Remove test code

* Slight optimization in if
  • Loading branch information
nwmac committed Nov 26, 2020
1 parent a01768a commit 8a9f015
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/jetstream/plugins/metrics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,24 @@ func (m *MetricsSpecification) Connect(ec echo.Context, cnsiRecord interfaces.CN

var h = m.portalProxy.GetHttpClient(cnsiRecord.SkipSSLValidation)
res, err := h.Do(req)
// Error performing the request?
if err != nil {
log.Errorf("Error performing http request - response: %v, error: %v", res, err)
errMessage := ""
if res.StatusCode == http.StatusUnauthorized {
errMessage = ": Unauthorized"
}
return nil, false, interfaces.NewHTTPShadowError(
res.StatusCode,
fmt.Sprintf("Could not connect to the endpoint%s", errMessage),
"Could not connect to the endpoint: %s", err)
}

if err == nil && res.StatusCode == http.StatusNotFound {
defer res.Body.Close()

// If we got anything other than a 200, then we did not find the Stratos Metrics metadata file
if res.StatusCode != http.StatusOK {
log.Debug("Did not find Stratos Metrics metadata file")
log.Debug("Checking if this is a prometheus endpoint")
// This could be a bosh-prometheus endpoint, verify that this is a prometheus endpoint
statusEndpoint := fmt.Sprintf("%s/api/v1/status/config", cnsiRecord.APIEndpoint)
Expand All @@ -195,28 +211,24 @@ func (m *MetricsSpecification) Connect(ec echo.Context, cnsiRecord interfaces.CN
}
m.addAuth(req, auth)

// Get for /api/v1/status/config
response, err := h.Do(req)
if err != nil {
log.Errorf("Error fetching /api/v1/status/config - response: %v, error: %v", response, err)
return nil, false, interfaces.LogHTTPError(res, err)
}

defer response.Body.Close()
if err != nil || response.StatusCode != http.StatusOK {
log.Errorf("Error performing http request - response: %v, error: %v", response, err)
if response.StatusCode != http.StatusOK {
log.Errorf("Error fetching /api/v1/status/config - response: %v, error: %v", response, err)
return nil, false, interfaces.LogHTTPError(res, err)
}

tr.Metadata, _ = m.createMetadata(cnsiRecord.APIEndpoint, h, auth)
return tr, false, nil
} else if err != nil || res.StatusCode != http.StatusOK {
log.Errorf("Error performing http request - response: %v, error: %v", res, err)
errMessage := ""
if res.StatusCode == http.StatusUnauthorized {
errMessage = ": Unauthorized"
}
return nil, false, interfaces.NewHTTPShadowError(
res.StatusCode,
fmt.Sprintf("Could not connect to the endpoint%s", errMessage),
"Could not connect to the endpoint: %s", err)
}

defer res.Body.Close()
// We read the Stratos Metadata file ok
body, _ := ioutil.ReadAll(res.Body)
// Put the body in the token metadata
tr.Metadata = string(body)
Expand Down

0 comments on commit 8a9f015

Please sign in to comment.