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.
 
 
 

240 lines
8.0 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 trace
  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. cloudtracepb "google.golang.org/genproto/googleapis/devtools/cloudtrace/v1"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. "google.golang.org/grpc/metadata"
  29. )
  30. // CallOptions contains the retry settings for each method of Client.
  31. type CallOptions struct {
  32. PatchTraces []gax.CallOption
  33. GetTrace []gax.CallOption
  34. ListTraces []gax.CallOption
  35. }
  36. func defaultClientOptions() []option.ClientOption {
  37. return []option.ClientOption{
  38. option.WithEndpoint("cloudtrace.googleapis.com:443"),
  39. option.WithScopes(DefaultAuthScopes()...),
  40. }
  41. }
  42. func defaultCallOptions() *CallOptions {
  43. retry := map[[2]string][]gax.CallOption{
  44. {"default", "idempotent"}: {
  45. gax.WithRetry(func() gax.Retryer {
  46. return gax.OnCodes([]codes.Code{
  47. codes.DeadlineExceeded,
  48. codes.Unavailable,
  49. }, gax.Backoff{
  50. Initial: 100 * time.Millisecond,
  51. Max: 1000 * time.Millisecond,
  52. Multiplier: 1.2,
  53. })
  54. }),
  55. },
  56. }
  57. return &CallOptions{
  58. PatchTraces: retry[[2]string{"default", "idempotent"}],
  59. GetTrace: retry[[2]string{"default", "idempotent"}],
  60. ListTraces: retry[[2]string{"default", "idempotent"}],
  61. }
  62. }
  63. // Client is a client for interacting with Stackdriver Trace API.
  64. //
  65. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  66. type Client struct {
  67. // The connection to the service.
  68. conn *grpc.ClientConn
  69. // The gRPC API client.
  70. client cloudtracepb.TraceServiceClient
  71. // The call options for this service.
  72. CallOptions *CallOptions
  73. // The x-goog-* metadata to be sent with each request.
  74. xGoogMetadata metadata.MD
  75. }
  76. // NewClient creates a new trace service client.
  77. //
  78. // This file describes an API for collecting and viewing traces and spans
  79. // within a trace. A Trace is a collection of spans corresponding to a single
  80. // operation or set of operations for an application. A span is an individual
  81. // timed event which forms a node of the trace tree. Spans for a single trace
  82. // may span multiple services.
  83. func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
  84. conn, err := transport.DialGRPC(ctx, append(defaultClientOptions(), opts...)...)
  85. if err != nil {
  86. return nil, err
  87. }
  88. c := &Client{
  89. conn: conn,
  90. CallOptions: defaultCallOptions(),
  91. client: cloudtracepb.NewTraceServiceClient(conn),
  92. }
  93. c.SetGoogleClientInfo()
  94. return c, nil
  95. }
  96. // Connection returns the client's connection to the API service.
  97. func (c *Client) Connection() *grpc.ClientConn {
  98. return c.conn
  99. }
  100. // Close closes the connection to the API service. The user should invoke this when
  101. // the client is no longer required.
  102. func (c *Client) Close() error {
  103. return c.conn.Close()
  104. }
  105. // SetGoogleClientInfo sets the name and version of the application in
  106. // the `x-goog-api-client` header passed on each request. Intended for
  107. // use by Google-written clients.
  108. func (c *Client) SetGoogleClientInfo(keyval ...string) {
  109. kv := append([]string{"gl-go", versionGo()}, keyval...)
  110. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  111. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  112. }
  113. // PatchTraces sends new traces to Stackdriver Trace or updates existing traces. If the ID
  114. // of a trace that you send matches that of an existing trace, any fields
  115. // in the existing trace and its spans are overwritten by the provided values,
  116. // and any new fields provided are merged with the existing trace data. If the
  117. // ID does not match, a new trace is created.
  118. func (c *Client) PatchTraces(ctx context.Context, req *cloudtracepb.PatchTracesRequest, opts ...gax.CallOption) error {
  119. ctx = insertMetadata(ctx, c.xGoogMetadata)
  120. opts = append(c.CallOptions.PatchTraces[0:len(c.CallOptions.PatchTraces):len(c.CallOptions.PatchTraces)], opts...)
  121. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  122. var err error
  123. _, err = c.client.PatchTraces(ctx, req, settings.GRPC...)
  124. return err
  125. }, opts...)
  126. return err
  127. }
  128. // GetTrace gets a single trace by its ID.
  129. func (c *Client) GetTrace(ctx context.Context, req *cloudtracepb.GetTraceRequest, opts ...gax.CallOption) (*cloudtracepb.Trace, error) {
  130. ctx = insertMetadata(ctx, c.xGoogMetadata)
  131. opts = append(c.CallOptions.GetTrace[0:len(c.CallOptions.GetTrace):len(c.CallOptions.GetTrace)], opts...)
  132. var resp *cloudtracepb.Trace
  133. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  134. var err error
  135. resp, err = c.client.GetTrace(ctx, req, settings.GRPC...)
  136. return err
  137. }, opts...)
  138. if err != nil {
  139. return nil, err
  140. }
  141. return resp, nil
  142. }
  143. // ListTraces returns of a list of traces that match the specified filter conditions.
  144. func (c *Client) ListTraces(ctx context.Context, req *cloudtracepb.ListTracesRequest, opts ...gax.CallOption) *TraceIterator {
  145. ctx = insertMetadata(ctx, c.xGoogMetadata)
  146. opts = append(c.CallOptions.ListTraces[0:len(c.CallOptions.ListTraces):len(c.CallOptions.ListTraces)], opts...)
  147. it := &TraceIterator{}
  148. req = proto.Clone(req).(*cloudtracepb.ListTracesRequest)
  149. it.InternalFetch = func(pageSize int, pageToken string) ([]*cloudtracepb.Trace, string, error) {
  150. var resp *cloudtracepb.ListTracesResponse
  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, settings gax.CallSettings) error {
  158. var err error
  159. resp, err = c.client.ListTraces(ctx, req, settings.GRPC...)
  160. return err
  161. }, opts...)
  162. if err != nil {
  163. return nil, "", err
  164. }
  165. return resp.Traces, 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. it.pageInfo.MaxSize = int(req.PageSize)
  177. return it
  178. }
  179. // TraceIterator manages a stream of *cloudtracepb.Trace.
  180. type TraceIterator struct {
  181. items []*cloudtracepb.Trace
  182. pageInfo *iterator.PageInfo
  183. nextFunc func() error
  184. // InternalFetch is for use by the Google Cloud Libraries only.
  185. // It is not part of the stable interface of this package.
  186. //
  187. // InternalFetch returns results from a single call to the underlying RPC.
  188. // The number of results is no greater than pageSize.
  189. // If there are no more results, nextPageToken is empty and err is nil.
  190. InternalFetch func(pageSize int, pageToken string) (results []*cloudtracepb.Trace, nextPageToken string, err error)
  191. }
  192. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  193. func (it *TraceIterator) PageInfo() *iterator.PageInfo {
  194. return it.pageInfo
  195. }
  196. // Next returns the next result. Its second return value is iterator.Done if there are no more
  197. // results. Once Next returns Done, all subsequent calls will return Done.
  198. func (it *TraceIterator) Next() (*cloudtracepb.Trace, error) {
  199. var item *cloudtracepb.Trace
  200. if err := it.nextFunc(); err != nil {
  201. return item, err
  202. }
  203. item = it.items[0]
  204. it.items = it.items[1:]
  205. return item, nil
  206. }
  207. func (it *TraceIterator) bufLen() int {
  208. return len(it.items)
  209. }
  210. func (it *TraceIterator) takeBuf() interface{} {
  211. b := it.items
  212. it.items = nil
  213. return b
  214. }