use of com.google.apphosting.api.logservice.LogServicePb.LogReadResponse in project appengine-java-standard by GoogleCloudPlatform.
the class LogServiceImpl method fetchAsync.
Future<LogQueryResult> fetchAsync(LogQuery query) {
LogReadRequest.Builder request = LogReadRequest.newBuilder().setAppId(getCurrentEnvironmentOrThrow().getAppId());
Long startTimeUs = query.getStartTimeUsec();
if (startTimeUs != null) {
request.setStartTime(startTimeUs);
}
Long endTimeUs = query.getEndTimeUsec();
if (endTimeUs != null) {
request.setEndTime(endTimeUs);
}
int batchSize = requireNonNull(query.getBatchSize(), "Null batch size");
request.setCount(batchSize);
LogLevel minLogLevel = query.getMinLogLevel();
if (minLogLevel != null) {
request.setMinimumLogLevel(minLogLevel.ordinal());
}
request.setIncludeIncomplete(query.getIncludeIncomplete()).setIncludeAppLogs(query.getIncludeAppLogs());
// Use a set to de-dupe entries.
Set<LogQuery.Version> convertedModuleInfos = Sets.newTreeSet(LogQuery.VERSION_COMPARATOR);
// NOTE: LogQuery enforces that at most one of these lists is populated.
if (!query.getMajorVersionIds().isEmpty()) {
for (String versionId : query.getMajorVersionIds()) {
convertedModuleInfos.add(new LogQuery.Version("default", versionId));
}
} else if (!query.getVersions().isEmpty()) {
convertedModuleInfos.addAll(query.getVersions());
} else {
String currentVersionId = getCurrentEnvironmentOrThrow().getVersionId();
// Get just the major version id - for 1.2332 it is just '1'.
String versionId = currentVersionId.split("\\.")[0];
convertedModuleInfos.add(new LogQuery.Version(getCurrentEnvironmentOrThrow().getModuleId(), versionId));
}
for (LogQuery.Version moduleInfo : convertedModuleInfos) {
LogModuleVersion.Builder requestModuleVersion = request.addModuleVersionBuilder();
if (!moduleInfo.getModuleId().equals("default")) {
requestModuleVersion.setModuleId(moduleInfo.getModuleId());
}
requestModuleVersion.setVersionId(moduleInfo.getVersionId());
}
for (String requestId : query.getRequestIds()) {
request.addRequestId(ByteString.copyFromUtf8(requestId));
}
String offset = query.getOffset();
if (offset != null) {
request.setOffset(LogQueryResult.parseOffset(offset));
}
final LogQuery finalizedQuery = query;
ApiProxy.ApiConfig apiConfig = new ApiProxy.ApiConfig();
Future<byte[]> responseBytes = ApiProxy.makeAsyncCall(PACKAGE, READ_RPC_NAME, request.build().toByteArray(), apiConfig);
return new FutureWrapper<byte[], LogQueryResult>(responseBytes) {
@Override
protected LogQueryResult wrap(byte @Nullable [] responseBytes) {
try {
LogReadResponse response = LogReadResponse.parseFrom(responseBytes, ExtensionRegistry.getEmptyRegistry());
return new LogQueryResult(response, finalizedQuery);
} catch (InvalidProtocolBufferException | UninitializedMessageException e) {
throw new LogServiceException("Could not parse LogReadResponse", e);
}
}
@Override
protected Throwable convertException(Throwable cause) {
if (cause instanceof ApiProxy.ApplicationException) {
ApiProxy.ApplicationException e = (ApiProxy.ApplicationException) cause;
ErrorCode errorCode = LogServiceError.ErrorCode.forNumber(e.getApplicationError());
if (errorCode == LogServiceError.ErrorCode.INVALID_REQUEST) {
return new InvalidRequestException(e.getErrorDetail());
}
return new LogServiceException(e.getErrorDetail());
}
return cause;
}
};
}
use of com.google.apphosting.api.logservice.LogServicePb.LogReadResponse in project appengine-java-standard by GoogleCloudPlatform.
the class LogServiceImplTest method testGetLogsForRequestIds.
@Test
public void testGetLogsForRequestIds() throws Exception {
int numRequested = LogService.DEFAULT_ITEMS_PER_FETCH;
List<RequestLog> expectedData = getTestData(numRequested);
List<String> requestIds = new ArrayList<>();
LogReadRequest.Builder request = createLogReadRequest(null, null, null, null, requestIds).toBuilder();
LogReadResponse response = createLogReadResponse(expectedData);
ArrayList<String> queriedIds = new ArrayList<>();
// List<ByteString> expectedIds = request.getRequestIdList();
for (int i = 0; i < numRequested; i++) {
String requestId = Integer.toString(i);
queriedIds.add(requestId);
// expectedIds.add(ByteString.copyFromUtf8(requestId));
request.addRequestId(ByteString.copyFromUtf8(requestId));
}
setupExpectations(request.build(), response);
LogQuery query = LogQuery.Builder.withRequestIds(queriedIds);
List<RequestLogs> actualData = new ArrayList<>();
for (RequestLogs record : new LogServiceImpl().fetch(query)) {
actualData.add(record);
}
String expectedLogs = getJustCombinedRequestFields(expectedData);
String actualLogs = getJustCombinedFields(actualData);
assertThat(actualLogs).isEqualTo(expectedLogs);
}
use of com.google.apphosting.api.logservice.LogServicePb.LogReadResponse in project appengine-java-standard by GoogleCloudPlatform.
the class LogServiceImplTest method testFetchWithOffset.
@Test
public void testFetchWithOffset() throws Exception {
List<RequestLog> expectedData = getTestData(LogService.DEFAULT_ITEMS_PER_FETCH);
LogReadRequest.Builder request = createLogReadRequest(null, null, null, null).toBuilder();
LogOffset offset = LogOffset.newBuilder().setRequestId(ByteString.copyFrom(new byte[] { (byte) 0xfe, (byte) 0xff, (byte) 0xcd })).build();
request.setOffset(offset);
LogReadResponse response = createLogReadResponse(expectedData);
setupExpectations(request.build(), response);
LogQuery query = LogQuery.Builder.withDefaults();
query.offset(base64().encode(offset.toByteArray()));
new LogServiceImpl().fetch(query);
// Test negative case.
// Not parseable as Base64.
query.offset("!");
assertThrows(IllegalArgumentException.class, () -> new LogServiceImpl().fetch(query));
}
use of com.google.apphosting.api.logservice.LogServicePb.LogReadResponse in project appengine-java-standard by GoogleCloudPlatform.
the class LogServiceImplTest method testGetLogsForLast24Hours.
@Test
public void testGetLogsForLast24Hours() throws Exception {
List<RequestLog> expectedData = getTestData(LogService.DEFAULT_ITEMS_PER_FETCH);
Long startTime = getTimeForNDaysAgo(1, null);
Long endTime = null;
Integer batchSize = null;
LogReadRequest request = createLogReadRequest(startTime, endTime, batchSize, null);
LogReadResponse response = createLogReadResponse(expectedData);
setupExpectations(request, response);
LogQuery query = LogQuery.Builder.withStartTimeUsec(startTime);
List<RequestLogs> actualData = new ArrayList<>();
for (RequestLogs record : new LogServiceImpl().fetch(query)) {
actualData.add(record);
}
String expectedLogs = getJustCombinedRequestFields(expectedData);
String actualLogs = getJustCombinedFields(actualData);
assertThat(actualLogs).isEqualTo(expectedLogs);
}
use of com.google.apphosting.api.logservice.LogServicePb.LogReadResponse in project appengine-java-standard by GoogleCloudPlatform.
the class LogServiceImplTest method testGetLogsForTwoDaysAgo.
@Test
public void testGetLogsForTwoDaysAgo() throws Exception {
List<RequestLog> expectedData = getTestData(LogService.DEFAULT_ITEMS_PER_FETCH);
Long endTime = getTimeForNDaysAgo(1, null);
Long startTime = getTimeForNDaysAgo(1, endTime);
Integer batchSize = null;
LogReadRequest request = createLogReadRequest(startTime, endTime, batchSize, null);
LogReadResponse response = createLogReadResponse(expectedData);
setupExpectations(request, response);
LogQuery query = LogQuery.Builder.withStartTimeUsec(startTime).endTimeUsec(endTime);
List<RequestLogs> actualData = new ArrayList<>();
for (RequestLogs record : new LogServiceImpl().fetch(query)) {
actualData.add(record);
}
String expectedLogs = getJustCombinedRequestFields(expectedData);
String actualLogs = getJustCombinedFields(actualData);
assertThat(actualLogs).isEqualTo(expectedLogs);
}
Aggregations