Skip to content

Commit

Permalink
Allow using multiple memcached clients at the same time. (#2648) (#2698)
Browse files Browse the repository at this point in the history
* Allow using multiple memcached clients at the same time.

Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>

* Added unit test to verify that we can have multiple clients.

Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>

* Added CHANGELOG.md entry.

Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>

* Remove unused argument.

Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
# Conflicts:
#	CHANGELOG.md
  • Loading branch information
bwplotka committed Jun 1, 2020
1 parent df3db54 commit 1f09857
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
30 changes: 14 additions & 16 deletions pkg/cacheutil/memcached_client.go
Expand Up @@ -182,12 +182,14 @@ func NewMemcachedClientWithConfig(logger log.Logger, name string, config Memcach
client.Timeout = config.Timeout
client.MaxIdleConns = config.MaxIdleConnections

return newMemcachedClient(logger, name, client, selector, config, reg)
if reg != nil {
reg = prometheus.WrapRegistererWith(prometheus.Labels{"name": name}, reg)
}
return newMemcachedClient(logger, client, selector, config, reg)
}

func newMemcachedClient(
logger log.Logger,
name string,
client memcachedClientBackend,
selector *MemcachedJumpHashSelector,
config MemcachedClientConfig,
Expand All @@ -196,7 +198,7 @@ func newMemcachedClient(
dnsProvider := dns.NewProvider(
logger,
extprom.WrapRegistererWithPrefix("thanos_memcached_", reg),
dns.ResolverType(dns.GolangResolverType),
dns.GolangResolverType,
)

c := &memcachedClient{
Expand All @@ -214,34 +216,30 @@ func newMemcachedClient(
}

c.operations = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Name: "thanos_memcached_operations_total",
Help: "Total number of operations against memcached.",
ConstLabels: prometheus.Labels{"name": name},
Name: "thanos_memcached_operations_total",
Help: "Total number of operations against memcached.",
}, []string{"operation"})
c.operations.WithLabelValues(opGetMulti)
c.operations.WithLabelValues(opSet)

c.failures = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Name: "thanos_memcached_operation_failures_total",
Help: "Total number of operations against memcached that failed.",
ConstLabels: prometheus.Labels{"name": name},
Name: "thanos_memcached_operation_failures_total",
Help: "Total number of operations against memcached that failed.",
}, []string{"operation"})
c.failures.WithLabelValues(opGetMulti)
c.failures.WithLabelValues(opSet)

c.skipped = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Name: "thanos_memcached_operation_skipped_total",
Help: "Total number of operations against memcached that have been skipped.",
ConstLabels: prometheus.Labels{"name": name},
Name: "thanos_memcached_operation_skipped_total",
Help: "Total number of operations against memcached that have been skipped.",
}, []string{"operation", "reason"})
c.skipped.WithLabelValues(opGetMulti, reasonMaxItemSize)
c.skipped.WithLabelValues(opSet, reasonMaxItemSize)

c.duration = promauto.With(reg).NewHistogramVec(prometheus.HistogramOpts{
Name: "thanos_memcached_operation_duration_seconds",
Help: "Duration of operations against memcached.",
ConstLabels: prometheus.Labels{"name": name},
Buckets: []float64{0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 3, 6, 10},
Name: "thanos_memcached_operation_duration_seconds",
Help: "Duration of operations against memcached.",
Buckets: []float64{0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 3, 6, 10},
}, []string{"operation"})
c.duration.WithLabelValues(opGetMulti)
c.duration.WithLabelValues(opSet)
Expand Down
18 changes: 17 additions & 1 deletion pkg/cacheutil/memcached_client_test.go
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/fortytw2/leaktest"
"github.com/go-kit/kit/log"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/thanos-io/thanos/pkg/model"
"github.com/thanos-io/thanos/pkg/testutil"
Expand Down Expand Up @@ -378,7 +379,7 @@ func TestMemcachedClient_GetMulti(t *testing.T) {
func prepare(config MemcachedClientConfig, backendMock *memcachedClientBackendMock) (*memcachedClient, error) {
logger := log.NewNopLogger()
selector := &MemcachedJumpHashSelector{}
client, err := newMemcachedClient(logger, "test", backendMock, selector, config, nil)
client, err := newMemcachedClient(logger, backendMock, selector, config, nil)

return client, err
}
Expand Down Expand Up @@ -439,3 +440,18 @@ func (c *memcachedClientBackendMock) waitItems(expected int) error {

return errors.New("timeout expired while waiting for items in the memcached mock")
}

func TestMultipleClientsCanUseSameRegistry(t *testing.T) {
reg := prometheus.NewRegistry()

config := defaultMemcachedClientConfig
config.Addresses = []string{"127.0.0.1:11211"}

client1, err := NewMemcachedClientWithConfig(log.NewNopLogger(), "a", config, reg)
testutil.Ok(t, err)
defer client1.Stop()

client2, err := NewMemcachedClientWithConfig(log.NewNopLogger(), "b", config, reg)
testutil.Ok(t, err)
defer client2.Stop()
}

0 comments on commit 1f09857

Please sign in to comment.