Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

416 rader
14 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. monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
  26. loggingpb "google.golang.org/genproto/googleapis/logging/v2"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/codes"
  29. "google.golang.org/grpc/metadata"
  30. )
  31. // CallOptions contains the retry settings for each method of Client.
  32. type CallOptions struct {
  33. DeleteLog []gax.CallOption
  34. WriteLogEntries []gax.CallOption
  35. ListLogEntries []gax.CallOption
  36. ListMonitoredResourceDescriptors []gax.CallOption
  37. ListLogs []gax.CallOption
  38. }
  39. func defaultClientOptions() []option.ClientOption {
  40. return []option.ClientOption{
  41. option.WithEndpoint("logging.googleapis.com:443"),
  42. option.WithScopes(DefaultAuthScopes()...),
  43. }
  44. }
  45. func defaultCallOptions() *CallOptions {
  46. retry := map[[2]string][]gax.CallOption{
  47. {"default", "idempotent"}: {
  48. gax.WithRetry(func() gax.Retryer {
  49. return gax.OnCodes([]codes.Code{
  50. codes.DeadlineExceeded,
  51. codes.Internal,
  52. codes.Unavailable,
  53. }, gax.Backoff{
  54. Initial: 100 * time.Millisecond,
  55. Max: 1000 * time.Millisecond,
  56. Multiplier: 1.2,
  57. })
  58. }),
  59. },
  60. {"list", "idempotent"}: {
  61. gax.WithRetry(func() gax.Retryer {
  62. return gax.OnCodes([]codes.Code{
  63. codes.DeadlineExceeded,
  64. codes.Internal,
  65. codes.Unavailable,
  66. }, gax.Backoff{
  67. Initial: 100 * time.Millisecond,
  68. Max: 1000 * time.Millisecond,
  69. Multiplier: 1.2,
  70. })
  71. }),
  72. },
  73. }
  74. return &CallOptions{
  75. DeleteLog: retry[[2]string{"default", "idempotent"}],
  76. WriteLogEntries: retry[[2]string{"default", "non_idempotent"}],
  77. ListLogEntries: retry[[2]string{"list", "idempotent"}],
  78. ListMonitoredResourceDescriptors: retry[[2]string{"default", "idempotent"}],
  79. ListLogs: retry[[2]string{"default", "idempotent"}],
  80. }
  81. }
  82. // Client is a client for interacting with Stackdriver Logging API.
  83. //
  84. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  85. type Client struct {
  86. // The connection to the service.
  87. conn *grpc.ClientConn
  88. // The gRPC API client.
  89. client loggingpb.LoggingServiceV2Client
  90. // The call options for this service.
  91. CallOptions *CallOptions
  92. // The x-goog-* metadata to be sent with each request.
  93. xGoogMetadata metadata.MD
  94. }
  95. // NewClient creates a new logging service v2 client.
  96. //
  97. // Service for ingesting and querying logs.
  98. func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
  99. conn, err := transport.DialGRPC(ctx, append(defaultClientOptions(), opts...)...)
  100. if err != nil {
  101. return nil, err
  102. }
  103. c := &Client{
  104. conn: conn,
  105. CallOptions: defaultCallOptions(),
  106. client: loggingpb.NewLoggingServiceV2Client(conn),
  107. }
  108. c.SetGoogleClientInfo()
  109. return c, nil
  110. }
  111. // Connection returns the client's connection to the API service.
  112. func (c *Client) Connection() *grpc.ClientConn {
  113. return c.conn
  114. }
  115. // Close closes the connection to the API service. The user should invoke this when
  116. // the client is no longer required.
  117. func (c *Client) Close() error {
  118. return c.conn.Close()
  119. }
  120. // SetGoogleClientInfo sets the name and version of the application in
  121. // the `x-goog-api-client` header passed on each request. Intended for
  122. // use by Google-written clients.
  123. func (c *Client) SetGoogleClientInfo(keyval ...string) {
  124. kv := append([]string{"gl-go", version.Go()}, keyval...)
  125. kv = append(kv, "gapic", version.Repo, "gax", gax.Version, "grpc", grpc.Version)
  126. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  127. }
  128. // DeleteLog deletes all the log entries in a log.
  129. // The log reappears if it receives new entries.
  130. // Log entries written shortly before the delete operation might not be
  131. // deleted.
  132. func (c *Client) DeleteLog(ctx context.Context, req *loggingpb.DeleteLogRequest, opts ...gax.CallOption) error {
  133. ctx = insertMetadata(ctx, c.xGoogMetadata)
  134. opts = append(c.CallOptions.DeleteLog[0:len(c.CallOptions.DeleteLog):len(c.CallOptions.DeleteLog)], opts...)
  135. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  136. var err error
  137. _, err = c.client.DeleteLog(ctx, req, settings.GRPC...)
  138. return err
  139. }, opts...)
  140. return err
  141. }
  142. // WriteLogEntries ## Log entry resources
  143. //
  144. // Writes log entries to Stackdriver Logging. This API method is the
  145. // only way to send log entries to Stackdriver Logging. This method
  146. // is used, directly or indirectly, by the Stackdriver Logging agent
  147. // (fluentd) and all logging libraries configured to use Stackdriver
  148. // Logging.
  149. func (c *Client) WriteLogEntries(ctx context.Context, req *loggingpb.WriteLogEntriesRequest, opts ...gax.CallOption) (*loggingpb.WriteLogEntriesResponse, error) {
  150. ctx = insertMetadata(ctx, c.xGoogMetadata)
  151. opts = append(c.CallOptions.WriteLogEntries[0:len(c.CallOptions.WriteLogEntries):len(c.CallOptions.WriteLogEntries)], opts...)
  152. var resp *loggingpb.WriteLogEntriesResponse
  153. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  154. var err error
  155. resp, err = c.client.WriteLogEntries(ctx, req, settings.GRPC...)
  156. return err
  157. }, opts...)
  158. if err != nil {
  159. return nil, err
  160. }
  161. return resp, nil
  162. }
  163. // ListLogEntries lists log entries. Use this method to retrieve log entries from
  164. // Stackdriver Logging. For ways to export log entries, see
  165. // Exporting Logs (at /logging/docs/export).
  166. func (c *Client) ListLogEntries(ctx context.Context, req *loggingpb.ListLogEntriesRequest, opts ...gax.CallOption) *LogEntryIterator {
  167. ctx = insertMetadata(ctx, c.xGoogMetadata)
  168. opts = append(c.CallOptions.ListLogEntries[0:len(c.CallOptions.ListLogEntries):len(c.CallOptions.ListLogEntries)], opts...)
  169. it := &LogEntryIterator{}
  170. it.InternalFetch = func(pageSize int, pageToken string) ([]*loggingpb.LogEntry, string, error) {
  171. var resp *loggingpb.ListLogEntriesResponse
  172. req.PageToken = pageToken
  173. if pageSize > math.MaxInt32 {
  174. req.PageSize = math.MaxInt32
  175. } else {
  176. req.PageSize = int32(pageSize)
  177. }
  178. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  179. var err error
  180. resp, err = c.client.ListLogEntries(ctx, req, settings.GRPC...)
  181. return err
  182. }, opts...)
  183. if err != nil {
  184. return nil, "", err
  185. }
  186. return resp.Entries, resp.NextPageToken, nil
  187. }
  188. fetch := func(pageSize int, pageToken string) (string, error) {
  189. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  190. if err != nil {
  191. return "", err
  192. }
  193. it.items = append(it.items, items...)
  194. return nextPageToken, nil
  195. }
  196. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  197. return it
  198. }
  199. // ListMonitoredResourceDescriptors lists the descriptors for monitored resource types used by Stackdriver
  200. // Logging.
  201. func (c *Client) ListMonitoredResourceDescriptors(ctx context.Context, req *loggingpb.ListMonitoredResourceDescriptorsRequest, opts ...gax.CallOption) *MonitoredResourceDescriptorIterator {
  202. ctx = insertMetadata(ctx, c.xGoogMetadata)
  203. opts = append(c.CallOptions.ListMonitoredResourceDescriptors[0:len(c.CallOptions.ListMonitoredResourceDescriptors):len(c.CallOptions.ListMonitoredResourceDescriptors)], opts...)
  204. it := &MonitoredResourceDescriptorIterator{}
  205. it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResourceDescriptor, string, error) {
  206. var resp *loggingpb.ListMonitoredResourceDescriptorsResponse
  207. req.PageToken = pageToken
  208. if pageSize > math.MaxInt32 {
  209. req.PageSize = math.MaxInt32
  210. } else {
  211. req.PageSize = int32(pageSize)
  212. }
  213. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  214. var err error
  215. resp, err = c.client.ListMonitoredResourceDescriptors(ctx, req, settings.GRPC...)
  216. return err
  217. }, opts...)
  218. if err != nil {
  219. return nil, "", err
  220. }
  221. return resp.ResourceDescriptors, resp.NextPageToken, nil
  222. }
  223. fetch := func(pageSize int, pageToken string) (string, error) {
  224. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  225. if err != nil {
  226. return "", err
  227. }
  228. it.items = append(it.items, items...)
  229. return nextPageToken, nil
  230. }
  231. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  232. return it
  233. }
  234. // ListLogs lists the logs in projects, organizations, folders, or billing accounts.
  235. // Only logs that have entries are listed.
  236. func (c *Client) ListLogs(ctx context.Context, req *loggingpb.ListLogsRequest, opts ...gax.CallOption) *StringIterator {
  237. ctx = insertMetadata(ctx, c.xGoogMetadata)
  238. opts = append(c.CallOptions.ListLogs[0:len(c.CallOptions.ListLogs):len(c.CallOptions.ListLogs)], opts...)
  239. it := &StringIterator{}
  240. it.InternalFetch = func(pageSize int, pageToken string) ([]string, string, error) {
  241. var resp *loggingpb.ListLogsResponse
  242. req.PageToken = pageToken
  243. if pageSize > math.MaxInt32 {
  244. req.PageSize = math.MaxInt32
  245. } else {
  246. req.PageSize = int32(pageSize)
  247. }
  248. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  249. var err error
  250. resp, err = c.client.ListLogs(ctx, req, settings.GRPC...)
  251. return err
  252. }, opts...)
  253. if err != nil {
  254. return nil, "", err
  255. }
  256. return resp.LogNames, resp.NextPageToken, nil
  257. }
  258. fetch := func(pageSize int, pageToken string) (string, error) {
  259. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  260. if err != nil {
  261. return "", err
  262. }
  263. it.items = append(it.items, items...)
  264. return nextPageToken, nil
  265. }
  266. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  267. return it
  268. }
  269. // LogEntryIterator manages a stream of *loggingpb.LogEntry.
  270. type LogEntryIterator struct {
  271. items []*loggingpb.LogEntry
  272. pageInfo *iterator.PageInfo
  273. nextFunc func() error
  274. // InternalFetch is for use by the Google Cloud Libraries only.
  275. // It is not part of the stable interface of this package.
  276. //
  277. // InternalFetch returns results from a single call to the underlying RPC.
  278. // The number of results is no greater than pageSize.
  279. // If there are no more results, nextPageToken is empty and err is nil.
  280. InternalFetch func(pageSize int, pageToken string) (results []*loggingpb.LogEntry, nextPageToken string, err error)
  281. }
  282. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  283. func (it *LogEntryIterator) PageInfo() *iterator.PageInfo {
  284. return it.pageInfo
  285. }
  286. // Next returns the next result. Its second return value is iterator.Done if there are no more
  287. // results. Once Next returns Done, all subsequent calls will return Done.
  288. func (it *LogEntryIterator) Next() (*loggingpb.LogEntry, error) {
  289. var item *loggingpb.LogEntry
  290. if err := it.nextFunc(); err != nil {
  291. return item, err
  292. }
  293. item = it.items[0]
  294. it.items = it.items[1:]
  295. return item, nil
  296. }
  297. func (it *LogEntryIterator) bufLen() int {
  298. return len(it.items)
  299. }
  300. func (it *LogEntryIterator) takeBuf() interface{} {
  301. b := it.items
  302. it.items = nil
  303. return b
  304. }
  305. // MonitoredResourceDescriptorIterator manages a stream of *monitoredrespb.MonitoredResourceDescriptor.
  306. type MonitoredResourceDescriptorIterator struct {
  307. items []*monitoredrespb.MonitoredResourceDescriptor
  308. pageInfo *iterator.PageInfo
  309. nextFunc func() error
  310. // InternalFetch is for use by the Google Cloud Libraries only.
  311. // It is not part of the stable interface of this package.
  312. //
  313. // InternalFetch returns results from a single call to the underlying RPC.
  314. // The number of results is no greater than pageSize.
  315. // If there are no more results, nextPageToken is empty and err is nil.
  316. InternalFetch func(pageSize int, pageToken string) (results []*monitoredrespb.MonitoredResourceDescriptor, nextPageToken string, err error)
  317. }
  318. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  319. func (it *MonitoredResourceDescriptorIterator) PageInfo() *iterator.PageInfo {
  320. return it.pageInfo
  321. }
  322. // Next returns the next result. Its second return value is iterator.Done if there are no more
  323. // results. Once Next returns Done, all subsequent calls will return Done.
  324. func (it *MonitoredResourceDescriptorIterator) Next() (*monitoredrespb.MonitoredResourceDescriptor, error) {
  325. var item *monitoredrespb.MonitoredResourceDescriptor
  326. if err := it.nextFunc(); err != nil {
  327. return item, err
  328. }
  329. item = it.items[0]
  330. it.items = it.items[1:]
  331. return item, nil
  332. }
  333. func (it *MonitoredResourceDescriptorIterator) bufLen() int {
  334. return len(it.items)
  335. }
  336. func (it *MonitoredResourceDescriptorIterator) takeBuf() interface{} {
  337. b := it.items
  338. it.items = nil
  339. return b
  340. }
  341. // StringIterator manages a stream of string.
  342. type StringIterator struct {
  343. items []string
  344. pageInfo *iterator.PageInfo
  345. nextFunc func() error
  346. // InternalFetch is for use by the Google Cloud Libraries only.
  347. // It is not part of the stable interface of this package.
  348. //
  349. // InternalFetch returns results from a single call to the underlying RPC.
  350. // The number of results is no greater than pageSize.
  351. // If there are no more results, nextPageToken is empty and err is nil.
  352. InternalFetch func(pageSize int, pageToken string) (results []string, nextPageToken string, err error)
  353. }
  354. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  355. func (it *StringIterator) PageInfo() *iterator.PageInfo {
  356. return it.pageInfo
  357. }
  358. // Next returns the next result. Its second return value is iterator.Done if there are no more
  359. // results. Once Next returns Done, all subsequent calls will return Done.
  360. func (it *StringIterator) Next() (string, error) {
  361. var item string
  362. if err := it.nextFunc(); err != nil {
  363. return item, err
  364. }
  365. item = it.items[0]
  366. it.items = it.items[1:]
  367. return item, nil
  368. }
  369. func (it *StringIterator) bufLen() int {
  370. return len(it.items)
  371. }
  372. func (it *StringIterator) takeBuf() interface{} {
  373. b := it.items
  374. it.items = nil
  375. return b
  376. }