Skip to content

Commit

Permalink
allow redis key prefix to be specified in config
Browse files Browse the repository at this point in the history
  • Loading branch information
HubertFraczek authored and Hubert Fraczek committed Feb 4, 2025
1 parent 5d51fa0 commit a270e3f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 9 deletions.
4 changes: 3 additions & 1 deletion extension/storage/redisstorageextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ The extension requires read and write access to a Redis cluster.

## Config
- `endpoint` (required): The endpoint of the redis instance to connect to. Default: `localhost:6379`
- `password` (optional): the password to connect to the redis instance. Default: ``
- `password` (optional): The password to connect to the redis instance. Default: ``
- `db` (optional): Database to be selected after connecting to the server. Default: 0
- `expiration` (optional): TTL for all storage entries. Default TTL means the key has no expiration time. Default: 0
- `prefix` (optional): Prefix to be used for a redis key. Default:`<component_kind>_<component_type>_<component_name>_<storage_extension_name>` - e.g. `receiver_filelog_` if filelog receiver & redisstorage extension have no names

## Example

Expand All @@ -32,6 +33,7 @@ extensions:
password: ""
db: 0
expiration: 5m
prefix: test_
service:
extensions: [redis_storage, redis_storage/all_settings]
Expand Down
1 change: 1 addition & 0 deletions extension/storage/redisstorageextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ type Config struct {
Password configopaque.String `mapstructure:"password"`
DB int `mapstructure:"db"`
Expiration time.Duration `mapstructure:"expiration"`
Prefix string `mapstructure:"prefix"`
}
1 change: 1 addition & 0 deletions extension/storage/redisstorageextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestLoadConfig(t *testing.T) {
Password: "passwd",
DB: 1,
Expiration: 3 * time.Hour,
Prefix: "test_",
},
},
}
Expand Down
23 changes: 15 additions & 8 deletions extension/storage/redisstorageextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,27 @@ func (rc redisClient) Close(_ context.Context) error {

// GetClient returns a storage client for an individual component
func (rs *redisStorage) GetClient(_ context.Context, kind component.Kind, ent component.ID, name string) (storage.Client, error) {
var rawName string
if name == "" {
rawName = fmt.Sprintf("%s_%s_%s", kindString(kind), ent.Type(), ent.Name())
} else {
rawName = fmt.Sprintf("%s_%s_%s_%s", kindString(kind), ent.Type(), ent.Name(), name)
}

return redisClient{
client: rs.client,
prefix: rawName,
prefix: rs.getPrefix(ent, kindString(kind), name),
expiration: rs.cfg.Expiration,
}, nil
}

func (rs *redisStorage) getPrefix(ent component.ID, kind, name string) string {
prefix := rs.cfg.Prefix
if prefix != "" {
return prefix
}

if name == "" {
prefix = fmt.Sprintf("%s_%s_%s", kind, ent.Type(), ent.Name())
} else {
prefix = fmt.Sprintf("%s_%s_%s_%s", kind, ent.Type(), ent.Name(), name)
}
return prefix
}

func kindString(k component.Kind) string {
switch k {
case component.KindReceiver:
Expand Down
59 changes: 59 additions & 0 deletions extension/storage/redisstorageextension/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,65 @@ func TestTwoClientsWithDifferentNames(t *testing.T) {
require.Equal(t, myBytes2, data)
}

func TestGetPrefix(t *testing.T) {
t.Parallel()

tests := []struct {
prefix string
ent component.ID
kind string
name string
expected string
}{
{
prefix: "test_",
ent: newTestEntity("my_component"),
kind: "receiver",
name: "",
expected: "test_",
},
{
prefix: "",
ent: newTestEntity("my_component"),
kind: "receiver",
name: "",
expected: "receiver_nop_my_component",
},
{
prefix: "",
ent: newTestEntity("my_component"),
kind: "receiver",
name: "rdsExt",
expected: "receiver_nop_my_component_rdsExt",
},
{
prefix: "",
ent: newTestEntity(""),
kind: "receiver",
name: "rdsExt",
expected: "receiver_nop__rdsExt",
},
{
prefix: "",
ent: newTestEntity(""),
kind: "receiver",
name: "",
expected: "receiver_nop_",
},
}

for _, tt := range tests {
t.Run(tt.prefix, func(t *testing.T) {
cfg := &Config{
Prefix: tt.prefix,
}
rs := redisStorage{cfg: cfg}
got := rs.getPrefix(tt.ent, tt.kind, tt.name)
require.Equal(t, got, tt.expected)
})
}
}

func newTestExtension(t *testing.T) storage.Extension {
f := NewFactory()
cfg := f.CreateDefaultConfig().(*Config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ redis_storage/all_settings:
password: passwd
db: 1
expiration: 3h
prefix: test_

0 comments on commit a270e3f

Please sign in to comment.