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