Skip to content

Commit

Permalink
Fix issue where empty metrics could be returned
Browse files Browse the repository at this point in the history
The scraper was pre-initializing metrics, and would
return them regardless of whether or not any data points
were actually collected. This resulted in empty metrics
being returned.
  • Loading branch information
djaglowski committed Jan 31, 2022
1 parent 44f3142 commit bb43807
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
## 🧰 Bug fixes 🧰

- `resourcedetectionprocessor`: fix `meta` allow list excluding keys with nil values (#7424)

- `postgresqlreceiver`: Fix issue where empty metrics could be returned after failed connection (#7502)
## v0.43.0

## 💡 Enhancements 💡
Expand Down
40 changes: 20 additions & 20 deletions receiver/postgresqlreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ func addToIntMetric(metric pdata.NumberDataPointSlice, attributes pdata.Attribut

// scrape scrapes the metric stats, transforms them and attributes them into a metric slices.
func (p *postgreSQLScraper) scrape(ctx context.Context) (pdata.Metrics, error) {
databases := p.config.Databases
listClient, err := p.clientFactory.getClient(p.config, "")
if err != nil {
p.logger.Error("Failed to initialize connection to postgres", zap.Error(err))
return pdata.NewMetrics(), err
}
defer listClient.Close()

if len(databases) == 0 {
dbList, err := listClient.listDatabases(ctx)
if err != nil {
p.logger.Error("Failed to request list of databases from postgres", zap.Error(err))
return pdata.NewMetrics(), err
}
databases = dbList
}

// metric initialization
md := pdata.NewMetrics()
ilm := md.ResourceMetrics().AppendEmpty().InstrumentationLibraryMetrics().AppendEmpty()
Expand All @@ -95,26 +112,6 @@ func (p *postgreSQLScraper) scrape(ctx context.Context) (pdata.Metrics, error) {

var errors scrapererror.ScrapeErrors

databases := p.config.Databases
listClient, err := p.clientFactory.getClient(p.config, "")
if err != nil {
p.logger.Error("Failed to initialize connection to postgres", zap.Error(err))
errors.Add(err)
return md, errors.Combine()
}
defer listClient.Close()

if len(databases) == 0 {
dbList, err := listClient.listDatabases(ctx)
if err != nil {
p.logger.Error("Failed to request list of databases from postgres", zap.Error(err))
errors.Add(err)
return md, errors.Combine()
}

databases = dbList
}

p.collectCommitsAndRollbacks(ctx, now, listClient, databases, commits, rollbacks, errors)
p.collectDatabaseSize(ctx, now, listClient, databases, databaseSize, errors)
p.collectBackends(ctx, now, listClient, databases, backends, errors)
Expand All @@ -132,6 +129,9 @@ func (p *postgreSQLScraper) scrape(ctx context.Context) (pdata.Metrics, error) {
p.collectDatabaseTableMetrics(ctx, now, dbClient, databaseRows, operations, errors)
}

if md.DataPointCount() == 0 {
md = pdata.NewMetrics()
}
return md, errors.Combine()
}

Expand Down
13 changes: 13 additions & 0 deletions receiver/postgresqlreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@ import (

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/model/pdata"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest/golden"
)

func TestUnsuccessfulScrape(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
cfg.Endpoint = "fake:11111"

scraper := newPostgreSQLScraper(zap.NewNop(), cfg, &defaultClientFactory{})
actualMetrics, err := scraper.scrape(context.Background())
require.Error(t, err)

require.NoError(t, scrapertest.CompareMetrics(pdata.NewMetrics(), actualMetrics))
}

func TestScraper(t *testing.T) {
factory := new(mockClientFactory)
factory.initMocks([]string{"otel"})
Expand Down

0 comments on commit bb43807

Please sign in to comment.