From 64d4feeb6fd552feef083b50c137b2f1547d7114 Mon Sep 17 00:00:00 2001 From: Kris Budde Date: Fri, 26 Apr 2019 20:32:22 +0200 Subject: [PATCH] accept bert and json content-type 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 #121 --- decoder.go | 6 +++--- exporter_overview.go | 4 ++-- rabbitClient.go | 21 +++++++++++---------- 3 files changed, 16 insertions(+), 15 deletions(-) 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" }