Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Allow config options in file as yaml mappings #1737

Merged
merged 2 commits into from
Nov 15, 2022
Merged

Conversation

alejandrodnm
Copy link
Contributor

@alejandrodnm alejandrodnm commented Nov 3, 2022

Replaces the ff package with a custom implementation that uses Viper to parse the config file and sets the values for the flag.Flags.

By design the ff package doesn't allow flag subsets or nested maps inside the config yaml and all config options must be explicitely defined as flag.Flag. This limits the ability to specify more dynamic config options like lists or maps of user defined rules (Ex: metric rollups).

Viper provides the flexibility we require for parsing the config file, and unmarshaling directly into more complex datastructures. To define how to unmarshal a subtree of the configuration into a datastructure via unmarshalRule:

withUnmarshalRules(
  []unmarshalRule{{"startup.dataset", &dataset.Config}}
)

Given the the config file contains the subtree:

startup:
  dataset:
    metrics:
      ...
    traces:
      ...

Viper can access a nested field by passing a . delimited path of keys. In this scenario, the subtree startup.dataset will be unmarshal into &dataset.Config by Viper using the github.com/mitchellh/mapstructure package.

This change also allows us to format our configuration files to use yaml mappings instead of long key names with ., thus maintaining backwards compatibility:

web.listen-address: localhost:9201
web.auth.password: my-password
web.auth.username: promscale

Can be rewritten as:

web:
  listen-address: localhost:9201
  auth:
    password: my-password
    username: promscale

Merge requirements

Please take into account the following non-code changes that you may need to make with your PR:

  • CHANGELOG entry for user-facing changes
  • Updated the relevant documentation

pkg/runner/client.go Outdated Show resolved Hide resolved
Comment on lines +41 to +54
ChunkInterval DayDuration `mapstructure:"default_chunk_interval" yaml:"default_chunk_interval"`
Compression *bool `mapstructure:"compress_data" yaml:"compress_data"` // Using pointer to check if the the value was set.
HALeaseRefresh DayDuration `mapstructure:"ha_lease_refresh" yaml:"ha_lease_refresh"`
HALeaseTimeout DayDuration `mapstructure:"ha_lease_timeout" yaml:"ha_lease_timeout"`
RetentionPeriod DayDuration `mapstructure:"default_retention_period" yaml:"default_retention_period"`
}

// Traces contains dataset configuration options for traces data.
type Traces struct {
RetentionPeriod DayDuration `yaml:"default_retention_period"`
RetentionPeriod DayDuration `mapstructure:"default_retention_period" yaml:"default_retention_period"`
Copy link
Contributor Author

@alejandrodnm alejandrodnm Nov 3, 2022

Choose a reason for hiding this comment

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

These can be exposed as command line flags, is there a reason why they're not?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

These can be exposed as command line flags, is there a reason why they're not?

I think we see dataset config as a place for those flags that are purely related to DB (hence dataset), which is why these are not kept as flags.

@alejandrodnm alejandrodnm force-pushed the adn/dataset-as-map branch 2 times, most recently from 803c9cc to 6829137 Compare November 3, 2022 15:22
@alejandrodnm alejandrodnm marked this pull request as ready for review November 3, 2022 15:40
@alejandrodnm alejandrodnm requested a review from a team as a code owner November 3, 2022 15:40
Copy link
Contributor

@cevian cevian left a comment

Choose a reason for hiding this comment

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

Overall LGTM. I want to ask @antekresic to review as well.

Copy link
Contributor

@antekresic antekresic left a comment

Choose a reason for hiding this comment

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

Looks good overall, some minor tweak suggestions and a test case to add.

pkg/dataset/duration.go Outdated Show resolved Hide resolved
pkg/runner/flags.go Show resolved Hide resolved
pkg/runner/client.go Outdated Show resolved Hide resolved
docs/dataset.md Show resolved Hide resolved
pkg/runner/config_parser.go Outdated Show resolved Hide resolved
pkg/runner/config_parser.go Outdated Show resolved Hide resolved
pkg/runner/config_parser.go Outdated Show resolved Hide resolved
pkg/runner/config_parser.go Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
pkg/runner/flags_test.go Show resolved Hide resolved
@alejandrodnm alejandrodnm force-pushed the adn/dataset-as-map branch 2 times, most recently from f2ebbaa to 57662ae Compare November 4, 2022 15:59
@alejandrodnm
Copy link
Contributor Author

cc @paulfantom this is the PR to allow a yaml mapping instead of a string for the dataset config

@alejandrodnm alejandrodnm force-pushed the adn/dataset-as-map branch 4 times, most recently from e040fc6 to b5dca9a Compare November 14, 2022 11:19
Replaces the `ff` package with a custom implementation that uses Viper
to parse the config file and sets the values for the `flag.Flag`s.

By design the ff package doesn't allow flag subsets or nested maps inside
the config yaml and all config options must be explicitely defined as
`flag.Flag`. This limits the ability to specify more dynamic config options
like lists or maps of user defined rules (Ex: metric rollups).

Viper provides the flexibility we require for parsing the config file, and
unmarshaling directly into more complex datastructures. To define how to
unmarshal a subtree of the configuration into a datastructure via
unmarshalRule. An unmarshalRule for the dataset config was added as:

```
withUnmarshalRules(
  []unmarshalRule{{"startup.dataset", &dataset.Config}}
)
```

Given a config file that contains the subtree:

```
startup:
  dataset:
    metrics:
      ...
    traces:
      ...
```

The subtree `startup.dataset` will be unmarshal into `&dataset.Config`
by Viper using the github.com/mitchellh/mapstructure package.

Viper can access a nested field by passing a `.` delimited path of keys.
This change also allows us to format our configuration files to use
yaml mappings instead of long key names with `.`, thus maintaining
backwards compatibility:

```
web.listen-address: localhost:9201
web.auth.password: my-password
web.auth.username: promscale
```

Can be rewritten as:

```
web:
  listen-address: localhost:9201
  auth:
    password: my-password
    username: promscale
```
pkg/runner/flags.go Show resolved Hide resolved
docs/configuration.md Show resolved Hide resolved
@alejandrodnm alejandrodnm merged commit 9df822a into master Nov 15, 2022
@alejandrodnm alejandrodnm deleted the adn/dataset-as-map branch November 15, 2022 13:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants