1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package monitoring
18
19 import (
20 "context"
21 "fmt"
22 "math"
23 "net/url"
24 "time"
25
26 "cloud.google.com/go/monitoring/apiv3/v2/monitoringpb"
27 gax "github.com/googleapis/gax-go/v2"
28 "google.golang.org/api/iterator"
29 "google.golang.org/api/option"
30 gtransport "google.golang.org/api/transport/grpc"
31 "google.golang.org/grpc"
32 "google.golang.org/grpc/codes"
33 "google.golang.org/grpc/metadata"
34 "google.golang.org/protobuf/proto"
35 )
36
37 var newServiceMonitoringClientHook clientHook
38
39
40 type ServiceMonitoringCallOptions struct {
41 CreateService []gax.CallOption
42 GetService []gax.CallOption
43 ListServices []gax.CallOption
44 UpdateService []gax.CallOption
45 DeleteService []gax.CallOption
46 CreateServiceLevelObjective []gax.CallOption
47 GetServiceLevelObjective []gax.CallOption
48 ListServiceLevelObjectives []gax.CallOption
49 UpdateServiceLevelObjective []gax.CallOption
50 DeleteServiceLevelObjective []gax.CallOption
51 }
52
53 func defaultServiceMonitoringClientOptions() []option.ClientOption {
54 return []option.ClientOption{
55 option.WithEndpoint("monitoring.googleapis.com:443"),
56 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
57 option.WithScopes(DefaultAuthScopes()...),
58 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
59 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
60 }
61 }
62
63 func defaultServiceMonitoringCallOptions() *ServiceMonitoringCallOptions {
64 return &ServiceMonitoringCallOptions{
65 CreateService: []gax.CallOption{},
66 GetService: []gax.CallOption{
67 gax.WithRetry(func() gax.Retryer {
68 return gax.OnCodes([]codes.Code{
69 codes.DeadlineExceeded,
70 codes.Unavailable,
71 }, gax.Backoff{
72 Initial: 100 * time.Millisecond,
73 Max: 30000 * time.Millisecond,
74 Multiplier: 1.30,
75 })
76 }),
77 },
78 ListServices: []gax.CallOption{
79 gax.WithRetry(func() gax.Retryer {
80 return gax.OnCodes([]codes.Code{
81 codes.DeadlineExceeded,
82 codes.Unavailable,
83 }, gax.Backoff{
84 Initial: 100 * time.Millisecond,
85 Max: 30000 * time.Millisecond,
86 Multiplier: 1.30,
87 })
88 }),
89 },
90 UpdateService: []gax.CallOption{},
91 DeleteService: []gax.CallOption{
92 gax.WithRetry(func() gax.Retryer {
93 return gax.OnCodes([]codes.Code{
94 codes.DeadlineExceeded,
95 codes.Unavailable,
96 }, gax.Backoff{
97 Initial: 100 * time.Millisecond,
98 Max: 30000 * time.Millisecond,
99 Multiplier: 1.30,
100 })
101 }),
102 },
103 CreateServiceLevelObjective: []gax.CallOption{},
104 GetServiceLevelObjective: []gax.CallOption{
105 gax.WithRetry(func() gax.Retryer {
106 return gax.OnCodes([]codes.Code{
107 codes.DeadlineExceeded,
108 codes.Unavailable,
109 }, gax.Backoff{
110 Initial: 100 * time.Millisecond,
111 Max: 30000 * time.Millisecond,
112 Multiplier: 1.30,
113 })
114 }),
115 },
116 ListServiceLevelObjectives: []gax.CallOption{
117 gax.WithRetry(func() gax.Retryer {
118 return gax.OnCodes([]codes.Code{
119 codes.DeadlineExceeded,
120 codes.Unavailable,
121 }, gax.Backoff{
122 Initial: 100 * time.Millisecond,
123 Max: 30000 * time.Millisecond,
124 Multiplier: 1.30,
125 })
126 }),
127 },
128 UpdateServiceLevelObjective: []gax.CallOption{},
129 DeleteServiceLevelObjective: []gax.CallOption{
130 gax.WithRetry(func() gax.Retryer {
131 return gax.OnCodes([]codes.Code{
132 codes.DeadlineExceeded,
133 codes.Unavailable,
134 }, gax.Backoff{
135 Initial: 100 * time.Millisecond,
136 Max: 30000 * time.Millisecond,
137 Multiplier: 1.30,
138 })
139 }),
140 },
141 }
142 }
143
144
145
146
147 type ServiceMonitoringClient struct {
148
149 connPool gtransport.ConnPool
150
151
152 serviceMonitoringClient monitoringpb.ServiceMonitoringServiceClient
153
154
155 CallOptions *ServiceMonitoringCallOptions
156
157
158 xGoogMetadata metadata.MD
159 }
160
161
162
163
164
165
166
167 func NewServiceMonitoringClient(ctx context.Context, opts ...option.ClientOption) (*ServiceMonitoringClient, error) {
168 clientOpts := defaultServiceMonitoringClientOptions()
169
170 if newServiceMonitoringClientHook != nil {
171 hookOpts, err := newServiceMonitoringClientHook(ctx, clientHookParams{})
172 if err != nil {
173 return nil, err
174 }
175 clientOpts = append(clientOpts, hookOpts...)
176 }
177
178 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
179 if err != nil {
180 return nil, err
181 }
182 c := &ServiceMonitoringClient{
183 connPool: connPool,
184 CallOptions: defaultServiceMonitoringCallOptions(),
185
186 serviceMonitoringClient: monitoringpb.NewServiceMonitoringServiceClient(connPool),
187 }
188 c.setGoogleClientInfo()
189
190 return c, nil
191 }
192
193
194
195
196 func (c *ServiceMonitoringClient) Connection() *grpc.ClientConn {
197 return c.connPool.Conn()
198 }
199
200
201
202 func (c *ServiceMonitoringClient) Close() error {
203 return c.connPool.Close()
204 }
205
206
207
208
209 func (c *ServiceMonitoringClient) setGoogleClientInfo(keyval ...string) {
210 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
211 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
212 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
213 }
214
215
216 func (c *ServiceMonitoringClient) CreateService(ctx context.Context, req *monitoringpb.CreateServiceRequest, opts ...gax.CallOption) (*monitoringpb.Service, error) {
217 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
218 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
219 opts = append(c.CallOptions.CreateService[0:len(c.CallOptions.CreateService):len(c.CallOptions.CreateService)], opts...)
220 var resp *monitoringpb.Service
221 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
222 var err error
223 resp, err = c.serviceMonitoringClient.CreateService(ctx, req, settings.GRPC...)
224 return err
225 }, opts...)
226 if err != nil {
227 return nil, err
228 }
229 return resp, nil
230 }
231
232
233 func (c *ServiceMonitoringClient) GetService(ctx context.Context, req *monitoringpb.GetServiceRequest, opts ...gax.CallOption) (*monitoringpb.Service, error) {
234 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
235 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
236 opts = append(c.CallOptions.GetService[0:len(c.CallOptions.GetService):len(c.CallOptions.GetService)], opts...)
237 var resp *monitoringpb.Service
238 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
239 var err error
240 resp, err = c.serviceMonitoringClient.GetService(ctx, req, settings.GRPC...)
241 return err
242 }, opts...)
243 if err != nil {
244 return nil, err
245 }
246 return resp, nil
247 }
248
249
250 func (c *ServiceMonitoringClient) ListServices(ctx context.Context, req *monitoringpb.ListServicesRequest, opts ...gax.CallOption) *ServiceIterator {
251 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
252 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
253 opts = append(c.CallOptions.ListServices[0:len(c.CallOptions.ListServices):len(c.CallOptions.ListServices)], opts...)
254 it := &ServiceIterator{}
255 req = proto.Clone(req).(*monitoringpb.ListServicesRequest)
256 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.Service, string, error) {
257 var resp *monitoringpb.ListServicesResponse
258 req.PageToken = pageToken
259 if pageSize > math.MaxInt32 {
260 req.PageSize = math.MaxInt32
261 } else {
262 req.PageSize = int32(pageSize)
263 }
264 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
265 var err error
266 resp, err = c.serviceMonitoringClient.ListServices(ctx, req, settings.GRPC...)
267 return err
268 }, opts...)
269 if err != nil {
270 return nil, "", err
271 }
272
273 it.Response = resp
274 return resp.Services, resp.NextPageToken, nil
275 }
276 fetch := func(pageSize int, pageToken string) (string, error) {
277 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
278 if err != nil {
279 return "", err
280 }
281 it.items = append(it.items, items...)
282 return nextPageToken, nil
283 }
284 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
285 it.pageInfo.MaxSize = int(req.PageSize)
286 it.pageInfo.Token = req.PageToken
287 return it
288 }
289
290
291 func (c *ServiceMonitoringClient) UpdateService(ctx context.Context, req *monitoringpb.UpdateServiceRequest, opts ...gax.CallOption) (*monitoringpb.Service, error) {
292 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "service.name", url.QueryEscape(req.GetService().GetName())))
293 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
294 opts = append(c.CallOptions.UpdateService[0:len(c.CallOptions.UpdateService):len(c.CallOptions.UpdateService)], opts...)
295 var resp *monitoringpb.Service
296 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
297 var err error
298 resp, err = c.serviceMonitoringClient.UpdateService(ctx, req, settings.GRPC...)
299 return err
300 }, opts...)
301 if err != nil {
302 return nil, err
303 }
304 return resp, nil
305 }
306
307
308 func (c *ServiceMonitoringClient) DeleteService(ctx context.Context, req *monitoringpb.DeleteServiceRequest, opts ...gax.CallOption) error {
309 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
310 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
311 opts = append(c.CallOptions.DeleteService[0:len(c.CallOptions.DeleteService):len(c.CallOptions.DeleteService)], opts...)
312 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
313 var err error
314 _, err = c.serviceMonitoringClient.DeleteService(ctx, req, settings.GRPC...)
315 return err
316 }, opts...)
317 return err
318 }
319
320
321 func (c *ServiceMonitoringClient) CreateServiceLevelObjective(ctx context.Context, req *monitoringpb.CreateServiceLevelObjectiveRequest, opts ...gax.CallOption) (*monitoringpb.ServiceLevelObjective, error) {
322 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
323 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
324 opts = append(c.CallOptions.CreateServiceLevelObjective[0:len(c.CallOptions.CreateServiceLevelObjective):len(c.CallOptions.CreateServiceLevelObjective)], opts...)
325 var resp *monitoringpb.ServiceLevelObjective
326 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
327 var err error
328 resp, err = c.serviceMonitoringClient.CreateServiceLevelObjective(ctx, req, settings.GRPC...)
329 return err
330 }, opts...)
331 if err != nil {
332 return nil, err
333 }
334 return resp, nil
335 }
336
337
338 func (c *ServiceMonitoringClient) GetServiceLevelObjective(ctx context.Context, req *monitoringpb.GetServiceLevelObjectiveRequest, opts ...gax.CallOption) (*monitoringpb.ServiceLevelObjective, error) {
339 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
340 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
341 opts = append(c.CallOptions.GetServiceLevelObjective[0:len(c.CallOptions.GetServiceLevelObjective):len(c.CallOptions.GetServiceLevelObjective)], opts...)
342 var resp *monitoringpb.ServiceLevelObjective
343 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
344 var err error
345 resp, err = c.serviceMonitoringClient.GetServiceLevelObjective(ctx, req, settings.GRPC...)
346 return err
347 }, opts...)
348 if err != nil {
349 return nil, err
350 }
351 return resp, nil
352 }
353
354
355 func (c *ServiceMonitoringClient) ListServiceLevelObjectives(ctx context.Context, req *monitoringpb.ListServiceLevelObjectivesRequest, opts ...gax.CallOption) *ServiceLevelObjectiveIterator {
356 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
357 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
358 opts = append(c.CallOptions.ListServiceLevelObjectives[0:len(c.CallOptions.ListServiceLevelObjectives):len(c.CallOptions.ListServiceLevelObjectives)], opts...)
359 it := &ServiceLevelObjectiveIterator{}
360 req = proto.Clone(req).(*monitoringpb.ListServiceLevelObjectivesRequest)
361 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.ServiceLevelObjective, string, error) {
362 var resp *monitoringpb.ListServiceLevelObjectivesResponse
363 req.PageToken = pageToken
364 if pageSize > math.MaxInt32 {
365 req.PageSize = math.MaxInt32
366 } else {
367 req.PageSize = int32(pageSize)
368 }
369 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
370 var err error
371 resp, err = c.serviceMonitoringClient.ListServiceLevelObjectives(ctx, req, settings.GRPC...)
372 return err
373 }, opts...)
374 if err != nil {
375 return nil, "", err
376 }
377
378 it.Response = resp
379 return resp.ServiceLevelObjectives, resp.NextPageToken, nil
380 }
381 fetch := func(pageSize int, pageToken string) (string, error) {
382 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
383 if err != nil {
384 return "", err
385 }
386 it.items = append(it.items, items...)
387 return nextPageToken, nil
388 }
389 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
390 it.pageInfo.MaxSize = int(req.PageSize)
391 it.pageInfo.Token = req.PageToken
392 return it
393 }
394
395
396 func (c *ServiceMonitoringClient) UpdateServiceLevelObjective(ctx context.Context, req *monitoringpb.UpdateServiceLevelObjectiveRequest, opts ...gax.CallOption) (*monitoringpb.ServiceLevelObjective, error) {
397 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "service_level_objective.name", url.QueryEscape(req.GetServiceLevelObjective().GetName())))
398 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
399 opts = append(c.CallOptions.UpdateServiceLevelObjective[0:len(c.CallOptions.UpdateServiceLevelObjective):len(c.CallOptions.UpdateServiceLevelObjective)], opts...)
400 var resp *monitoringpb.ServiceLevelObjective
401 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
402 var err error
403 resp, err = c.serviceMonitoringClient.UpdateServiceLevelObjective(ctx, req, settings.GRPC...)
404 return err
405 }, opts...)
406 if err != nil {
407 return nil, err
408 }
409 return resp, nil
410 }
411
412
413 func (c *ServiceMonitoringClient) DeleteServiceLevelObjective(ctx context.Context, req *monitoringpb.DeleteServiceLevelObjectiveRequest, opts ...gax.CallOption) error {
414 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
415 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
416 opts = append(c.CallOptions.DeleteServiceLevelObjective[0:len(c.CallOptions.DeleteServiceLevelObjective):len(c.CallOptions.DeleteServiceLevelObjective)], opts...)
417 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
418 var err error
419 _, err = c.serviceMonitoringClient.DeleteServiceLevelObjective(ctx, req, settings.GRPC...)
420 return err
421 }, opts...)
422 return err
423 }
424
425
426 type ServiceIterator struct {
427 items []*monitoringpb.Service
428 pageInfo *iterator.PageInfo
429 nextFunc func() error
430
431
432
433
434 Response interface{}
435
436
437
438
439
440
441
442 InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.Service, nextPageToken string, err error)
443 }
444
445
446 func (it *ServiceIterator) PageInfo() *iterator.PageInfo {
447 return it.pageInfo
448 }
449
450
451
452 func (it *ServiceIterator) Next() (*monitoringpb.Service, error) {
453 var item *monitoringpb.Service
454 if err := it.nextFunc(); err != nil {
455 return item, err
456 }
457 item = it.items[0]
458 it.items = it.items[1:]
459 return item, nil
460 }
461
462 func (it *ServiceIterator) bufLen() int {
463 return len(it.items)
464 }
465
466 func (it *ServiceIterator) takeBuf() interface{} {
467 b := it.items
468 it.items = nil
469 return b
470 }
471
472
473 type ServiceLevelObjectiveIterator struct {
474 items []*monitoringpb.ServiceLevelObjective
475 pageInfo *iterator.PageInfo
476 nextFunc func() error
477
478
479
480
481 Response interface{}
482
483
484
485
486
487
488
489 InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.ServiceLevelObjective, nextPageToken string, err error)
490 }
491
492
493 func (it *ServiceLevelObjectiveIterator) PageInfo() *iterator.PageInfo {
494 return it.pageInfo
495 }
496
497
498
499 func (it *ServiceLevelObjectiveIterator) Next() (*monitoringpb.ServiceLevelObjective, error) {
500 var item *monitoringpb.ServiceLevelObjective
501 if err := it.nextFunc(); err != nil {
502 return item, err
503 }
504 item = it.items[0]
505 it.items = it.items[1:]
506 return item, nil
507 }
508
509 func (it *ServiceLevelObjectiveIterator) bufLen() int {
510 return len(it.items)
511 }
512
513 func (it *ServiceLevelObjectiveIterator) takeBuf() interface{} {
514 b := it.items
515 it.items = nil
516 return b
517 }
518
View as plain text