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.
 
 
 

269 lines
9.2 KiB

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