Skip to content

Commit

Permalink
feat: add the Tailing API to get a live stream of the tail end of fil…
Browse files Browse the repository at this point in the history
…tered logs

PiperOrigin-RevId: 344435830

Source-Author: Google APIs <noreply@google.com>
Source-Date: Thu Nov 26 09:56:05 2020 -0800
Source-Repo: googleapis/googleapis
Source-Sha: e8857c4c36948e7e0500377cd7fcecbf2459afc8
Source-Link: googleapis/googleapis@e8857c4
  • Loading branch information
yoshi-automation authored and jskeet committed Nov 27, 2020
1 parent 5c91cb1 commit 3e216d0
Show file tree
Hide file tree
Showing 5 changed files with 1,090 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ namespace Google.Cloud.Logging.V2.Snippets
{
using Google.Api;
using Google.Api.Gax;
using Google.Api.Gax.Grpc;
using Google.Api.Gax.ResourceNames;
using Google.Protobuf.WellKnownTypes;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -929,6 +931,7 @@ public void ListLogsRequestObject()
ListLogsRequest request = new ListLogsRequest
{
ParentAsProjectName = ProjectName.FromProject("[PROJECT]"),
ResourceNames = { "", },
};
// Make the request
PagedEnumerable<ListLogsResponse, string> response = loggingServiceV2Client.ListLogs(request);
Expand Down Expand Up @@ -977,6 +980,7 @@ public async Task ListLogsRequestObjectAsync()
ListLogsRequest request = new ListLogsRequest
{
ParentAsProjectName = ProjectName.FromProject("[PROJECT]"),
ResourceNames = { "", },
};
// Make the request
PagedAsyncEnumerable<ListLogsResponse, string> response = loggingServiceV2Client.ListLogsAsync(request);
Expand Down Expand Up @@ -1464,5 +1468,54 @@ await response.AsRawResponses().ForEachAsync((ListLogsResponse page) =>
string nextPageToken = singlePage.NextPageToken;
// End snippet
}

/// <summary>Snippet for TailLogEntries</summary>
public async Task TailLogEntries()
{
// Snippet: TailLogEntries(CallSettings, BidirectionalStreamingSettings)
// Create client
LoggingServiceV2Client loggingServiceV2Client = LoggingServiceV2Client.Create();
// Initialize streaming call, retrieving the stream object
LoggingServiceV2Client.TailLogEntriesStream response = loggingServiceV2Client.TailLogEntries();

// Sending requests and retrieving responses can be arbitrarily interleaved
// Exact sequence will depend on client/server behavior

// Create task to do something with responses from server
Task responseHandlerTask = Task.Run(async () =>
{
// Note that C# 8 code can use await foreach
AsyncResponseStream<TailLogEntriesResponse> responseStream = response.GetResponseStream();
while (await responseStream.MoveNextAsync())
{
TailLogEntriesResponse responseItem = responseStream.Current;
// Do something with streamed response
}
// The response stream has completed
});

// Send requests to the server
bool done = false;
while (!done)
{
// Initialize a request
TailLogEntriesRequest request = new TailLogEntriesRequest
{
ResourceNames = { "", },
Filter = "",
BufferWindow = new Duration(),
};
// Stream a request to the server
await response.WriteAsync(request);
// Set "done" to true when sending requests is complete
}

// Complete writing requests to the stream
await response.WriteCompleteAsync();
// Await the response handler
// This will complete once all server responses have been processed
await responseHandlerTask;
// End snippet
}
}
}
Loading

0 comments on commit 3e216d0

Please sign in to comment.