-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from kaytu-io/gcp-metric-client
Initializing Metric Client
- Loading branch information
Showing
4 changed files
with
153 additions
and
10 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
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,89 @@ | ||
package gcp | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
monitoring "cloud.google.com/go/monitoring/apiv3/v2" | ||
"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb" | ||
"google.golang.org/protobuf/types/known/durationpb" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
type CloudMonitoring struct { | ||
client *monitoring.MetricClient | ||
GCP | ||
} | ||
|
||
func NewCloudMonitoring(scopes []string) *CloudMonitoring { | ||
return &CloudMonitoring{ | ||
GCP: NewGCP(scopes), | ||
} | ||
} | ||
|
||
func (c *CloudMonitoring) InitializeClient(ctx context.Context) error { | ||
|
||
c.GCP.GetCredentials(ctx) | ||
|
||
metricClient, err := monitoring.NewMetricClient(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.client = metricClient | ||
|
||
return nil | ||
} | ||
|
||
func (c *CloudMonitoring) CloseClient() error { | ||
err := c.client.Close() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
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), | ||
Filter: fmt.Sprintf( | ||
`metric.type="%s" AND resource.labels.instance_id="%s"`, | ||
metricName, | ||
instanceID, | ||
), | ||
Interval: &monitoringpb.TimeInterval{ | ||
EndTime: timestamppb.New(endTime), | ||
StartTime: timestamppb.New(startTime), | ||
}, | ||
Aggregation: &monitoringpb.Aggregation{ | ||
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, | ||
} | ||
|
||
return request | ||
} | ||
|
||
func (c *CloudMonitoring) GetMetric(request *monitoringpb.ListTimeSeriesRequest) *monitoringpb.TimeSeries { | ||
|
||
it := c.client.ListTimeSeries(context.Background(), request) | ||
|
||
resp, err := it.Next() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return resp | ||
|
||
} |
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,55 @@ | ||
package gcp | ||
|
||
import ( | ||
"context" | ||
"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", | ||
}, | ||
) | ||
err := metric.InitializeClient(context.Background()) | ||
if err != nil { | ||
t.Errorf("[%s]: %s", t.Name(), err.Error()) | ||
} | ||
|
||
// 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 : %.10f", point.GetValue().GetDoubleValue()) | ||
// } | ||
|
||
metric.CloseClient() | ||
} |