Skip to content

Commit

Permalink
Merge pull request #39 from damsallem/main
Browse files Browse the repository at this point in the history
Clean old discovery metadata aerospike-namespaces
  • Loading branch information
damsallem authored Feb 26, 2024
2 parents 3441cf9 + 845a361 commit c72af41
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 99 deletions.
5 changes: 0 additions & 5 deletions configs/aerospike_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ client_config:
# Skip ssl verification
tls_skip_verify: false
### Probe discovery configuration ###
# The key to discover Aerospike's namespaces through service discovery
# Set it to "" to instead discover namespaces automatically via the info command
# DEPRECATED since 02/19/2024
namespace_meta_key: "aerospike-namespaces"
# The key prefix to discover Aerospike's namespaces through service discovery
# old "namespace_meta_key" has been DEPRECATED because of 512 bytes limitation of the value in consul.
namespace_meta_key_prefix: "aerospike-monitoring-"
### Probe configuration ###
monitoring_set: monitoring
Expand Down
1 change: 0 additions & 1 deletion pkg/aerospike/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ var (
PasswordEnv: "AEROSPIKE_PASSWORD",
TLSTag: "tls",
TLSHostnameMetaKey: "tls-hostname",
NamespaceMetaKey: "",
NamespaceMetaKeyPrefix: "aerospike-monitoring-",
MonitoringSet: "monitoring",
LatencyKeyPrefix: "monitoring_latency_",
Expand Down
25 changes: 3 additions & 22 deletions pkg/aerospike/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,45 +78,26 @@ func (conf *AerospikeProbeConfig) generateNamespacedEndpointsFromEntry(logger lo

func (conf AerospikeProbeConfig) getNamespacesFromEntry(logger log.Logger, entry discovery.ServiceEntry) map[string]struct{} {
namespaces := make(map[string]struct{})
fallback := false

// Correct way to get namespaces.
for metaKey, metaValue := range entry.Meta {
if !strings.HasPrefix(metaKey, conf.AerospikeEndpointConfig.NamespaceMetaKeyPrefix) {
continue
}
ready, err := strconv.ParseBool(metaValue)
// if the value of the NamespaceMetaKeyPrefix MetaData is not a boolean then fallback to the old method
if err != nil {
level.Error(logger).Log("msg", fmt.Sprintf("Fail to parse boolean value from MetaData %s. Fallbacking to deprecated method.", metaKey), "err", err)
fallback = true
break
continue
}
// if ready is at false, then iterate to the next MetaData and try to resolve other namespaces
if !ready {
continue
}
ns := metaKey[len(conf.AerospikeEndpointConfig.NamespaceMetaKeyPrefix):] // MetaKey is like : "aerospike-monitoring-closeststore"
// MetaKey is like : "aerospike-monitoring-foo"
ns := metaKey[len(conf.AerospikeEndpointConfig.NamespaceMetaKeyPrefix):]
if len(ns) > 0 {
namespaces[ns] = struct{}{}
}
}

// DEPRECATED way to get namespaces in case of fallback required or empty namespaces with the new method
if fallback || len(namespaces) == 0 {
nsString, ok := entry.Meta[conf.AerospikeEndpointConfig.NamespaceMetaKey]
if ok {
// Clear namespaces for any previously found entry from the old method
for k := range namespaces {
delete(namespaces, k)
}
nsFromDiscovery := strings.Split(nsString, ";")
for _, ns := range nsFromDiscovery {
namespaces[ns] = struct{}{}
}
}
}

return namespaces
}

Expand Down
81 changes: 21 additions & 60 deletions pkg/aerospike/discovery_test.go
Original file line number Diff line number Diff line change
@@ -1,79 +1,44 @@
package aerospike

import (
"errors"
"reflect"
"testing"
"time"

"github.com/criteo/blackbox-prober/pkg/discovery"
"github.com/criteo/blackbox-prober/pkg/topology"
"github.com/go-kit/log"
)

type testEndpoint struct {
topology.DummyEndpoint
deadline time.Time
CheckCallCount int
RefreshCallCount int
}

func (te *testEndpoint) Refresh() error {
te.RefreshCallCount += 1
if te.RefreshCallCount%2 == 0 {
return errors.New("fake err")
}
return nil
}

func TestGetNamespacesFromEntry(t *testing.T) {
entry_newValid := discovery.ServiceEntry{
entry_Valid := discovery.ServiceEntry{
Meta: map[string]string{
"aerospike-monitoring-test1": "true",
"aerospike-monitoring-test2": "true",
"aerospike-monitoring-test3": "false",
},
}
expected_newValid := map[string]struct{}{
"test1": struct{}{},
"test2": struct{}{},
}

entry_oldValid := discovery.ServiceEntry{
Meta: map[string]string{
"aerospike-namespaces": "test1;test2;test3",
},
}
expected_oldValid := map[string]struct{}{
"test1": struct{}{},
"test2": struct{}{},
"test3": struct{}{},
expected_Valid := map[string]struct{}{
"test1": {},
"test2": {},
}

entry_noFallback := discovery.ServiceEntry{
entry_OneInvalid := discovery.ServiceEntry{
Meta: map[string]string{
"aerospike-namespaces": "test3",
"aerospike-monitoring-test1": "true",
"aerospike-monitoring-test2": "false",
"aerospike-monitoring-test3": "true",
"aerospike-monitoring-test2": "foo",
"aerospike-monitoring-test3": "false",
},
}
expected_noFallback := map[string]struct{}{
"test1": struct{}{},
"test3": struct{}{},
expected_OneInvalid := map[string]struct{}{
"test1": {},
}

entry_fallback := discovery.ServiceEntry{
entry_Invalid := discovery.ServiceEntry{
Meta: map[string]string{
"aerospike-namespaces": "test3",
"aerospike-monitoring-test1": "true",
"aerospike-monitoring-test1": "bar",
"aerospike-monitoring-test2": "foo",
"aerospike-monitoring-test3": "false",
},
}
expected_fallback := map[string]struct{}{
"test3": struct{}{},
}
expected_Invalid := map[string]struct{}{}

entry_empty := discovery.ServiceEntry{
Meta: map[string]string{},
Expand All @@ -87,21 +52,17 @@ func TestGetNamespacesFromEntry(t *testing.T) {
NamespaceMetaKeyPrefix: "aerospike-monitoring-",
}

namespaces := config.getNamespacesFromEntry(log.NewNopLogger(), entry_newValid)
if !reflect.DeepEqual(namespaces, expected_newValid) {
t.Errorf("getNamespacesFromEntry didn't return expected value for entry 'entry_newValid'.")
}
namespaces = config.getNamespacesFromEntry(log.NewNopLogger(), entry_oldValid)
if !reflect.DeepEqual(namespaces, expected_oldValid) {
t.Errorf("getNamespacesFromEntry didn't return expected value for entry 'entry_oldValid'.")
namespaces := config.getNamespacesFromEntry(log.NewNopLogger(), entry_Valid)
if !reflect.DeepEqual(namespaces, expected_Valid) {
t.Errorf("getNamespacesFromEntry didn't return expected value for entry 'entry_Valid'.")
}
namespaces = config.getNamespacesFromEntry(log.NewNopLogger(), entry_noFallback)
if !reflect.DeepEqual(namespaces, expected_noFallback) {
t.Errorf("getNamespacesFromEntry didn't return expected value for entry 'entry_noFallback'.")
namespaces = config.getNamespacesFromEntry(log.NewNopLogger(), entry_OneInvalid)
if !reflect.DeepEqual(namespaces, expected_OneInvalid) {
t.Errorf("getNamespacesFromEntry didn't return expected value for entry 'entry_OneInvalid'.")
}
namespaces = config.getNamespacesFromEntry(log.NewNopLogger(), entry_fallback)
if !reflect.DeepEqual(namespaces, expected_fallback) {
t.Errorf("getNamespacesFromEntry didn't return expected value for entry 'entry_fallback'.")
namespaces = config.getNamespacesFromEntry(log.NewNopLogger(), entry_Invalid)
if !reflect.DeepEqual(namespaces, expected_Invalid) {
t.Errorf("getNamespacesFromEntry didn't return expected value for entry 'entry_Invalid'.")
}
namespaces = config.getNamespacesFromEntry(log.NewNopLogger(), entry_empty)
if !reflect.DeepEqual(namespaces, expected_empty) {
Expand Down
11 changes: 0 additions & 11 deletions probes/aerospike/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ For advertising "foo" and "bar" namespace
`aerospike-monitoring-foo: true`
`aerospike-monitoring-bar: true`

If any of these MetaData entries are not present of does not define a bool value
then it will fallback to the old method of namespace discovery:
`namespace_meta_key` which contains namespaces separated by `;`

Example:
`namespace_meta_key` per default is set to `aerospike-namespaces`.

For advertising "foo" and "bar" namespace

`aerospike-namespaces: foo;bar`

## Latency checks executed at cluster level

In the Aerospike probe, all latency checks are being run on cluster level. Normally
Expand Down

0 comments on commit c72af41

Please sign in to comment.