Skip to content

Commit

Permalink
add exclude deleted flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb-Hurshman committed Aug 13, 2024
1 parent ba43c71 commit 9a176d8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ Exports [Snowflake](www.snowflake.com) warehouse, database, table, and replicati
The exporter may be configured through its command line flags:

```
-h, --help Show context-sensitive help (also try --help-long and --help-man).
-h, --help Show context-sensitive help (also try --help-long and --help-man).
--web.listen-address=:9975 ...
Addresses on which to expose metrics and web interface. Repeatable for multiple addresses.
Addresses on which to expose metrics and web interface. Repeatable for multiple addresses.
--web.telemetry-path="/metrics"
Path under which to expose metrics.
--account=ACCOUNT The account to collect metrics for.
--username=USERNAME The username for the user used when querying metrics.
--password=PASSWORD The password for the user used when querying metrics.
--private-key-path The path to the user's RSA private key file.
--private-key-password The password for the user's RSA private key.
--role="ACCOUNTADMIN" The role to use when querying metrics.
--warehouse=WAREHOUSE The warehouse to use when querying metrics.
--version Show application version.
--log.level=info Only log messages with the given severity or above. One of: [debug, info, warn, error]
--log.format=logfmt Output format of log messages. One of: [logfmt, json]
Path under which to expose metrics.
--account=ACCOUNT The account to collect metrics for.
--username=USERNAME The username for the user used when querying metrics.
--password=PASSWORD The password for the user used when querying metrics.
--private-key-path The path to the user's RSA private key file.
--private-key-password The password for the user's RSA private key.
--role="ACCOUNTADMIN" The role to use when querying metrics.
--warehouse=WAREHOUSE The warehouse to use when querying metrics.
--exclude-deleted-tables Exclude deleted tables when collecting table storage metrics.
--version Show application version.
--log.level=info Only log messages with the given severity or above. One of: [debug, info, warn, error]
--log.format=logfmt Output format of log messages. One of: [logfmt, json]
```

Example usage:
Expand Down
2 changes: 2 additions & 0 deletions cmd/snowflake-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
privateKeyPassword = kingpin.Flag("private-key-password", "The password for the user's RSA private key.").Envar("SNOWFLAKE_EXPORTER_PRIVATE_KEY_PASSWORD").String()
role = kingpin.Flag("role", "The role to use when querying metrics.").Default("ACCOUNTADMIN").Envar("SNOWFLAKE_EXPORTER_ROLE").String()
warehouse = kingpin.Flag("warehouse", "The warehouse to use when querying metrics.").Envar("SNOWFLAKE_EXPORTER_WAREHOUSE").Required().String()
excludeDeleted = kingpin.Flag("exclude-deleted-tables", "Exclude deleted objects when collecting metrics.").Default("false").Bool()
)

const (
Expand Down Expand Up @@ -76,6 +77,7 @@ func main() {
PrivateKeyPassword: *privateKeyPassword,
Role: *role,
Warehouse: *warehouse,
ExcludeDeleted: *excludeDeleted,
}

if err := c.Validate(); err != nil {
Expand Down
20 changes: 15 additions & 5 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,12 +631,22 @@ func (c *Collector) collectAutoClusteringMetrics(db *sql.DB, metrics chan<- prom
}

func (c *Collector) collectTableStorageMetrics(db *sql.DB, metrics chan<- prometheus.Metric) error {
level.Debug(c.logger).Log("msg", "Collecting table storage metrics.")
rows, err := db.Query(tableStorageMetricQuery)
level.Debug(c.logger).Log("msg", "Done querying table storage metrics.")
if err != nil {
return fmt.Errorf("failed to query metrics: %w", err)
var rows *sql.Rows
var err error
if c.config.ExcludeDeleted {
level.Debug(c.logger).Log("msg", "Collecting table storage metrics excluding deleted tables.")
rows, err = db.Query(tableStorageExcludeDeletedMetricQuery)
if err != nil {
return fmt.Errorf("failed to query metrics: %w", err)
}
} else {
level.Debug(c.logger).Log("msg", "Collecting table storage metrics.")
rows, err = db.Query(tableStorageMetricQuery)
if err != nil {
return fmt.Errorf("failed to query metrics: %w", err)
}
}
level.Debug(c.logger).Log("msg", "Done querying table storage metrics.")
defer rows.Close()

for rows.Next() {
Expand Down
1 change: 1 addition & 0 deletions collector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Config struct {
PrivateKeyPath string
PrivateKeyPassword string
PrivateKey *rsa.PrivateKey
ExcludeDeleted bool
}

var (
Expand Down
6 changes: 6 additions & 0 deletions collector/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ const (
WHERE TABLE_ENTERED_FAILSAFE IS NULL OR TABLE_ENTERED_FAILSAFE >= dateadd(day, -8, current_timestamp())
GROUP BY TABLE_NAME, ID, TABLE_CATALOG, TABLE_CATALOG_ID, TABLE_SCHEMA, TABLE_SCHEMA_ID;`

tableStorageExcludeDeletedMetricQuery = `SELECT TABLE_NAME, ID, TABLE_SCHEMA, TABLE_SCHEMA_ID, TABLE_CATALOG, TABLE_CATALOG_ID,
sum(ACTIVE_BYTES), sum(TIME_TRAVEL_BYTES), sum(FAILSAFE_BYTES), sum(RETAINED_FOR_CLONE_BYTES)
FROM ACCOUNT_USAGE.TABLE_STORAGE_METRICS
WHERE DELETED = FALSE
GROUP BY TABLE_NAME, ID, TABLE_CATALOG, TABLE_CATALOG_ID, TABLE_SCHEMA, TABLE_SCHEMA_ID;`

// https://docs.snowflake.com/en/sql-reference/account-usage/table_storage_metrics
deletedTablesMetricQuery = `SELECT COUNT(DISTINCT TABLE_NAME, ID, TABLE_SCHEMA, TABLE_SCHEMA_ID, TABLE_CATALOG, TABLE_CATALOG_ID) AS NUM_TABLES
FROM ACCOUNT_USAGE.TABLE_STORAGE_METRICS
Expand Down

0 comments on commit 9a176d8

Please sign in to comment.