forked from spring-cloud/spring-cloud-app-broker
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support of service instance logging endpoints
The endpoint is compatible with service instance logs CF CLI plugin https://github.com/pivotal-cf/service-instance-logs-cli-plugin
- Loading branch information
1 parent
53dec44
commit 01bb402
Showing
26 changed files
with
1,474 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
dependencyManagement { | ||
imports { | ||
mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}" | ||
} | ||
} | ||
|
||
ext { | ||
immutablesVersion = "2.8.1" | ||
awaitilityVersion = "4.0.2" | ||
} | ||
|
||
dependencies { | ||
compile "org.springframework:spring-webflux" | ||
compile "org.springframework:spring-context" | ||
compile "org.springframework.boot:spring-boot-autoconfigure" | ||
|
||
compile "org.cloudfoundry:cloudfoundry-client-reactor:${cfJavaClientVersion}" | ||
compile "org.cloudfoundry:cloudfoundry-operations:${cfJavaClientVersion}" | ||
compile "org.immutables:value:${immutablesVersion}" | ||
|
||
testImplementation("org.springframework.boot:spring-boot-starter-test") { | ||
exclude group: 'junit', module: 'junit' | ||
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' | ||
} | ||
testImplementation "org.junit.jupiter:junit-jupiter-api" | ||
testImplementation "org.awaitility:awaitility:${awaitilityVersion}" | ||
} |
25 changes: 25 additions & 0 deletions
25
...ing/src/main/java/org/springframework/cloud/appbroker/logging/ApplicationIdsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2002-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.appbroker.logging; | ||
|
||
import reactor.core.publisher.Flux; | ||
|
||
public interface ApplicationIdsProvider { | ||
|
||
Flux<String> getApplicationIds(String serviceInstanceId); | ||
|
||
} |
111 changes: 111 additions & 0 deletions
111
...roker-logging/src/main/java/org/springframework/cloud/appbroker/logging/LoggingUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2002-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.appbroker.logging; | ||
|
||
import okio.ByteString; | ||
import org.cloudfoundry.doppler.EventType; | ||
import org.cloudfoundry.doppler.MessageType; | ||
import org.cloudfoundry.dropsonde.events.Envelope; | ||
import org.cloudfoundry.dropsonde.events.LogMessage; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public final class LoggingUtils { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(LoggingUtils.class); | ||
|
||
private LoggingUtils() { | ||
} | ||
|
||
public static Envelope convertDopplerEnvelopeToDropsonde(org.cloudfoundry.doppler.Envelope envelope) { | ||
final Envelope.Builder builder = new Envelope.Builder() | ||
.deployment(envelope.getDeployment()) | ||
.eventType(toDropsondeEventType(envelope.getEventType())) | ||
.index(envelope.getIndex()) | ||
.ip(envelope.getIp()) | ||
.job(envelope.getJob()) | ||
.origin(envelope.getOrigin()) | ||
.tags(envelope.getTags()) | ||
.timestamp(envelope.getTimestamp()); | ||
|
||
if (envelope.getEventType() == EventType.LOG_MESSAGE) { | ||
final org.cloudfoundry.doppler.LogMessage logMessage = envelope.getLogMessage(); | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("Decoding message [" + logMessage.getTimestamp() + "]: " + logMessage.getMessage()); | ||
} | ||
|
||
builder.logMessage(new LogMessage.Builder() | ||
.app_id(logMessage.getApplicationId()) | ||
.message(ByteString.encodeUtf8(logMessage.getMessage())) | ||
.message_type(toDropsondeMessageType(logMessage.getMessageType())) | ||
.source_instance(logMessage.getSourceInstance()) | ||
.source_type(logMessage.getSourceType()) | ||
.timestamp(logMessage.getTimestamp()) | ||
.build()); | ||
} | ||
else { | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("Unable to decode message of type " + envelope.getEventType()); | ||
} | ||
} | ||
|
||
return builder.build(); | ||
} | ||
|
||
public static org.cloudfoundry.doppler.Envelope injectAppNameIntoLogSourceInstance(String appName, | ||
org.cloudfoundry.doppler.Envelope envelope) { | ||
if (envelope.getEventType() != EventType.LOG_MESSAGE) { | ||
return envelope; | ||
} | ||
return org.cloudfoundry.doppler.Envelope.builder().from(envelope).logMessage( | ||
org.cloudfoundry.doppler.LogMessage.builder().from(envelope.getLogMessage()) | ||
.sourceInstance(appName + " " + envelope.getLogMessage().getSourceInstance()) | ||
.build() | ||
).build(); | ||
} | ||
|
||
|
||
private static LogMessage.MessageType toDropsondeMessageType(MessageType messageType) { | ||
switch (messageType) { | ||
case ERR: | ||
return LogMessage.MessageType.ERR; | ||
case OUT: | ||
return LogMessage.MessageType.OUT; | ||
default: | ||
throw new IllegalArgumentException("Unknown message type " + messageType); | ||
} | ||
} | ||
|
||
private static Envelope.EventType toDropsondeEventType(EventType eventType) { | ||
switch (eventType) { | ||
case ERROR: | ||
return Envelope.EventType.Error; | ||
case CONTAINER_METRIC: | ||
return Envelope.EventType.ContainerMetric; | ||
case COUNTER_EVENT: | ||
return Envelope.EventType.CounterEvent; | ||
case HTTP_START_STOP: | ||
return Envelope.EventType.HttpStartStop; | ||
case LOG_MESSAGE: | ||
return Envelope.EventType.LogMessage; | ||
case VALUE_METRIC: | ||
return Envelope.EventType.ValueMetric; | ||
} | ||
throw new IllegalArgumentException("Unknown event type: " + eventType); | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
...ava/org/springframework/cloud/appbroker/logging/recent/ApplicationRecentLogsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2002-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.appbroker.logging.recent; | ||
|
||
import org.cloudfoundry.client.CloudFoundryClient; | ||
import org.cloudfoundry.client.v2.applications.GetApplicationRequest; | ||
import org.cloudfoundry.doppler.DopplerClient; | ||
import org.cloudfoundry.doppler.RecentLogsRequest; | ||
import org.cloudfoundry.dropsonde.events.Envelope; | ||
import reactor.core.publisher.Flux; | ||
|
||
import org.springframework.cloud.appbroker.logging.ApplicationIdsProvider; | ||
import org.springframework.cloud.appbroker.logging.LoggingUtils; | ||
|
||
class ApplicationRecentLogsProvider implements RecentLogsProvider { | ||
|
||
private final CloudFoundryClient client; | ||
|
||
private final DopplerClient dopplerClient; | ||
|
||
private final ApplicationIdsProvider applicationIdsProvider; | ||
|
||
public ApplicationRecentLogsProvider(CloudFoundryClient client, DopplerClient dopplerClient, | ||
ApplicationIdsProvider applicationIdsProvider) { | ||
this.client = client; | ||
this.dopplerClient = dopplerClient; | ||
this.applicationIdsProvider = applicationIdsProvider; | ||
} | ||
|
||
@Override | ||
public Flux<Envelope> getLogs(String serviceInstanceId) { | ||
return this.applicationIdsProvider.getApplicationIds(serviceInstanceId) | ||
.flatMap(this::recentLogs) | ||
.map(LoggingUtils::convertDopplerEnvelopeToDropsonde); | ||
} | ||
|
||
protected Flux<org.cloudfoundry.doppler.Envelope> recentLogs(String applicationId) { | ||
return client.applicationsV2().get(GetApplicationRequest.builder().applicationId(applicationId).build()) | ||
.map(response -> response.getEntity().getName()) | ||
.flatMapMany(appName -> | ||
dopplerClient.recentLogs(RecentLogsRequest.builder().applicationId(applicationId).build()) | ||
.map(envelope -> LoggingUtils.injectAppNameIntoLogSourceInstance(appName, envelope)) | ||
); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
.../src/main/java/org/springframework/cloud/appbroker/logging/recent/RecentLogsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2002-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.appbroker.logging.recent; | ||
|
||
import org.cloudfoundry.dropsonde.events.Envelope; | ||
import reactor.core.publisher.Flux; | ||
|
||
public interface RecentLogsProvider { | ||
|
||
Flux<Envelope> getLogs(String serviceInstanceId); | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...gframework/cloud/appbroker/logging/recent/ServiceInstanceRecentLogsAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2002-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.appbroker.logging.recent; | ||
|
||
import org.cloudfoundry.client.CloudFoundryClient; | ||
import org.cloudfoundry.doppler.DopplerClient; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.cloud.appbroker.logging.ApplicationIdsProvider; | ||
import org.springframework.cloud.appbroker.logging.recent.endpoint.RecentLogsController; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@ConditionalOnBean(ApplicationIdsProvider.class) | ||
public class ServiceInstanceRecentLogsAutoConfiguration { | ||
|
||
@Bean | ||
public RecentLogsProvider recentLogsProvider( | ||
CloudFoundryClient cloudFoundryClient, | ||
DopplerClient dopplerClient, | ||
ApplicationIdsProvider applicationIdsProvider) { | ||
return new ApplicationRecentLogsProvider(cloudFoundryClient, dopplerClient, applicationIdsProvider); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public RecentLogsController recentLogsController(RecentLogsProvider recentLogsProvider) { | ||
return new RecentLogsController(recentLogsProvider); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...n/java/org/springframework/cloud/appbroker/logging/recent/endpoint/EncodingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2002-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.appbroker.logging.recent.endpoint; | ||
|
||
class EncodingException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 1837485200518028161L; | ||
|
||
public EncodingException(Throwable throwable) { | ||
super("Failed to encode: " + throwable.getMessage(), throwable); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...ava/org/springframework/cloud/appbroker/logging/recent/endpoint/LogMessageComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2002-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.appbroker.logging.recent.endpoint; | ||
|
||
import java.util.Comparator; | ||
|
||
import org.cloudfoundry.dropsonde.events.Envelope; | ||
|
||
class LogMessageComparator implements Comparator<Envelope> { | ||
|
||
@Override | ||
public int compare(Envelope o1, Envelope o2) { | ||
return Long.compare(getTimestamp(o1), getTimestamp(o2)); | ||
} | ||
|
||
private long getTimestamp(Envelope e) { | ||
if (e.logMessage != null && e.logMessage.timestamp != null) { | ||
return e.logMessage.timestamp; | ||
} | ||
if (e.timestamp == null) { | ||
return 0; | ||
} | ||
else { | ||
return e.timestamp; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.