From 6520f8f5f37de1329cbb42ab4d27e799cb2ccdc4 Mon Sep 17 00:00:00 2001 From: Jonathan Lange Date: Thu, 30 Jun 2016 09:35:21 +0100 Subject: [PATCH] Pass in memcache client --- app/multitenant/dynamo_collector.go | 34 +++++++---------------------- prog/app.go | 27 +++++++++++++++++++++-- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/app/multitenant/dynamo_collector.go b/app/multitenant/dynamo_collector.go index ad7cffdd8..82db7ef56 100644 --- a/app/multitenant/dynamo_collector.go +++ b/app/multitenant/dynamo_collector.go @@ -23,14 +23,12 @@ import ( ) const ( - hourField = "hour" - tsField = "ts" - reportField = "report" - reportCacheSize = (15 / 3) * 10 * 5 // (window size * report rate) * number of hosts per user * number of users - reportCacheExpiration = 15 * time.Second - memcacheExpiration = 15 // seconds - memcacheUpdateInterval = 1 * time.Minute - natsTimeout = 10 * time.Second + hourField = "hour" + tsField = "ts" + reportField = "report" + reportCacheSize = (15 / 3) * 10 * 5 // (window size * report rate) * number of hosts per user * number of users + reportCacheExpiration = 15 * time.Second + natsTimeout = 10 * time.Second ) var ( @@ -130,8 +128,8 @@ func NewDynamoDBCollector( userIDer UserIDer, dynamoDBConfig *aws.Config, tableName string, s3Store *S3Store, - natsHost, - memcachedHost string, memcachedTimeout time.Duration, memcachedService string, + natsHost string, + memcacheClient *MemcacheClient, ) (DynamoDBCollector, error) { var nc *nats.Conn if natsHost != "" { @@ -142,22 +140,6 @@ func NewDynamoDBCollector( } } - var memcacheClient *MemcacheClient - if memcachedHost != "" { - var err error - memcacheClient, err = NewMemcacheClient(memcachedHost, memcachedTimeout, memcachedService, memcacheUpdateInterval, memcacheExpiration) - if err != nil { - // TODO(jml): Ideally, we wouldn't abort here, we would instead - // log errors when we try to use the memcache & fail to do so, as - // aborting here introduces ordering dependencies into our - // deployment. - // - // Note: this error only happens when either the memcachedHost or - // any of the SRV records that it points to fail to resolve. - return nil, err - } - } - return &dynamoDBCollector{ db: dynamodb.New(session.New(dynamoDBConfig)), s3: s3Store, diff --git a/prog/app.go b/prog/app.go index 2e8cceb7d..e898eaccf 100644 --- a/prog/app.go +++ b/prog/app.go @@ -27,6 +27,11 @@ import ( "github.com/weaveworks/scope/probe/docker" ) +const ( + memcacheExpiration = 15 // seconds + memcacheUpdateInterval = 1 * time.Minute +) + var ( requestDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{ Namespace: "scope", @@ -100,11 +105,29 @@ func collectorFactory(userIDer multitenant.UserIDer, collectorURL, s3URL, natsHo return nil, err } bucketName := strings.TrimPrefix(s3.Path, "/") - s3Store := multitenant.NewS3Client(s3Config, bucketName) tableName := strings.TrimPrefix(parsed.Path, "/") + s3Store := multitenant.NewS3Client(s3Config, bucketName) + var memcacheClient *multitenant.MemcacheClient + if memcachedHostname != "" { + memcacheClient, err = multitenant.NewMemcacheClient( + memcachedHostname, memcachedTimeout, memcachedService, + memcacheUpdateInterval, memcacheExpiration, + ) + if err != nil { + // TODO(jml): Ideally, we wouldn't abort here, we would instead + // log errors when we try to use the memcache & fail to do so, as + // aborting here introduces ordering dependencies into our + // deployment. + // + // Note: this error only happens when either the memcachedHost + // or any of the SRV records that it points to fail to + // resolve. + return nil, err + } + } dynamoCollector, err := multitenant.NewDynamoDBCollector( userIDer, dynamoDBConfig, tableName, &s3Store, natsHostname, - memcachedHostname, memcachedTimeout, memcachedService, + memcacheClient, ) if err != nil { return nil, err