Skip to content

Commit

Permalink
tsplot: Add additional test coverage where useful. (#15)
Browse files Browse the repository at this point in the history
Test coverage added for:
- SetQueryFilter
- SetAlignmentPeriod
- Setting aggregation opts
  • Loading branch information
jharshman authored Aug 13, 2021
1 parent df4c8e6 commit b97b9fd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 14 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,25 @@ func main() {
... snip ...
now := time.Now()
mq := &tsplot.MetricQuery{
Project: "bitly-gcp-prod"
MetricDescriptor: "custom.googleapis.com/opencensus/fishnet/queuereader_fishnet/messages_total"
StartTime: now.Add(-time.Hour * 2) // start two hours ago
EndTime: now
}
// enable cross series reducer
query.SetReduce(true)
tsi, err := mq.PerformWithClient(client)
start := time.Now().Add(-1 * time.Hour)
end := time.Now()
mq := &tsplot.NewMetricQuery(
"bitly-gcp-prod", // GCP project
"custom.googleapis.com/opencensus/fishent/queuereader_fishnet/messages_total", // metric descriptor
&start, // start of time window
&end, // end of time window
)
// disable cross series reducer (MEAN reduction is default)
query.Set_REDUCE_NONE()
// set different alignment window. (Default is 1 minute)
query.SetAlignmentPeriod(time.Minute * 2)
tsi, err := mq.PerformWithClient(client) // client is provided by user
if err != nil {
fmt.Printf("error performing query: %v\n", err)
os.Exit(1)
}
}
```

Expand All @@ -53,6 +56,7 @@ func main() {
ts := tsplot.TimeSeries{}
// optionally iterate over returned time series
timeSeries, _ := tsi.Next()
ts[metric] = ts.GetPoints()
Expand Down
60 changes: 60 additions & 0 deletions tsplot/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package tsplot

import (
"testing"
"time"

monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
)

func TestMetricQuery_BuildRequest(t *testing.T) {
Expand Down Expand Up @@ -36,3 +39,60 @@ func TestMetricQuery_BuildRequest(t *testing.T) {
}
}
}

func TestMetricQuery_SetQueryFilter(t *testing.T) {
expectedFilter := "some advanced query"
st := time.Now().Add(-1 * time.Hour)
et := time.Now()
query := NewMetricQuery("bitly-gcp-prod", "", &st, &et)
query.SetQueryFilter(expectedFilter)

tsr, err := query.request()
if err != nil {
t.Error(err)
}

filter := tsr.GetFilter()
if filter != expectedFilter {
t.Fatalf("query filter not overriden. got: %s, expected: %s", filter, expectedFilter)
}
}

func TestMetricQuery_SetAlignmentPeriod(t *testing.T) {
expectedAlignmentPeriod := time.Minute * 10
st := time.Now().Add(-1 * time.Hour)
et := time.Now()
query := NewMetricQuery("bitly-gcp-prod", "some metric", &st, &et)
query.SetAlignmentPeriod(expectedAlignmentPeriod)

req, err := query.request()
if err != nil {
t.Error(err)
}

alignmentPeriod := req.GetAggregation().GetAlignmentPeriod().GetSeconds()
if req.GetAggregation().GetAlignmentPeriod().GetSeconds() != int64(expectedAlignmentPeriod.Seconds()) {
t.Fatalf("alignment period not overriden. got %d, expected: %s", alignmentPeriod, expectedAlignmentPeriod)
}
}

func TestMetricQuery_AggregationOptions(t *testing.T) {
st := time.Now().Add(-1 * time.Hour)
et := time.Now()
query := NewMetricQuery("bitly-gcp-prod", "some metric", &st, &et)
query.Set_ALIGN_NONE()
query.Set_REDUCE_NONE()

req, err := query.request()
if err != nil {
t.Error(err)
}

aggregation := req.GetAggregation()
if aggregation.GetPerSeriesAligner() != monitoringpb.Aggregation_ALIGN_NONE {
t.Fatal("aligner not overridden")
}
if aggregation.GetCrossSeriesReducer() != monitoringpb.Aggregation_REDUCE_NONE {
t.Fatal("reducer not overridden")
}
}

0 comments on commit b97b9fd

Please sign in to comment.