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.
 
 
 

238 lines
8.0 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 trace
  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. 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", version.Go()}, keyval...)
  110. kv = append(kv, "gapic", version.Repo, "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. it.InternalFetch = func(pageSize int, pageToken string) ([]*cloudtracepb.Trace, string, error) {
  149. var resp *cloudtracepb.ListTracesResponse
  150. req.PageToken = pageToken
  151. if pageSize > math.MaxInt32 {
  152. req.PageSize = math.MaxInt32
  153. } else {
  154. req.PageSize = int32(pageSize)
  155. }
  156. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  157. var err error
  158. resp, err = c.client.ListTraces(ctx, req, settings.GRPC...)
  159. return err
  160. }, opts...)
  161. if err != nil {
  162. return nil, "", err
  163. }
  164. return resp.Traces, resp.NextPageToken, nil
  165. }
  166. fetch := func(pageSize int, pageToken string) (string, error) {
  167. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  168. if err != nil {
  169. return "", err
  170. }
  171. it.items = append(it.items, items...)
  172. return nextPageToken, nil
  173. }
  174. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  175. return it
  176. }
  177. // TraceIterator manages a stream of *cloudtracepb.Trace.
  178. type TraceIterator struct {
  179. items []*cloudtracepb.Trace
  180. pageInfo *iterator.PageInfo
  181. nextFunc func() error
  182. // InternalFetch is for use by the Google Cloud Libraries only.
  183. // It is not part of the stable interface of this package.
  184. //
  185. // InternalFetch returns results from a single call to the underlying RPC.
  186. // The number of results is no greater than pageSize.
  187. // If there are no more results, nextPageToken is empty and err is nil.
  188. InternalFetch func(pageSize int, pageToken string) (results []*cloudtracepb.Trace, nextPageToken string, err error)
  189. }
  190. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  191. func (it *TraceIterator) PageInfo() *iterator.PageInfo {
  192. return it.pageInfo
  193. }
  194. // Next returns the next result. Its second return value is iterator.Done if there are no more
  195. // results. Once Next returns Done, all subsequent calls will return Done.
  196. func (it *TraceIterator) Next() (*cloudtracepb.Trace, error) {
  197. var item *cloudtracepb.Trace
  198. if err := it.nextFunc(); err != nil {
  199. return item, err
  200. }
  201. item = it.items[0]
  202. it.items = it.items[1:]
  203. return item, nil
  204. }
  205. func (it *TraceIterator) bufLen() int {
  206. return len(it.items)
  207. }
  208. func (it *TraceIterator) takeBuf() interface{} {
  209. b := it.items
  210. it.items = nil
  211. return b
  212. }