Skip to content

Commit

Permalink
accept bert and json content-type
Browse files Browse the repository at this point in the history
shovel plugin does not support bert encoding.
If only bert is accepted it fails with error 406.
Now if bert is enabled, bert is preferred but json is also accepted.

shovel support  kbudde#121
  • Loading branch information
kbudde committed Apr 26, 2019
1 parent c96d056 commit 64d4fee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ type RabbitReply interface {
}

// MakeReply instantiates the apropriate reply parser for a given
// reply and the current configuration.
func MakeReply(config rabbitExporterConfig, body []byte) (RabbitReply, error) {
if isCapEnabled(config, rabbitCapBert) {
// reply and the content-type header
func MakeReply(contentType string, body []byte) (RabbitReply, error) {
if contentType == "application/bert" {
return makeBERTReply(body)
}
return makeJSONReply(body)
Expand Down
4 changes: 2 additions & 2 deletions exporter_overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func (e exporterOverview) NodeInfo() NodeInfo {
}

func (e *exporterOverview) Collect(ctx context.Context, ch chan<- prometheus.Metric) error {
body, err := apiRequest(config, "overview")
body, contentType, err := apiRequest(config, "overview")
if err != nil {
return err
}

reply, err := MakeReply(config, body)
reply, err := MakeReply(contentType, body)
if err != nil {
return err
}
Expand Down
21 changes: 11 additions & 10 deletions rabbitClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func initClient() {

}

func apiRequest(config rabbitExporterConfig, endpoint string) ([]byte, error) {
func apiRequest(config rabbitExporterConfig, endpoint string) ([]byte, string, error) {
var args string
enabled, exists := config.RabbitCapabilities[rabbitCapNoSort]
if enabled && exists {
Expand All @@ -48,7 +48,7 @@ func apiRequest(config rabbitExporterConfig, endpoint string) ([]byte, error) {
req, err := http.NewRequest("GET", config.RabbitURL+"/api/"+endpoint+args, nil)
if err != nil {
log.WithFields(log.Fields{"error": err, "host": config.RabbitURL}).Error("Error while constructing rabbitHost request")
return nil, errors.New("Error while constructing rabbitHost request")
return nil, "", errors.New("Error while constructing rabbitHost request")
}

req.SetBasicAuth(config.RabbitUsername, config.RabbitPassword)
Expand All @@ -62,25 +62,26 @@ func apiRequest(config rabbitExporterConfig, endpoint string) ([]byte, error) {
status = resp.StatusCode
}
log.WithFields(log.Fields{"error": err, "host": config.RabbitURL, "statusCode": status}).Error("Error while retrieving data from rabbitHost")
return nil, errors.New("Error while retrieving data from rabbitHost")
return nil, "", errors.New("Error while retrieving data from rabbitHost")
}

body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
content := resp.Header.Get("Content-type")
if err != nil {
return nil, err
return nil, "", err
}
log.WithFields(log.Fields{"body": string(body), "endpoint": endpoint}).Debug("Metrics loaded")

return body, nil
return body, content, nil
}

func loadMetrics(config rabbitExporterConfig, endpoint string) (RabbitReply, error) {
body, err := apiRequest(config, endpoint)
body, content, err := apiRequest(config, endpoint)
if err != nil {
return nil, err
}
return MakeReply(config, body)
return MakeReply(content, body)
}

func getStatsInfo(config rabbitExporterConfig, apiEndpoint string, labels []string) ([]StatsInfo, error) {
Expand All @@ -99,12 +100,12 @@ func getStatsInfo(config rabbitExporterConfig, apiEndpoint string, labels []stri
func getMetricMap(config rabbitExporterConfig, apiEndpoint string) (MetricMap, error) {
var overview MetricMap

body, err := apiRequest(config, apiEndpoint)
body, content, err := apiRequest(config, apiEndpoint)
if err != nil {
return overview, err
}

reply, err := MakeReply(config, body)
reply, err := MakeReply(content, body)
if err != nil {
return overview, err
}
Expand All @@ -114,7 +115,7 @@ func getMetricMap(config rabbitExporterConfig, apiEndpoint string) (MetricMap, e

func acceptContentType(config rabbitExporterConfig) string {
if isCapEnabled(config, rabbitCapBert) {
return "application/bert"
return "application/bert, application/json;q=0.1"
}
return "application/json"
}

0 comments on commit 64d4fee

Please sign in to comment.