Skip to content

Commit

Permalink
add redis as alternative metrics store (#1653)
Browse files Browse the repository at this point in the history
* add redis as alternative metrics store

Signed-off-by: Michael Kalantar <[email protected]>

* fix lint problems

Signed-off-by: Michael Kalantar <[email protected]>

* fix lint problems

Signed-off-by: Michael Kalantar <[email protected]>

* test coverage

Signed-off-by: Michael Kalantar <[email protected]>

* refactor

Signed-off-by: Michael Kalantar <[email protected]>

* add package comment

Signed-off-by: Michael Kalantar <[email protected]>

* refactor client config

Signed-off-by: Michael Kalantar <[email protected]>

* update defaults

Signed-off-by: Michael Kalantar <[email protected]>

* add comments

Signed-off-by: Michael Kalantar <[email protected]>

* add comments

Signed-off-by: Michael Kalantar <[email protected]>

* refactor names

Signed-off-by: Michael Kalantar <[email protected]>

* comment fixes

Signed-off-by: Michael Kalantar <[email protected]>

* rename files

Signed-off-by: Michael Kalantar <[email protected]>

---------

Signed-off-by: Michael Kalantar <[email protected]>
  • Loading branch information
kalantar authored Nov 14, 2023
1 parent c0ca303 commit d6b68de
Show file tree
Hide file tree
Showing 21 changed files with 877 additions and 221 deletions.
15 changes: 2 additions & 13 deletions abn/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,24 @@ import (
"context"
"fmt"
"net"
"os"

"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/dgraph-io/badger/v4"
pb "github.com/iter8-tools/iter8/abn/grpc"
util "github.com/iter8-tools/iter8/base"
"github.com/iter8-tools/iter8/base/log"
"github.com/iter8-tools/iter8/storage"
"github.com/iter8-tools/iter8/storage/badgerdb"
storageclient "github.com/iter8-tools/iter8/storage/client"

// auth package is necessary to enable authentication with various cloud providers
_ "k8s.io/client-go/plugin/pkg/client/auth"
)

const (
// MetricsDirEnv is the environment variable identifying the directory with metrics storage
MetricsDirEnv = "METRICS_DIR"

configEnv = "ABN_CONFIG_FILE"
defaultPortNumber = 50051
)

var (
// MetricsClient is the metrics client
MetricsClient storage.Interface
)

// newServer returns a new gRPC server
func newServer() *abnServer {
return &abnServer{}
Expand Down Expand Up @@ -118,7 +107,7 @@ func LaunchGRPCServer(opts []grpc.ServerOption, stopCh <-chan struct{}) error {
pb.RegisterABNServer(grpcServer, newServer())

// configure MetricsClient if needed
MetricsClient, err = badgerdb.GetClient(badger.DefaultOptions(os.Getenv(MetricsDirEnv)), badgerdb.AdditionalOptions{})
storageclient.MetricsClient, err = storageclient.GetClient()
if err != nil {
log.Logger.Error("Unable to configure metrics storage client ", err)
return err
Expand Down
9 changes: 5 additions & 4 deletions abn/service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
util "github.com/iter8-tools/iter8/base"
"github.com/iter8-tools/iter8/base/log"
"github.com/iter8-tools/iter8/controllers"
storageclient "github.com/iter8-tools/iter8/storage/client"
)

var allRoutemaps controllers.AllRouteMapsInterface = &controllers.DefaultRoutemaps{}
Expand Down Expand Up @@ -47,10 +48,10 @@ func lookupInternal(application string, user string) (controllers.RoutemapInterf
}

// record user; ignore error if any; this is best effort
if MetricsClient == nil {
if storageclient.MetricsClient == nil {
return nil, invalidVersion, fmt.Errorf("no metrics client")
}
_ = MetricsClient.SetUser(application, versionNumber, *s.GetVersions()[versionNumber].GetSignature(), user)
_ = storageclient.MetricsClient.SetUser(application, versionNumber, *s.GetVersions()[versionNumber].GetSignature(), user)

return s, versionNumber, nil
}
Expand Down Expand Up @@ -134,10 +135,10 @@ func writeMetricInternal(application, user, metric, valueStr string) error {
v := s.GetVersions()[versionNumber]
transaction := uuid.NewString()

if MetricsClient == nil {
if storageclient.MetricsClient == nil {
return fmt.Errorf("no metrics client")
}
err = MetricsClient.SetMetric(
err = storageclient.MetricsClient.SetMetric(
application, versionNumber, *v.GetSignature(),
metric, user, transaction,
value)
Expand Down
5 changes: 3 additions & 2 deletions abn/service_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/google/uuid"
util "github.com/iter8-tools/iter8/base"
"github.com/iter8-tools/iter8/storage/badgerdb"
storageclient "github.com/iter8-tools/iter8/storage/client"
"github.com/stretchr/testify/assert"
)

Expand All @@ -15,7 +16,7 @@ func TestLookupInternal(t *testing.T) {
var err error
// set up test metrics db for recording users
tempDirPath := t.TempDir()
MetricsClient, err = badgerdb.GetClient(badger.DefaultOptions(tempDirPath), badgerdb.AdditionalOptions{})
storageclient.MetricsClient, err = badgerdb.GetClient(badger.DefaultOptions(tempDirPath), badgerdb.AdditionalOptions{})
assert.NoError(t, err)

// setup: add desired routemaps to allRoutemaps
Expand Down Expand Up @@ -44,7 +45,7 @@ func TestWeights(t *testing.T) {

// set up test metrics db for recording users
tempDirPath := t.TempDir()
MetricsClient, err = badgerdb.GetClient(badger.DefaultOptions(tempDirPath), badgerdb.AdditionalOptions{})
storageclient.MetricsClient, err = badgerdb.GetClient(badger.DefaultOptions(tempDirPath), badgerdb.AdditionalOptions{})
assert.NoError(t, err)

// setup: add desired routemaps to allRoutemaps
Expand Down
36 changes: 28 additions & 8 deletions abn/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"math/rand"
"net"
"os"
"path/filepath"
"reflect"
"testing"
"time"

"github.com/alicebob/miniredis"
"github.com/dgraph-io/badger/v4"
pb "github.com/iter8-tools/iter8/abn/grpc"
util "github.com/iter8-tools/iter8/base"
"github.com/iter8-tools/iter8/storage/badgerdb"
storageclient "github.com/iter8-tools/iter8/storage/client"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
Expand Down Expand Up @@ -185,7 +186,7 @@ func setupGRPCService(t *testing.T) (*pb.ABNClient, func()) {
grpcServer := grpc.NewServer(serverOptions...)
pb.RegisterABNServer(grpcServer, newServer())
tempDirPath := t.TempDir()
MetricsClient, err = badgerdb.GetClient(badger.DefaultOptions(tempDirPath), badgerdb.AdditionalOptions{})
storageclient.MetricsClient, err = badgerdb.GetClient(badger.DefaultOptions(tempDirPath), badgerdb.AdditionalOptions{})
assert.NoError(t, err)
go func() {
_ = grpcServer.Serve(lis)
Expand Down Expand Up @@ -222,10 +223,10 @@ func getMetricsCount(t *testing.T, namespace string, name string, version int, m
}

// TODO: better error handling when there is no metrics client
if MetricsClient == nil {
if storageclient.MetricsClient == nil {
return 0
}
versionmetrics, err := MetricsClient.GetMetrics(namespace+"/"+name, version, *signature)
versionmetrics, err := storageclient.MetricsClient.GetMetrics(namespace+"/"+name, version, *signature)
if err != nil {
return 0
}
Expand All @@ -243,12 +244,31 @@ func TestLaunchGRPCServer(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

// define METRICS_DIR
err := os.Setenv(MetricsDirEnv, t.TempDir())
server, _ := miniredis.Run()
assert.NotNil(t, server)

abnConfig := `port: 50051`

metricsConfig := `port: 8080
implementation: redis
redis:
address: ` + server.Addr()

af, err := os.CreateTemp("", "abn*.yaml")
assert.NoError(t, err)
abnConfigFile := af.Name()
_, err = af.WriteString(abnConfig)
assert.NoError(t, err)

configFile := filepath.Clean(util.CompletePath("../testdata", "abninputs/config.yaml"))
err = os.Setenv("ABN_CONFIG_FILE", configFile)
mf, err := os.CreateTemp("", "metrics*.yaml")
assert.NoError(t, err)
metricsConfigFile := mf.Name()
_, err = mf.WriteString(metricsConfig)
assert.NoError(t, err)

err = os.Setenv("ABN_CONFIG_FILE", abnConfigFile)
assert.NoError(t, err)
err = os.Setenv("METRICS_CONFIG_FILE", metricsConfigFile)
assert.NoError(t, err)

err = LaunchGRPCServer([]grpc.ServerOption{}, ctx.Done())
Expand Down
2 changes: 1 addition & 1 deletion charts/controller/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apiVersion: v2
name: controller
version: 0.18.3
version: 0.18.4
description: Iter8 controller controller
type: application
keywords:
Expand Down
7 changes: 5 additions & 2 deletions charts/controller/templates/persistentvolumeclaim.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

{{- if or (not .Values.metrics) (not .Values.metrics.implementation) (eq "badgerdb" .Values.metrics.implementation) }}
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
Expand All @@ -8,5 +10,6 @@ spec:
- ReadWriteOnce
resources:
requests:
storage: {{ .Values.storage }}
storageClassName: {{ .Values.storageClassName }}
storage: {{ default "50Mi" .Values.metrics.badgerdb.storage }}
storageClassName: {{ default "standard" .Values.metrics.badgerdb.storageClassName }}
{{- end }}
8 changes: 6 additions & 2 deletions charts/controller/templates/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ spec:
- name: config
mountPath: "/config"
readOnly: true
{{- if or (not .Values.metrics) (not .Values.metrics.implementation) (eq "badgerdb" .Values.metrics.implementation) }}
- name: metrics
mountPath: "/metrics"
mountPath: {{ default "/metrics" .Values.metrics.badgerdb.dir }}
{{- end }}
resources:
{{ toYaml .Values.resources | indent 10 | trim }}
securityContext:
Expand All @@ -58,6 +60,8 @@ spec:
- name: config
configMap:
name: {{ .Release.Name }}
{{- if or (not .Values.metrics) (not .Values.metrics.implementation) (eq "badgerdb" .Values.metrics.implementation) }}
- name: metrics
persistentVolumeClaim:
claimName: {{ .Release.Name }}
claimName: {{ .Release.Name }}
{{- end }}
31 changes: 25 additions & 6 deletions charts/controller/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,32 @@ resources:
memory: "128Mi"
cpu: "500m"

### PersistentVolumeClaim parameters
storage: 50Mi
storageClassName: standard

### A/B/n service port
### A/B/n
abn:
# port for Iter8 gRPC service
port: 50051
### Metrics service port

### Metrics
metrics:
# port on which HTTP service (for Grafana) should be exposed
port: 8080
# implementation technology for metrics storage
# Valid values are badgerdb (default) and redis
# The set of properties used to configure the metrics store depend on the
# implementation selected.
implementation: badgerdb
# default properties specific to BadgerDB
badgerdb:
# storage that should be created to support badger db
storage: 50Mi
storageClassName: standard
# location to mount storage
dir: /metrics
# default properties specific to Redis
redis:
address: redis:6379
# password: (default - none)
# username: (default - none)
# db: (default 0)


6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
dario.cat/mergo v1.0.0
fortio.org/fortio v1.60.2
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/alicebob/miniredis v2.5.0+incompatible
github.com/antonmedv/expr v1.15.3
github.com/bojand/ghz v0.117.0
github.com/dgraph-io/badger/v4 v4.2.0
Expand All @@ -31,6 +32,7 @@ require (
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/montanaflynn/stats v0.7.1
github.com/pkg/errors v0.9.1
github.com/redis/go-redis/v9 v9.3.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -66,6 +68,7 @@ require (
github.com/Masterminds/squirrel v1.5.4 // indirect
github.com/Microsoft/hcsshim v0.11.0 // indirect
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b // indirect
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 // indirect
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bufbuild/protocompile v0.4.0 // indirect
Expand All @@ -80,6 +83,7 @@ require (
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/cli v23.0.3+incompatible // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v23.0.3+incompatible // indirect
Expand Down Expand Up @@ -111,6 +115,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/gomodule/redigo v1.8.2 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/flatbuffers v1.12.1 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
Expand Down Expand Up @@ -171,6 +176,7 @@ require (
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
github.com/yuin/gopher-lua v1.1.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 h1:uvdUDbHQHO85qeSydJtItA4T55Pw6BtAejd0APRJOCE=
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
github.com/alicebob/miniredis v2.5.0+incompatible h1:yBHoLpsyjupjz3NL3MhKMVkR41j82Yjf3KFv7ApYzUI=
github.com/alicebob/miniredis v2.5.0+incompatible/go.mod h1:8HZjEj4yU0dwhYHky+DxYx+6BMjkBbe5ONFIF1MXffk=
github.com/antonmedv/expr v1.15.3 h1:q3hOJZNvLvhqE8OHBs1cFRdbXFNKuA+bHmRaI+AmRmI=
github.com/antonmedv/expr v1.15.3/go.mod h1:0E/6TxnOlRNp81GMzX9QfDPAmHo2Phg00y4JUv1ihsE=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
Expand All @@ -73,6 +77,10 @@ github.com/bojand/ghz v0.117.0 h1:dTMxg+tUcLMw8BYi7vQPjXsrM2DJ20ns53hz1am1SbQ=
github.com/bojand/ghz v0.117.0/go.mod h1:MXspmKdJie7NAS0IHzqG9X5h6zO3tIRGQ6Tkt8sAwa4=
github.com/bshuster-repo/logrus-logstash-hook v1.0.0 h1:e+C0SB5R1pu//O4MQ3f9cFuPGoOVeF2fE4Og9otCc70=
github.com/bshuster-repo/logrus-logstash-hook v1.0.0/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd h1:rFt+Y/IK1aEZkEHchZRSq9OQbsSzIT/OrI8YFFmRIng=
Expand Down Expand Up @@ -123,6 +131,8 @@ github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWa
github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/distribution/distribution/v3 v3.0.0-20221208165359-362910506bc2 h1:aBfCb7iqHmDEIp6fBvC/hQUddQfg+3qdYjwzaiP9Hnc=
github.com/distribution/distribution/v3 v3.0.0-20221208165359-362910506bc2/go.mod h1:WHNsWjnIn2V1LYOrME7e8KxSeKunYHsxEm4am0BUtcI=
github.com/docker/cli v23.0.3+incompatible h1:Zcse1DuDqBdgI7OQDV8Go7b83xLgfhW1eza4HfEdxpY=
Expand Down Expand Up @@ -434,6 +444,8 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
github.com/prometheus/procfs v0.0.3/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ=
github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg=
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
github.com/redis/go-redis/v9 v9.3.0 h1:RiVDjmig62jIWp7Kk4XVLs0hzV6pI3PyTnnL0cnn0u0=
github.com/redis/go-redis/v9 v9.3.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
Expand Down Expand Up @@ -484,6 +496,8 @@ github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/gopher-lua v1.1.0 h1:BojcDhfyDWgU2f2TOzYK/g5p2gxMrku8oupLDqlnSqE=
github.com/yuin/gopher-lua v1.1.0/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43 h1:+lm10QQTNSBd8DVTNGHx7o/IKu9HYDvLMffDhbyLccI=
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs=
github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50 h1:hlE8//ciYMztlGpl/VA+Zm1AcTPHYkHJPbHqE6WJUXE=
Expand Down
Loading

0 comments on commit d6b68de

Please sign in to comment.