...

Source file src/cloud.google.com/go/monitoring/apiv3/v2/metric_client.go

Documentation: cloud.google.com/go/monitoring/apiv3/v2

     1  // Copyright 2024 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Code generated by protoc-gen-go_gapic. DO NOT EDIT.
    16  
    17  package monitoring
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"math"
    23  	"net/url"
    24  	"time"
    25  
    26  	monitoringpb "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  	"google.golang.org/api/option/internaloption"
    31  	gtransport "google.golang.org/api/transport/grpc"
    32  	metricpb "google.golang.org/genproto/googleapis/api/metric"
    33  	monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
    34  	"google.golang.org/grpc"
    35  	"google.golang.org/grpc/codes"
    36  	"google.golang.org/protobuf/proto"
    37  )
    38  
    39  var newMetricClientHook clientHook
    40  
    41  // MetricCallOptions contains the retry settings for each method of MetricClient.
    42  type MetricCallOptions struct {
    43  	ListMonitoredResourceDescriptors []gax.CallOption
    44  	GetMonitoredResourceDescriptor   []gax.CallOption
    45  	ListMetricDescriptors            []gax.CallOption
    46  	GetMetricDescriptor              []gax.CallOption
    47  	CreateMetricDescriptor           []gax.CallOption
    48  	DeleteMetricDescriptor           []gax.CallOption
    49  	ListTimeSeries                   []gax.CallOption
    50  	CreateTimeSeries                 []gax.CallOption
    51  	CreateServiceTimeSeries          []gax.CallOption
    52  }
    53  
    54  func defaultMetricGRPCClientOptions() []option.ClientOption {
    55  	return []option.ClientOption{
    56  		internaloption.WithDefaultEndpoint("monitoring.googleapis.com:443"),
    57  		internaloption.WithDefaultEndpointTemplate("monitoring.UNIVERSE_DOMAIN:443"),
    58  		internaloption.WithDefaultMTLSEndpoint("monitoring.mtls.googleapis.com:443"),
    59  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    60  		internaloption.WithDefaultAudience("https://monitoring.googleapis.com/"),
    61  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    62  		internaloption.EnableJwtWithScope(),
    63  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    64  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    65  	}
    66  }
    67  
    68  func defaultMetricCallOptions() *MetricCallOptions {
    69  	return &MetricCallOptions{
    70  		ListMonitoredResourceDescriptors: []gax.CallOption{
    71  			gax.WithTimeout(30000 * time.Millisecond),
    72  			gax.WithRetry(func() gax.Retryer {
    73  				return gax.OnCodes([]codes.Code{
    74  					codes.Unavailable,
    75  				}, gax.Backoff{
    76  					Initial:    100 * time.Millisecond,
    77  					Max:        30000 * time.Millisecond,
    78  					Multiplier: 1.30,
    79  				})
    80  			}),
    81  		},
    82  		GetMonitoredResourceDescriptor: []gax.CallOption{
    83  			gax.WithTimeout(30000 * time.Millisecond),
    84  			gax.WithRetry(func() gax.Retryer {
    85  				return gax.OnCodes([]codes.Code{
    86  					codes.Unavailable,
    87  				}, gax.Backoff{
    88  					Initial:    100 * time.Millisecond,
    89  					Max:        30000 * time.Millisecond,
    90  					Multiplier: 1.30,
    91  				})
    92  			}),
    93  		},
    94  		ListMetricDescriptors: []gax.CallOption{
    95  			gax.WithTimeout(30000 * time.Millisecond),
    96  			gax.WithRetry(func() gax.Retryer {
    97  				return gax.OnCodes([]codes.Code{
    98  					codes.Unavailable,
    99  				}, gax.Backoff{
   100  					Initial:    100 * time.Millisecond,
   101  					Max:        30000 * time.Millisecond,
   102  					Multiplier: 1.30,
   103  				})
   104  			}),
   105  		},
   106  		GetMetricDescriptor: []gax.CallOption{
   107  			gax.WithTimeout(30000 * time.Millisecond),
   108  			gax.WithRetry(func() gax.Retryer {
   109  				return gax.OnCodes([]codes.Code{
   110  					codes.Unavailable,
   111  				}, gax.Backoff{
   112  					Initial:    100 * time.Millisecond,
   113  					Max:        30000 * time.Millisecond,
   114  					Multiplier: 1.30,
   115  				})
   116  			}),
   117  		},
   118  		CreateMetricDescriptor: []gax.CallOption{
   119  			gax.WithTimeout(12000 * time.Millisecond),
   120  		},
   121  		DeleteMetricDescriptor: []gax.CallOption{
   122  			gax.WithTimeout(30000 * time.Millisecond),
   123  			gax.WithRetry(func() gax.Retryer {
   124  				return gax.OnCodes([]codes.Code{
   125  					codes.Unavailable,
   126  				}, gax.Backoff{
   127  					Initial:    100 * time.Millisecond,
   128  					Max:        30000 * time.Millisecond,
   129  					Multiplier: 1.30,
   130  				})
   131  			}),
   132  		},
   133  		ListTimeSeries: []gax.CallOption{
   134  			gax.WithTimeout(90000 * time.Millisecond),
   135  			gax.WithRetry(func() gax.Retryer {
   136  				return gax.OnCodes([]codes.Code{
   137  					codes.Unavailable,
   138  				}, gax.Backoff{
   139  					Initial:    100 * time.Millisecond,
   140  					Max:        30000 * time.Millisecond,
   141  					Multiplier: 1.30,
   142  				})
   143  			}),
   144  		},
   145  		CreateTimeSeries: []gax.CallOption{
   146  			gax.WithTimeout(12000 * time.Millisecond),
   147  		},
   148  		CreateServiceTimeSeries: []gax.CallOption{},
   149  	}
   150  }
   151  
   152  // internalMetricClient is an interface that defines the methods available from Cloud Monitoring API.
   153  type internalMetricClient interface {
   154  	Close() error
   155  	setGoogleClientInfo(...string)
   156  	Connection() *grpc.ClientConn
   157  	ListMonitoredResourceDescriptors(context.Context, *monitoringpb.ListMonitoredResourceDescriptorsRequest, ...gax.CallOption) *MonitoredResourceDescriptorIterator
   158  	GetMonitoredResourceDescriptor(context.Context, *monitoringpb.GetMonitoredResourceDescriptorRequest, ...gax.CallOption) (*monitoredrespb.MonitoredResourceDescriptor, error)
   159  	ListMetricDescriptors(context.Context, *monitoringpb.ListMetricDescriptorsRequest, ...gax.CallOption) *MetricDescriptorIterator
   160  	GetMetricDescriptor(context.Context, *monitoringpb.GetMetricDescriptorRequest, ...gax.CallOption) (*metricpb.MetricDescriptor, error)
   161  	CreateMetricDescriptor(context.Context, *monitoringpb.CreateMetricDescriptorRequest, ...gax.CallOption) (*metricpb.MetricDescriptor, error)
   162  	DeleteMetricDescriptor(context.Context, *monitoringpb.DeleteMetricDescriptorRequest, ...gax.CallOption) error
   163  	ListTimeSeries(context.Context, *monitoringpb.ListTimeSeriesRequest, ...gax.CallOption) *TimeSeriesIterator
   164  	CreateTimeSeries(context.Context, *monitoringpb.CreateTimeSeriesRequest, ...gax.CallOption) error
   165  	CreateServiceTimeSeries(context.Context, *monitoringpb.CreateTimeSeriesRequest, ...gax.CallOption) error
   166  }
   167  
   168  // MetricClient is a client for interacting with Cloud Monitoring API.
   169  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   170  //
   171  // Manages metric descriptors, monitored resource descriptors, and
   172  // time series data.
   173  type MetricClient struct {
   174  	// The internal transport-dependent client.
   175  	internalClient internalMetricClient
   176  
   177  	// The call options for this service.
   178  	CallOptions *MetricCallOptions
   179  }
   180  
   181  // Wrapper methods routed to the internal client.
   182  
   183  // Close closes the connection to the API service. The user should invoke this when
   184  // the client is no longer required.
   185  func (c *MetricClient) Close() error {
   186  	return c.internalClient.Close()
   187  }
   188  
   189  // setGoogleClientInfo sets the name and version of the application in
   190  // the `x-goog-api-client` header passed on each request. Intended for
   191  // use by Google-written clients.
   192  func (c *MetricClient) setGoogleClientInfo(keyval ...string) {
   193  	c.internalClient.setGoogleClientInfo(keyval...)
   194  }
   195  
   196  // Connection returns a connection to the API service.
   197  //
   198  // Deprecated: Connections are now pooled so this method does not always
   199  // return the same resource.
   200  func (c *MetricClient) Connection() *grpc.ClientConn {
   201  	return c.internalClient.Connection()
   202  }
   203  
   204  // ListMonitoredResourceDescriptors lists monitored resource descriptors that match a filter.
   205  func (c *MetricClient) ListMonitoredResourceDescriptors(ctx context.Context, req *monitoringpb.ListMonitoredResourceDescriptorsRequest, opts ...gax.CallOption) *MonitoredResourceDescriptorIterator {
   206  	return c.internalClient.ListMonitoredResourceDescriptors(ctx, req, opts...)
   207  }
   208  
   209  // GetMonitoredResourceDescriptor gets a single monitored resource descriptor.
   210  func (c *MetricClient) GetMonitoredResourceDescriptor(ctx context.Context, req *monitoringpb.GetMonitoredResourceDescriptorRequest, opts ...gax.CallOption) (*monitoredrespb.MonitoredResourceDescriptor, error) {
   211  	return c.internalClient.GetMonitoredResourceDescriptor(ctx, req, opts...)
   212  }
   213  
   214  // ListMetricDescriptors lists metric descriptors that match a filter.
   215  func (c *MetricClient) ListMetricDescriptors(ctx context.Context, req *monitoringpb.ListMetricDescriptorsRequest, opts ...gax.CallOption) *MetricDescriptorIterator {
   216  	return c.internalClient.ListMetricDescriptors(ctx, req, opts...)
   217  }
   218  
   219  // GetMetricDescriptor gets a single metric descriptor.
   220  func (c *MetricClient) GetMetricDescriptor(ctx context.Context, req *monitoringpb.GetMetricDescriptorRequest, opts ...gax.CallOption) (*metricpb.MetricDescriptor, error) {
   221  	return c.internalClient.GetMetricDescriptor(ctx, req, opts...)
   222  }
   223  
   224  // CreateMetricDescriptor creates a new metric descriptor.
   225  // The creation is executed asynchronously.
   226  // User-created metric descriptors define
   227  // custom metrics (at https://cloud.google.com/monitoring/custom-metrics).
   228  // The metric descriptor is updated if it already exists,
   229  // except that metric labels are never removed.
   230  func (c *MetricClient) CreateMetricDescriptor(ctx context.Context, req *monitoringpb.CreateMetricDescriptorRequest, opts ...gax.CallOption) (*metricpb.MetricDescriptor, error) {
   231  	return c.internalClient.CreateMetricDescriptor(ctx, req, opts...)
   232  }
   233  
   234  // DeleteMetricDescriptor deletes a metric descriptor. Only user-created
   235  // custom metrics (at https://cloud.google.com/monitoring/custom-metrics) can be
   236  // deleted.
   237  func (c *MetricClient) DeleteMetricDescriptor(ctx context.Context, req *monitoringpb.DeleteMetricDescriptorRequest, opts ...gax.CallOption) error {
   238  	return c.internalClient.DeleteMetricDescriptor(ctx, req, opts...)
   239  }
   240  
   241  // ListTimeSeries lists time series that match a filter.
   242  func (c *MetricClient) ListTimeSeries(ctx context.Context, req *monitoringpb.ListTimeSeriesRequest, opts ...gax.CallOption) *TimeSeriesIterator {
   243  	return c.internalClient.ListTimeSeries(ctx, req, opts...)
   244  }
   245  
   246  // CreateTimeSeries creates or adds data to one or more time series.
   247  // The response is empty if all time series in the request were written.
   248  // If any time series could not be written, a corresponding failure message is
   249  // included in the error response.
   250  // This method does not support
   251  // resource locations constraint of an organization
   252  // policy (at https://cloud.google.com/resource-manager/docs/organization-policy/defining-locations#setting_the_organization_policy).
   253  func (c *MetricClient) CreateTimeSeries(ctx context.Context, req *monitoringpb.CreateTimeSeriesRequest, opts ...gax.CallOption) error {
   254  	return c.internalClient.CreateTimeSeries(ctx, req, opts...)
   255  }
   256  
   257  // CreateServiceTimeSeries creates or adds data to one or more service time series. A service time
   258  // series is a time series for a metric from a Google Cloud service. The
   259  // response is empty if all time series in the request were written. If any
   260  // time series could not be written, a corresponding failure message is
   261  // included in the error response. This endpoint rejects writes to
   262  // user-defined metrics.
   263  // This method is only for use by Google Cloud services. Use
   264  // projects.timeSeries.create
   265  // instead.
   266  func (c *MetricClient) CreateServiceTimeSeries(ctx context.Context, req *monitoringpb.CreateTimeSeriesRequest, opts ...gax.CallOption) error {
   267  	return c.internalClient.CreateServiceTimeSeries(ctx, req, opts...)
   268  }
   269  
   270  // metricGRPCClient is a client for interacting with Cloud Monitoring API over gRPC transport.
   271  //
   272  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   273  type metricGRPCClient struct {
   274  	// Connection pool of gRPC connections to the service.
   275  	connPool gtransport.ConnPool
   276  
   277  	// Points back to the CallOptions field of the containing MetricClient
   278  	CallOptions **MetricCallOptions
   279  
   280  	// The gRPC API client.
   281  	metricClient monitoringpb.MetricServiceClient
   282  
   283  	// The x-goog-* metadata to be sent with each request.
   284  	xGoogHeaders []string
   285  }
   286  
   287  // NewMetricClient creates a new metric service client based on gRPC.
   288  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   289  //
   290  // Manages metric descriptors, monitored resource descriptors, and
   291  // time series data.
   292  func NewMetricClient(ctx context.Context, opts ...option.ClientOption) (*MetricClient, error) {
   293  	clientOpts := defaultMetricGRPCClientOptions()
   294  	if newMetricClientHook != nil {
   295  		hookOpts, err := newMetricClientHook(ctx, clientHookParams{})
   296  		if err != nil {
   297  			return nil, err
   298  		}
   299  		clientOpts = append(clientOpts, hookOpts...)
   300  	}
   301  
   302  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   303  	if err != nil {
   304  		return nil, err
   305  	}
   306  	client := MetricClient{CallOptions: defaultMetricCallOptions()}
   307  
   308  	c := &metricGRPCClient{
   309  		connPool:     connPool,
   310  		metricClient: monitoringpb.NewMetricServiceClient(connPool),
   311  		CallOptions:  &client.CallOptions,
   312  	}
   313  	c.setGoogleClientInfo()
   314  
   315  	client.internalClient = c
   316  
   317  	return &client, nil
   318  }
   319  
   320  // Connection returns a connection to the API service.
   321  //
   322  // Deprecated: Connections are now pooled so this method does not always
   323  // return the same resource.
   324  func (c *metricGRPCClient) Connection() *grpc.ClientConn {
   325  	return c.connPool.Conn()
   326  }
   327  
   328  // setGoogleClientInfo sets the name and version of the application in
   329  // the `x-goog-api-client` header passed on each request. Intended for
   330  // use by Google-written clients.
   331  func (c *metricGRPCClient) setGoogleClientInfo(keyval ...string) {
   332  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   333  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   334  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   335  }
   336  
   337  // Close closes the connection to the API service. The user should invoke this when
   338  // the client is no longer required.
   339  func (c *metricGRPCClient) Close() error {
   340  	return c.connPool.Close()
   341  }
   342  
   343  func (c *metricGRPCClient) ListMonitoredResourceDescriptors(ctx context.Context, req *monitoringpb.ListMonitoredResourceDescriptorsRequest, opts ...gax.CallOption) *MonitoredResourceDescriptorIterator {
   344  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   345  
   346  	hds = append(c.xGoogHeaders, hds...)
   347  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   348  	opts = append((*c.CallOptions).ListMonitoredResourceDescriptors[0:len((*c.CallOptions).ListMonitoredResourceDescriptors):len((*c.CallOptions).ListMonitoredResourceDescriptors)], opts...)
   349  	it := &MonitoredResourceDescriptorIterator{}
   350  	req = proto.Clone(req).(*monitoringpb.ListMonitoredResourceDescriptorsRequest)
   351  	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResourceDescriptor, string, error) {
   352  		resp := &monitoringpb.ListMonitoredResourceDescriptorsResponse{}
   353  		if pageToken != "" {
   354  			req.PageToken = pageToken
   355  		}
   356  		if pageSize > math.MaxInt32 {
   357  			req.PageSize = math.MaxInt32
   358  		} else if pageSize != 0 {
   359  			req.PageSize = int32(pageSize)
   360  		}
   361  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   362  			var err error
   363  			resp, err = c.metricClient.ListMonitoredResourceDescriptors(ctx, req, settings.GRPC...)
   364  			return err
   365  		}, opts...)
   366  		if err != nil {
   367  			return nil, "", err
   368  		}
   369  
   370  		it.Response = resp
   371  		return resp.GetResourceDescriptors(), resp.GetNextPageToken(), nil
   372  	}
   373  	fetch := func(pageSize int, pageToken string) (string, error) {
   374  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   375  		if err != nil {
   376  			return "", err
   377  		}
   378  		it.items = append(it.items, items...)
   379  		return nextPageToken, nil
   380  	}
   381  
   382  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   383  	it.pageInfo.MaxSize = int(req.GetPageSize())
   384  	it.pageInfo.Token = req.GetPageToken()
   385  
   386  	return it
   387  }
   388  
   389  func (c *metricGRPCClient) GetMonitoredResourceDescriptor(ctx context.Context, req *monitoringpb.GetMonitoredResourceDescriptorRequest, opts ...gax.CallOption) (*monitoredrespb.MonitoredResourceDescriptor, error) {
   390  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   391  
   392  	hds = append(c.xGoogHeaders, hds...)
   393  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   394  	opts = append((*c.CallOptions).GetMonitoredResourceDescriptor[0:len((*c.CallOptions).GetMonitoredResourceDescriptor):len((*c.CallOptions).GetMonitoredResourceDescriptor)], opts...)
   395  	var resp *monitoredrespb.MonitoredResourceDescriptor
   396  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   397  		var err error
   398  		resp, err = c.metricClient.GetMonitoredResourceDescriptor(ctx, req, settings.GRPC...)
   399  		return err
   400  	}, opts...)
   401  	if err != nil {
   402  		return nil, err
   403  	}
   404  	return resp, nil
   405  }
   406  
   407  func (c *metricGRPCClient) ListMetricDescriptors(ctx context.Context, req *monitoringpb.ListMetricDescriptorsRequest, opts ...gax.CallOption) *MetricDescriptorIterator {
   408  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   409  
   410  	hds = append(c.xGoogHeaders, hds...)
   411  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   412  	opts = append((*c.CallOptions).ListMetricDescriptors[0:len((*c.CallOptions).ListMetricDescriptors):len((*c.CallOptions).ListMetricDescriptors)], opts...)
   413  	it := &MetricDescriptorIterator{}
   414  	req = proto.Clone(req).(*monitoringpb.ListMetricDescriptorsRequest)
   415  	it.InternalFetch = func(pageSize int, pageToken string) ([]*metricpb.MetricDescriptor, string, error) {
   416  		resp := &monitoringpb.ListMetricDescriptorsResponse{}
   417  		if pageToken != "" {
   418  			req.PageToken = pageToken
   419  		}
   420  		if pageSize > math.MaxInt32 {
   421  			req.PageSize = math.MaxInt32
   422  		} else if pageSize != 0 {
   423  			req.PageSize = int32(pageSize)
   424  		}
   425  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   426  			var err error
   427  			resp, err = c.metricClient.ListMetricDescriptors(ctx, req, settings.GRPC...)
   428  			return err
   429  		}, opts...)
   430  		if err != nil {
   431  			return nil, "", err
   432  		}
   433  
   434  		it.Response = resp
   435  		return resp.GetMetricDescriptors(), resp.GetNextPageToken(), nil
   436  	}
   437  	fetch := func(pageSize int, pageToken string) (string, error) {
   438  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   439  		if err != nil {
   440  			return "", err
   441  		}
   442  		it.items = append(it.items, items...)
   443  		return nextPageToken, nil
   444  	}
   445  
   446  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   447  	it.pageInfo.MaxSize = int(req.GetPageSize())
   448  	it.pageInfo.Token = req.GetPageToken()
   449  
   450  	return it
   451  }
   452  
   453  func (c *metricGRPCClient) GetMetricDescriptor(ctx context.Context, req *monitoringpb.GetMetricDescriptorRequest, opts ...gax.CallOption) (*metricpb.MetricDescriptor, error) {
   454  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   455  
   456  	hds = append(c.xGoogHeaders, hds...)
   457  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   458  	opts = append((*c.CallOptions).GetMetricDescriptor[0:len((*c.CallOptions).GetMetricDescriptor):len((*c.CallOptions).GetMetricDescriptor)], opts...)
   459  	var resp *metricpb.MetricDescriptor
   460  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   461  		var err error
   462  		resp, err = c.metricClient.GetMetricDescriptor(ctx, req, settings.GRPC...)
   463  		return err
   464  	}, opts...)
   465  	if err != nil {
   466  		return nil, err
   467  	}
   468  	return resp, nil
   469  }
   470  
   471  func (c *metricGRPCClient) CreateMetricDescriptor(ctx context.Context, req *monitoringpb.CreateMetricDescriptorRequest, opts ...gax.CallOption) (*metricpb.MetricDescriptor, error) {
   472  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   473  
   474  	hds = append(c.xGoogHeaders, hds...)
   475  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   476  	opts = append((*c.CallOptions).CreateMetricDescriptor[0:len((*c.CallOptions).CreateMetricDescriptor):len((*c.CallOptions).CreateMetricDescriptor)], opts...)
   477  	var resp *metricpb.MetricDescriptor
   478  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   479  		var err error
   480  		resp, err = c.metricClient.CreateMetricDescriptor(ctx, req, settings.GRPC...)
   481  		return err
   482  	}, opts...)
   483  	if err != nil {
   484  		return nil, err
   485  	}
   486  	return resp, nil
   487  }
   488  
   489  func (c *metricGRPCClient) DeleteMetricDescriptor(ctx context.Context, req *monitoringpb.DeleteMetricDescriptorRequest, opts ...gax.CallOption) error {
   490  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   491  
   492  	hds = append(c.xGoogHeaders, hds...)
   493  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   494  	opts = append((*c.CallOptions).DeleteMetricDescriptor[0:len((*c.CallOptions).DeleteMetricDescriptor):len((*c.CallOptions).DeleteMetricDescriptor)], opts...)
   495  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   496  		var err error
   497  		_, err = c.metricClient.DeleteMetricDescriptor(ctx, req, settings.GRPC...)
   498  		return err
   499  	}, opts...)
   500  	return err
   501  }
   502  
   503  func (c *metricGRPCClient) ListTimeSeries(ctx context.Context, req *monitoringpb.ListTimeSeriesRequest, opts ...gax.CallOption) *TimeSeriesIterator {
   504  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   505  
   506  	hds = append(c.xGoogHeaders, hds...)
   507  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   508  	opts = append((*c.CallOptions).ListTimeSeries[0:len((*c.CallOptions).ListTimeSeries):len((*c.CallOptions).ListTimeSeries)], opts...)
   509  	it := &TimeSeriesIterator{}
   510  	req = proto.Clone(req).(*monitoringpb.ListTimeSeriesRequest)
   511  	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.TimeSeries, string, error) {
   512  		resp := &monitoringpb.ListTimeSeriesResponse{}
   513  		if pageToken != "" {
   514  			req.PageToken = pageToken
   515  		}
   516  		if pageSize > math.MaxInt32 {
   517  			req.PageSize = math.MaxInt32
   518  		} else if pageSize != 0 {
   519  			req.PageSize = int32(pageSize)
   520  		}
   521  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   522  			var err error
   523  			resp, err = c.metricClient.ListTimeSeries(ctx, req, settings.GRPC...)
   524  			return err
   525  		}, opts...)
   526  		if err != nil {
   527  			return nil, "", err
   528  		}
   529  
   530  		it.Response = resp
   531  		return resp.GetTimeSeries(), resp.GetNextPageToken(), nil
   532  	}
   533  	fetch := func(pageSize int, pageToken string) (string, error) {
   534  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   535  		if err != nil {
   536  			return "", err
   537  		}
   538  		it.items = append(it.items, items...)
   539  		return nextPageToken, nil
   540  	}
   541  
   542  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   543  	it.pageInfo.MaxSize = int(req.GetPageSize())
   544  	it.pageInfo.Token = req.GetPageToken()
   545  
   546  	return it
   547  }
   548  
   549  func (c *metricGRPCClient) CreateTimeSeries(ctx context.Context, req *monitoringpb.CreateTimeSeriesRequest, opts ...gax.CallOption) error {
   550  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   551  
   552  	hds = append(c.xGoogHeaders, hds...)
   553  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   554  	opts = append((*c.CallOptions).CreateTimeSeries[0:len((*c.CallOptions).CreateTimeSeries):len((*c.CallOptions).CreateTimeSeries)], opts...)
   555  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   556  		var err error
   557  		_, err = c.metricClient.CreateTimeSeries(ctx, req, settings.GRPC...)
   558  		return err
   559  	}, opts...)
   560  	return err
   561  }
   562  
   563  func (c *metricGRPCClient) CreateServiceTimeSeries(ctx context.Context, req *monitoringpb.CreateTimeSeriesRequest, opts ...gax.CallOption) error {
   564  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   565  
   566  	hds = append(c.xGoogHeaders, hds...)
   567  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   568  	opts = append((*c.CallOptions).CreateServiceTimeSeries[0:len((*c.CallOptions).CreateServiceTimeSeries):len((*c.CallOptions).CreateServiceTimeSeries)], opts...)
   569  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   570  		var err error
   571  		_, err = c.metricClient.CreateServiceTimeSeries(ctx, req, settings.GRPC...)
   572  		return err
   573  	}, opts...)
   574  	return err
   575  }
   576  

View as plain text