-
Notifications
You must be signed in to change notification settings - Fork 816
Metrics for Dynamo capacity #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package chunk | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/common/log" | ||
) | ||
|
||
const ( | ||
readLabel = "read" | ||
writeLabel = "write" | ||
) | ||
|
||
type dynamoWatcher struct { | ||
accountMaxCapacity *prometheus.GaugeVec | ||
tableCapacity *prometheus.GaugeVec | ||
|
||
dynamoDB *dynamodb.DynamoDB | ||
tableName string | ||
|
||
updateInterval time.Duration | ||
quit chan struct{} | ||
wait sync.WaitGroup | ||
} | ||
|
||
// Watcher watches something and reports to Prometheus. | ||
type Watcher interface { | ||
Stop() | ||
prometheus.Collector | ||
} | ||
|
||
// WatchDynamo watches Dynamo and reports on resource limits. | ||
func WatchDynamo(dynamoDBURL string, interval time.Duration) (Watcher, error) { | ||
url, err := url.Parse(dynamoDBURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
dynamoDBConfig, err := awsConfigFromURL(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client := dynamodb.New(session.New(dynamoDBConfig)) | ||
|
||
tableName := strings.TrimPrefix(url.Path, "/") | ||
w := &dynamoWatcher{ | ||
accountMaxCapacity: prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Namespace: "cortex", | ||
Name: "dynamo_account_max_capacity_units", | ||
Help: "Account-wide DynamoDB capacity, measured in DynamoDB capacity units.", | ||
}, []string{"op"}), | ||
tableCapacity: prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Namespace: "cortex", | ||
Name: "dynamo_table_capacity_units", | ||
Help: "Per-table DynamoDB capacity, measured in DynamoDB capacity units.", | ||
}, []string{"op", "table"}), | ||
dynamoDB: client, | ||
tableName: tableName, | ||
updateInterval: interval, | ||
quit: make(chan struct{}), | ||
} | ||
go w.updateLoop() | ||
return w, nil | ||
} | ||
|
||
// Stop stops the dynamo watcher. | ||
func (w *dynamoWatcher) Stop() { | ||
close(w.quit) | ||
w.wait.Wait() | ||
} | ||
|
||
func (w *dynamoWatcher) updateLoop() { | ||
defer w.wait.Done() | ||
ticker := time.NewTicker(w.updateInterval) | ||
for { | ||
select { | ||
case <-ticker.C: | ||
log.Debugf("Updating limits from dynamo") | ||
err := w.updateAccountLimits() | ||
if err != nil { | ||
// TODO: Back off if err is throttling related. | ||
log.Warnf("Could not fetch account limits from dynamo: %v", err) | ||
} | ||
err = w.updateTableLimits() | ||
if err != nil { | ||
log.Warnf("Could not fetch table limits from dynamo: %v", err) | ||
} | ||
case <-w.quit: | ||
ticker.Stop() | ||
} | ||
} | ||
} | ||
|
||
func (w *dynamoWatcher) updateAccountLimits() error { | ||
limits, err := w.dynamoDB.DescribeLimits(&dynamodb.DescribeLimitsInput{}) | ||
if err != nil { | ||
return err | ||
} | ||
w.accountMaxCapacity.WithLabelValues(readLabel).Set(float64(*limits.AccountMaxReadCapacityUnits)) | ||
w.accountMaxCapacity.WithLabelValues(writeLabel).Set(float64(*limits.AccountMaxWriteCapacityUnits)) | ||
return nil | ||
} | ||
|
||
func (w *dynamoWatcher) updateTableLimits() error { | ||
output, err := w.dynamoDB.DescribeTable(&dynamodb.DescribeTableInput{ | ||
TableName: &w.tableName, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
throughput := output.Table.ProvisionedThroughput | ||
w.tableCapacity.WithLabelValues(readLabel, w.tableName).Set(float64(*throughput.ReadCapacityUnits)) | ||
w.tableCapacity.WithLabelValues(writeLabel, w.tableName).Set(float64(*throughput.WriteCapacityUnits)) | ||
return nil | ||
} | ||
|
||
// Describe implements prometheus.Collector. | ||
func (w *dynamoWatcher) Describe(ch chan<- *prometheus.Desc) { | ||
w.accountMaxCapacity.Describe(ch) | ||
w.tableCapacity.Describe(ch) | ||
} | ||
|
||
// Collect implements prometheus.Collector. | ||
func (w *dynamoWatcher) Collect(ch chan<- prometheus.Metric) { | ||
w.accountMaxCapacity.Collect(ch) | ||
w.tableCapacity.Collect(ch) | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use a global gauge for this; this style is very verbose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you & @juliusv please sort this out between yourselves? I don't have a strong preference either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I favor encapsulated metrics that don't poop all over your global package namespace (was annoying in the past when reusing a package, especially when they also register themselves globally, but then aren't even set properly or aren't always relevant for the user of a package), but I don't care enough to fight about it here, and reuse of Cortex packages is less likely. Do it any way you guys prefer, you'll have to live with it longer :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I strongly favour global metrics, mainly as its less code, and its harder to forget to register them! (140c822)
I seem to remember something about custom registries? Could each component have its own registry, and can you register and registry to another registry?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PS maybe lets not let this discussion hold up this PR