Skip to content

Unable to create PostgresCollector when using collector.NewPostgresCollector #1171

@matthewnolf

Description

@matthewnolf

What did you do?
I want to embed the exporter within my (external) application, and so am making use of the collector package to create a new Postgres collector I can use.

Usage:

enabledCollectors := ["database"]
c, err := collector.NewPostgresCollector(logger, cfg.ExcludeDatabases, dsn, enabledCollectors)

if err != nil {
	return nil, fmt.Errorf("failed to create postgres_exporter collector for DSN %d: %w", i, err)
}

// do stuff with c

What did you expect to see?
The returned PostgresCollector is created successfully, and respects the given parameters when configuring, including the enabledCollectors. The PostgresCollector can then be used as a prometheus.Collector in my application.

What did you see instead? Under which circumstances?
The PostgresCollector fails to be created with the error:

disabled collector: database

Note this happens with any valid collector

All collectors are disabled when using the collector package, and it's therefore impossible to create a PostgresCollector that scrapes any metrics.

This looks to be caused by the fact that each collector is disabled (and ignore the default value for the collector) until the kingpin flags, which are registered within collector.registerCollector(), are parsed.

https://github.com/prometheus-community/postgres_exporter/blob/master/collector/collector.go#L83-L84

flag := kingpin.Flag(flagName, flagHelp).Default(defaultValue).Action(collectorFlagAction(name)).Bool()

collectorState[name] = flag

When using the collector package externally within another application, I am not parsing application (kingpin specifically) flags and so end up with each collector being disabled. This results in an error when trying to create a PostgresCollector if any collector is provided as an argument.

Environment
N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions