Skip to content

Commit

Permalink
finalized metrics for PR
Browse files Browse the repository at this point in the history
  • Loading branch information
ADorigi committed Jun 14, 2024
1 parent 62f5626 commit 852ab72
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ goreleaser: clean

# test components of plugin/gcp
testgcp:
go test -v ./plugin/gcp
go test -v ./plugin/gcp -count=1
20 changes: 13 additions & 7 deletions plugin/gcp/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ func (c *CloudMonitoring) CloseClient() error {
return nil
}

func (c *CloudMonitoring) NewMetricRequest(metricName string, instanceID string) *monitoringpb.ListTimeSeriesRequest {

endtime := time.Now()
starttime := endtime.Add(-24 * 1 * time.Hour) // 24 hours before current time
func (c *CloudMonitoring) NewInstanceMetricRequest(
metricName string, // fully qualified name of the metric
instanceID string, // compute instance ID
startTime time.Time, // start time of requested time series
endTime time.Time, // end time of requested time series
periodInSeconds int64, // period, for which the datapoints will be aggregated into one, in seconds
) *monitoringpb.ListTimeSeriesRequest {

request := &monitoringpb.ListTimeSeriesRequest{
Name: fmt.Sprintf("projects/%s", c.ProjectID),
Expand All @@ -57,11 +60,14 @@ func (c *CloudMonitoring) NewMetricRequest(metricName string, instanceID string)
instanceID,
),
Interval: &monitoringpb.TimeInterval{
EndTime: timestamppb.New(endtime),
StartTime: timestamppb.New(starttime),
EndTime: timestamppb.New(endTime),
StartTime: timestamppb.New(startTime),
},
Aggregation: &monitoringpb.Aggregation{
AlignmentPeriod: durationpb.New(time.Minute),
AlignmentPeriod: &durationpb.Duration{
Seconds: periodInSeconds,
},
PerSeriesAligner: monitoringpb.Aggregation_ALIGN_MEAN, // will represent all the datapoints in the above period, with a mean
},
View: monitoringpb.ListTimeSeriesRequest_FULL,
}
Expand Down
20 changes: 16 additions & 4 deletions plugin/gcp/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ import (
"log"
"os"
"testing"
"time"
)

// run this test as
//
// `TEST_INSTANCE_ID="<an-instance-id>" make testgcp`
func TestGetMetrics(t *testing.T) {

//test variables
id := os.Getenv("TEST_INSTANCE_ID")
endtime := time.Now()
starttime := endtime.Add(-24 * 1 * time.Hour) // 24 hours before current time

log.Printf("running %s", t.Name())

// creating and initializing client
metric := NewCloudMonitoring(
[]string{
"https://www.googleapis.com/auth/monitoring.read",
Expand All @@ -25,19 +31,25 @@ func TestGetMetrics(t *testing.T) {
t.Errorf("[%s]: %s", t.Name(), err.Error())
}

request := metric.NewMetricRequest(
// creating the metric request for the instance
request := metric.NewInstanceMetricRequest(
"compute.googleapis.com/instance/cpu/utilization",
id,
starttime,
endtime,
60,
)

// execute the request
resp := metric.GetMetric(request)

log.Printf("metrics: %s", resp.GetMetric().String())
log.Printf("resource: %s", resp.GetResource().String())
log.Printf("# of points: %d", len(resp.Points))

for _, point := range resp.Points {
log.Printf("Point : %s", point.String())
}
// for _, point := range resp.Points {
// log.Printf("Point : %.10f", point.GetValue().GetDoubleValue())
// }

metric.CloseClient()
}

0 comments on commit 852ab72

Please sign in to comment.