Skip to content

Commit

Permalink
[Metricbeat]Log prometheus errors instead of parsing families (elasti…
Browse files Browse the repository at this point in the history
…c#15712)

* log prometheus errors instead of parsing

(cherry picked from commit edc17b3)
  • Loading branch information
Pablo Mercado authored and jsoriano committed Feb 11, 2020
1 parent eba5514 commit bb1662c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

*Affecting all Beats*

- TLS or Beats that accept connections over TLS and validate client certificates. {pull}14146[14146]
- Fix panics that could result from invalid TLS certificates. This can affect Beats that connect over TLS, or Beats that accept connections over TLS and validate client certificates. {pull}14146[14146]
- Fix panic in the Logstash output when trying to send events to closed connection. {pull}15568[15568]
- Fix missing output in dockerlogbeat {pull}15719[15719]
- Fix logging target settings being ignored when Beats are started via systemd or docker. {issue}12024[12024] {pull}15422[15442]
- Do not load dashboards where not available. {pull}15802[15802]
- Fix issue where TLS settings would be ignored when a forward proxy was in use. {pull}15516{15516}
- Update replicaset group to apps/v1 {pull}15854[15802]
- Fix issue where default go logger is not discarded when either * or stdout is selected. {issue}10251[10251] {pull}15708[15708]

*Auditbeat*

Expand All @@ -54,6 +63,11 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

*Metricbeat*

- Add dedot for tags in ec2 metricset and cloudwatch metricset. {issue}15843[15843] {pull}15844[15844]
- Use RFC3339 format for timestamps collected using the SQL module. {pull}15847[15847]
- Avoid parsing errors returned from prometheus endpoints. {pull}15712[15712]
- Change lookup_fields from metricset.host to service.address {pull}15883[15883]
- Add dedot for cloudwatch metric name. {issue}15916[15916] {pull}15917[15917]

*Packetbeat*

Expand Down
14 changes: 13 additions & 1 deletion metricbeat/helper/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ package prometheus
import (
"fmt"
"io"
"io/ioutil"
"net/http"

"github.com/pkg/errors"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/helper"
"github.com/elastic/beats/metricbeat/mb"
)
Expand All @@ -43,6 +45,7 @@ type Prometheus interface {

type prometheus struct {
httpfetcher
logger *logp.Logger
}

type httpfetcher interface {
Expand All @@ -52,10 +55,11 @@ type httpfetcher interface {
// NewPrometheusClient creates new prometheus helper
func NewPrometheusClient(base mb.BaseMetricSet) (Prometheus, error) {
http, err := helper.NewHTTP(base)

if err != nil {
return nil, err
}
return &prometheus{http}, nil
return &prometheus{http, base.Logger()}, nil
}

// GetFamilies requests metric families from prometheus endpoint and returns them
Expand All @@ -66,6 +70,14 @@ func (p *prometheus) GetFamilies() ([]*dto.MetricFamily, error) {
}
defer resp.Body.Close()

if resp.StatusCode > 399 {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err == nil {
p.logger.Debug("error received from prometheus endpoint: ", string(bodyBytes))
}
return nil, fmt.Errorf("unexpected status code %d from server", resp.StatusCode)
}

format := expfmt.ResponseFormat(resp.Header)
if format == "" {
return nil, fmt.Errorf("Invalid format for response of response")
Expand Down
10 changes: 6 additions & 4 deletions metricbeat/helper/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"
)

Expand Down Expand Up @@ -185,14 +186,15 @@ var _ = httpfetcher(&mockFetcher{})
// returns the mockFetcher.Response contents
func (m mockFetcher) FetchResponse() (*http.Response, error) {
return &http.Response{
Header: make(http.Header),
Body: ioutil.NopCloser(bytes.NewReader([]byte(m.response))),
StatusCode: 200,
Header: make(http.Header),
Body: ioutil.NopCloser(bytes.NewReader([]byte(m.response))),
}, nil
}

func TestPrometheus(t *testing.T) {

p := &prometheus{mockFetcher{response: promMetrics}}
p := &prometheus{mockFetcher{response: promMetrics}, logp.NewLogger("test")}

tests := []struct {
mapping *MetricsMapping
Expand Down Expand Up @@ -933,7 +935,7 @@ func TestPrometheusKeyLabels(t *testing.T) {

for _, tc := range testCases {
r := &mbtest.CapturingReporterV2{}
p := &prometheus{mockFetcher{response: tc.prometheusResponse}}
p := &prometheus{mockFetcher{response: tc.prometheusResponse}, logp.NewLogger("test")}
p.ReportProcessedMetrics(tc.mapping, r)
if !assert.Nil(t, r.GetErrors(),
"error reporting/processing metrics, at %q", tc.testName) {
Expand Down

0 comments on commit bb1662c

Please sign in to comment.