Search in sources :

Example 1 with LogReadResponse

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;
        }
    };
}
Also used : ByteString(com.google.protobuf.ByteString) LogReadRequest(com.google.apphosting.api.logservice.LogServicePb.LogReadRequest) LogModuleVersion(com.google.apphosting.api.logservice.LogServicePb.LogModuleVersion) UninitializedMessageException(com.google.protobuf.UninitializedMessageException) ApiProxy(com.google.apphosting.api.ApiProxy) FutureWrapper(com.google.appengine.api.utils.FutureWrapper) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) LogModuleVersion(com.google.apphosting.api.logservice.LogServicePb.LogModuleVersion) ErrorCode(com.google.apphosting.api.logservice.LogServicePb.LogServiceError.ErrorCode) LogReadResponse(com.google.apphosting.api.logservice.LogServicePb.LogReadResponse)

Example 2 with LogReadResponse

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);
}
Also used : ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) RequestLog(com.google.apphosting.api.logservice.LogServicePb.RequestLog) LogReadRequest(com.google.apphosting.api.logservice.LogServicePb.LogReadRequest) LogReadResponse(com.google.apphosting.api.logservice.LogServicePb.LogReadResponse) Test(org.junit.Test)

Example 3 with LogReadResponse

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));
}
Also used : RequestLog(com.google.apphosting.api.logservice.LogServicePb.RequestLog) LogReadRequest(com.google.apphosting.api.logservice.LogServicePb.LogReadRequest) LogOffset(com.google.apphosting.api.logservice.LogServicePb.LogOffset) LogReadResponse(com.google.apphosting.api.logservice.LogServicePb.LogReadResponse) Test(org.junit.Test)

Example 4 with LogReadResponse

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);
}
Also used : RequestLog(com.google.apphosting.api.logservice.LogServicePb.RequestLog) LogReadRequest(com.google.apphosting.api.logservice.LogServicePb.LogReadRequest) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) LogReadResponse(com.google.apphosting.api.logservice.LogServicePb.LogReadResponse) Test(org.junit.Test)

Example 5 with LogReadResponse

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);
}
Also used : RequestLog(com.google.apphosting.api.logservice.LogServicePb.RequestLog) LogReadRequest(com.google.apphosting.api.logservice.LogServicePb.LogReadRequest) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) LogReadResponse(com.google.apphosting.api.logservice.LogServicePb.LogReadResponse) Test(org.junit.Test)

Aggregations

LogReadResponse (com.google.apphosting.api.logservice.LogServicePb.LogReadResponse)8 LogReadRequest (com.google.apphosting.api.logservice.LogServicePb.LogReadRequest)7 RequestLog (com.google.apphosting.api.logservice.LogServicePb.RequestLog)7 ByteString (com.google.protobuf.ByteString)7 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 LogModuleVersion (com.google.apphosting.api.logservice.LogServicePb.LogModuleVersion)2 LogOffset (com.google.apphosting.api.logservice.LogServicePb.LogOffset)2 FutureWrapper (com.google.appengine.api.utils.FutureWrapper)1 ApiProxy (com.google.apphosting.api.ApiProxy)1 LogLine (com.google.apphosting.api.logservice.LogServicePb.LogLine)1 ErrorCode (com.google.apphosting.api.logservice.LogServicePb.LogServiceError.ErrorCode)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 UninitializedMessageException (com.google.protobuf.UninitializedMessageException)1 BigInteger (java.math.BigInteger)1