metrics_client.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 logging
  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. loggingpb "google.golang.org/genproto/googleapis/logging/v2"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. "google.golang.org/grpc/metadata"
  29. )
  30. // MetricsCallOptions contains the retry settings for each method of MetricsClient.
  31. type MetricsCallOptions struct {
  32. ListLogMetrics []gax.CallOption
  33. GetLogMetric []gax.CallOption
  34. CreateLogMetric []gax.CallOption
  35. UpdateLogMetric []gax.CallOption
  36. DeleteLogMetric []gax.CallOption
  37. }
  38. func defaultMetricsClientOptions() []option.ClientOption {
  39. return []option.ClientOption{
  40. option.WithEndpoint("logging.googleapis.com:443"),
  41. option.WithScopes(DefaultAuthScopes()...),
  42. }
  43. }
  44. func defaultMetricsCallOptions() *MetricsCallOptions {
  45. retry := map[[2]string][]gax.CallOption{
  46. {"default", "idempotent"}: {
  47. gax.WithRetry(func() gax.Retryer {
  48. return gax.OnCodes([]codes.Code{
  49. codes.DeadlineExceeded,
  50. codes.Internal,
  51. codes.Unavailable,
  52. }, gax.Backoff{
  53. Initial: 100 * time.Millisecond,
  54. Max: 1000 * time.Millisecond,
  55. Multiplier: 1.2,
  56. })
  57. }),
  58. },
  59. }
  60. return &MetricsCallOptions{
  61. ListLogMetrics: retry[[2]string{"default", "idempotent"}],
  62. GetLogMetric: retry[[2]string{"default", "idempotent"}],
  63. CreateLogMetric: retry[[2]string{"default", "non_idempotent"}],
  64. UpdateLogMetric: retry[[2]string{"default", "non_idempotent"}],
  65. DeleteLogMetric: retry[[2]string{"default", "idempotent"}],
  66. }
  67. }
  68. // MetricsClient is a client for interacting with Stackdriver Logging API.
  69. type MetricsClient struct {
  70. // The connection to the service.
  71. conn *grpc.ClientConn
  72. // The gRPC API client.
  73. metricsClient loggingpb.MetricsServiceV2Client
  74. // The call options for this service.
  75. CallOptions *MetricsCallOptions
  76. // The x-goog-* metadata to be sent with each request.
  77. xGoogMetadata metadata.MD
  78. }
  79. // NewMetricsClient creates a new metrics service v2 client.
  80. //
  81. // Service for configuring logs-based metrics.
  82. func NewMetricsClient(ctx context.Context, opts ...option.ClientOption) (*MetricsClient, error) {
  83. conn, err := transport.DialGRPC(ctx, append(defaultMetricsClientOptions(), opts...)...)
  84. if err != nil {
  85. return nil, err
  86. }
  87. c := &MetricsClient{
  88. conn: conn,
  89. CallOptions: defaultMetricsCallOptions(),
  90. metricsClient: loggingpb.NewMetricsServiceV2Client(conn),
  91. }
  92. c.SetGoogleClientInfo()
  93. return c, nil
  94. }
  95. // Connection returns the client's connection to the API service.
  96. func (c *MetricsClient) Connection() *grpc.ClientConn {
  97. return c.conn
  98. }
  99. // Close closes the connection to the API service. The user should invoke this when
  100. // the client is no longer required.
  101. func (c *MetricsClient) Close() error {
  102. return c.conn.Close()
  103. }
  104. // SetGoogleClientInfo sets the name and version of the application in
  105. // the `x-goog-api-client` header passed on each request. Intended for
  106. // use by Google-written clients.
  107. func (c *MetricsClient) SetGoogleClientInfo(keyval ...string) {
  108. kv := append([]string{"gl-go", version.Go()}, keyval...)
  109. kv = append(kv, "gapic", version.Repo, "gax", gax.Version, "grpc", grpc.Version)
  110. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  111. }
  112. // ListLogMetrics lists logs-based metrics.
  113. func (c *MetricsClient) ListLogMetrics(ctx context.Context, req *loggingpb.ListLogMetricsRequest, opts ...gax.CallOption) *LogMetricIterator {
  114. ctx = insertMetadata(ctx, c.xGoogMetadata)
  115. opts = append(c.CallOptions.ListLogMetrics[0:len(c.CallOptions.ListLogMetrics):len(c.CallOptions.ListLogMetrics)], opts...)
  116. it := &LogMetricIterator{}
  117. it.InternalFetch = func(pageSize int, pageToken string) ([]*loggingpb.LogMetric, string, error) {
  118. var resp *loggingpb.ListLogMetricsResponse
  119. req.PageToken = pageToken
  120. if pageSize > math.MaxInt32 {
  121. req.PageSize = math.MaxInt32
  122. } else {
  123. req.PageSize = int32(pageSize)
  124. }
  125. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  126. var err error
  127. resp, err = c.metricsClient.ListLogMetrics(ctx, req, settings.GRPC...)
  128. return err
  129. }, opts...)
  130. if err != nil {
  131. return nil, "", err
  132. }
  133. return resp.Metrics, resp.NextPageToken, nil
  134. }
  135. fetch := func(pageSize int, pageToken string) (string, error) {
  136. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  137. if err != nil {
  138. return "", err
  139. }
  140. it.items = append(it.items, items...)
  141. return nextPageToken, nil
  142. }
  143. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  144. return it
  145. }
  146. // GetLogMetric gets a logs-based metric.
  147. func (c *MetricsClient) GetLogMetric(ctx context.Context, req *loggingpb.GetLogMetricRequest, opts ...gax.CallOption) (*loggingpb.LogMetric, error) {
  148. ctx = insertMetadata(ctx, c.xGoogMetadata)
  149. opts = append(c.CallOptions.GetLogMetric[0:len(c.CallOptions.GetLogMetric):len(c.CallOptions.GetLogMetric)], opts...)
  150. var resp *loggingpb.LogMetric
  151. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  152. var err error
  153. resp, err = c.metricsClient.GetLogMetric(ctx, req, settings.GRPC...)
  154. return err
  155. }, opts...)
  156. if err != nil {
  157. return nil, err
  158. }
  159. return resp, nil
  160. }
  161. // CreateLogMetric creates a logs-based metric.
  162. func (c *MetricsClient) CreateLogMetric(ctx context.Context, req *loggingpb.CreateLogMetricRequest, opts ...gax.CallOption) (*loggingpb.LogMetric, error) {
  163. ctx = insertMetadata(ctx, c.xGoogMetadata)
  164. opts = append(c.CallOptions.CreateLogMetric[0:len(c.CallOptions.CreateLogMetric):len(c.CallOptions.CreateLogMetric)], opts...)
  165. var resp *loggingpb.LogMetric
  166. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  167. var err error
  168. resp, err = c.metricsClient.CreateLogMetric(ctx, req, settings.GRPC...)
  169. return err
  170. }, opts...)
  171. if err != nil {
  172. return nil, err
  173. }
  174. return resp, nil
  175. }
  176. // UpdateLogMetric creates or updates a logs-based metric.
  177. func (c *MetricsClient) UpdateLogMetric(ctx context.Context, req *loggingpb.UpdateLogMetricRequest, opts ...gax.CallOption) (*loggingpb.LogMetric, error) {
  178. ctx = insertMetadata(ctx, c.xGoogMetadata)
  179. opts = append(c.CallOptions.UpdateLogMetric[0:len(c.CallOptions.UpdateLogMetric):len(c.CallOptions.UpdateLogMetric)], opts...)
  180. var resp *loggingpb.LogMetric
  181. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  182. var err error
  183. resp, err = c.metricsClient.UpdateLogMetric(ctx, req, settings.GRPC...)
  184. return err
  185. }, opts...)
  186. if err != nil {
  187. return nil, err
  188. }
  189. return resp, nil
  190. }
  191. // DeleteLogMetric deletes a logs-based metric.
  192. func (c *MetricsClient) DeleteLogMetric(ctx context.Context, req *loggingpb.DeleteLogMetricRequest, opts ...gax.CallOption) error {
  193. ctx = insertMetadata(ctx, c.xGoogMetadata)
  194. opts = append(c.CallOptions.DeleteLogMetric[0:len(c.CallOptions.DeleteLogMetric):len(c.CallOptions.DeleteLogMetric)], opts...)
  195. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  196. var err error
  197. _, err = c.metricsClient.DeleteLogMetric(ctx, req, settings.GRPC...)
  198. return err
  199. }, opts...)
  200. return err
  201. }
  202. // LogMetricIterator manages a stream of *loggingpb.LogMetric.
  203. type LogMetricIterator struct {
  204. items []*loggingpb.LogMetric
  205. pageInfo *iterator.PageInfo
  206. nextFunc func() error
  207. // InternalFetch is for use by the Google Cloud Libraries only.
  208. // It is not part of the stable interface of this package.
  209. //
  210. // InternalFetch returns results from a single call to the underlying RPC.
  211. // The number of results is no greater than pageSize.
  212. // If there are no more results, nextPageToken is empty and err is nil.
  213. InternalFetch func(pageSize int, pageToken string) (results []*loggingpb.LogMetric, nextPageToken string, err error)
  214. }
  215. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  216. func (it *LogMetricIterator) PageInfo() *iterator.PageInfo {
  217. return it.pageInfo
  218. }
  219. // Next returns the next result. Its second return value is iterator.Done if there are no more
  220. // results. Once Next returns Done, all subsequent calls will return Done.
  221. func (it *LogMetricIterator) Next() (*loggingpb.LogMetric, error) {
  222. var item *loggingpb.LogMetric
  223. if err := it.nextFunc(); err != nil {
  224. return item, err
  225. }
  226. item = it.items[0]
  227. it.items = it.items[1:]
  228. return item, nil
  229. }
  230. func (it *LogMetricIterator) bufLen() int {
  231. return len(it.items)
  232. }
  233. func (it *LogMetricIterator) takeBuf() interface{} {
  234. b := it.items
  235. it.items = nil
  236. return b
  237. }