diff --git a/decoder.go b/decoder.go index 2f31cc2..ca32e18 100644 --- a/decoder.go +++ b/decoder.go @@ -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) diff --git a/exporter_overview.go b/exporter_overview.go index 28bcc60..08efd59 100644 --- a/exporter_overview.go +++ b/exporter_overview.go @@ -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 } diff --git a/rabbitClient.go b/rabbitClient.go index daf8f1c..920ea13 100644 --- a/rabbitClient.go +++ b/rabbitClient.go @@ -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 { @@ -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) @@ -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) { @@ -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 } @@ -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" }