Skip to content

Commit

Permalink
Add support of service instance logging endpoints
Browse files Browse the repository at this point in the history
The endpoint is compatible with service instance logs CF CLI plugin

https://github.com/pivotal-cf/service-instance-logs-cli-plugin
  • Loading branch information
alek-sys authored and royclarkson committed Apr 30, 2020
1 parent 53dec44 commit 01bb402
Show file tree
Hide file tree
Showing 26 changed files with 1,474 additions and 0 deletions.
1 change: 1 addition & 0 deletions settings.gradle
Expand Up @@ -8,5 +8,6 @@ include "spring-cloud-app-broker-autoconfigure"
include "spring-cloud-app-broker-integration-tests"
include "spring-cloud-app-broker-acceptance-tests"
include "spring-cloud-app-broker-security-credhub"
include "spring-cloud-app-broker-logging"
include "spring-cloud-starter-app-broker"
include "spring-cloud-starter-app-broker-cloudfoundry"
27 changes: 27 additions & 0 deletions spring-cloud-app-broker-logging/build.gradle
@@ -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}"
}
@@ -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);

}
@@ -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);
}

}
@@ -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))
);
}

}
@@ -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);

}
@@ -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);
}

}
@@ -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);
}

}
@@ -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;
}
}

}

0 comments on commit 01bb402

Please sign in to comment.