From 703933895bdc9f46d0018fc26d9f72b606f4627e Mon Sep 17 00:00:00 2001 From: hanahmily Date: Tue, 27 Feb 2018 17:29:48 +0800 Subject: [PATCH 1/5] Add apm-webapp module --- apm-webapp/pom.xml | 157 ++++++++++++++++++ .../apm/webapp/ApplicationStartUp.java | 42 +++++ .../apm/webapp/config/UIConfig.java | 36 ++++ .../apm/webapp/tools/CollectorServerList.java | 89 ++++++++++ .../apm/webapp/tools/HttpClientTools.java | 73 ++++++++ .../apm/webapp/tools/RewritePathFilter.java | 65 ++++++++ apm-webapp/src/main/resources/application.yml | 14 ++ pom.xml | 11 +- 8 files changed, 482 insertions(+), 5 deletions(-) create mode 100644 apm-webapp/pom.xml create mode 100644 apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/ApplicationStartUp.java create mode 100644 apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/config/UIConfig.java create mode 100644 apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/tools/CollectorServerList.java create mode 100644 apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/tools/HttpClientTools.java create mode 100644 apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/tools/RewritePathFilter.java create mode 100644 apm-webapp/src/main/resources/application.yml diff --git a/apm-webapp/pom.xml b/apm-webapp/pom.xml new file mode 100644 index 000000000..95119a833 --- /dev/null +++ b/apm-webapp/pom.xml @@ -0,0 +1,157 @@ + + + + apm + org.apache.skywalking + 5.0.0-alpha-SNAPSHOT + + 4.0.0 + + apm-webapp + jar + + 1.8 + UTF-8 + 1.5.10.RELEASE + 2.6.2 + ${project.parent.basedir}/skywalking-ui/src/main/frontend + + + + + + org.springframework.cloud + spring-cloud-dependencies + Edgware.SR1 + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + ${spring.boot.version} + + + org.springframework.boot + spring-boot-starter-actuator + ${spring.boot.version} + + + org.springframework.boot + spring-boot-configuration-processor + ${spring.boot.version} + + + org.springframework.boot + spring-boot-devtools + ${spring.boot.version} + + + com.google.code.gson + gson + 2.8.2 + + + org.apache.httpcomponents + httpclient + 4.5.3 + + + org.springframework.cloud + spring-cloud-starter-config + + + org.springframework.cloud + spring-cloud-starter-netflix-zuul + + + + + skywalking-webapp + + + + + + maven-compiler-plugin + + ${compiler.version} + ${compiler.version} + ${project.build.sourceEncoding} + + + + com.github.eirslett + frontend-maven-plugin + 1.6 + + ${ui.path} + v8.9.4 + + + + install node and npm + + install-node-and-npm + + + + npm install + + npm + + + install --registry=http://registry.npmjs.org/ + + + + npm run build + + npm + + + run build + + + + + + org.apache.maven.plugins + maven-resources-plugin + + ${project.build.sourceEncoding} + ${project.build.directory} + + + ${basedir}/target/classes/public + ${ui.path}/dist + + + ${basedir}/target/classes + src/main/resources + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring.boot.version} + + + + repackage + + + + + + + + \ No newline at end of file diff --git a/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/ApplicationStartUp.java b/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/ApplicationStartUp.java new file mode 100644 index 000000000..5f3fe6102 --- /dev/null +++ b/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/ApplicationStartUp.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.skywalking.apm.webapp; + +import org.apache.skywalking.apm.webapp.config.UIConfig; +import org.apache.skywalking.apm.webapp.tools.RewritePathFilter; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.support.SpringBootServletInitializer; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +@EnableZuulProxy +public class ApplicationStartUp extends SpringBootServletInitializer { + + public static void main(String[] args) throws Exception { + ApplicationContext applicationContext = SpringApplication.run(ApplicationStartUp.class, args); + } + + @Bean + public RewritePathFilter addWritePathFilter(UIConfig uiConfig) { + return new RewritePathFilter(uiConfig.getRewritePath()); + } +} diff --git a/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/config/UIConfig.java b/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/config/UIConfig.java new file mode 100644 index 000000000..c5a1f298c --- /dev/null +++ b/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/config/UIConfig.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.skywalking.apm.webapp.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +/** + * @author peng-yongsheng + */ +@Configuration +public class UIConfig { + + @Value("${collector.path}") + private String rewritePath; + + public String getRewritePath() { + return rewritePath; + } +} diff --git a/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/tools/CollectorServerList.java b/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/tools/CollectorServerList.java new file mode 100644 index 000000000..95f3c2ecf --- /dev/null +++ b/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/tools/CollectorServerList.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.skywalking.apm.webapp.tools; + +import com.google.common.base.Strings; +import com.google.common.collect.Lists; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.netflix.client.config.CommonClientConfigKey; +import com.netflix.client.config.IClientConfig; +import com.netflix.loadbalancer.AbstractServerList; +import com.netflix.loadbalancer.Server; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +public class CollectorServerList extends AbstractServerList { + private static final Logger logger = LoggerFactory.getLogger(CollectorServerList.class); + + private final CopyOnWriteArrayList servers = new CopyOnWriteArrayList<>(); + + private final Gson gson = new Gson(); + + private IClientConfig clientConfig; + + public List getInitialListOfServers() { + return fetchServer(); + } + + public List getUpdatedListOfServers() { + return fetchServer(); + } + + public void initWithNiwsConfig(IClientConfig clientConfig) { + this.clientConfig = clientConfig; + } + + protected List derive(String value) { + List list = Lists.newArrayList(); + if (Strings.isNullOrEmpty(value)) { + return list; + } + String[] serverArray = value.split(","); + for (String s : serverArray) { + list.add(new Server(s.trim())); + } + return list; + } + + private List fetchServer() { + for (Server server : derive(this.clientConfig.get(CommonClientConfigKey.ListOfServers))) { + try { + String uiServerResponse = HttpClientTools.INSTANCE.get("http://" + server.getHostPort() + "/ui/jetty", null); + logger.debug("uiServerResponse: {}", uiServerResponse); + JsonArray serverArray = gson.fromJson(uiServerResponse, JsonArray.class); + if (serverArray == null || serverArray.size() == 0) { + logger.warn("emtry grpc server array, skip : {}", server); + continue; + } + return Lists.newArrayList(StreamSupport.stream(serverArray.spliterator(), false).map(f -> new Server(f.getAsString())).collect(Collectors.toList())); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + } + logger.warn("none agentstream server return available grpc server."); + return servers; + } +} diff --git a/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/tools/HttpClientTools.java b/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/tools/HttpClientTools.java new file mode 100644 index 000000000..da162a83b --- /dev/null +++ b/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/tools/HttpClientTools.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.skywalking.apm.webapp.tools; + +import org.apache.http.HttpEntity; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.URI; +import java.util.List; + +/** + * @author peng-yongsheng + */ +public enum HttpClientTools { + INSTANCE; + + private Logger logger = LoggerFactory.getLogger(HttpClientTools.class); + + public String get(String url, List params) throws IOException { + CloseableHttpClient httpClient = HttpClients.createDefault(); + try { + HttpGet httpget = new HttpGet(url); + if (params == null) { + httpget.setURI(new URI(httpget.getURI().toString())); + } else { + String paramStr = EntityUtils.toString(new UrlEncodedFormEntity(params)); + httpget.setURI(new URI(httpget.getURI().toString() + "?" + paramStr)); + } + logger.debug("executing get request %s", httpget.getURI()); + + try (CloseableHttpResponse response = httpClient.execute(httpget)) { + HttpEntity entity = response.getEntity(); + if (entity != null) { + return EntityUtils.toString(entity); + } + } + } catch (Exception e) { + logger.warn("bad url=" + url, e); + } finally { + try { + httpClient.close(); + } catch (IOException e) { + logger.warn("bad url=" + url, e); + } + } + return null; + } +} diff --git a/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/tools/RewritePathFilter.java b/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/tools/RewritePathFilter.java new file mode 100644 index 000000000..e3303f821 --- /dev/null +++ b/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/tools/RewritePathFilter.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.skywalking.apm.webapp.tools; + +import com.netflix.zuul.ZuulFilter; +import com.netflix.zuul.context.RequestContext; + +import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER; + +/** + * Rewrite url to rewritePath + * + * @author gaohongtao + */ +public class RewritePathFilter extends ZuulFilter { + + private static final String REQUEST_URI = "requestURI"; + + private static final int ORDER = PRE_DECORATION_FILTER_ORDER + 1; + + public RewritePathFilter(String rewritePath) { + this.rewritePath = rewritePath; + } + + private final String rewritePath; + + @Override + public int filterOrder() { + return ORDER; + } + + @Override + public String filterType() { + return "pre"; + } + + @Override + public boolean shouldFilter() { + RequestContext ctx = RequestContext.getCurrentContext(); + return ctx.containsKey(REQUEST_URI); + } + + @Override + public Object run() { + RequestContext ctx = RequestContext.getCurrentContext(); + ctx.set(REQUEST_URI, rewritePath); + return null; + } +} diff --git a/apm-webapp/src/main/resources/application.yml b/apm-webapp/src/main/resources/application.yml new file mode 100644 index 000000000..b88a906e0 --- /dev/null +++ b/apm-webapp/src/main/resources/application.yml @@ -0,0 +1,14 @@ +server: + port: 8080 +zuul: + ignoredServices: '*' + routes: + api: + path: /api/** + serviceId: collector + +collector: + path: /graphql + ribbon: + listOfServers: 127.0.0.1:10800 + NIWSServerListClassName: org.apache.skywalking.apm.webapp.tools.CollectorServerList diff --git a/pom.xml b/pom.xml index ba3e8bb78..a2f22138a 100644 --- a/pom.xml +++ b/pom.xml @@ -58,11 +58,12 @@ - apm-commons - apm-sniffer - apm-application-toolkit - apm-collector - apm-protocol + + + + + + apm-webapp pom From 9e6160eeef98e17225f9816d3d9e743db31a504f Mon Sep 17 00:00:00 2001 From: hanahmily Date: Wed, 28 Feb 2018 14:32:57 +0800 Subject: [PATCH 2/5] Update submodule skywalking-ui --- apm-webapp/pom.xml | 2 +- skywalking-ui | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apm-webapp/pom.xml b/apm-webapp/pom.xml index 95119a833..b0dd76f69 100644 --- a/apm-webapp/pom.xml +++ b/apm-webapp/pom.xml @@ -16,7 +16,7 @@ UTF-8 1.5.10.RELEASE 2.6.2 - ${project.parent.basedir}/skywalking-ui/src/main/frontend + ${project.parent.basedir}/skywalking-ui diff --git a/skywalking-ui b/skywalking-ui index d4e49aa05..73d211b10 160000 --- a/skywalking-ui +++ b/skywalking-ui @@ -1 +1 @@ -Subproject commit d4e49aa0581e510884e85cc145e647dcb179f5a1 +Subproject commit 73d211b10a4158190c0b8188e5ed8efb2499adfc From d5ce8fdfc45ddad8fb107d60a47bccc4739c230d Mon Sep 17 00:00:00 2001 From: hanahmily Date: Wed, 28 Feb 2018 14:41:52 +0800 Subject: [PATCH 3/5] Add travis debug --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8b2c7b1d4..84e4b1395 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,6 @@ before_install: install: - jdk_switcher use oraclejdk8 - - mvn clean install --quiet jacoco:report coveralls:report + - mvn clean install --quiet jacoco:report coveralls:report -X - mvn clean javadoc:javadoc -Dmaven.test.skip=true --quiet From 19c58e07d329f48fbbdf6e1dc5217a6090485aac Mon Sep 17 00:00:00 2001 From: hanahmily Date: Wed, 28 Feb 2018 15:34:00 +0800 Subject: [PATCH 4/5] Add test case --- .travis.yml | 2 +- .../webapp/tools/RewritePathFilterTest.java | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 apm-webapp/src/test/java/org/apache/skywalking/apm/webapp/tools/RewritePathFilterTest.java diff --git a/.travis.yml b/.travis.yml index 84e4b1395..8b2c7b1d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,6 @@ before_install: install: - jdk_switcher use oraclejdk8 - - mvn clean install --quiet jacoco:report coveralls:report -X + - mvn clean install --quiet jacoco:report coveralls:report - mvn clean javadoc:javadoc -Dmaven.test.skip=true --quiet diff --git a/apm-webapp/src/test/java/org/apache/skywalking/apm/webapp/tools/RewritePathFilterTest.java b/apm-webapp/src/test/java/org/apache/skywalking/apm/webapp/tools/RewritePathFilterTest.java new file mode 100644 index 000000000..b856fb608 --- /dev/null +++ b/apm-webapp/src/test/java/org/apache/skywalking/apm/webapp/tools/RewritePathFilterTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.skywalking.apm.webapp.tools; + +import com.netflix.zuul.context.RequestContext; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.*; +import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER; + +public class RewritePathFilterTest { + + private RewritePathFilter filter; + + @Before + public void init() { + filter = new RewritePathFilter("/graphql"); + } + + @Test + public void filterOrder() { + assertThat(filter.filterOrder(), is(PRE_DECORATION_FILTER_ORDER + 1)); + } + + @Test + public void filterType() { + assertThat(filter.filterType(), is("pre")); + } + + @Test + public void shouldFilter() { + assertFalse(filter.shouldFilter()); + RequestContext.getCurrentContext().set("requestURI"); + assertTrue(filter.shouldFilter()); + } + + @Test + public void run() { + filter.run(); + assertThat(RequestContext.getCurrentContext().get("requestURI"), is("/graphql")); + } +} \ No newline at end of file From 022f7033eea45bc96d10180b92ed9cb56986d2a8 Mon Sep 17 00:00:00 2001 From: hanahmily Date: Wed, 28 Feb 2018 15:40:29 +0800 Subject: [PATCH 5/5] Overhaul pom.xml --- pom.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index a2f22138a..08e93f514 100644 --- a/pom.xml +++ b/pom.xml @@ -58,11 +58,11 @@ - - - - - + apm-commons + apm-sniffer + apm-application-toolkit + apm-collector + apm-protocol apm-webapp pom