Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pg_stat_statements to MetricBeat Postgresql module #7060

Merged

Conversation

zinefer
Copy link
Contributor

@zinefer zinefer commented May 9, 2018

This pull request adds a new metricset, statements to the MetricBeat Postgres module.

This metricset will poll pg_stat_statements and provide per-query statistic information which is not obtainable with just the current activity and database metricsets.

Related Issue #7048

@elasticmachine
Copy link
Collaborator

Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually?

assert.Contains(t, event["user"].(common.MapStr), "id")

assert.Contains(t, event, "database")
db_oid := event["database"].(common.MapStr)["oid"].(int64)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use underscores in Go names; var db_oid should be dbOid

assert.Contains(t, event["user"].(common.MapStr), "id")

assert.Contains(t, event, "database")
db_oid := event["database"].(common.MapStr)["oid"].(int64)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use underscores in Go names; var db_oid should be dbOid

Copy link
Member

@ruflin ruflin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a changelog? Should we also add this to our system tests?

@@ -41,6 +39,8 @@ metricbeat.modules:
- uptime # System Uptime
#- core # Per CPU core usage
#- diskio # Disk IO
#- filesystem # File system usage for each mountpoint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably need to rebase on master to get rid of this diff.

"rtt": 115
},
"postgresql": {
"statements": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the metricset be called statement? Because each even only contains one statement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd agree in general with that, but if we keep it as statements then all metricsets follow the exact naming of pg stats tables (pg_stat_activity, pg_stat_database, pg_stat_bgwriter and pg_stat_statements).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I would prefer the the naming convention superseed service specific namings: https://www.elastic.co/guide/en/beats/devguide/current/event-conventions.html This provides a unified querying experience on the consumer side.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, go for statement then.

@ruflin ruflin requested a review from jsoriano May 11, 2018 09:06
Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It mostly LGTM, but I'd add it as beta, and disabled by default.

func init() {
mb.Registry.MustAddMetricSet("postgresql", "statements", New,
mb.WithHostParser(postgresql.ParseURL),
mb.DefaultMetricSet(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it generate events on each executed statement? Maybe this is too much data to be enabled by default.

// New create a new instance of the MetricSet
// Part of new is also setting up the configuration by processing additional
// configuration entries if needed.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this metricset as beta, for that print a warning here with something like:

cfgwarn.Beta("The statement metricset is beta")

And change release to beta in fields.yml.

@zinefer
Copy link
Contributor Author

zinefer commented May 14, 2018

Thanks for the review. I'll make the suggested changes shortly.

@zinefer zinefer force-pushed the feature/metricbeat-statements-metricset branch from 876250f to 45aceea Compare May 15, 2018 15:37
assert.Contains(t, event["user"].(common.MapStr), "id")

assert.Contains(t, event, "database")
db_oid := event["database"].(common.MapStr)["oid"].(int64)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use underscores in Go names; var db_oid should be dbOid

@zinefer
Copy link
Contributor Author

zinefer commented May 24, 2018

Where should I add a changelog? To the pull request decription?

Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the comments, but it seems that some code is missing after the rename of the metricset.

"metricset": {
"host": "postgresql:5432",
"module": "postgresql",
"name": "statements",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

statement

"rtt": 115
},
"postgresql": {
"statements": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

statement

// Fetch methods implements the data gathering and data conversion to the right
// format. It publishes the event which is then forwarded to the output. In case
// of an error set the Error field of mb.Event or simply call report.Error().
func (m *MetricSet) Fetch(report mb.ReporterV2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual code for fetching is missing, but it was here in the previous versions 🤔

@jsoriano
Copy link
Member

@zinefer changelog is in the CHANGELOG.md file in the repository, add a line there and include it in the pull request. You will also need to update your branch with master, there are merge conflicts.

find lost fetch code
add changelog message
catch rename stragglers
…beat-statements-metricset

# Conflicts:
#	metricbeat/include/fields.go
@zinefer
Copy link
Contributor Author

zinefer commented May 29, 2018

Thanks for the help everyone!

Hopefully I've added to the changelog correctly.

==== Added

*MetricBeat*
- Add postgresql statement metricset. {issue}7048[7048] {pull}7060[7060]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add it to the existing "Added" section for version HEAD please 🙂

@jsoriano
Copy link
Member

jenkins, test this please

@jsoriano
Copy link
Member

@zinefer there seems to be a related test failing now 🤔 https://travis-ci.org/elastic/beats/jobs/386280791#L3561

@zinefer
Copy link
Contributor Author

zinefer commented May 31, 2018

Yeah, I'm on it. :)

I need to enable pg_stat_statements in the docker container.

Edit: Having an issue with my environment... docker-compose fails on my machine.

@jsoriano
Copy link
Member

jsoriano commented Jun 1, 2018

Oh, tests pass now but there are conflicts in the fields file, could you please update your branch and make update again?

…beat-statements-metricset

# Conflicts:
#	metricbeat/include/fields.go
@zinefer
Copy link
Contributor Author

zinefer commented Jun 1, 2018

No problem. :)

@jsoriano
Copy link
Member

jsoriano commented Jun 1, 2018

jenkins, test this please

@jsoriano
Copy link
Member

jsoriano commented Jun 1, 2018

It looks good, let's wait to jenkins :)

…beat-statements-metricset

# Conflicts:
#	metricbeat/include/fields.go
@jsoriano
Copy link
Member

jsoriano commented Jun 4, 2018

jenkins, test this again, please

@jsoriano jsoriano merged commit 7a80be8 into elastic:master Jun 4, 2018
@jsoriano
Copy link
Member

jsoriano commented Jun 4, 2018

@zinefer merged, thanks for your contribution!

@Matt-Yorkley
Copy link

Matt-Yorkley commented Jun 17, 2019

@zinefer are there additional steps needed to get this working? I'm running it now, and my logs show: [postgresql.log][FATAL] database "metricbeat" does not exist. There's no way to specify another database?

Edit: I restarted everything and it picked up my configs. The error message is gone, but the Postgres metrics are not displayed anywhere...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants