metrics_client.go 9.9 KB

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