Skip to content

Commit a3fcc82

Browse files
committed
feat: Add RecentLogs support
1 parent badb304 commit a3fcc82

File tree

3 files changed

+64
-23
lines changed

3 files changed

+64
-23
lines changed

cloudfoundry-client/src/main/java/org/cloudfoundry/doppler/DopplerClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ public interface DopplerClient {
3939
*/
4040
Flux<Envelope> firehose(FirehoseRequest request);
4141

42+
//TODO Adapt the message
4243
/**
4344
* Makes the <a href="https://github.com/cloudfoundry/loggregator/tree/develop/src/trafficcontroller#endpoints">Recent Logs</a> request
4445
*
46+
* @deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding infrastructure
4547
* @param request the Recent Logs request
4648
* @return the events from the recent logs
4749
*/
50+
@Deprecated
4851
Flux<Envelope> recentLogs(RecentLogsRequest request);
4952

5053
/**

cloudfoundry-client/src/main/java/org/cloudfoundry/logcache/v1/LogCacheClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,12 @@ public interface LogCacheClient {
4646
* @return the read response
4747
*/
4848
Mono<ReadResponse> read(ReadRequest request);
49+
50+
/**
51+
* Makes the Log Cache RecentLogs /api/v1/read request
52+
*
53+
* @param request the Recent Logs request
54+
* @return the events from the recent logs
55+
*/
56+
Mono<ReadResponse> recentLogs(ReadRequest request);
4957
}

cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.function.Predicate;
4141
import java.util.function.UnaryOperator;
4242
import java.util.stream.Collectors;
43+
4344
import org.cloudfoundry.client.CloudFoundryClient;
4445
import org.cloudfoundry.client.v2.OrderDirection;
4546
import org.cloudfoundry.client.v2.applications.AbstractApplicationResource;
@@ -154,6 +155,9 @@
154155
import org.cloudfoundry.doppler.LogMessage;
155156
import org.cloudfoundry.doppler.RecentLogsRequest;
156157
import org.cloudfoundry.doppler.StreamRequest;
158+
import org.cloudfoundry.logcache.v1.EnvelopeType;
159+
import org.cloudfoundry.logcache.v1.LogCacheClient;
160+
import org.cloudfoundry.logcache.v1.ReadRequest;
157161
import org.cloudfoundry.operations.util.OperationsLogging;
158162
import org.cloudfoundry.util.DateUtils;
159163
import org.cloudfoundry.util.DelayTimeoutException;
@@ -189,8 +193,6 @@ public final class DefaultApplications implements Applications {
189193

190194
private static final int CF_STAGING_TIME_EXPIRED = 170007;
191195

192-
private static final int CF_INSUFFICIENT_RESOURCES = 170008;
193-
194196
private static final String[] ENTRY_FIELDS_CRASH = {"index", "reason", "exit_description"};
195197

196198
private static final String[] ENTRY_FIELDS_NORMAL = {
@@ -200,6 +202,9 @@ public final class DefaultApplications implements Applications {
200202
private static final Comparator<LogMessage> LOG_MESSAGE_COMPARATOR =
201203
Comparator.comparing(LogMessage::getTimestamp);
202204

205+
private static final Comparator<org.cloudfoundry.logcache.v1.Envelope> LOG_MESSAGE_COMPARATOR_LOG_CACHE =
206+
Comparator.comparing(org.cloudfoundry.logcache.v1.Envelope::getTimestamp);
207+
203208
private static final Duration LOG_MESSAGE_TIMESPAN = Duration.ofMillis(500);
204209

205210
private static final int MAX_NUMBER_OF_RECENT_EVENTS = 50;
@@ -715,11 +720,18 @@ public Mono<Void> pushManifestV3(PushManifestV3Request request) {
715720
.flatMap(
716721
function(
717722
(appId, packageId) ->
718-
buildAndStageAndWaitForRunning(
719-
cloudFoundryClient,
720-
manifestApp,
721-
packageId,
722-
appId)))))
723+
buildAndStage(
724+
cloudFoundryClient,
725+
manifestApp,
726+
packageId)
727+
.flatMap(
728+
dropletId ->
729+
applyDropletAndWaitForRunning(
730+
cloudFoundryClient,
731+
manifestApp
732+
.getName(),
733+
appId,
734+
dropletId))))))
723735
.then();
724736
}
725737

@@ -1172,21 +1184,6 @@ private static Mono<Void> bindServices(
11721184
.then();
11731185
}
11741186

1175-
private static Mono<Void> buildAndStageAndWaitForRunning(
1176-
CloudFoundryClient cloudFoundryClient,
1177-
ManifestV3Application manifestApp,
1178-
String packageId,
1179-
String appId) {
1180-
return buildAndStage(cloudFoundryClient, manifestApp, packageId)
1181-
.flatMap(
1182-
dropletId ->
1183-
applyDropletAndWaitForRunning(
1184-
cloudFoundryClient,
1185-
manifestApp.getName(),
1186-
appId,
1187-
dropletId));
1188-
}
1189-
11901187
private static Mono<String> buildAndStage(
11911188
CloudFoundryClient cloudFoundryClient,
11921189
ManifestV3Application manifestApp,
@@ -1501,7 +1498,6 @@ private static Mono<ApplicationInstancesResponse> getApplicationInstances(
15011498
CF_INSTANCES_ERROR,
15021499
CF_STAGING_NOT_FINISHED,
15031500
CF_STAGING_TIME_EXPIRED,
1504-
CF_INSUFFICIENT_RESOURCES,
15051501
CF_STAGING_ERROR),
15061502
t -> Mono.just(ApplicationInstancesResponse.builder().build()));
15071503
}
@@ -1617,6 +1613,14 @@ private static Flux<LogMessage> getLogs(
16171613
}
16181614
}
16191615

1616+
private static Flux<LogMessage> getRecentLogs(Mono<LogCacheClient> logCacheClient, String applicationId) {
1617+
return requestLogsRecentLogCache(logCacheClient, applicationId)
1618+
.filter(e -> EnvelopeType.LOG.getValue().equals(e.getLog().getType().getValue()))
1619+
.map(org.cloudfoundry.logcache.v1.Envelope::getLog)
1620+
.collectSortedList(LOG_MESSAGE_COMPARATOR_LOG_CACHE)
1621+
.flatMapIterable(d -> d);
1622+
}
1623+
16201624
@SuppressWarnings("unchecked")
16211625
private static Map<String, Object> getMetadataRequest(EventEntity entity) {
16221626
Map<String, Optional<Object>> metadata =
@@ -2509,6 +2513,32 @@ private static Flux<Envelope> requestLogsRecent(
25092513
RecentLogsRequest.builder().applicationId(applicationId).build()));
25102514
}
25112515

2516+
private static Flux<org.cloudfoundry.logcache.v1.Envelope> requestLogsRecentLogCache(
2517+
Mono<LogCacheClient> logCacheClient, String applicationId) {
2518+
return logCacheClient.flatMapMany(
2519+
client ->
2520+
client.recentLogs(
2521+
ReadRequest.builder()
2522+
.sourceId(applicationId)
2523+
.envelopeType(EnvelopeType.LOG)
2524+
.limit(100)
2525+
.build()
2526+
)
2527+
.flatMap(
2528+
response ->
2529+
Mono.justOrEmpty(
2530+
response.getEnvelopes().getBatch().stream().findFirst()
2531+
)
2532+
)
2533+
.repeatWhenEmpty(
2534+
exponentialBackOff(
2535+
Duration.ofSeconds(1),
2536+
Duration.ofSeconds(5),
2537+
Duration.ofMinutes(1))
2538+
)
2539+
);
2540+
}
2541+
25122542
private static Flux<Envelope> requestLogsStream(
25132543
Mono<DopplerClient> dopplerClient, String applicationId) {
25142544
return dopplerClient.flatMapMany(

0 commit comments

Comments
 (0)