Skip to content

Commit

Permalink
Merge pull request #1962 from HubSpot/get-request-logs-for-date-range
Browse files Browse the repository at this point in the history
Support searching for request logs with a specified date range via SingularityClient.
  • Loading branch information
ssalinas authored Jul 18, 2019
2 parents 2e841d3 + 2a4afd3 commit 6f8fcd0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
import com.hubspot.singularity.api.SingularityPauseRequest;
import com.hubspot.singularity.api.SingularityPriorityFreeze;
import com.hubspot.singularity.api.SingularityRunNowRequest;
import com.hubspot.singularity.api.SingularityS3SearchRequest;
import com.hubspot.singularity.api.SingularityS3SearchResult;
import com.hubspot.singularity.api.SingularityScaleRequest;
import com.hubspot.singularity.api.SingularityUnpauseRequest;
import com.hubspot.singularity.api.SingularityUpdateGroupsRequest;
Expand Down Expand Up @@ -200,6 +202,7 @@ public class SingularityClient {
private static final String SANDBOX_READ_FILE_FORMAT = SANDBOX_FORMAT + "/%s/read";

private static final String S3_LOG_FORMAT = "%s/logs";
private static final String S3_LOG_SEARCH_LOGS = S3_LOG_FORMAT + "/search";
private static final String S3_LOG_GET_TASK_LOGS = S3_LOG_FORMAT + "/task/%s";
private static final String S3_LOG_GET_REQUEST_LOGS = S3_LOG_FORMAT + "/request/%s";
private static final String S3_LOG_GET_DEPLOY_LOGS = S3_LOG_FORMAT + "/request/%s/deploy/%s";
Expand Down Expand Up @@ -1371,6 +1374,26 @@ public Optional<MesosFileChunkObject> readSandBoxFile(String taskId, String path
// S3 LOGS
//

/**
* Retrieve the list of logs stored in S3 based on a specified search request
*
* @param searchRequest
* The parameters upon which to base the search
* @return
* A result in the form of a {@link SingularityS3SearchResult}
*/
public SingularityS3SearchResult getLogs(SingularityS3SearchRequest searchRequest) {
final Function<String, String> requestUri = (host) -> String.format(S3_LOG_SEARCH_LOGS, getApiBase(host));

final Optional<SingularityS3SearchResult> maybeResult = post(requestUri, "S3 log search", Optional.of(searchRequest), Optional.of(SingularityS3SearchResult.class));

if (!maybeResult.isPresent()) {
throw new SingularityClientException("Singularity url not found", 404);
} else {
return maybeResult.get();
}
}

/**
* Retrieve the list of logs stored in S3 for a specific task
*
Expand Down Expand Up @@ -1398,11 +1421,24 @@ public Collection<SingularityS3Log> getTaskLogs(String taskId) {
* A collection of {@link SingularityS3Log}
*/
public Collection<SingularityS3Log> getRequestLogs(String requestId) {
return getRequestLogs(requestId, Optional.absent(), Optional.absent());
}

public Collection<SingularityS3Log> getRequestLogs(String requestId, Optional<Long> start, Optional<Long> end) {
final Function<String, String> requestUri = (host) -> String.format(S3_LOG_GET_REQUEST_LOGS, getApiBase(host), requestId);

final String type = String.format("S3 logs for request %s", requestId);

return getCollection(requestUri, type, S3_LOG_COLLECTION);
Map<String, Object> dateRange = new HashMap<>();
if (start.isPresent()) {
dateRange.put("start", start.get());
}

if (end.isPresent()) {
dateRange.put("end", end.get());
}

return getCollectionWithParams(requestUri, type, Optional.of(dateRange), S3_LOG_COLLECTION);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,12 @@ private boolean isCurrentDeploy(String requestId, String deployId) {
private Collection<String> getS3PrefixesForRequest(S3Configuration s3Configuration, String requestId, Optional<Long> startArg, Optional<Long> endArg, String group) {
Optional<SingularityRequestHistory> firstHistory = requestHistoryHelper.getFirstHistory(requestId);

checkNotFound(firstHistory.isPresent(), "No request history found for %s", requestId);

long start = firstHistory.get().getCreatedAt();
if (startArg.isPresent()) {
start = Math.max(startArg.get(), start);
if (!startArg.isPresent()) {
checkBadRequest(firstHistory.isPresent(), "No request history found for %s. A start time must be specified.", requestId);
}

long start = Math.max(startArg.or(0L), firstHistory.get().getCreatedAt());

Optional<SingularityRequestHistory> lastHistory = requestHistoryHelper.getLastHistory(requestId);

long end = System.currentTimeMillis();
Expand Down

0 comments on commit 6f8fcd0

Please sign in to comment.