Skip to content

Commit

Permalink
Copy cortex/pkg/ruler package dependency into Loki (#5089)
Browse files Browse the repository at this point in the history
* Fork cortex `ruler/rulestore/local`.

* Fork cortex ruler into Loki as package `ruler/base`.

* Use forked cortex ruler instead of vendored.

* Update `vendor/modules.txt` and Makefile instruction.

* Merge `pkg/ruler/base/rulestore` with `pkg/ruler/rulestore`.

* Move `pkg/ruler/base/rulespb` to `pkg/ruler/rulespb`.

* Rename `cortex_ruler` usage to `base_ruler`.

* Fix lint issues.

* Add forked ruler tests.

* Use own `pkg/storage/bucket` instead of Cortex one.

* Swap to `pkg/storage/chunk/{aws,gcp,etc}`.

* Update with correct prometheus import.

* Change error comparison.

* Code style changes
  • Loading branch information
DylanGuedes authored Jan 12, 2022
1 parent 52fa211 commit 3d7e9ce
Show file tree
Hide file tree
Showing 52 changed files with 4,699 additions and 268 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ else
protoc -I ./vendor:./$(@D) --gogoslick_out=plugins=grpc:./vendor ./$(patsubst %.pb.go,%.proto,$@); \
;; \
*) \
protoc -I .:./vendor/github.com/gogo/protobuf:./vendor/github.com/thanos-io/thanos/pkg:./vendor:./$(@D) --gogoslick_out=Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,plugins=grpc,paths=source_relative:./ ./$(patsubst %.pb.go,%.proto,$@); \
protoc -I .:./vendor/github.com/gogo/protobuf:./vendor/github.com/thanos-io/thanos/pkg:./vendor:./$(@D) --gogoslick_out=Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,plugins=grpc,paths=source_relative:./ ./$(patsubst %.pb.go,%.proto,$@); \
;; \
esac
endif
Expand Down
10 changes: 5 additions & 5 deletions pkg/loki/config_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

cortexcache "github.com/cortexproject/cortex/pkg/chunk/cache"
"github.com/cortexproject/cortex/pkg/ruler/rulestore/local"
"github.com/grafana/dskit/flagext"
"github.com/pkg/errors"

Expand All @@ -17,6 +16,7 @@ import (
"github.com/grafana/loki/pkg/util"
"github.com/grafana/loki/pkg/util/cfg"

"github.com/grafana/loki/pkg/ruler/rulestore/local"
loki_storage "github.com/grafana/loki/pkg/storage"
chunk_storage "github.com/grafana/loki/pkg/storage/chunk/storage"
loki_net "github.com/grafana/loki/pkg/util/net"
Expand Down Expand Up @@ -378,7 +378,7 @@ func applyStorageConfig(cfg, defaults *ConfigWrapper) error {

applyConfig = func(r *ConfigWrapper) {
r.Ruler.StoreConfig.Type = "azure"
r.Ruler.StoreConfig.Azure = r.Common.Storage.Azure.ToCortexAzureConfig()
r.Ruler.StoreConfig.Azure = r.Common.Storage.Azure
r.StorageConfig.AzureStorageConfig = r.Common.Storage.Azure
r.StorageConfig.Hedging = r.Common.Storage.Hedging
r.CompactorConfig.SharedStoreType = chunk_storage.StorageTypeAzure
Expand All @@ -405,7 +405,7 @@ func applyStorageConfig(cfg, defaults *ConfigWrapper) error {

applyConfig = func(r *ConfigWrapper) {
r.Ruler.StoreConfig.Type = "gcs"
r.Ruler.StoreConfig.GCS = r.Common.Storage.GCS.ToCortexGCSConfig()
r.Ruler.StoreConfig.GCS = r.Common.Storage.GCS
r.StorageConfig.GCSConfig = r.Common.Storage.GCS
r.CompactorConfig.SharedStoreType = chunk_storage.StorageTypeGCS
r.StorageConfig.Hedging = r.Common.Storage.Hedging
Expand All @@ -417,7 +417,7 @@ func applyStorageConfig(cfg, defaults *ConfigWrapper) error {

applyConfig = func(r *ConfigWrapper) {
r.Ruler.StoreConfig.Type = "s3"
r.Ruler.StoreConfig.S3 = r.Common.Storage.S3.ToCortexS3Config()
r.Ruler.StoreConfig.S3 = r.Common.Storage.S3
r.StorageConfig.AWSStorageConfig.S3Config = r.Common.Storage.S3
r.CompactorConfig.SharedStoreType = chunk_storage.StorageTypeS3
r.StorageConfig.Hedging = r.Common.Storage.Hedging
Expand All @@ -429,7 +429,7 @@ func applyStorageConfig(cfg, defaults *ConfigWrapper) error {

applyConfig = func(r *ConfigWrapper) {
r.Ruler.StoreConfig.Type = "swift"
r.Ruler.StoreConfig.Swift = r.Common.Storage.Swift.ToCortexSwiftConfig()
r.Ruler.StoreConfig.Swift = r.Common.Storage.Swift
r.StorageConfig.Swift = r.Common.Storage.Swift
r.CompactorConfig.SharedStoreType = chunk_storage.StorageTypeSwift
r.StorageConfig.Hedging = r.Common.Storage.Hedging
Expand Down
25 changes: 12 additions & 13 deletions pkg/loki/config_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import (
"testing"
"time"

"github.com/cortexproject/cortex/pkg/storage/bucket/swift"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

cortex_aws "github.com/cortexproject/cortex/pkg/chunk/aws"
cortex_azure "github.com/cortexproject/cortex/pkg/chunk/azure"
cortex_gcp "github.com/cortexproject/cortex/pkg/chunk/gcp"
cortex_swift "github.com/cortexproject/cortex/pkg/storage/bucket/swift"

"github.com/grafana/loki/pkg/distributor"
"github.com/grafana/loki/pkg/loki/common"
"github.com/grafana/loki/pkg/storage/chunk/aws"
"github.com/grafana/loki/pkg/storage/chunk/azure"
"github.com/grafana/loki/pkg/storage/chunk/gcp"
"github.com/grafana/loki/pkg/storage/chunk/storage"
"github.com/grafana/loki/pkg/util"
"github.com/grafana/loki/pkg/util/cfg"
Expand Down Expand Up @@ -255,9 +254,9 @@ memberlist:

assert.Equal(t, "s3", config.Ruler.StoreConfig.Type)

for _, actual := range []cortex_aws.S3Config{
for _, actual := range []aws.S3Config{
config.Ruler.StoreConfig.S3,
config.StorageConfig.AWSStorageConfig.S3Config.ToCortexS3Config(),
config.StorageConfig.AWSStorageConfig.S3Config,
} {
require.NotNil(t, actual.S3.URL)
assert.Equal(t, *expected, *actual.S3.URL)
Expand All @@ -272,7 +271,7 @@ memberlist:
assert.Equal(t, 5*time.Minute, actual.HTTPConfig.ResponseHeaderTimeout)
assert.Equal(t, false, actual.HTTPConfig.InsecureSkipVerify)

assert.Equal(t, cortex_aws.SignatureVersionV4, actual.SignatureVersion,
assert.Equal(t, aws.SignatureVersionV4, actual.SignatureVersion,
"signature version should equal default value")
assert.Equal(t, 90*time.Second, actual.HTTPConfig.IdleConnTimeout,
"idle connection timeout should equal default value")
Expand Down Expand Up @@ -303,9 +302,9 @@ memberlist:

assert.Equal(t, "gcs", config.Ruler.StoreConfig.Type)

for _, actual := range []cortex_gcp.GCSConfig{
for _, actual := range []gcp.GCSConfig{
config.Ruler.StoreConfig.GCS,
config.StorageConfig.GCSConfig.ToCortexGCSConfig(),
config.StorageConfig.GCSConfig,
} {
assert.Equal(t, "foobar", actual.BucketName)
assert.Equal(t, 27, actual.ChunkBufferSize)
Expand Down Expand Up @@ -344,9 +343,9 @@ memberlist:

assert.Equal(t, "azure", config.Ruler.StoreConfig.Type)

for _, actual := range []cortex_azure.BlobStorageConfig{
for _, actual := range []azure.BlobStorageConfig{
config.Ruler.StoreConfig.Azure,
config.StorageConfig.AzureStorageConfig.ToCortexAzureConfig(),
config.StorageConfig.AzureStorageConfig,
} {
assert.Equal(t, "AzureGlobal", actual.Environment,
"should equal default environment since unspecified in config")
Expand Down Expand Up @@ -401,7 +400,7 @@ memberlist:

assert.Equal(t, "swift", config.Ruler.StoreConfig.Type)

for _, actual := range []cortex_swift.Config{
for _, actual := range []swift.Config{
config.Ruler.StoreConfig.Swift.Config,
config.StorageConfig.Swift.Config,
} {
Expand Down
6 changes: 3 additions & 3 deletions pkg/loki/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
rt "runtime"

cortex_tripper "github.com/cortexproject/cortex/pkg/querier/queryrange"
cortex_ruler "github.com/cortexproject/cortex/pkg/ruler"
"github.com/cortexproject/cortex/pkg/util"
util_log "github.com/cortexproject/cortex/pkg/util/log"
"github.com/fatih/color"
Expand Down Expand Up @@ -39,6 +38,7 @@ import (
"github.com/grafana/loki/pkg/querier/queryrange"
"github.com/grafana/loki/pkg/querier/worker"
"github.com/grafana/loki/pkg/ruler"
base_ruler "github.com/grafana/loki/pkg/ruler/base"
"github.com/grafana/loki/pkg/ruler/rulestore"
"github.com/grafana/loki/pkg/runtime"
"github.com/grafana/loki/pkg/scheduler"
Expand Down Expand Up @@ -251,9 +251,9 @@ type Loki struct {
Store storage.Store
tableManager *chunk.TableManager
frontend Frontend
ruler *cortex_ruler.Ruler
ruler *base_ruler.Ruler
RulerStorage rulestore.RuleStore
rulerAPI *cortex_ruler.API
rulerAPI *base_ruler.API
stopper queryrange.Stopper
runtimeConfig *runtimeconfig.Manager
MemberlistKV *memberlist.KVInitService
Expand Down
8 changes: 4 additions & 4 deletions pkg/loki/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/NYTimes/gziphandler"
cortex_ruler "github.com/cortexproject/cortex/pkg/ruler"
util_log "github.com/cortexproject/cortex/pkg/util/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/kv/codec"
Expand All @@ -37,6 +36,7 @@ import (
"github.com/grafana/loki/pkg/querier"
"github.com/grafana/loki/pkg/querier/queryrange"
"github.com/grafana/loki/pkg/ruler"
base_ruler "github.com/grafana/loki/pkg/ruler/base"
"github.com/grafana/loki/pkg/runtime"
"github.com/grafana/loki/pkg/scheduler"
"github.com/grafana/loki/pkg/scheduler/schedulerpb"
Expand Down Expand Up @@ -601,7 +601,7 @@ func (t *Loki) initRulerStorage() (_ services.Service, err error) {
}
}

t.RulerStorage, err = cortex_ruler.NewLegacyRuleStore(t.Cfg.Ruler.StoreConfig, ruler.GroupLoader{}, util_log.Logger)
t.RulerStorage, err = base_ruler.NewLegacyRuleStore(t.Cfg.Ruler.StoreConfig, t.Cfg.StorageConfig.Hedging, ruler.GroupLoader{}, util_log.Logger)

return
}
Expand Down Expand Up @@ -634,13 +634,13 @@ func (t *Loki) initRuler() (_ services.Service, err error) {
return
}

t.rulerAPI = cortex_ruler.NewAPI(t.ruler, t.RulerStorage, util_log.Logger)
t.rulerAPI = base_ruler.NewAPI(t.ruler, t.RulerStorage, util_log.Logger)

// Expose HTTP endpoints.
if t.Cfg.Ruler.EnableAPI {

t.Server.HTTP.Path("/ruler/ring").Methods("GET", "POST").Handler(t.ruler)
cortex_ruler.RegisterRulerServer(t.Server.GRPC, t.ruler)
base_ruler.RegisterRulerServer(t.Server.GRPC, t.ruler)

// Prometheus Rule API Routes
t.Server.HTTP.Path("/prometheus/api/v1/rules").Methods("GET").Handler(t.HTTPAuthMiddleware.Wrap(http.HandlerFunc(t.rulerAPI.PrometheusRules)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ruler
package base

import (
"encoding/json"
Expand All @@ -10,6 +10,8 @@ import (
"strings"
"time"

"github.com/cortexproject/cortex/pkg/cortexpb"
util_log "github.com/cortexproject/cortex/pkg/util/log"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/gorilla/mux"
Expand All @@ -20,11 +22,9 @@ import (
"github.com/weaveworks/common/user"
"gopkg.in/yaml.v3"

"github.com/cortexproject/cortex/pkg/cortexpb"
"github.com/cortexproject/cortex/pkg/ruler/rulespb"
"github.com/cortexproject/cortex/pkg/ruler/rulestore"
"github.com/cortexproject/cortex/pkg/tenant"
util_log "github.com/cortexproject/cortex/pkg/util/log"
"github.com/grafana/loki/pkg/ruler/rulespb"
"github.com/grafana/loki/pkg/ruler/rulestore"
"github.com/grafana/loki/pkg/tenant"
)

// In order to reimplement the prometheus rules API, a large amount of code was copied over
Expand Down
Loading

0 comments on commit 3d7e9ce

Please sign in to comment.