metrics_client.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // Copyright 2021 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 protoc-gen-go_gapic. 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/option/internaloption"
  27. gtransport "google.golang.org/api/transport/grpc"
  28. loggingpb "google.golang.org/genproto/googleapis/logging/v2"
  29. "google.golang.org/grpc"
  30. "google.golang.org/grpc/codes"
  31. "google.golang.org/grpc/metadata"
  32. )
  33. var newMetricsClientHook clientHook
  34. // MetricsCallOptions contains the retry settings for each method of MetricsClient.
  35. type MetricsCallOptions struct {
  36. ListLogMetrics []gax.CallOption
  37. GetLogMetric []gax.CallOption
  38. CreateLogMetric []gax.CallOption
  39. UpdateLogMetric []gax.CallOption
  40. DeleteLogMetric []gax.CallOption
  41. }
  42. func defaultMetricsClientOptions() []option.ClientOption {
  43. return []option.ClientOption{
  44. internaloption.WithDefaultEndpoint("logging.googleapis.com:443"),
  45. internaloption.WithDefaultMTLSEndpoint("logging.mtls.googleapis.com:443"),
  46. internaloption.WithDefaultAudience("https://logging.googleapis.com/"),
  47. internaloption.WithDefaultScopes(DefaultAuthScopes()...),
  48. option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
  49. option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
  50. grpc.MaxCallRecvMsgSize(math.MaxInt32))),
  51. }
  52. }
  53. func defaultMetricsCallOptions() *MetricsCallOptions {
  54. return &MetricsCallOptions{
  55. ListLogMetrics: []gax.CallOption{
  56. gax.WithRetry(func() gax.Retryer {
  57. return gax.OnCodes([]codes.Code{
  58. codes.DeadlineExceeded,
  59. codes.Internal,
  60. codes.Unavailable,
  61. }, gax.Backoff{
  62. Initial: 100 * time.Millisecond,
  63. Max: 60000 * time.Millisecond,
  64. Multiplier: 1.30,
  65. })
  66. }),
  67. },
  68. GetLogMetric: []gax.CallOption{
  69. gax.WithRetry(func() gax.Retryer {
  70. return gax.OnCodes([]codes.Code{
  71. codes.DeadlineExceeded,
  72. codes.Internal,
  73. codes.Unavailable,
  74. }, gax.Backoff{
  75. Initial: 100 * time.Millisecond,
  76. Max: 60000 * time.Millisecond,
  77. Multiplier: 1.30,
  78. })
  79. }),
  80. },
  81. CreateLogMetric: []gax.CallOption{},
  82. UpdateLogMetric: []gax.CallOption{
  83. gax.WithRetry(func() gax.Retryer {
  84. return gax.OnCodes([]codes.Code{
  85. codes.DeadlineExceeded,
  86. codes.Internal,
  87. codes.Unavailable,
  88. }, gax.Backoff{
  89. Initial: 100 * time.Millisecond,
  90. Max: 60000 * time.Millisecond,
  91. Multiplier: 1.30,
  92. })
  93. }),
  94. },
  95. DeleteLogMetric: []gax.CallOption{
  96. gax.WithRetry(func() gax.Retryer {
  97. return gax.OnCodes([]codes.Code{
  98. codes.DeadlineExceeded,
  99. codes.Internal,
  100. codes.Unavailable,
  101. }, gax.Backoff{
  102. Initial: 100 * time.Millisecond,
  103. Max: 60000 * time.Millisecond,
  104. Multiplier: 1.30,
  105. })
  106. }),
  107. },
  108. }
  109. }
  110. // MetricsClient is a client for interacting with Cloud Logging API.
  111. //
  112. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  113. type MetricsClient struct {
  114. // Connection pool of gRPC connections to the service.
  115. connPool gtransport.ConnPool
  116. // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
  117. disableDeadlines bool
  118. // The gRPC API client.
  119. metricsClient loggingpb.MetricsServiceV2Client
  120. // The call options for this service.
  121. CallOptions *MetricsCallOptions
  122. // The x-goog-* metadata to be sent with each request.
  123. xGoogMetadata metadata.MD
  124. }
  125. // NewMetricsClient creates a new metrics service v2 client.
  126. //
  127. // Service for configuring logs-based metrics.
  128. func NewMetricsClient(ctx context.Context, opts ...option.ClientOption) (*MetricsClient, error) {
  129. clientOpts := defaultMetricsClientOptions()
  130. if newMetricsClientHook != nil {
  131. hookOpts, err := newMetricsClientHook(ctx, clientHookParams{})
  132. if err != nil {
  133. return nil, err
  134. }
  135. clientOpts = append(clientOpts, hookOpts...)
  136. }
  137. disableDeadlines, err := checkDisableDeadlines()
  138. if err != nil {
  139. return nil, err
  140. }
  141. connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
  142. if err != nil {
  143. return nil, err
  144. }
  145. c := &MetricsClient{
  146. connPool: connPool,
  147. disableDeadlines: disableDeadlines,
  148. CallOptions: defaultMetricsCallOptions(),
  149. metricsClient: loggingpb.NewMetricsServiceV2Client(connPool),
  150. }
  151. c.setGoogleClientInfo()
  152. return c, nil
  153. }
  154. // Connection returns a connection to the API service.
  155. //
  156. // Deprecated.
  157. func (c *MetricsClient) Connection() *grpc.ClientConn {
  158. return c.connPool.Conn()
  159. }
  160. // Close closes the connection to the API service. The user should invoke this when
  161. // the client is no longer required.
  162. func (c *MetricsClient) Close() error {
  163. return c.connPool.Close()
  164. }
  165. // setGoogleClientInfo sets the name and version of the application in
  166. // the `x-goog-api-client` header passed on each request. Intended for
  167. // use by Google-written clients.
  168. func (c *MetricsClient) setGoogleClientInfo(keyval ...string) {
  169. kv := append([]string{"gl-go", versionGo()}, keyval...)
  170. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  171. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  172. }
  173. // ListLogMetrics lists logs-based metrics.
  174. func (c *MetricsClient) ListLogMetrics(ctx context.Context, req *loggingpb.ListLogMetricsRequest, opts ...gax.CallOption) *LogMetricIterator {
  175. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
  176. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  177. opts = append(c.CallOptions.ListLogMetrics[0:len(c.CallOptions.ListLogMetrics):len(c.CallOptions.ListLogMetrics)], opts...)
  178. it := &LogMetricIterator{}
  179. req = proto.Clone(req).(*loggingpb.ListLogMetricsRequest)
  180. it.InternalFetch = func(pageSize int, pageToken string) ([]*loggingpb.LogMetric, string, error) {
  181. var resp *loggingpb.ListLogMetricsResponse
  182. req.PageToken = pageToken
  183. if pageSize > math.MaxInt32 {
  184. req.PageSize = math.MaxInt32
  185. } else {
  186. req.PageSize = int32(pageSize)
  187. }
  188. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  189. var err error
  190. resp, err = c.metricsClient.ListLogMetrics(ctx, req, settings.GRPC...)
  191. return err
  192. }, opts...)
  193. if err != nil {
  194. return nil, "", err
  195. }
  196. it.Response = resp
  197. return resp.GetMetrics(), resp.GetNextPageToken(), nil
  198. }
  199. fetch := func(pageSize int, pageToken string) (string, error) {
  200. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  201. if err != nil {
  202. return "", err
  203. }
  204. it.items = append(it.items, items...)
  205. return nextPageToken, nil
  206. }
  207. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  208. it.pageInfo.MaxSize = int(req.GetPageSize())
  209. it.pageInfo.Token = req.GetPageToken()
  210. return it
  211. }
  212. // GetLogMetric gets a logs-based metric.
  213. func (c *MetricsClient) GetLogMetric(ctx context.Context, req *loggingpb.GetLogMetricRequest, opts ...gax.CallOption) (*loggingpb.LogMetric, error) {
  214. if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
  215. cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond)
  216. defer cancel()
  217. ctx = cctx
  218. }
  219. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "metric_name", url.QueryEscape(req.GetMetricName())))
  220. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  221. opts = append(c.CallOptions.GetLogMetric[0:len(c.CallOptions.GetLogMetric):len(c.CallOptions.GetLogMetric)], opts...)
  222. var resp *loggingpb.LogMetric
  223. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  224. var err error
  225. resp, err = c.metricsClient.GetLogMetric(ctx, req, settings.GRPC...)
  226. return err
  227. }, opts...)
  228. if err != nil {
  229. return nil, err
  230. }
  231. return resp, nil
  232. }
  233. // CreateLogMetric creates a logs-based metric.
  234. func (c *MetricsClient) CreateLogMetric(ctx context.Context, req *loggingpb.CreateLogMetricRequest, opts ...gax.CallOption) (*loggingpb.LogMetric, error) {
  235. if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
  236. cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond)
  237. defer cancel()
  238. ctx = cctx
  239. }
  240. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
  241. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  242. opts = append(c.CallOptions.CreateLogMetric[0:len(c.CallOptions.CreateLogMetric):len(c.CallOptions.CreateLogMetric)], opts...)
  243. var resp *loggingpb.LogMetric
  244. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  245. var err error
  246. resp, err = c.metricsClient.CreateLogMetric(ctx, req, settings.GRPC...)
  247. return err
  248. }, opts...)
  249. if err != nil {
  250. return nil, err
  251. }
  252. return resp, nil
  253. }
  254. // UpdateLogMetric creates or updates a logs-based metric.
  255. func (c *MetricsClient) UpdateLogMetric(ctx context.Context, req *loggingpb.UpdateLogMetricRequest, opts ...gax.CallOption) (*loggingpb.LogMetric, error) {
  256. if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
  257. cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond)
  258. defer cancel()
  259. ctx = cctx
  260. }
  261. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "metric_name", url.QueryEscape(req.GetMetricName())))
  262. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  263. opts = append(c.CallOptions.UpdateLogMetric[0:len(c.CallOptions.UpdateLogMetric):len(c.CallOptions.UpdateLogMetric)], opts...)
  264. var resp *loggingpb.LogMetric
  265. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  266. var err error
  267. resp, err = c.metricsClient.UpdateLogMetric(ctx, req, settings.GRPC...)
  268. return err
  269. }, opts...)
  270. if err != nil {
  271. return nil, err
  272. }
  273. return resp, nil
  274. }
  275. // DeleteLogMetric deletes a logs-based metric.
  276. func (c *MetricsClient) DeleteLogMetric(ctx context.Context, req *loggingpb.DeleteLogMetricRequest, opts ...gax.CallOption) error {
  277. if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
  278. cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond)
  279. defer cancel()
  280. ctx = cctx
  281. }
  282. md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "metric_name", url.QueryEscape(req.GetMetricName())))
  283. ctx = insertMetadata(ctx, c.xGoogMetadata, md)
  284. opts = append(c.CallOptions.DeleteLogMetric[0:len(c.CallOptions.DeleteLogMetric):len(c.CallOptions.DeleteLogMetric)], opts...)
  285. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  286. var err error
  287. _, err = c.metricsClient.DeleteLogMetric(ctx, req, settings.GRPC...)
  288. return err
  289. }, opts...)
  290. return err
  291. }
  292. // LogMetricIterator manages a stream of *loggingpb.LogMetric.
  293. type LogMetricIterator struct {
  294. items []*loggingpb.LogMetric
  295. pageInfo *iterator.PageInfo
  296. nextFunc func() error
  297. // Response is the raw response for the current page.
  298. // It must be cast to the RPC response type.
  299. // Calling Next() or InternalFetch() updates this value.
  300. Response interface{}
  301. // InternalFetch is for use by the Google Cloud Libraries only.
  302. // It is not part of the stable interface of this package.
  303. //
  304. // InternalFetch returns results from a single call to the underlying RPC.
  305. // The number of results is no greater than pageSize.
  306. // If there are no more results, nextPageToken is empty and err is nil.
  307. InternalFetch func(pageSize int, pageToken string) (results []*loggingpb.LogMetric, nextPageToken string, err error)
  308. }
  309. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  310. func (it *LogMetricIterator) PageInfo() *iterator.PageInfo {
  311. return it.pageInfo
  312. }
  313. // Next returns the next result. Its second return value is iterator.Done if there are no more
  314. // results. Once Next returns Done, all subsequent calls will return Done.
  315. func (it *LogMetricIterator) Next() (*loggingpb.LogMetric, error) {
  316. var item *loggingpb.LogMetric
  317. if err := it.nextFunc(); err != nil {
  318. return item, err
  319. }
  320. item = it.items[0]
  321. it.items = it.items[1:]
  322. return item, nil
  323. }
  324. func (it *LogMetricIterator) bufLen() int {
  325. return len(it.items)
  326. }
  327. func (it *LogMetricIterator) takeBuf() interface{} {
  328. b := it.items
  329. it.items = nil
  330. return b
  331. }