1 // Copyright 2016, Google Inc. All rights reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 // AUTO-GENERATED CODE. DO NOT EDIT.
26 gax "github.com/googleapis/gax-go"
27 "golang.org/x/net/context"
28 "google.golang.org/api/iterator"
29 "google.golang.org/api/option"
30 "google.golang.org/api/transport"
31 monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
32 loggingpb "google.golang.org/genproto/googleapis/logging/v2"
33 "google.golang.org/grpc"
34 "google.golang.org/grpc/codes"
35 "google.golang.org/grpc/metadata"
39 loggingParentPathTemplate = gax.MustCompilePathTemplate("projects/{project}")
40 loggingLogPathTemplate = gax.MustCompilePathTemplate("projects/{project}/logs/{log}")
43 // CallOptions contains the retry settings for each method of Client.
44 type CallOptions struct {
45 DeleteLog []gax.CallOption
46 WriteLogEntries []gax.CallOption
47 ListLogEntries []gax.CallOption
48 ListMonitoredResourceDescriptors []gax.CallOption
51 func defaultClientOptions() []option.ClientOption {
52 return []option.ClientOption{
53 option.WithEndpoint("logging.googleapis.com:443"),
55 "https://www.googleapis.com/auth/cloud-platform",
56 "https://www.googleapis.com/auth/cloud-platform.read-only",
57 "https://www.googleapis.com/auth/logging.admin",
58 "https://www.googleapis.com/auth/logging.read",
59 "https://www.googleapis.com/auth/logging.write",
64 func defaultCallOptions() *CallOptions {
65 retry := map[[2]string][]gax.CallOption{
66 {"default", "idempotent"}: {
67 gax.WithRetry(func() gax.Retryer {
68 return gax.OnCodes([]codes.Code{
69 codes.DeadlineExceeded,
72 Initial: 100 * time.Millisecond,
73 Max: 1000 * time.Millisecond,
78 {"list", "idempotent"}: {
79 gax.WithRetry(func() gax.Retryer {
80 return gax.OnCodes([]codes.Code{
81 codes.DeadlineExceeded,
84 Initial: 100 * time.Millisecond,
85 Max: 1000 * time.Millisecond,
92 DeleteLog: retry[[2]string{"default", "idempotent"}],
93 WriteLogEntries: retry[[2]string{"default", "non_idempotent"}],
94 ListLogEntries: retry[[2]string{"list", "idempotent"}],
95 ListMonitoredResourceDescriptors: retry[[2]string{"default", "idempotent"}],
99 // Client is a client for interacting with Stackdriver Logging API.
101 // The connection to the service.
102 conn *grpc.ClientConn
104 // The gRPC API client.
105 client loggingpb.LoggingServiceV2Client
107 // The call options for this service.
108 CallOptions *CallOptions
110 // The metadata to be sent with each request.
114 // NewClient creates a new logging service v2 client.
116 // Service for ingesting and querying logs.
117 func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
118 conn, err := transport.DialGRPC(ctx, append(defaultClientOptions(), opts...)...)
124 CallOptions: defaultCallOptions(),
126 client: loggingpb.NewLoggingServiceV2Client(conn),
128 c.SetGoogleClientInfo("gax", gax.Version)
132 // Connection returns the client's connection to the API service.
133 func (c *Client) Connection() *grpc.ClientConn {
137 // Close closes the connection to the API service. The user should invoke this when
138 // the client is no longer required.
139 func (c *Client) Close() error {
140 return c.conn.Close()
143 // SetGoogleClientInfo sets the name and version of the application in
144 // the `x-goog-api-client` header passed on each request. Intended for
145 // use by Google-written clients.
146 func (c *Client) SetGoogleClientInfo(name, version string) {
147 goVersion := strings.Replace(runtime.Version(), " ", "_", -1)
148 v := fmt.Sprintf("%s/%s %s gax/%s go/%s", name, version, gapicNameVersion, gax.Version, goVersion)
149 c.metadata = metadata.Pairs("x-goog-api-client", v)
152 // LoggingParentPath returns the path for the parent resource.
153 func LoggingParentPath(project string) string {
154 path, err := loggingParentPathTemplate.Render(map[string]string{
163 // LoggingLogPath returns the path for the log resource.
164 func LoggingLogPath(project, log string) string {
165 path, err := loggingLogPathTemplate.Render(map[string]string{
175 // DeleteLog deletes all the log entries in a log.
176 // The log reappears if it receives new entries.
177 func (c *Client) DeleteLog(ctx context.Context, req *loggingpb.DeleteLogRequest) error {
178 md, _ := metadata.FromContext(ctx)
179 ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
180 err := gax.Invoke(ctx, func(ctx context.Context) error {
182 _, err = c.client.DeleteLog(ctx, req)
184 }, c.CallOptions.DeleteLog...)
188 // WriteLogEntries writes log entries to Stackdriver Logging. All log entries are
189 // written by this method.
190 func (c *Client) WriteLogEntries(ctx context.Context, req *loggingpb.WriteLogEntriesRequest) (*loggingpb.WriteLogEntriesResponse, error) {
191 md, _ := metadata.FromContext(ctx)
192 ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
193 var resp *loggingpb.WriteLogEntriesResponse
194 err := gax.Invoke(ctx, func(ctx context.Context) error {
196 resp, err = c.client.WriteLogEntries(ctx, req)
198 }, c.CallOptions.WriteLogEntries...)
205 // ListLogEntries lists log entries. Use this method to retrieve log entries from Cloud
206 // Logging. For ways to export log entries, see
207 // [Exporting Logs](/logging/docs/export).
208 func (c *Client) ListLogEntries(ctx context.Context, req *loggingpb.ListLogEntriesRequest) *LogEntryIterator {
209 md, _ := metadata.FromContext(ctx)
210 ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
211 it := &LogEntryIterator{}
212 it.InternalFetch = func(pageSize int, pageToken string) ([]*loggingpb.LogEntry, string, error) {
213 var resp *loggingpb.ListLogEntriesResponse
214 req.PageToken = pageToken
215 if pageSize > math.MaxInt32 {
216 req.PageSize = math.MaxInt32
218 req.PageSize = int32(pageSize)
220 err := gax.Invoke(ctx, func(ctx context.Context) error {
222 resp, err = c.client.ListLogEntries(ctx, req)
224 }, c.CallOptions.ListLogEntries...)
228 return resp.Entries, resp.NextPageToken, nil
230 fetch := func(pageSize int, pageToken string) (string, error) {
231 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
235 it.items = append(it.items, items...)
236 return nextPageToken, nil
238 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
242 // ListMonitoredResourceDescriptors lists the monitored resource descriptors used by Stackdriver Logging.
243 func (c *Client) ListMonitoredResourceDescriptors(ctx context.Context, req *loggingpb.ListMonitoredResourceDescriptorsRequest) *MonitoredResourceDescriptorIterator {
244 md, _ := metadata.FromContext(ctx)
245 ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
246 it := &MonitoredResourceDescriptorIterator{}
247 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResourceDescriptor, string, error) {
248 var resp *loggingpb.ListMonitoredResourceDescriptorsResponse
249 req.PageToken = pageToken
250 if pageSize > math.MaxInt32 {
251 req.PageSize = math.MaxInt32
253 req.PageSize = int32(pageSize)
255 err := gax.Invoke(ctx, func(ctx context.Context) error {
257 resp, err = c.client.ListMonitoredResourceDescriptors(ctx, req)
259 }, c.CallOptions.ListMonitoredResourceDescriptors...)
263 return resp.ResourceDescriptors, resp.NextPageToken, nil
265 fetch := func(pageSize int, pageToken string) (string, error) {
266 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
270 it.items = append(it.items, items...)
271 return nextPageToken, nil
273 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
277 // LogEntryIterator manages a stream of *loggingpb.LogEntry.
278 type LogEntryIterator struct {
279 items []*loggingpb.LogEntry
280 pageInfo *iterator.PageInfo
281 nextFunc func() error
283 // InternalFetch is for use by the Google Cloud Libraries only.
284 // It is not part of the stable interface of this package.
286 // InternalFetch returns results from a single call to the underlying RPC.
287 // The number of results is no greater than pageSize.
288 // If there are no more results, nextPageToken is empty and err is nil.
289 InternalFetch func(pageSize int, pageToken string) (results []*loggingpb.LogEntry, nextPageToken string, err error)
292 // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
293 func (it *LogEntryIterator) PageInfo() *iterator.PageInfo {
297 // Next returns the next result. Its second return value is iterator.Done if there are no more
298 // results. Once Next returns Done, all subsequent calls will return Done.
299 func (it *LogEntryIterator) Next() (*loggingpb.LogEntry, error) {
300 var item *loggingpb.LogEntry
301 if err := it.nextFunc(); err != nil {
305 it.items = it.items[1:]
309 func (it *LogEntryIterator) bufLen() int {
313 func (it *LogEntryIterator) takeBuf() interface{} {
319 // MonitoredResourceDescriptorIterator manages a stream of *monitoredrespb.MonitoredResourceDescriptor.
320 type MonitoredResourceDescriptorIterator struct {
321 items []*monitoredrespb.MonitoredResourceDescriptor
322 pageInfo *iterator.PageInfo
323 nextFunc func() error
325 // InternalFetch is for use by the Google Cloud Libraries only.
326 // It is not part of the stable interface of this package.
328 // InternalFetch returns results from a single call to the underlying RPC.
329 // The number of results is no greater than pageSize.
330 // If there are no more results, nextPageToken is empty and err is nil.
331 InternalFetch func(pageSize int, pageToken string) (results []*monitoredrespb.MonitoredResourceDescriptor, nextPageToken string, err error)
334 // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
335 func (it *MonitoredResourceDescriptorIterator) PageInfo() *iterator.PageInfo {
339 // Next returns the next result. Its second return value is iterator.Done if there are no more
340 // results. Once Next returns Done, all subsequent calls will return Done.
341 func (it *MonitoredResourceDescriptorIterator) Next() (*monitoredrespb.MonitoredResourceDescriptor, error) {
342 var item *monitoredrespb.MonitoredResourceDescriptor
343 if err := it.nextFunc(); err != nil {
347 it.items = it.items[1:]
351 func (it *MonitoredResourceDescriptorIterator) bufLen() int {
355 func (it *MonitoredResourceDescriptorIterator) takeBuf() interface{} {