-
Notifications
You must be signed in to change notification settings - Fork 113
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
Add hostname RPS metric collector #552
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7633ac5
Extract fake controller to use it in other tests
lucastt aa7b64e
Add hostname RPS metric collector
lucastt 65dd585
Add documentation regarding hostname-rps collector to readme
lucastt 02ec228
Add multi hostname per metric support
lucastt 153d754
Add support for traffic weight specification
lucastt 2f5d3f5
Change hostname collector type to requests-per-second
lucastt f28653d
Use regexp.Compile to avoid performance problems
lucastt b8532b7
Update HPA to autoscaling/v2 in new collectors
lucastt d6a33fe
Update documentation for multi hostnames per metric and weight param
lucastt cd98605
Delete unwanted comments
lucastt ff6d479
Compile regex at plugin level for time optimization
lucastt a276b64
Fix error string warnings
lucastt b89ca19
Rename collector to external RPS
lucastt d171e04
Update documentation with new metric name
lucastt 69df60e
Update flags with new metric collector name
lucastt 35e3fe8
Update README.md
mikkeloscar 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 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
This file contains 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,128 @@ | ||
package collector | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
autoscalingv2 "k8s.io/api/autoscaling/v2" | ||
) | ||
|
||
const ( | ||
ExternalRPSMetricType = "requests-per-second" | ||
ExternalRPSQuery = `scalar(sum(rate(%s{host=~"%s"}[1m])) * %.4f)` | ||
) | ||
|
||
type ExternalRPSCollectorPlugin struct { | ||
metricName string | ||
promPlugin CollectorPlugin | ||
pattern *regexp.Regexp | ||
} | ||
|
||
type ExternalRPSCollector struct { | ||
interval time.Duration | ||
promCollector Collector | ||
} | ||
|
||
func NewExternalRPSCollectorPlugin( | ||
promPlugin CollectorPlugin, | ||
metricName string, | ||
) (*ExternalRPSCollectorPlugin, error) { | ||
if metricName == "" { | ||
return nil, fmt.Errorf("failed to initialize hostname collector plugin, metric name was not defined") | ||
} | ||
|
||
p, err := regexp.Compile("^[a-zA-Z0-9.-]+$") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create regular expression to match hostname format") | ||
} | ||
|
||
return &ExternalRPSCollectorPlugin{ | ||
metricName: metricName, | ||
promPlugin: promPlugin, | ||
pattern: p, | ||
}, nil | ||
} | ||
|
||
// NewCollector initializes a new skipper collector from the specified HPA. | ||
func (p *ExternalRPSCollectorPlugin) NewCollector( | ||
hpa *autoscalingv2.HorizontalPodAutoscaler, | ||
config *MetricConfig, | ||
interval time.Duration, | ||
) (Collector, error) { | ||
if config == nil { | ||
return nil, fmt.Errorf("metric config not present, it is not possible to initialize the collector") | ||
} | ||
// Need to copy config and add a promQL query in order to get | ||
// RPS data from a specific hostname from prometheus. The idea | ||
// of the copy is to not modify the original config struct. | ||
confCopy := *config | ||
|
||
if _, ok := config.Config["hostnames"]; !ok { | ||
return nil, fmt.Errorf("Hostname is not specified, unable to create collector") | ||
} | ||
|
||
hostnames := strings.Split(config.Config["hostnames"], ",") | ||
if p.pattern == nil { | ||
return nil, fmt.Errorf("plugin did not specify hostname regex pattern, unable to create collector") | ||
} | ||
for _, h := range hostnames { | ||
if ok := p.pattern.MatchString(h); !ok { | ||
return nil, fmt.Errorf( | ||
"invalid hostname format, unable to create collector: %s", | ||
h, | ||
) | ||
} | ||
} | ||
|
||
weight := 1.0 | ||
if w, ok := config.Config["weight"]; ok { | ||
num, err := strconv.ParseFloat(w, 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not parse weight annotation, unable to create collector: %s", w) | ||
} | ||
weight = num / 100.0 | ||
} | ||
|
||
|
||
|
||
confCopy.Config = map[string]string{ | ||
"query": fmt.Sprintf( | ||
ExternalRPSQuery, | ||
p.metricName, | ||
strings.ReplaceAll(strings.Join(hostnames, "|"), ".", "_"), | ||
weight, | ||
), | ||
} | ||
|
||
c, err := p.promPlugin.NewCollector(hpa, &confCopy, interval) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ExternalRPSCollector{ | ||
interval: interval, | ||
promCollector: c, | ||
}, nil | ||
} | ||
|
||
// GetMetrics gets hostname metrics from Prometheus | ||
func (c *ExternalRPSCollector) GetMetrics() ([]CollectedMetric, error) { | ||
v, err := c.promCollector.GetMetrics() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(v) != 1 { | ||
return nil, fmt.Errorf("expected to only get one metric value, got %d", len(v)) | ||
} | ||
return v, nil | ||
} | ||
|
||
// Interval returns the interval at which the collector should run. | ||
func (c *ExternalRPSCollector) Interval() time.Duration { | ||
return c.interval | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
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.
Looks like this must be plural
hostnames