logging_client.go 15 KB

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