diff options
author | Ola Aunrønning <ola.aunroe@gmail.com> | 2018-08-24 14:54:27 +0200 |
---|---|---|
committer | Ola Aunrønning <ola.aunroe@gmail.com> | 2018-09-10 13:07:15 +0200 |
commit | 44053e445e8f893514667325be40613830056e6e (patch) | |
tree | bd1f6be2b7ec2f419d2a9363de96fecb32988152 /configserver | |
parent | 79fbe75a324084d7e871e4aa7b82500e7ccd35b3 (diff) |
Add functionality for retrieving logs
Diffstat (limited to 'configserver')
5 files changed, 116 insertions, 1 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java index a8b4844ca43..585a1a67f4d 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java @@ -36,6 +36,7 @@ import com.yahoo.vespa.config.server.configchange.RestartActions; import com.yahoo.vespa.config.server.deploy.DeployHandlerLogger; import com.yahoo.vespa.config.server.deploy.Deployment; import com.yahoo.vespa.config.server.http.CompressedApplicationInputStream; +import com.yahoo.vespa.config.server.http.LogRetriever; import com.yahoo.vespa.config.server.http.SimpleHttpFetcher; import com.yahoo.vespa.config.server.http.v2.PrepareResult; import com.yahoo.vespa.config.server.provision.HostProvisionerProvider; @@ -50,6 +51,7 @@ import com.yahoo.vespa.config.server.session.SilentDeployLogger; import com.yahoo.vespa.config.server.tenant.Rotations; import com.yahoo.vespa.config.server.tenant.Tenant; import com.yahoo.vespa.config.server.tenant.TenantRepository; +import com.yahoo.vespa.model.VespaModel; import java.io.File; import java.io.IOException; @@ -477,6 +479,13 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye return convergeChecker.servicesToCheck(getApplication(applicationId), uri, timeout); } + // ---------------- Logs ---------------------------------------------------------------- + + public HttpResponse getLogs(ApplicationId applicationId) { + String logServerHostName = getLogServerHostname(applicationId); + return LogRetriever.getLogs(logServerHostName); + } + // ---------------- Session operations ---------------------------------------------------------------- /** @@ -690,6 +699,13 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye } } + private String getLogServerHostname(ApplicationId applicationId) { + Application application = getApplication(applicationId); + VespaModel model = (VespaModel) application.getModel(); + String logServerHostname = model.getAdmin().getLogserver().getHostName(); + return logServerHostname; + } + /** Returns version to use when deploying application in given environment */ static Version decideVersion(ApplicationId application, Environment environment, Version targetVersion, boolean bootstrap) { if (environment.isManuallyDeployed() && diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/http/LogRetriever.java b/configserver/src/main/java/com/yahoo/vespa/config/server/http/LogRetriever.java new file mode 100644 index 00000000000..f4952b5b9fc --- /dev/null +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/http/LogRetriever.java @@ -0,0 +1,41 @@ +package com.yahoo.vespa.config.server.http; + +import com.yahoo.container.jdisc.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.logging.Level; +import java.util.logging.Logger; + + +public class LogRetriever { + + private final static Logger log = Logger.getLogger(LogRetriever.class.getName()); + + public static HttpResponse getLogs(String logServerHostname) { + HttpGet get = new HttpGet(logServerHostname); + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { + org.apache.http.HttpResponse response = httpClient.execute(get); + String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); + return new HttpResponse(response.getStatusLine().getStatusCode()) { + @Override + public void render(OutputStream outputStream) throws IOException { + if (response.getEntity() != null ) outputStream.write(responseBody.getBytes()); + } + }; + } catch (IOException e) { + log.log(Level.WARNING, "Failed to retrieve logs from log server", e); + return new HttpResponse(404) { + @Override + public void render(OutputStream outputStream) throws IOException { + outputStream.write(e.toString().getBytes()); + } + }; + } + + } +} diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java index 2004ab95144..b65cb370f93 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java @@ -96,6 +96,10 @@ public class ApplicationHandler extends HttpHandler { return applicationRepository.filedistributionStatus(applicationId, timeout); } + if (isLogRequest(request)) { + return applicationRepository.getLogs(applicationId); + } + return new GetApplicationResponse(Response.Status.OK, applicationRepository.getApplicationGeneration(applicationId)); } @@ -140,7 +144,13 @@ public class ApplicationHandler extends HttpHandler { "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*/serviceconverge/*", "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*/clustercontroller/*/status/*", "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*", - "http://*/application/v2/tenant/*/application/*"); + "http://*/application/v2/tenant/*/application/*", + "http://*/application/v2/tenant/*/application/*/logs"); + } + + private static boolean isLogRequest(HttpRequest request) { + return getBindingMatch(request).groupCount() == 4 && + request.getUri().getPath().endsWith("/logs"); } private static boolean isServiceConvergeListRequest(HttpRequest request) { diff --git a/configserver/src/main/resources/configserver-app/services.xml b/configserver/src/main/resources/configserver-app/services.xml index 8a99869e69a..60dd7b0cea2 100644 --- a/configserver/src/main/resources/configserver-app/services.xml +++ b/configserver/src/main/resources/configserver-app/services.xml @@ -147,6 +147,8 @@ <binding>https://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*</binding> <binding>http://*/application/v2/tenant/*/application/*</binding> <binding>https://*/application/v2/tenant/*/application/*</binding> + <binding>http://*/application/v2/tenant/*/application/*/logs</binding> + <binding>https://*/application/v2/tenant/*/application/*/logs</binding> </handler> <handler id='com.yahoo.vespa.config.server.http.v2.HttpGetConfigHandler' bundle='configserver'> <binding>http://*/config/v2/tenant/*/application/*/*</binding> diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/LogRetrieverTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/LogRetrieverTest.java new file mode 100644 index 00000000000..2c3622e3dac --- /dev/null +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/LogRetrieverTest.java @@ -0,0 +1,46 @@ +package com.yahoo.vespa.config.server.http; + +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import com.yahoo.container.jdisc.HttpResponse; +import org.junit.Rule; +import org.junit.Test; + + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.okJson; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.junit.Assert.assertEquals; + +public class LogRetrieverTest { + + private String logServerHostName = "http://localhost:8080/"; + @Rule + public final WireMockRule wireMock = new WireMockRule(options().port(8080), true); + + @Test + public void testThatLogHandlerPropagatesResponseBody() throws IOException { + String expectedBody = "{logs-json}"; + stubFor(get(urlEqualTo("/")).willReturn(okJson(expectedBody))); + HttpResponse response = LogRetriever.getLogs(logServerHostName); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + response.render(byteArrayOutputStream); + assertEquals(expectedBody, byteArrayOutputStream.toString()); + assertEquals(200, response.getStatus()); + } + + @Test + public void testThatNotFoundLogServerReturns404() throws IOException { + stubFor(get(urlEqualTo("/")).willReturn(aResponse().withStatus(200))); + HttpResponse response = LogRetriever.getLogs("http://wrong-host:8080/"); + assertEquals(404, response.getStatus()); + } + + + +}
\ No newline at end of file |