...

Source file src/cloud.google.com/go/monitoring/apiv3/v2/query_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  
    25  	monitoringpb "cloud.google.com/go/monitoring/apiv3/v2/monitoringpb"
    26  	gax "github.com/googleapis/gax-go/v2"
    27  	"google.golang.org/api/iterator"
    28  	"google.golang.org/api/option"
    29  	"google.golang.org/api/option/internaloption"
    30  	gtransport "google.golang.org/api/transport/grpc"
    31  	"google.golang.org/grpc"
    32  	"google.golang.org/protobuf/proto"
    33  )
    34  
    35  var newQueryClientHook clientHook
    36  
    37  // QueryCallOptions contains the retry settings for each method of QueryClient.
    38  type QueryCallOptions struct {
    39  	QueryTimeSeries []gax.CallOption
    40  }
    41  
    42  func defaultQueryGRPCClientOptions() []option.ClientOption {
    43  	return []option.ClientOption{
    44  		internaloption.WithDefaultEndpoint("monitoring.googleapis.com:443"),
    45  		internaloption.WithDefaultEndpointTemplate("monitoring.UNIVERSE_DOMAIN:443"),
    46  		internaloption.WithDefaultMTLSEndpoint("monitoring.mtls.googleapis.com:443"),
    47  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    48  		internaloption.WithDefaultAudience("https://monitoring.googleapis.com/"),
    49  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    50  		internaloption.EnableJwtWithScope(),
    51  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    52  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    53  	}
    54  }
    55  
    56  func defaultQueryCallOptions() *QueryCallOptions {
    57  	return &QueryCallOptions{
    58  		QueryTimeSeries: []gax.CallOption{},
    59  	}
    60  }
    61  
    62  // internalQueryClient is an interface that defines the methods available from Cloud Monitoring API.
    63  type internalQueryClient interface {
    64  	Close() error
    65  	setGoogleClientInfo(...string)
    66  	Connection() *grpc.ClientConn
    67  	QueryTimeSeries(context.Context, *monitoringpb.QueryTimeSeriesRequest, ...gax.CallOption) *TimeSeriesDataIterator
    68  }
    69  
    70  // QueryClient is a client for interacting with Cloud Monitoring API.
    71  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
    72  //
    73  // The QueryService API is used to manage time series data in Cloud
    74  // Monitoring. Time series data is a collection of data points that describes
    75  // the time-varying values of a metric.
    76  type QueryClient struct {
    77  	// The internal transport-dependent client.
    78  	internalClient internalQueryClient
    79  
    80  	// The call options for this service.
    81  	CallOptions *QueryCallOptions
    82  }
    83  
    84  // Wrapper methods routed to the internal client.
    85  
    86  // Close closes the connection to the API service. The user should invoke this when
    87  // the client is no longer required.
    88  func (c *QueryClient) Close() error {
    89  	return c.internalClient.Close()
    90  }
    91  
    92  // setGoogleClientInfo sets the name and version of the application in
    93  // the `x-goog-api-client` header passed on each request. Intended for
    94  // use by Google-written clients.
    95  func (c *QueryClient) setGoogleClientInfo(keyval ...string) {
    96  	c.internalClient.setGoogleClientInfo(keyval...)
    97  }
    98  
    99  // Connection returns a connection to the API service.
   100  //
   101  // Deprecated: Connections are now pooled so this method does not always
   102  // return the same resource.
   103  func (c *QueryClient) Connection() *grpc.ClientConn {
   104  	return c.internalClient.Connection()
   105  }
   106  
   107  // QueryTimeSeries queries time series using Monitoring Query Language.
   108  func (c *QueryClient) QueryTimeSeries(ctx context.Context, req *monitoringpb.QueryTimeSeriesRequest, opts ...gax.CallOption) *TimeSeriesDataIterator {
   109  	return c.internalClient.QueryTimeSeries(ctx, req, opts...)
   110  }
   111  
   112  // queryGRPCClient is a client for interacting with Cloud Monitoring API over gRPC transport.
   113  //
   114  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   115  type queryGRPCClient struct {
   116  	// Connection pool of gRPC connections to the service.
   117  	connPool gtransport.ConnPool
   118  
   119  	// Points back to the CallOptions field of the containing QueryClient
   120  	CallOptions **QueryCallOptions
   121  
   122  	// The gRPC API client.
   123  	queryClient monitoringpb.QueryServiceClient
   124  
   125  	// The x-goog-* metadata to be sent with each request.
   126  	xGoogHeaders []string
   127  }
   128  
   129  // NewQueryClient creates a new query service client based on gRPC.
   130  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   131  //
   132  // The QueryService API is used to manage time series data in Cloud
   133  // Monitoring. Time series data is a collection of data points that describes
   134  // the time-varying values of a metric.
   135  func NewQueryClient(ctx context.Context, opts ...option.ClientOption) (*QueryClient, error) {
   136  	clientOpts := defaultQueryGRPCClientOptions()
   137  	if newQueryClientHook != nil {
   138  		hookOpts, err := newQueryClientHook(ctx, clientHookParams{})
   139  		if err != nil {
   140  			return nil, err
   141  		}
   142  		clientOpts = append(clientOpts, hookOpts...)
   143  	}
   144  
   145  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	client := QueryClient{CallOptions: defaultQueryCallOptions()}
   150  
   151  	c := &queryGRPCClient{
   152  		connPool:    connPool,
   153  		queryClient: monitoringpb.NewQueryServiceClient(connPool),
   154  		CallOptions: &client.CallOptions,
   155  	}
   156  	c.setGoogleClientInfo()
   157  
   158  	client.internalClient = c
   159  
   160  	return &client, nil
   161  }
   162  
   163  // Connection returns a connection to the API service.
   164  //
   165  // Deprecated: Connections are now pooled so this method does not always
   166  // return the same resource.
   167  func (c *queryGRPCClient) Connection() *grpc.ClientConn {
   168  	return c.connPool.Conn()
   169  }
   170  
   171  // setGoogleClientInfo sets the name and version of the application in
   172  // the `x-goog-api-client` header passed on each request. Intended for
   173  // use by Google-written clients.
   174  func (c *queryGRPCClient) setGoogleClientInfo(keyval ...string) {
   175  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   176  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   177  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   178  }
   179  
   180  // Close closes the connection to the API service. The user should invoke this when
   181  // the client is no longer required.
   182  func (c *queryGRPCClient) Close() error {
   183  	return c.connPool.Close()
   184  }
   185  
   186  func (c *queryGRPCClient) QueryTimeSeries(ctx context.Context, req *monitoringpb.QueryTimeSeriesRequest, opts ...gax.CallOption) *TimeSeriesDataIterator {
   187  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   188  
   189  	hds = append(c.xGoogHeaders, hds...)
   190  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   191  	opts = append((*c.CallOptions).QueryTimeSeries[0:len((*c.CallOptions).QueryTimeSeries):len((*c.CallOptions).QueryTimeSeries)], opts...)
   192  	it := &TimeSeriesDataIterator{}
   193  	req = proto.Clone(req).(*monitoringpb.QueryTimeSeriesRequest)
   194  	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.TimeSeriesData, string, error) {
   195  		resp := &monitoringpb.QueryTimeSeriesResponse{}
   196  		if pageToken != "" {
   197  			req.PageToken = pageToken
   198  		}
   199  		if pageSize > math.MaxInt32 {
   200  			req.PageSize = math.MaxInt32
   201  		} else if pageSize != 0 {
   202  			req.PageSize = int32(pageSize)
   203  		}
   204  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   205  			var err error
   206  			resp, err = c.queryClient.QueryTimeSeries(ctx, req, settings.GRPC...)
   207  			return err
   208  		}, opts...)
   209  		if err != nil {
   210  			return nil, "", err
   211  		}
   212  
   213  		it.Response = resp
   214  		return resp.GetTimeSeriesData(), resp.GetNextPageToken(), nil
   215  	}
   216  	fetch := func(pageSize int, pageToken string) (string, error) {
   217  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   218  		if err != nil {
   219  			return "", err
   220  		}
   221  		it.items = append(it.items, items...)
   222  		return nextPageToken, nil
   223  	}
   224  
   225  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   226  	it.pageInfo.MaxSize = int(req.GetPageSize())
   227  	it.pageInfo.Token = req.GetPageToken()
   228  
   229  	return it
   230  }
   231  

View as plain text