-
Notifications
You must be signed in to change notification settings - Fork 98
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
Automate scale test #1804
Closed
+758
−361
Closed
Automate scale test #1804
Changes from all commits
Commits
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,145 @@ | ||
package framework | ||
|
||
import ( | ||
"context" | ||
"encoding/csv" | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
monitoring "cloud.google.com/go/monitoring/apiv3/v2" | ||
"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb" | ||
|
||
"google.golang.org/api/iterator" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
const ( | ||
CpuMetricName = "kubernetes.io/container/cpu/core_usage_time" | ||
MemoryMetricName = "kubernetes.io/container/memory/used_bytes" | ||
PromReloads = "prometheus.googleapis.com/nginx_gateway_fabric_nginx_reloads_total/counter" | ||
) | ||
|
||
// NGFPrometheusMetrics are the relevant NGFPrometheusMetrics for scale tests | ||
type NGFPrometheusMetrics struct { | ||
ReloadCount string | ||
ReloadErrsCount string | ||
ReloadAvgTime string | ||
ReloadsUnder500ms string | ||
EventsCount string | ||
EventsErrsCount string | ||
EventsAvgTime string | ||
EventsUnder500ms string | ||
} | ||
|
||
// WriteGKEMetricsData gathers the requested timeseries metrics data and writes it to the given CSV file. | ||
func WriteGKEMetricsData(podName, metricType string, startTime, endTime int64, csvWriter *csv.Writer) error { | ||
// Create a context and specify the desired project ID. | ||
ctx := context.Background() | ||
// projectID := os.Getenv("GKE_PROJECT") | ||
// TODO: Remove hardcoded project ID | ||
projectID := "" | ||
|
||
// Create a MetricsClient for the Google Cloud Monitoring API. | ||
metricsClient, err := monitoring.NewMetricClient(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to create MetricsClient: %v", err) | ||
} | ||
defer metricsClient.Close() | ||
|
||
// Create a filter string to retrieve the required metric for the specific pod. | ||
metricValueName := strings.Split(metricType, "/")[3] | ||
valueAggregate := fmt.Sprintf("value_aggregate: aggregate(value.%s)", metricValueName) | ||
filter := fmt.Sprintf(`resource.type = "k8s_container" AND resource.label.pod_name = "%s" AND metric.type = "%s"`, podName, metricType) | ||
aggregation := &monitoringpb.Aggregation{ | ||
GroupByFields: []string{valueAggregate, "max(value_aggregate)"}, | ||
} | ||
|
||
fmt.Println("Filter: ", filter) | ||
// Prepare the request to retrieve time series data. | ||
req := &monitoringpb.ListTimeSeriesRequest{ | ||
Name: "projects/" + projectID, | ||
Filter: filter, | ||
Aggregation: aggregation, | ||
Interval: &monitoringpb.TimeInterval{ | ||
StartTime: ×tamppb.Timestamp{Seconds: startTime}, | ||
EndTime: ×tamppb.Timestamp{Seconds: endTime}, | ||
}, | ||
} | ||
|
||
fmt.Println("start-time", startTime, "end-time", endTime) | ||
|
||
dataFound, err := writeMetricsData(ctx, metricsClient, req, csvWriter) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if !dataFound { | ||
// no data yet, wait and try again with new timestamp | ||
time.Sleep(1 * time.Minute) | ||
req.Interval.EndTime = ×tamppb.Timestamp{Seconds: time.Now().Unix()} | ||
dataFound, err = writeMetricsData(ctx, metricsClient, req, csvWriter) | ||
if err != nil { | ||
return err | ||
} | ||
if !dataFound { | ||
fmt.Println("Waited for 1 minute but still no data for time period, exiting...") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func writeMetricsData(ctx context.Context, metricsClient *monitoring.MetricClient, req *monitoringpb.ListTimeSeriesRequest, csvWriter *csv.Writer) (bool, error) { | ||
var dataFound bool | ||
// Retrieve time series data. | ||
it := metricsClient.ListTimeSeries(ctx, req) | ||
|
||
for { | ||
ts, err := it.Next() | ||
if errors.Is(err, iterator.Done) { | ||
fmt.Println("no more entries in iterator") | ||
break | ||
} | ||
if err != nil { | ||
return false, fmt.Errorf("failed to iterate time series data: %v", err) | ||
} | ||
fmt.Println("Points in time series: ", len(ts.Points)) | ||
for _, point := range ts.Points { | ||
timestamp := point.Interval.EndTime.Seconds | ||
fmt.Println() | ||
data := fmt.Sprintf("%.2f", point.GetValue().GetDoubleValue()) | ||
if data != "0.00" { | ||
dataFound = true | ||
err = csvWriter.Write([]string{strconv.FormatInt(timestamp, 10), data}) | ||
fmt.Println("Timestamp: ", strconv.FormatInt(timestamp, 10), timestamp) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to write data to CSV file: %v", err) | ||
} | ||
} else { | ||
fmt.Println("No data yet, try again...") | ||
} | ||
} | ||
} | ||
return dataFound, nil | ||
} | ||
|
||
//func getPrometheusMetricsData() (error, NGFPrometheusMetrics) { | ||
// // Create a context and specify the desired project ID. | ||
// ctx := context.Background() | ||
// // projectID := os.Getenv("GKE_PROJECT") | ||
// // TODO: Remove hardcoded project ID | ||
// projectID := "f5-gcs-7899-ptg-ingrss-ctlr" | ||
// | ||
// var ngfPromMetrics NGFPrometheusMetrics | ||
// | ||
// // Create a MetricsClient for the Google Cloud Monitoring API. | ||
// metricsClient, err := monitoring.NewMetricClient(ctx) | ||
// if err != nil { | ||
// return fmt.Errorf("failed to create MetricsClient: %v", err), ngfPromMetrics | ||
// } | ||
// defer metricsClient.Close() | ||
// | ||
//} |
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.
Per a conversation offline, I bet it would be easier to get metrics if we just deploy our own Prometheus, and not use the GCP monitoring. Then we also don't need this extra dependency.