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.
 
 
 

267 regels
9.1 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 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. //
  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", version.Go()}, keyval...)
  111. kv = append(kv, "gapic", version.Repo, "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. it.InternalFetch = func(pageSize int, pageToken string) ([]*loggingpb.LogMetric, string, error) {
  120. var resp *loggingpb.ListLogMetricsResponse
  121. req.PageToken = pageToken
  122. if pageSize > math.MaxInt32 {
  123. req.PageSize = math.MaxInt32
  124. } else {
  125. req.PageSize = int32(pageSize)
  126. }
  127. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  128. var err error
  129. resp, err = c.metricsClient.ListLogMetrics(ctx, req, settings.GRPC...)
  130. return err
  131. }, opts...)
  132. if err != nil {
  133. return nil, "", err
  134. }
  135. return resp.Metrics, resp.NextPageToken, nil
  136. }
  137. fetch := func(pageSize int, pageToken string) (string, error) {
  138. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  139. if err != nil {
  140. return "", err
  141. }
  142. it.items = append(it.items, items...)
  143. return nextPageToken, nil
  144. }
  145. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  146. return it
  147. }
  148. // GetLogMetric gets a logs-based metric.
  149. func (c *MetricsClient) GetLogMetric(ctx context.Context, req *loggingpb.GetLogMetricRequest, opts ...gax.CallOption) (*loggingpb.LogMetric, error) {
  150. ctx = insertMetadata(ctx, c.xGoogMetadata)
  151. opts = append(c.CallOptions.GetLogMetric[0:len(c.CallOptions.GetLogMetric):len(c.CallOptions.GetLogMetric)], opts...)
  152. var resp *loggingpb.LogMetric
  153. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  154. var err error
  155. resp, err = c.metricsClient.GetLogMetric(ctx, req, settings.GRPC...)
  156. return err
  157. }, opts...)
  158. if err != nil {
  159. return nil, err
  160. }
  161. return resp, nil
  162. }
  163. // CreateLogMetric creates a logs-based metric.
  164. func (c *MetricsClient) CreateLogMetric(ctx context.Context, req *loggingpb.CreateLogMetricRequest, opts ...gax.CallOption) (*loggingpb.LogMetric, error) {
  165. ctx = insertMetadata(ctx, c.xGoogMetadata)
  166. opts = append(c.CallOptions.CreateLogMetric[0:len(c.CallOptions.CreateLogMetric):len(c.CallOptions.CreateLogMetric)], opts...)
  167. var resp *loggingpb.LogMetric
  168. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  169. var err error
  170. resp, err = c.metricsClient.CreateLogMetric(ctx, req, settings.GRPC...)
  171. return err
  172. }, opts...)
  173. if err != nil {
  174. return nil, err
  175. }
  176. return resp, nil
  177. }
  178. // UpdateLogMetric creates or updates a logs-based metric.
  179. func (c *MetricsClient) UpdateLogMetric(ctx context.Context, req *loggingpb.UpdateLogMetricRequest, opts ...gax.CallOption) (*loggingpb.LogMetric, error) {
  180. ctx = insertMetadata(ctx, c.xGoogMetadata)
  181. opts = append(c.CallOptions.UpdateLogMetric[0:len(c.CallOptions.UpdateLogMetric):len(c.CallOptions.UpdateLogMetric)], opts...)
  182. var resp *loggingpb.LogMetric
  183. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  184. var err error
  185. resp, err = c.metricsClient.UpdateLogMetric(ctx, req, settings.GRPC...)
  186. return err
  187. }, opts...)
  188. if err != nil {
  189. return nil, err
  190. }
  191. return resp, nil
  192. }
  193. // DeleteLogMetric deletes a logs-based metric.
  194. func (c *MetricsClient) DeleteLogMetric(ctx context.Context, req *loggingpb.DeleteLogMetricRequest, opts ...gax.CallOption) error {
  195. ctx = insertMetadata(ctx, c.xGoogMetadata)
  196. opts = append(c.CallOptions.DeleteLogMetric[0:len(c.CallOptions.DeleteLogMetric):len(c.CallOptions.DeleteLogMetric)], opts...)
  197. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  198. var err error
  199. _, err = c.metricsClient.DeleteLogMetric(ctx, req, settings.GRPC...)
  200. return err
  201. }, opts...)
  202. return err
  203. }
  204. // LogMetricIterator manages a stream of *loggingpb.LogMetric.
  205. type LogMetricIterator struct {
  206. items []*loggingpb.LogMetric
  207. pageInfo *iterator.PageInfo
  208. nextFunc func() error
  209. // InternalFetch is for use by the Google Cloud Libraries only.
  210. // It is not part of the stable interface of this package.
  211. //
  212. // InternalFetch returns results from a single call to the underlying RPC.
  213. // The number of results is no greater than pageSize.
  214. // If there are no more results, nextPageToken is empty and err is nil.
  215. InternalFetch func(pageSize int, pageToken string) (results []*loggingpb.LogMetric, nextPageToken string, err error)
  216. }
  217. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  218. func (it *LogMetricIterator) PageInfo() *iterator.PageInfo {
  219. return it.pageInfo
  220. }
  221. // Next returns the next result. Its second return value is iterator.Done if there are no more
  222. // results. Once Next returns Done, all subsequent calls will return Done.
  223. func (it *LogMetricIterator) Next() (*loggingpb.LogMetric, error) {
  224. var item *loggingpb.LogMetric
  225. if err := it.nextFunc(); err != nil {
  226. return item, err
  227. }
  228. item = it.items[0]
  229. it.items = it.items[1:]
  230. return item, nil
  231. }
  232. func (it *LogMetricIterator) bufLen() int {
  233. return len(it.items)
  234. }
  235. func (it *LogMetricIterator) takeBuf() interface{} {
  236. b := it.items
  237. it.items = nil
  238. return b
  239. }