Skip to content

feat: reorganize code to remove the main package#1238

Merged
sysadmind merged 2 commits intomasterfrom
cristian/rename-main-package
Jan 28, 2026
Merged

feat: reorganize code to remove the main package#1238
sysadmind merged 2 commits intomasterfrom
cristian/rename-main-package

Conversation

@cristiangreco
Copy link
Contributor

@cristiangreco cristiangreco commented Jan 16, 2026

Move all main code to exporter, for easier code embedding.

This is mostly based on previous work from @thampiotr at github.com/grafana/postgres_exporter

@cristiangreco cristiangreco force-pushed the cristian/rename-main-package branch from cb47e7b to 8d2bfd5 Compare January 16, 2026 12:05
// limitations under the License.

package main
package exporter
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All the code added here is just moved from main.go

@cristiangreco cristiangreco marked this pull request as ready for review January 16, 2026 14:06
cmd/main.go Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this file should be moved out of the postgres_exporter package. That name is significant to as it becomes the default binary name.

Config: &config.Config{},
}

configFile = kingpin.Flag("config.file", "Postgres exporter configuration file.").Default("postgres_exporter.yml").String()
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that the flags make sense to move out of the main package. They're specific to the CLI for this exporter. I think it would be weird for another project to want these CLI flags as-is and with no ability to turn that off. Think of something like Grafana Alloy which I believe does import this project - this would potentially pollute their own CLI. I think that the exporter package should just make public something that can take a config or the values or some other pattern that projects can import.

}

func RunPostgresExporter() {
kingpin.Version(version.Print(exporterName))
Copy link
Contributor

Choose a reason for hiding this comment

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

This would also pollute the importing projects' CLI

@cristiangreco cristiangreco force-pushed the cristian/rename-main-package branch 7 times, most recently from ef0ebda to 099607d Compare January 22, 2026 11:46
Copy link
Contributor

@SuperQ SuperQ left a comment

Choose a reason for hiding this comment

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

LGTM

Move all `main` code to `exporter`, for easier code embedding.

Signed-off-by: Cristian Greco <cristian@regolo.cc>
@cristiangreco cristiangreco force-pushed the cristian/rename-main-package branch from 099607d to d8dfe15 Compare January 23, 2026 08:32
Copy link
Contributor

@sysadmind sysadmind left a comment

Choose a reason for hiding this comment

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

Wow so many instances where we used a global var for the logger. I never realized that was the case.

}

// WithLogger configures logger.
func WithLogger(logger *slog.Logger) ExporterOpt {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is somewhat fragile. If I'm not mistaken, this has to be before any argument that uses a logger and if you forget, other functions would just use the noop logger. That means the logs just disappear even though you had an error that was probably fatal.

What do you think about instead changing NewExporter to require the logger? You can still pass in noop, but that's an explicit decision.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I've updated the NewExporter signature to include the logger in 6aab94f

@cristiangreco cristiangreco force-pushed the cristian/rename-main-package branch 2 times, most recently from f684475 to 94d8cf1 Compare January 27, 2026 10:27
Signed-off-by: Cristian Greco <cristian@regolo.cc>
@cristiangreco cristiangreco force-pushed the cristian/rename-main-package branch from 94d8cf1 to 6aab94f Compare January 27, 2026 10:32
Copy link
Contributor

@sysadmind sysadmind left a comment

Choose a reason for hiding this comment

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

LGTM

@sysadmind sysadmind merged commit 3c0720a into master Jan 28, 2026
29 checks passed
@sysadmind sysadmind deleted the cristian/rename-main-package branch January 28, 2026 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants