fix(ruler): Initialize multi remote clients config (#7151)

<!--  Thanks for sending a pull request!  Before submitting:

1. Read our CONTRIBUTING.md guide
2. Name your PR as `<Feature Area>: Describe your change`.
a. Do not end the title with punctuation. It will be added in the
changelog.
b. Start with an imperative verb. Example: Fix the latency between
System A and System B.
  c. Use sentence case, not title case.
d. Use a complete phrase or sentence. The PR title will appear in a
changelog, so help other people understand what your change will be.
3. Rebase your PR if it gets out of sync with main
-->

**What this PR does / why we need it**:
To be backward compatible with single client config.
```
ts=2022-09-14T08:24:04.549961234Z caller=memberlist_logger.go:74 level=debug msg="Initiating push/pull sync with:  10.132.18.167:7946"                                                                                                                     │
│ panic: assignment to entry in nil map                                                                                                                                                                                                                      │
│ goroutine 1 [running]:                                                                                                                                                                                                                                     │
│ github.com/grafana/loki/pkg/ruler.NewRuler({{{0xc00002a120}, {0x0, 0x0, 0x0}, {0x6400000, 0x6400000, {0x0, 0x0}, 0x0, 0x0, ...}, ...}, ...}, ...)                                                                                                          │
│     /src/enterprise-logs/vendor/github.com/grafana/loki/pkg/ruler/ruler.go:21 +0x1b7   
```
**Which issue(s) this PR fixes**:
Fixes #<issue number>

**Special notes for your reviewer**:

<!--
Note about CHANGELOG entries, if a change adds:
* an important feature
* fixes an issue present in a previous release, 
* causes a change in operation that would be useful for an operator of
Loki to know
then please add a CHANGELOG entry.

For documentation changes, build changes, simple fixes etc please skip
this step. We are attempting to curate a changelog of the most relevant
and important changes to be easier to ingest by end users of Loki.

Note about the upgrade guide, if this changes:
* default configuration values
* metric names or label names
* changes existing log lines such as the metrics.go query output line
* configuration parameters 
* anything to do with any API
* any other change that would require special attention or extra steps
to upgrade
Please document clearly what changed AND what needs to be done in the
upgrade guide.
-->
**Checklist**
- [ ] Documentation added
- [ ] Tests updated
- [ ] Is this an important fix or new feature? Add an entry in the
`CHANGELOG.md`.
- [ ] Changes that require user attention or interaction to upgrade are
documented in `docs/sources/upgrading/_index.md`
pull/7106/head
Kaviraj Kanagaraj 4 years ago committed by GitHub
parent e7acb5356b
commit 6f0690e7bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      pkg/ruler/config.go
  2. 19
      pkg/ruler/config_test.go
  3. 5
      pkg/ruler/ruler.go

@ -106,4 +106,8 @@ func (c *RemoteWriteConfig) Clone() (*RemoteWriteConfig, error) {
func (c *RemoteWriteConfig) RegisterFlags(f *flag.FlagSet) {
f.BoolVar(&c.Enabled, "ruler.remote-write.enabled", false, "Remote-write recording rule samples to Prometheus-compatible remote-write receiver.")
f.DurationVar(&c.ConfigRefreshPeriod, "ruler.remote-write.config-refresh-period", 10*time.Second, "Minimum period to wait between refreshing remote-write reconfigurations. This should be greater than or equivalent to -limits.per-user-override-period.")
if c.Clients == nil {
c.Clients = make(map[string]config.RemoteWriteConfig)
}
}

@ -0,0 +1,19 @@
package ruler
import (
"flag"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRemoteWriteConfig(t *testing.T) {
fs := flag.NewFlagSet("test", flag.ExitOnError)
r := RemoteWriteConfig{}
r.RegisterFlags(fs)
// assert new multi clients config is backward compatible if with single tenant.
// if not provided
assert.NotNil(t, r.Clients)
}

@ -4,6 +4,7 @@ import (
"github.com/go-kit/log"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/config"
"github.com/grafana/loki/pkg/logql"
ruler "github.com/grafana/loki/pkg/ruler/base"
@ -18,6 +19,10 @@ func NewRuler(cfg Config, engine *logql.Engine, reg prometheus.Registerer, logge
}
if len(cfg.RemoteWrite.Clients) == 0 && cfg.RemoteWrite.Client != nil {
if cfg.RemoteWrite.Clients == nil {
cfg.RemoteWrite.Clients = make(map[string]config.RemoteWriteConfig)
}
cfg.RemoteWrite.Clients["default"] = *cfg.RemoteWrite.Client
}

Loading…
Cancel
Save