metrics_client.go 9.4 KB

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