Skip to content

Commit ac05f03

Browse files
committed
Use monitoring v1 API after fix in library
1 parent 37b1a74 commit ac05f03

File tree

3 files changed

+10
-30
lines changed

3 files changed

+10
-30
lines changed

cmd/monitoring.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package cmd
22

33
import (
4-
"bytes"
54
"context"
65
"encoding/json"
76
"fmt"
8-
"io"
97
"log"
10-
"net/http"
118
"slices"
129
"strings"
1310
"time"
@@ -127,7 +124,7 @@ func processAlertPolicy(
127124
ctx context.Context,
128125
queryClient *monitoring.QueryClient,
129126
metricClient *monitoring.MetricClient,
130-
httpClient *http.Client,
127+
monitoring_v1Service *monitoring_v1.Service,
131128
alertPolicy *monitoringpb.AlertPolicy,
132129
start *timestamppb.Timestamp,
133130
end *timestamppb.Timestamp,
@@ -168,35 +165,23 @@ func processAlertPolicy(
168165
}
169166
if pql != nil {
170167
seconds := pql.GetEvaluationInterval().GetSeconds()
171-
// https://github.com/googleapis/google-api-go-client/issues/2304
172-
body, err := json.Marshal(&monitoring_v1.QueryRangeRequest{
168+
resp, err := monitoring_v1Service.Projects.Location.Prometheus.Api.V1.QueryRange(name, "global", &monitoring_v1.QueryRangeRequest{
173169
Query: pql.GetQuery(),
174170
Start: start.AsTime().Format(time.RFC3339),
175171
End: end.AsTime().Format(time.RFC3339),
176172
Step: fmt.Sprintf("%ds", seconds),
177-
})
178-
if err != nil {
179-
policyOut.Error = err.Error()
180-
continue
181-
}
182-
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("https://monitoring.googleapis.com/v1/projects/%s/location/global/prometheus/api/v1/query_range", projectId), bytes.NewReader(body))
183-
if err != nil {
184-
policyOut.Error = err.Error()
185-
continue
186-
}
187-
resp, err := httpClient.Do(req)
173+
}).Do()
188174
if err != nil {
189175
policyOut.Error = err.Error()
190176
continue
191177
}
192-
defer resp.Body.Close()
193-
res, err := io.ReadAll(resp.Body)
178+
j, err := resp.MarshalJSON()
194179
if err != nil {
195180
policyOut.Error = err.Error()
196181
continue
197182
}
198183
pqlResp := &pqlResponse{}
199-
err = json.Unmarshal(res, pqlResp)
184+
err = json.Unmarshal(j, pqlResp)
200185
if err != nil {
201186
policyOut.Error = err.Error()
202187
continue

cmd/root.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb"
1414
resourcemanager "cloud.google.com/go/resourcemanager/apiv3"
1515
"github.com/spf13/cobra"
16-
"golang.org/x/oauth2/google"
16+
monitoring_v1 "google.golang.org/api/monitoring/v1"
1717
"google.golang.org/api/option"
1818
"google.golang.org/protobuf/types/known/timestamppb"
1919
)
@@ -140,14 +140,9 @@ func Execute() {
140140
if err != nil {
141141
log.Fatalf("Failed to create folders client: %v", err)
142142
}
143-
// https://github.com/googleapis/google-api-go-client/issues/2304
144-
// monitoring_v1Service, err := monitoring_v1.NewService(ctx, option.WithQuotaProject(quotaProject))
145-
// if err != nil {
146-
// log.Fatalf("Failed to create monitoring v1 client: %v", err)
147-
// }
148-
httpClient, err := google.DefaultClient(ctx, monitoring.DefaultAuthScopes()...)
143+
monitoring_v1Service, err := monitoring_v1.NewService(ctx, option.WithQuotaProject(quotaProject))
149144
if err != nil {
150-
log.Fatalf("Failed to create default http client: %v", err)
145+
log.Fatalf("Failed to create monitoring v1 client: %v", err)
151146
}
152147

153148
// If the application was executed with the --project or -p flag, put all the projects directly in the projects channel.
@@ -237,7 +232,7 @@ func Execute() {
237232
for i := 0; i < int(threads); i++ {
238233
go func() {
239234
for policy := range policiesIn {
240-
processAlertPolicy(ctx, queryClient, metricClient, httpClient, policy, start, end, policiesOut)
235+
processAlertPolicy(ctx, queryClient, metricClient, monitoring_v1Service, policy, start, end, policiesOut)
241236
}
242237
wg3.Done()
243238
}()

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
cloud.google.com/go/monitoring v1.21.0
88
cloud.google.com/go/resourcemanager v1.10.0
99
github.com/spf13/cobra v1.8.1
10-
golang.org/x/oauth2 v0.22.0
1110
google.golang.org/api v0.194.0
1211
google.golang.org/protobuf v1.34.2
1312
)
@@ -38,6 +37,7 @@ require (
3837
go.opentelemetry.io/otel/trace v1.28.0 // indirect
3938
golang.org/x/crypto v0.26.0 // indirect
4039
golang.org/x/net v0.28.0 // indirect
40+
golang.org/x/oauth2 v0.22.0 // indirect
4141
golang.org/x/sync v0.8.0 // indirect
4242
golang.org/x/sys v0.24.0 // indirect
4343
golang.org/x/text v0.17.0 // indirect

0 commit comments

Comments
 (0)