-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Add hostname RPS metric collector
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package collector | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
autoscalingv2 "k8s.io/api/autoscaling/v2beta2" | ||
) | ||
|
||
const ( | ||
HostnameMetricType = "hostname" | ||
HostnameRPSQuery = `scalar(sum(rate(%s{host=~"%s"}[1m])))` | ||
) | ||
|
||
type HostnameCollectorPlugin struct { | ||
metricName string | ||
promPlugin CollectorPlugin | ||
} | ||
|
||
type HostnameCollector struct { | ||
interval time.Duration | ||
promCollector Collector | ||
} | ||
|
||
func NewHostnameCollectorPlugin( | ||
promPlugin CollectorPlugin, | ||
metricName string, | ||
) (*HostnameCollectorPlugin, error) { | ||
if metricName == "" { | ||
return nil, fmt.Errorf("failed to initialize hostname collector plugin, metric name was not defined") | ||
} | ||
|
||
return &HostnameCollectorPlugin{ | ||
metricName: metricName, | ||
promPlugin: promPlugin, | ||
}, nil | ||
} | ||
|
||
func (p *HostnameCollectorPlugin) NewCollector( | ||
hpa *autoscalingv2.HorizontalPodAutoscaler, | ||
config *MetricConfig, | ||
interval time.Duration, | ||
) (Collector, error) { | ||
// 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 | ||
hostname := config.Config["hostname"] | ||
|
||
if hostname == "" { | ||
return nil, fmt.Errorf("hostname not specified, unable to create collector") | ||
} | ||
|
||
confCopy.Config = map[string]string{ | ||
"query": fmt.Sprintf(HostnameRPSQuery, p.metricName, hostname), | ||
} | ||
|
||
c, err := p.promPlugin.NewCollector(hpa, &confCopy, interval) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &HostnameCollector{ | ||
interval: interval, | ||
promCollector: c, | ||
}, nil | ||
} | ||
|
||
// GetMetrics gets hostname metrics from Prometheus | ||
func (c *HostnameCollector) 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)) | ||
} | ||
|
||
// TBD(Lucas): | ||
// The explanation bellow is only true if we want to implement object metrics. | ||
// I believe external metrics would suffice. | ||
// Apparently in case of k8s <v1.14 the value is not average for replica | ||
// In this case we need to calculate RPS per replica manually. Check skipper | ||
// collector. Anyway I need to check wether in the hostname metric I want | ||
// average RPS or total. I probably want average but still need to check... | ||
return v, nil | ||
} | ||
|
||
// Interval returns the interval at which the collector should run. | ||
func (c *HostnameCollector) Interval() time.Duration { | ||
return c.interval | ||
} |
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,3 @@ | ||
package collector | ||
|
||
// TODO(Lucas) |
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