You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

447 lines
17 KiB

  1. // Copyright 2018 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. // AUTO-GENERATED CODE. DO NOT EDIT.
  15. package monitoring
  16. import (
  17. "math"
  18. "time"
  19. "cloud.google.com/go/internal/version"
  20. gax "github.com/googleapis/gax-go"
  21. "golang.org/x/net/context"
  22. "google.golang.org/api/iterator"
  23. "google.golang.org/api/option"
  24. "google.golang.org/api/transport"
  25. metricpb "google.golang.org/genproto/googleapis/api/metric"
  26. monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
  27. monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
  28. "google.golang.org/grpc"
  29. "google.golang.org/grpc/codes"
  30. "google.golang.org/grpc/metadata"
  31. )
  32. // MetricCallOptions contains the retry settings for each method of MetricClient.
  33. type MetricCallOptions struct {
  34. ListMonitoredResourceDescriptors []gax.CallOption
  35. GetMonitoredResourceDescriptor []gax.CallOption
  36. ListMetricDescriptors []gax.CallOption
  37. GetMetricDescriptor []gax.CallOption
  38. CreateMetricDescriptor []gax.CallOption
  39. DeleteMetricDescriptor []gax.CallOption
  40. ListTimeSeries []gax.CallOption
  41. CreateTimeSeries []gax.CallOption
  42. }
  43. func defaultMetricClientOptions() []option.ClientOption {
  44. return []option.ClientOption{
  45. option.WithEndpoint("monitoring.googleapis.com:443"),
  46. option.WithScopes(DefaultAuthScopes()...),
  47. }
  48. }
  49. func defaultMetricCallOptions() *MetricCallOptions {
  50. retry := map[[2]string][]gax.CallOption{
  51. {"default", "idempotent"}: {
  52. gax.WithRetry(func() gax.Retryer {
  53. return gax.OnCodes([]codes.Code{
  54. codes.DeadlineExceeded,
  55. codes.Unavailable,
  56. }, gax.Backoff{
  57. Initial: 100 * time.Millisecond,
  58. Max: 60000 * time.Millisecond,
  59. Multiplier: 1.3,
  60. })
  61. }),
  62. },
  63. }
  64. return &MetricCallOptions{
  65. ListMonitoredResourceDescriptors: retry[[2]string{"default", "idempotent"}],
  66. GetMonitoredResourceDescriptor: retry[[2]string{"default", "idempotent"}],
  67. ListMetricDescriptors: retry[[2]string{"default", "idempotent"}],
  68. GetMetricDescriptor: retry[[2]string{"default", "idempotent"}],
  69. CreateMetricDescriptor: retry[[2]string{"default", "non_idempotent"}],
  70. DeleteMetricDescriptor: retry[[2]string{"default", "idempotent"}],
  71. ListTimeSeries: retry[[2]string{"default", "idempotent"}],
  72. CreateTimeSeries: retry[[2]string{"default", "non_idempotent"}],
  73. }
  74. }
  75. // MetricClient is a client for interacting with Stackdriver Monitoring API.
  76. //
  77. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  78. type MetricClient struct {
  79. // The connection to the service.
  80. conn *grpc.ClientConn
  81. // The gRPC API client.
  82. metricClient monitoringpb.MetricServiceClient
  83. // The call options for this service.
  84. CallOptions *MetricCallOptions
  85. // The x-goog-* metadata to be sent with each request.
  86. xGoogMetadata metadata.MD
  87. }
  88. // NewMetricClient creates a new metric service client.
  89. //
  90. // Manages metric descriptors, monitored resource descriptors, and
  91. // time series data.
  92. func NewMetricClient(ctx context.Context, opts ...option.ClientOption) (*MetricClient, error) {
  93. conn, err := transport.DialGRPC(ctx, append(defaultMetricClientOptions(), opts...)...)
  94. if err != nil {
  95. return nil, err
  96. }
  97. c := &MetricClient{
  98. conn: conn,
  99. CallOptions: defaultMetricCallOptions(),
  100. metricClient: monitoringpb.NewMetricServiceClient(conn),
  101. }
  102. c.setGoogleClientInfo()
  103. return c, nil
  104. }
  105. // Connection returns the client's connection to the API service.
  106. func (c *MetricClient) Connection() *grpc.ClientConn {
  107. return c.conn
  108. }
  109. // Close closes the connection to the API service. The user should invoke this when
  110. // the client is no longer required.
  111. func (c *MetricClient) Close() error {
  112. return c.conn.Close()
  113. }
  114. // setGoogleClientInfo sets the name and version of the application in
  115. // the `x-goog-api-client` header passed on each request. Intended for
  116. // use by Google-written clients.
  117. func (c *MetricClient) setGoogleClientInfo(keyval ...string) {
  118. kv := append([]string{"gl-go", version.Go()}, keyval...)
  119. kv = append(kv, "gapic", version.Repo, "gax", gax.Version, "grpc", grpc.Version)
  120. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  121. }
  122. // ListMonitoredResourceDescriptors lists monitored resource descriptors that match a filter. This method does not require a Stackdriver account.
  123. func (c *MetricClient) ListMonitoredResourceDescriptors(ctx context.Context, req *monitoringpb.ListMonitoredResourceDescriptorsRequest, opts ...gax.CallOption) *MonitoredResourceDescriptorIterator {
  124. ctx = insertMetadata(ctx, c.xGoogMetadata)
  125. opts = append(c.CallOptions.ListMonitoredResourceDescriptors[0:len(c.CallOptions.ListMonitoredResourceDescriptors):len(c.CallOptions.ListMonitoredResourceDescriptors)], opts...)
  126. it := &MonitoredResourceDescriptorIterator{}
  127. it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResourceDescriptor, string, error) {
  128. var resp *monitoringpb.ListMonitoredResourceDescriptorsResponse
  129. req.PageToken = pageToken
  130. if pageSize > math.MaxInt32 {
  131. req.PageSize = math.MaxInt32
  132. } else {
  133. req.PageSize = int32(pageSize)
  134. }
  135. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  136. var err error
  137. resp, err = c.metricClient.ListMonitoredResourceDescriptors(ctx, req, settings.GRPC...)
  138. return err
  139. }, opts...)
  140. if err != nil {
  141. return nil, "", err
  142. }
  143. return resp.ResourceDescriptors, resp.NextPageToken, nil
  144. }
  145. fetch := func(pageSize int, pageToken string) (string, error) {
  146. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  147. if err != nil {
  148. return "", err
  149. }
  150. it.items = append(it.items, items...)
  151. return nextPageToken, nil
  152. }
  153. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  154. return it
  155. }
  156. // GetMonitoredResourceDescriptor gets a single monitored resource descriptor. This method does not require a Stackdriver account.
  157. func (c *MetricClient) GetMonitoredResourceDescriptor(ctx context.Context, req *monitoringpb.GetMonitoredResourceDescriptorRequest, opts ...gax.CallOption) (*monitoredrespb.MonitoredResourceDescriptor, error) {
  158. ctx = insertMetadata(ctx, c.xGoogMetadata)
  159. opts = append(c.CallOptions.GetMonitoredResourceDescriptor[0:len(c.CallOptions.GetMonitoredResourceDescriptor):len(c.CallOptions.GetMonitoredResourceDescriptor)], opts...)
  160. var resp *monitoredrespb.MonitoredResourceDescriptor
  161. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  162. var err error
  163. resp, err = c.metricClient.GetMonitoredResourceDescriptor(ctx, req, settings.GRPC...)
  164. return err
  165. }, opts...)
  166. if err != nil {
  167. return nil, err
  168. }
  169. return resp, nil
  170. }
  171. // ListMetricDescriptors lists metric descriptors that match a filter. This method does not require a Stackdriver account.
  172. func (c *MetricClient) ListMetricDescriptors(ctx context.Context, req *monitoringpb.ListMetricDescriptorsRequest, opts ...gax.CallOption) *MetricDescriptorIterator {
  173. ctx = insertMetadata(ctx, c.xGoogMetadata)
  174. opts = append(c.CallOptions.ListMetricDescriptors[0:len(c.CallOptions.ListMetricDescriptors):len(c.CallOptions.ListMetricDescriptors)], opts...)
  175. it := &MetricDescriptorIterator{}
  176. it.InternalFetch = func(pageSize int, pageToken string) ([]*metricpb.MetricDescriptor, string, error) {
  177. var resp *monitoringpb.ListMetricDescriptorsResponse
  178. req.PageToken = pageToken
  179. if pageSize > math.MaxInt32 {
  180. req.PageSize = math.MaxInt32
  181. } else {
  182. req.PageSize = int32(pageSize)
  183. }
  184. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  185. var err error
  186. resp, err = c.metricClient.ListMetricDescriptors(ctx, req, settings.GRPC...)
  187. return err
  188. }, opts...)
  189. if err != nil {
  190. return nil, "", err
  191. }
  192. return resp.MetricDescriptors, resp.NextPageToken, nil
  193. }
  194. fetch := func(pageSize int, pageToken string) (string, error) {
  195. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  196. if err != nil {
  197. return "", err
  198. }
  199. it.items = append(it.items, items...)
  200. return nextPageToken, nil
  201. }
  202. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  203. return it
  204. }
  205. // GetMetricDescriptor gets a single metric descriptor. This method does not require a Stackdriver account.
  206. func (c *MetricClient) GetMetricDescriptor(ctx context.Context, req *monitoringpb.GetMetricDescriptorRequest, opts ...gax.CallOption) (*metricpb.MetricDescriptor, error) {
  207. ctx = insertMetadata(ctx, c.xGoogMetadata)
  208. opts = append(c.CallOptions.GetMetricDescriptor[0:len(c.CallOptions.GetMetricDescriptor):len(c.CallOptions.GetMetricDescriptor)], opts...)
  209. var resp *metricpb.MetricDescriptor
  210. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  211. var err error
  212. resp, err = c.metricClient.GetMetricDescriptor(ctx, req, settings.GRPC...)
  213. return err
  214. }, opts...)
  215. if err != nil {
  216. return nil, err
  217. }
  218. return resp, nil
  219. }
  220. // CreateMetricDescriptor creates a new metric descriptor.
  221. // User-created metric descriptors define
  222. // custom metrics (at /monitoring/custom-metrics).
  223. func (c *MetricClient) CreateMetricDescriptor(ctx context.Context, req *monitoringpb.CreateMetricDescriptorRequest, opts ...gax.CallOption) (*metricpb.MetricDescriptor, error) {
  224. ctx = insertMetadata(ctx, c.xGoogMetadata)
  225. opts = append(c.CallOptions.CreateMetricDescriptor[0:len(c.CallOptions.CreateMetricDescriptor):len(c.CallOptions.CreateMetricDescriptor)], opts...)
  226. var resp *metricpb.MetricDescriptor
  227. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  228. var err error
  229. resp, err = c.metricClient.CreateMetricDescriptor(ctx, req, settings.GRPC...)
  230. return err
  231. }, opts...)
  232. if err != nil {
  233. return nil, err
  234. }
  235. return resp, nil
  236. }
  237. // DeleteMetricDescriptor deletes a metric descriptor. Only user-created
  238. // custom metrics (at /monitoring/custom-metrics) can be deleted.
  239. func (c *MetricClient) DeleteMetricDescriptor(ctx context.Context, req *monitoringpb.DeleteMetricDescriptorRequest, opts ...gax.CallOption) error {
  240. ctx = insertMetadata(ctx, c.xGoogMetadata)
  241. opts = append(c.CallOptions.DeleteMetricDescriptor[0:len(c.CallOptions.DeleteMetricDescriptor):len(c.CallOptions.DeleteMetricDescriptor)], opts...)
  242. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  243. var err error
  244. _, err = c.metricClient.DeleteMetricDescriptor(ctx, req, settings.GRPC...)
  245. return err
  246. }, opts...)
  247. return err
  248. }
  249. // ListTimeSeries lists time series that match a filter. This method does not require a Stackdriver account.
  250. func (c *MetricClient) ListTimeSeries(ctx context.Context, req *monitoringpb.ListTimeSeriesRequest, opts ...gax.CallOption) *TimeSeriesIterator {
  251. ctx = insertMetadata(ctx, c.xGoogMetadata)
  252. opts = append(c.CallOptions.ListTimeSeries[0:len(c.CallOptions.ListTimeSeries):len(c.CallOptions.ListTimeSeries)], opts...)
  253. it := &TimeSeriesIterator{}
  254. it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.TimeSeries, string, error) {
  255. var resp *monitoringpb.ListTimeSeriesResponse
  256. req.PageToken = pageToken
  257. if pageSize > math.MaxInt32 {
  258. req.PageSize = math.MaxInt32
  259. } else {
  260. req.PageSize = int32(pageSize)
  261. }
  262. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  263. var err error
  264. resp, err = c.metricClient.ListTimeSeries(ctx, req, settings.GRPC...)
  265. return err
  266. }, opts...)
  267. if err != nil {
  268. return nil, "", err
  269. }
  270. return resp.TimeSeries, resp.NextPageToken, nil
  271. }
  272. fetch := func(pageSize int, pageToken string) (string, error) {
  273. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  274. if err != nil {
  275. return "", err
  276. }
  277. it.items = append(it.items, items...)
  278. return nextPageToken, nil
  279. }
  280. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  281. return it
  282. }
  283. // CreateTimeSeries creates or adds data to one or more time series.
  284. // The response is empty if all time series in the request were written.
  285. // If any time series could not be written, a corresponding failure message is
  286. // included in the error response.
  287. func (c *MetricClient) CreateTimeSeries(ctx context.Context, req *monitoringpb.CreateTimeSeriesRequest, opts ...gax.CallOption) error {
  288. ctx = insertMetadata(ctx, c.xGoogMetadata)
  289. opts = append(c.CallOptions.CreateTimeSeries[0:len(c.CallOptions.CreateTimeSeries):len(c.CallOptions.CreateTimeSeries)], opts...)
  290. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  291. var err error
  292. _, err = c.metricClient.CreateTimeSeries(ctx, req, settings.GRPC...)
  293. return err
  294. }, opts...)
  295. return err
  296. }
  297. // MetricDescriptorIterator manages a stream of *metricpb.MetricDescriptor.
  298. type MetricDescriptorIterator struct {
  299. items []*metricpb.MetricDescriptor
  300. pageInfo *iterator.PageInfo
  301. nextFunc func() error
  302. // InternalFetch is for use by the Google Cloud Libraries only.
  303. // It is not part of the stable interface of this package.
  304. //
  305. // InternalFetch returns results from a single call to the underlying RPC.
  306. // The number of results is no greater than pageSize.
  307. // If there are no more results, nextPageToken is empty and err is nil.
  308. InternalFetch func(pageSize int, pageToken string) (results []*metricpb.MetricDescriptor, nextPageToken string, err error)
  309. }
  310. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  311. func (it *MetricDescriptorIterator) PageInfo() *iterator.PageInfo {
  312. return it.pageInfo
  313. }
  314. // Next returns the next result. Its second return value is iterator.Done if there are no more
  315. // results. Once Next returns Done, all subsequent calls will return Done.
  316. func (it *MetricDescriptorIterator) Next() (*metricpb.MetricDescriptor, error) {
  317. var item *metricpb.MetricDescriptor
  318. if err := it.nextFunc(); err != nil {
  319. return item, err
  320. }
  321. item = it.items[0]
  322. it.items = it.items[1:]
  323. return item, nil
  324. }
  325. func (it *MetricDescriptorIterator) bufLen() int {
  326. return len(it.items)
  327. }
  328. func (it *MetricDescriptorIterator) takeBuf() interface{} {
  329. b := it.items
  330. it.items = nil
  331. return b
  332. }
  333. // MonitoredResourceDescriptorIterator manages a stream of *monitoredrespb.MonitoredResourceDescriptor.
  334. type MonitoredResourceDescriptorIterator struct {
  335. items []*monitoredrespb.MonitoredResourceDescriptor
  336. pageInfo *iterator.PageInfo
  337. nextFunc func() error
  338. // InternalFetch is for use by the Google Cloud Libraries only.
  339. // It is not part of the stable interface of this package.
  340. //
  341. // InternalFetch returns results from a single call to the underlying RPC.
  342. // The number of results is no greater than pageSize.
  343. // If there are no more results, nextPageToken is empty and err is nil.
  344. InternalFetch func(pageSize int, pageToken string) (results []*monitoredrespb.MonitoredResourceDescriptor, nextPageToken string, err error)
  345. }
  346. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  347. func (it *MonitoredResourceDescriptorIterator) PageInfo() *iterator.PageInfo {
  348. return it.pageInfo
  349. }
  350. // Next returns the next result. Its second return value is iterator.Done if there are no more
  351. // results. Once Next returns Done, all subsequent calls will return Done.
  352. func (it *MonitoredResourceDescriptorIterator) Next() (*monitoredrespb.MonitoredResourceDescriptor, error) {
  353. var item *monitoredrespb.MonitoredResourceDescriptor
  354. if err := it.nextFunc(); err != nil {
  355. return item, err
  356. }
  357. item = it.items[0]
  358. it.items = it.items[1:]
  359. return item, nil
  360. }
  361. func (it *MonitoredResourceDescriptorIterator) bufLen() int {
  362. return len(it.items)
  363. }
  364. func (it *MonitoredResourceDescriptorIterator) takeBuf() interface{} {
  365. b := it.items
  366. it.items = nil
  367. return b
  368. }
  369. // TimeSeriesIterator manages a stream of *monitoringpb.TimeSeries.
  370. type TimeSeriesIterator struct {
  371. items []*monitoringpb.TimeSeries
  372. pageInfo *iterator.PageInfo
  373. nextFunc func() error
  374. // InternalFetch is for use by the Google Cloud Libraries only.
  375. // It is not part of the stable interface of this package.
  376. //
  377. // InternalFetch returns results from a single call to the underlying RPC.
  378. // The number of results is no greater than pageSize.
  379. // If there are no more results, nextPageToken is empty and err is nil.
  380. InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.TimeSeries, nextPageToken string, err error)
  381. }
  382. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  383. func (it *TimeSeriesIterator) PageInfo() *iterator.PageInfo {
  384. return it.pageInfo
  385. }
  386. // Next returns the next result. Its second return value is iterator.Done if there are no more
  387. // results. Once Next returns Done, all subsequent calls will return Done.
  388. func (it *TimeSeriesIterator) Next() (*monitoringpb.TimeSeries, error) {
  389. var item *monitoringpb.TimeSeries
  390. if err := it.nextFunc(); err != nil {
  391. return item, err
  392. }
  393. item = it.items[0]
  394. it.items = it.items[1:]
  395. return item, nil
  396. }
  397. func (it *TimeSeriesIterator) bufLen() int {
  398. return len(it.items)
  399. }
  400. func (it *TimeSeriesIterator) takeBuf() interface{} {
  401. b := it.items
  402. it.items = nil
  403. return b
  404. }