logging_client.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. 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. var (
  34. loggingParentPathTemplate = gax.MustCompilePathTemplate("projects/{project}")
  35. loggingLogPathTemplate = gax.MustCompilePathTemplate("projects/{project}/logs/{log}")
  36. )
  37. // CallOptions contains the retry settings for each method of Client.
  38. type CallOptions struct {
  39. DeleteLog []gax.CallOption
  40. WriteLogEntries []gax.CallOption
  41. ListLogEntries []gax.CallOption
  42. ListMonitoredResourceDescriptors []gax.CallOption
  43. }
  44. func defaultClientOptions() []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 defaultCallOptions() *CallOptions {
  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. {"list", "idempotent"}: {
  71. gax.WithRetry(func() gax.Retryer {
  72. return gax.OnCodes([]codes.Code{
  73. codes.DeadlineExceeded,
  74. codes.Unavailable,
  75. }, gax.Backoff{
  76. Initial: 100 * time.Millisecond,
  77. Max: 1000 * time.Millisecond,
  78. Multiplier: 1.2,
  79. })
  80. }),
  81. },
  82. }
  83. return &CallOptions{
  84. DeleteLog: retry[[2]string{"default", "idempotent"}],
  85. WriteLogEntries: retry[[2]string{"default", "non_idempotent"}],
  86. ListLogEntries: retry[[2]string{"list", "idempotent"}],
  87. ListMonitoredResourceDescriptors: retry[[2]string{"default", "idempotent"}],
  88. }
  89. }
  90. // Client is a client for interacting with Stackdriver Logging API.
  91. type Client struct {
  92. // The connection to the service.
  93. conn *grpc.ClientConn
  94. // The gRPC API client.
  95. client loggingpb.LoggingServiceV2Client
  96. // The call options for this service.
  97. CallOptions *CallOptions
  98. // The metadata to be sent with each request.
  99. metadata metadata.MD
  100. }
  101. // NewClient creates a new logging service v2 client.
  102. //
  103. // Service for ingesting and querying logs.
  104. func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
  105. conn, err := transport.DialGRPC(ctx, append(defaultClientOptions(), opts...)...)
  106. if err != nil {
  107. return nil, err
  108. }
  109. c := &Client{
  110. conn: conn,
  111. CallOptions: defaultCallOptions(),
  112. client: loggingpb.NewLoggingServiceV2Client(conn),
  113. }
  114. c.SetGoogleClientInfo("gax", gax.Version)
  115. return c, nil
  116. }
  117. // Connection returns the client's connection to the API service.
  118. func (c *Client) Connection() *grpc.ClientConn {
  119. return c.conn
  120. }
  121. // Close closes the connection to the API service. The user should invoke this when
  122. // the client is no longer required.
  123. func (c *Client) Close() error {
  124. return c.conn.Close()
  125. }
  126. // SetGoogleClientInfo sets the name and version of the application in
  127. // the `x-goog-api-client` header passed on each request. Intended for
  128. // use by Google-written clients.
  129. func (c *Client) SetGoogleClientInfo(name, version string) {
  130. goVersion := strings.Replace(runtime.Version(), " ", "_", -1)
  131. v := fmt.Sprintf("%s/%s %s gax/%s go/%s", name, version, gapicNameVersion, gax.Version, goVersion)
  132. c.metadata = metadata.Pairs("x-goog-api-client", v)
  133. }
  134. // LoggingParentPath returns the path for the parent resource.
  135. func LoggingParentPath(project string) string {
  136. path, err := loggingParentPathTemplate.Render(map[string]string{
  137. "project": project,
  138. })
  139. if err != nil {
  140. panic(err)
  141. }
  142. return path
  143. }
  144. // LoggingLogPath returns the path for the log resource.
  145. func LoggingLogPath(project, log string) string {
  146. path, err := loggingLogPathTemplate.Render(map[string]string{
  147. "project": project,
  148. "log": log,
  149. })
  150. if err != nil {
  151. panic(err)
  152. }
  153. return path
  154. }
  155. // DeleteLog deletes all the log entries in a log.
  156. // The log reappears if it receives new entries.
  157. func (c *Client) DeleteLog(ctx context.Context, req *loggingpb.DeleteLogRequest) error {
  158. md, _ := metadata.FromContext(ctx)
  159. ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
  160. err := gax.Invoke(ctx, func(ctx context.Context) error {
  161. var err error
  162. _, err = c.client.DeleteLog(ctx, req)
  163. return err
  164. }, c.CallOptions.DeleteLog...)
  165. return err
  166. }
  167. // WriteLogEntries writes log entries to Stackdriver Logging. All log entries are
  168. // written by this method.
  169. func (c *Client) WriteLogEntries(ctx context.Context, req *loggingpb.WriteLogEntriesRequest) (*loggingpb.WriteLogEntriesResponse, error) {
  170. md, _ := metadata.FromContext(ctx)
  171. ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
  172. var resp *loggingpb.WriteLogEntriesResponse
  173. err := gax.Invoke(ctx, func(ctx context.Context) error {
  174. var err error
  175. resp, err = c.client.WriteLogEntries(ctx, req)
  176. return err
  177. }, c.CallOptions.WriteLogEntries...)
  178. if err != nil {
  179. return nil, err
  180. }
  181. return resp, nil
  182. }
  183. // ListLogEntries lists log entries. Use this method to retrieve log entries from Cloud
  184. // Logging. For ways to export log entries, see
  185. // [Exporting Logs](/logging/docs/export).
  186. func (c *Client) ListLogEntries(ctx context.Context, req *loggingpb.ListLogEntriesRequest) *LogEntryIterator {
  187. md, _ := metadata.FromContext(ctx)
  188. ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
  189. it := &LogEntryIterator{}
  190. it.InternalFetch = func(pageSize int, pageToken string) ([]*loggingpb.LogEntry, string, error) {
  191. var resp *loggingpb.ListLogEntriesResponse
  192. req.PageToken = pageToken
  193. if pageSize > math.MaxInt32 {
  194. req.PageSize = math.MaxInt32
  195. } else {
  196. req.PageSize = int32(pageSize)
  197. }
  198. err := gax.Invoke(ctx, func(ctx context.Context) error {
  199. var err error
  200. resp, err = c.client.ListLogEntries(ctx, req)
  201. return err
  202. }, c.CallOptions.ListLogEntries...)
  203. if err != nil {
  204. return nil, "", err
  205. }
  206. return resp.Entries, resp.NextPageToken, nil
  207. }
  208. fetch := func(pageSize int, pageToken string) (string, error) {
  209. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  210. if err != nil {
  211. return "", err
  212. }
  213. it.items = append(it.items, items...)
  214. return nextPageToken, nil
  215. }
  216. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  217. return it
  218. }
  219. // ListMonitoredResourceDescriptors lists the monitored resource descriptors used by Stackdriver Logging.
  220. func (c *Client) ListMonitoredResourceDescriptors(ctx context.Context, req *loggingpb.ListMonitoredResourceDescriptorsRequest) *MonitoredResourceDescriptorIterator {
  221. md, _ := metadata.FromContext(ctx)
  222. ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
  223. it := &MonitoredResourceDescriptorIterator{}
  224. it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResourceDescriptor, string, error) {
  225. var resp *loggingpb.ListMonitoredResourceDescriptorsResponse
  226. req.PageToken = pageToken
  227. if pageSize > math.MaxInt32 {
  228. req.PageSize = math.MaxInt32
  229. } else {
  230. req.PageSize = int32(pageSize)
  231. }
  232. err := gax.Invoke(ctx, func(ctx context.Context) error {
  233. var err error
  234. resp, err = c.client.ListMonitoredResourceDescriptors(ctx, req)
  235. return err
  236. }, c.CallOptions.ListMonitoredResourceDescriptors...)
  237. if err != nil {
  238. return nil, "", err
  239. }
  240. return resp.ResourceDescriptors, resp.NextPageToken, nil
  241. }
  242. fetch := func(pageSize int, pageToken string) (string, error) {
  243. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  244. if err != nil {
  245. return "", err
  246. }
  247. it.items = append(it.items, items...)
  248. return nextPageToken, nil
  249. }
  250. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  251. return it
  252. }
  253. // LogEntryIterator manages a stream of *loggingpb.LogEntry.
  254. type LogEntryIterator struct {
  255. items []*loggingpb.LogEntry
  256. pageInfo *iterator.PageInfo
  257. nextFunc func() error
  258. // InternalFetch is for use by the Google Cloud Libraries only.
  259. // It is not part of the stable interface of this package.
  260. //
  261. // InternalFetch returns results from a single call to the underlying RPC.
  262. // The number of results is no greater than pageSize.
  263. // If there are no more results, nextPageToken is empty and err is nil.
  264. InternalFetch func(pageSize int, pageToken string) (results []*loggingpb.LogEntry, nextPageToken string, err error)
  265. }
  266. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  267. func (it *LogEntryIterator) PageInfo() *iterator.PageInfo {
  268. return it.pageInfo
  269. }
  270. // Next returns the next result. Its second return value is iterator.Done if there are no more
  271. // results. Once Next returns Done, all subsequent calls will return Done.
  272. func (it *LogEntryIterator) Next() (*loggingpb.LogEntry, error) {
  273. var item *loggingpb.LogEntry
  274. if err := it.nextFunc(); err != nil {
  275. return item, err
  276. }
  277. item = it.items[0]
  278. it.items = it.items[1:]
  279. return item, nil
  280. }
  281. func (it *LogEntryIterator) bufLen() int {
  282. return len(it.items)
  283. }
  284. func (it *LogEntryIterator) takeBuf() interface{} {
  285. b := it.items
  286. it.items = nil
  287. return b
  288. }
  289. // MonitoredResourceDescriptorIterator manages a stream of *monitoredrespb.MonitoredResourceDescriptor.
  290. type MonitoredResourceDescriptorIterator struct {
  291. items []*monitoredrespb.MonitoredResourceDescriptor
  292. pageInfo *iterator.PageInfo
  293. nextFunc func() error
  294. // InternalFetch is for use by the Google Cloud Libraries only.
  295. // It is not part of the stable interface of this package.
  296. //
  297. // InternalFetch returns results from a single call to the underlying RPC.
  298. // The number of results is no greater than pageSize.
  299. // If there are no more results, nextPageToken is empty and err is nil.
  300. InternalFetch func(pageSize int, pageToken string) (results []*monitoredrespb.MonitoredResourceDescriptor, nextPageToken string, err error)
  301. }
  302. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  303. func (it *MonitoredResourceDescriptorIterator) PageInfo() *iterator.PageInfo {
  304. return it.pageInfo
  305. }
  306. // Next returns the next result. Its second return value is iterator.Done if there are no more
  307. // results. Once Next returns Done, all subsequent calls will return Done.
  308. func (it *MonitoredResourceDescriptorIterator) Next() (*monitoredrespb.MonitoredResourceDescriptor, error) {
  309. var item *monitoredrespb.MonitoredResourceDescriptor
  310. if err := it.nextFunc(); err != nil {
  311. return item, err
  312. }
  313. item = it.items[0]
  314. it.items = it.items[1:]
  315. return item, nil
  316. }
  317. func (it *MonitoredResourceDescriptorIterator) bufLen() int {
  318. return len(it.items)
  319. }
  320. func (it *MonitoredResourceDescriptorIterator) takeBuf() interface{} {
  321. b := it.items
  322. it.items = nil
  323. return b
  324. }