1// Copyright 2021 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 17package monitoring 18 19import ( 20 "context" 21 "fmt" 22 "math" 23 "net/url" 24 25 gax "github.com/googleapis/gax-go/v2" 26 "google.golang.org/api/iterator" 27 "google.golang.org/api/option" 28 "google.golang.org/api/option/internaloption" 29 gtransport "google.golang.org/api/transport/grpc" 30 monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" 31 "google.golang.org/grpc" 32 "google.golang.org/grpc/metadata" 33 "google.golang.org/protobuf/proto" 34) 35 36var newQueryClientHook clientHook 37 38// QueryCallOptions contains the retry settings for each method of QueryClient. 39type QueryCallOptions struct { 40 QueryTimeSeries []gax.CallOption 41} 42 43func defaultQueryGRPCClientOptions() []option.ClientOption { 44 return []option.ClientOption{ 45 internaloption.WithDefaultEndpoint("monitoring.googleapis.com:443"), 46 internaloption.WithDefaultMTLSEndpoint("monitoring.mtls.googleapis.com:443"), 47 internaloption.WithDefaultAudience("https://monitoring.googleapis.com/"), 48 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 49 internaloption.EnableJwtWithScope(), 50 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 51 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 52 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 53 } 54} 55 56func defaultQueryCallOptions() *QueryCallOptions { 57 return &QueryCallOptions{ 58 QueryTimeSeries: []gax.CallOption{}, 59 } 60} 61 62// internalQueryClient is an interface that defines the methods availaible from Cloud Monitoring API. 63type 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 Stackdriver 74// Monitoring. Time series data is a collection of data points that describes 75// the time-varying values of a metric. 76type 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. 88func (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. 95func (c *QueryClient) setGoogleClientInfo(keyval ...string) { 96 c.internalClient.setGoogleClientInfo(keyval...) 97} 98 99// Connection returns a connection to the API service. 100// 101// Deprecated. 102func (c *QueryClient) Connection() *grpc.ClientConn { 103 return c.internalClient.Connection() 104} 105 106// QueryTimeSeries queries time series using Monitoring Query Language. This method does not require a Workspace. 107func (c *QueryClient) QueryTimeSeries(ctx context.Context, req *monitoringpb.QueryTimeSeriesRequest, opts ...gax.CallOption) *TimeSeriesDataIterator { 108 return c.internalClient.QueryTimeSeries(ctx, req, opts...) 109} 110 111// queryGRPCClient is a client for interacting with Cloud Monitoring API over gRPC transport. 112// 113// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 114type queryGRPCClient struct { 115 // Connection pool of gRPC connections to the service. 116 connPool gtransport.ConnPool 117 118 // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE 119 disableDeadlines bool 120 121 // Points back to the CallOptions field of the containing QueryClient 122 CallOptions **QueryCallOptions 123 124 // The gRPC API client. 125 queryClient monitoringpb.QueryServiceClient 126 127 // The x-goog-* metadata to be sent with each request. 128 xGoogMetadata metadata.MD 129} 130 131// NewQueryClient creates a new query service client based on gRPC. 132// The returned client must be Closed when it is done being used to clean up its underlying connections. 133// 134// The QueryService API is used to manage time series data in Stackdriver 135// Monitoring. Time series data is a collection of data points that describes 136// the time-varying values of a metric. 137func NewQueryClient(ctx context.Context, opts ...option.ClientOption) (*QueryClient, error) { 138 clientOpts := defaultQueryGRPCClientOptions() 139 if newQueryClientHook != nil { 140 hookOpts, err := newQueryClientHook(ctx, clientHookParams{}) 141 if err != nil { 142 return nil, err 143 } 144 clientOpts = append(clientOpts, hookOpts...) 145 } 146 147 disableDeadlines, err := checkDisableDeadlines() 148 if err != nil { 149 return nil, err 150 } 151 152 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 153 if err != nil { 154 return nil, err 155 } 156 client := QueryClient{CallOptions: defaultQueryCallOptions()} 157 158 c := &queryGRPCClient{ 159 connPool: connPool, 160 disableDeadlines: disableDeadlines, 161 queryClient: monitoringpb.NewQueryServiceClient(connPool), 162 CallOptions: &client.CallOptions, 163 } 164 c.setGoogleClientInfo() 165 166 client.internalClient = c 167 168 return &client, nil 169} 170 171// Connection returns a connection to the API service. 172// 173// Deprecated. 174func (c *queryGRPCClient) Connection() *grpc.ClientConn { 175 return c.connPool.Conn() 176} 177 178// setGoogleClientInfo sets the name and version of the application in 179// the `x-goog-api-client` header passed on each request. Intended for 180// use by Google-written clients. 181func (c *queryGRPCClient) setGoogleClientInfo(keyval ...string) { 182 kv := append([]string{"gl-go", versionGo()}, keyval...) 183 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 184 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 185} 186 187// Close closes the connection to the API service. The user should invoke this when 188// the client is no longer required. 189func (c *queryGRPCClient) Close() error { 190 return c.connPool.Close() 191} 192 193func (c *queryGRPCClient) QueryTimeSeries(ctx context.Context, req *monitoringpb.QueryTimeSeriesRequest, opts ...gax.CallOption) *TimeSeriesDataIterator { 194 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 195 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 196 opts = append((*c.CallOptions).QueryTimeSeries[0:len((*c.CallOptions).QueryTimeSeries):len((*c.CallOptions).QueryTimeSeries)], opts...) 197 it := &TimeSeriesDataIterator{} 198 req = proto.Clone(req).(*monitoringpb.QueryTimeSeriesRequest) 199 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.TimeSeriesData, string, error) { 200 resp := &monitoringpb.QueryTimeSeriesResponse{} 201 if pageToken != "" { 202 req.PageToken = pageToken 203 } 204 if pageSize > math.MaxInt32 { 205 req.PageSize = math.MaxInt32 206 } else if pageSize != 0 { 207 req.PageSize = int32(pageSize) 208 } 209 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 210 var err error 211 resp, err = c.queryClient.QueryTimeSeries(ctx, req, settings.GRPC...) 212 return err 213 }, opts...) 214 if err != nil { 215 return nil, "", err 216 } 217 218 it.Response = resp 219 return resp.GetTimeSeriesData(), resp.GetNextPageToken(), nil 220 } 221 fetch := func(pageSize int, pageToken string) (string, error) { 222 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 223 if err != nil { 224 return "", err 225 } 226 it.items = append(it.items, items...) 227 return nextPageToken, nil 228 } 229 230 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 231 it.pageInfo.MaxSize = int(req.GetPageSize()) 232 it.pageInfo.Token = req.GetPageToken() 233 234 return it 235} 236 237// TimeSeriesDataIterator manages a stream of *monitoringpb.TimeSeriesData. 238type TimeSeriesDataIterator struct { 239 items []*monitoringpb.TimeSeriesData 240 pageInfo *iterator.PageInfo 241 nextFunc func() error 242 243 // Response is the raw response for the current page. 244 // It must be cast to the RPC response type. 245 // Calling Next() or InternalFetch() updates this value. 246 Response interface{} 247 248 // InternalFetch is for use by the Google Cloud Libraries only. 249 // It is not part of the stable interface of this package. 250 // 251 // InternalFetch returns results from a single call to the underlying RPC. 252 // The number of results is no greater than pageSize. 253 // If there are no more results, nextPageToken is empty and err is nil. 254 InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.TimeSeriesData, nextPageToken string, err error) 255} 256 257// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 258func (it *TimeSeriesDataIterator) PageInfo() *iterator.PageInfo { 259 return it.pageInfo 260} 261 262// Next returns the next result. Its second return value is iterator.Done if there are no more 263// results. Once Next returns Done, all subsequent calls will return Done. 264func (it *TimeSeriesDataIterator) Next() (*monitoringpb.TimeSeriesData, error) { 265 var item *monitoringpb.TimeSeriesData 266 if err := it.nextFunc(); err != nil { 267 return item, err 268 } 269 item = it.items[0] 270 it.items = it.items[1:] 271 return item, nil 272} 273 274func (it *TimeSeriesDataIterator) bufLen() int { 275 return len(it.items) 276} 277 278func (it *TimeSeriesDataIterator) takeBuf() interface{} { 279 b := it.items 280 it.items = nil 281 return b 282} 283