From 587a7bfc81bf27f540a29616ee2fbfa5a74f66c4 Mon Sep 17 00:00:00 2001 From: ayolajayamaha Date: Tue, 2 Aug 2016 10:37:50 +0530 Subject: [PATCH 01/12] zipkin --- phoenix-zipkin/pom.xml | 43 +++++++++++++++++++++++++ phoenix-zipkin/src/example/Example.java | 24 ++++++++++++++ phoenix-zipkin/src/example/Trace.java | 17 ++++++++++ 3 files changed, 84 insertions(+) create mode 100644 phoenix-zipkin/pom.xml create mode 100644 phoenix-zipkin/src/example/Example.java create mode 100644 phoenix-zipkin/src/example/Trace.java diff --git a/phoenix-zipkin/pom.xml b/phoenix-zipkin/pom.xml new file mode 100644 index 00000000000..55015f31b3c --- /dev/null +++ b/phoenix-zipkin/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + com.example + myproject + 0.0.1-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.3.6.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-web + + + io.zipkin.java + zipkin-server + 1.5.0 + + + io.zipkin.java + zipkin-autoconfigure-storage-mysql + 1.5.0 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/phoenix-zipkin/src/example/Example.java b/phoenix-zipkin/src/example/Example.java new file mode 100644 index 00000000000..3a4bac4f85c --- /dev/null +++ b/phoenix-zipkin/src/example/Example.java @@ -0,0 +1,24 @@ +import org.springframework.boot.*; +import org.springframework.boot.autoconfigure.*; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.*; +import org.springframework.web.bind.annotation.*; + +@RestController +@EnableAutoConfiguration +public class Example { + + + @RequestMapping(value="/trace", method=RequestMethod.GET) + public ResponseEntity get() { + + Trace t = new Trace("1","upsert into mytable"); + return new ResponseEntity(t, HttpStatus.OK); + } + + public static void main(String[] args) throws Exception { + SpringApplication.run(Example.class, args); + } + +} \ No newline at end of file diff --git a/phoenix-zipkin/src/example/Trace.java b/phoenix-zipkin/src/example/Trace.java new file mode 100644 index 00000000000..f00f868f7bd --- /dev/null +++ b/phoenix-zipkin/src/example/Trace.java @@ -0,0 +1,17 @@ +public class Trace{ +private final String id; + private final String name; + + public Trace(String id, String name) { + this.id = id; + this.name = name; + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } +} \ No newline at end of file From 34ffcba5c3c5eaedda95b0919a9736eaadf39889 Mon Sep 17 00:00:00 2001 From: ayolajayamaha Date: Tue, 2 Aug 2016 21:41:35 +0530 Subject: [PATCH 02/12] experimenting phoenix tracing to zipkin tracing --- phoenix-zipkin/pom.xml | 6 +++--- phoenix-zipkin/src/example/Example.java | 16 ++++++++++------ pom.xml | 1 + 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/phoenix-zipkin/pom.xml b/phoenix-zipkin/pom.xml index 55015f31b3c..6e18b9d5f92 100644 --- a/phoenix-zipkin/pom.xml +++ b/phoenix-zipkin/pom.xml @@ -4,7 +4,6 @@ 4.0.0 com.example - myproject 0.0.1-SNAPSHOT @@ -19,7 +18,7 @@ org.springframework.boot spring-boot-starter-web - + @@ -39,5 +38,6 @@ + myproject diff --git a/phoenix-zipkin/src/example/Example.java b/phoenix-zipkin/src/example/Example.java index 3a4bac4f85c..9b300b66361 100644 --- a/phoenix-zipkin/src/example/Example.java +++ b/phoenix-zipkin/src/example/Example.java @@ -9,16 +9,20 @@ @EnableAutoConfiguration public class Example { - - @RequestMapping(value="/trace", method=RequestMethod.GET) - public ResponseEntity get() { - - Trace t = new Trace("1","upsert into mytable"); - return new ResponseEntity(t, HttpStatus.OK); + + @RequestMapping("/greet") + String sayHello(@RequestParam("name") String name) { + if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("The 'name' parameter must not be null or empty"); + } + return String.format("Hello %s!", name); } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } + + + } \ No newline at end of file diff --git a/pom.xml b/pom.xml index e7e7ccf34ba..1b9c4afb766 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,7 @@ phoenix-hive phoenix-assembly phoenix-tracing-webapp + phoenix-zipkin From 11dbcd15fdd0f506964687a273cb0a98da16dae2 Mon Sep 17 00:00:00 2001 From: ayolajayamaha Date: Fri, 5 Aug 2016 10:05:56 +0530 Subject: [PATCH 03/12] zipkin application --- phoenix-zipkin/pom.xml | 100 +++++++++++++----- phoenix-zipkin/src/example/Example.java | 28 ----- phoenix-zipkin/src/example/Trace.java | 17 --- .../zipkin/PhoenixZipkinController.java | 50 +++++++++ .../phoenix/zipkin/ZipkinApplication.java | 37 +++++++ 5 files changed, 160 insertions(+), 72 deletions(-) delete mode 100644 phoenix-zipkin/src/example/Example.java delete mode 100644 phoenix-zipkin/src/example/Trace.java create mode 100644 phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java create mode 100644 phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java diff --git a/phoenix-zipkin/pom.xml b/phoenix-zipkin/pom.xml index 6e18b9d5f92..319f1a6dd22 100644 --- a/phoenix-zipkin/pom.xml +++ b/phoenix-zipkin/pom.xml @@ -3,8 +3,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.example - 0.0.1-SNAPSHOT + org.springframework + gs-spring-boot + 0.1.0 org.springframework.boot @@ -12,32 +13,77 @@ 1.3.6.RELEASE - - - org.springframework.boot - spring-boot-starter-web - - + + org.springframework.boot + spring-boot-starter-web + + + io.zipkin.java + zipkin-ui + 1.5.1 + + + io.zipkin.java + zipkin-server + 1.5.1 + + + io.zipkin.java + zipkin-autoconfigure-ui + 1.5.1 + + + + + + org.apache.phoenix + phoenix-core + 4.8.0-HBase-1.2-SNAPSHOT + + + org.slf4j + slf4j-log4j12 + + + log4j + log4j + + + + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - - myproject - + + 1.8 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + maven-failsafe-plugin + + + + integration-test + verify + + + + + + + + + diff --git a/phoenix-zipkin/src/example/Example.java b/phoenix-zipkin/src/example/Example.java deleted file mode 100644 index 9b300b66361..00000000000 --- a/phoenix-zipkin/src/example/Example.java +++ /dev/null @@ -1,28 +0,0 @@ -import org.springframework.boot.*; -import org.springframework.boot.autoconfigure.*; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.*; -import org.springframework.web.bind.annotation.*; - -@RestController -@EnableAutoConfiguration -public class Example { - - - @RequestMapping("/greet") - String sayHello(@RequestParam("name") String name) { - if (name == null || name.isEmpty()) { - throw new IllegalArgumentException("The 'name' parameter must not be null or empty"); - } - return String.format("Hello %s!", name); - } - - public static void main(String[] args) throws Exception { - SpringApplication.run(Example.class, args); - } - - - - -} \ No newline at end of file diff --git a/phoenix-zipkin/src/example/Trace.java b/phoenix-zipkin/src/example/Trace.java deleted file mode 100644 index f00f868f7bd..00000000000 --- a/phoenix-zipkin/src/example/Trace.java +++ /dev/null @@ -1,17 +0,0 @@ -public class Trace{ -private final String id; - private final String name; - - public Trace(String id, String name) { - this.id = id; - this.name = name; - } - - public String getId() { - return id; - } - - public String getName() { - return name; - } -} \ No newline at end of file diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java new file mode 100644 index 00000000000..69e277de7ac --- /dev/null +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java @@ -0,0 +1,50 @@ +package org.apache.phoenix.zipkin; + +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.RequestMapping; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.PreparedStatement; +import java.sql.Statement; + +@RestController +public class PhoenixZipkinController { + + @RequestMapping("/") + public String index() { + return "Greetings from Spring Boot!"; + } + @RequestMapping("/foo") + public String foo() throws SQLException{ + Statement stmt = null; + ResultSet rset = null; +String rString = ""; +try{ + Connection con = DriverManager.getConnection("jdbc:phoenix:localhost:2181"); + stmt = con.createStatement(); + + PreparedStatement statement = con.prepareStatement("select * from SYSTEM.TRACING_STATS"); + rset = statement.executeQuery(); + while (rset.next()) { + System.out.println(rset.getString("mycolumn")); +rString += rset.getString("mycolumn"); + } + statement.close(); + con.close(); +}catch(SQLException e){ +throw (e); +} + + return "Greetings from foo!, "+rString; + } +@RequestMapping("/api/v1/services") + public String services() { + return "[\"phoenix-server\"]"; + } +@RequestMapping("/api/v1/spans") + public String spans() { + return "[]"; + } +} diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java new file mode 100644 index 00000000000..ab246a75140 --- /dev/null +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java @@ -0,0 +1,37 @@ +package org.apache.phoenix.zipkin; + +import java.util.Arrays; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; +//import org.springframework.cloud.sleuth.zipkin.stream.EnableZipkinStreamServer; + + +/*@SpringBootApplication +@EnableZipkinStreamServer +public class ZipkinApplication { + + public static void main(String[] args) { + SpringApplication.run(ZipkinApplication.class, args); + } + + + +} +*/ +@SpringBootApplication +public class ZipkinApplication { + + public static void main(String[] args) { + ApplicationContext ctx = SpringApplication.run(ZipkinApplication.class, args); + + System.out.println("Let's inspect the beans provided by Spring Boot:"); + + String[] beanNames = ctx.getBeanDefinitionNames(); + Arrays.sort(beanNames); + for (String beanName : beanNames) { + System.out.println(beanName); + } + } +} \ No newline at end of file From bc4e31e7f6aa5acbe2b195eccaae4dcfbaa19cd7 Mon Sep 17 00:00:00 2001 From: ayolajayamaha Date: Sat, 6 Aug 2016 11:11:46 +0530 Subject: [PATCH 04/12] UI changes to rest --- phoenix-zipkin/pom.xml | 2 +- .../phoenix/zipkin/ConnectionFactory.java | 43 ++++ .../apache/phoenix/zipkin/EntityFactory.java | 101 ++++++++ .../zipkin/PhoenixZipkinController.java | 242 +++++++++++++++--- .../phoenix/zipkin/ZipkinApplication.java | 14 +- 5 files changed, 359 insertions(+), 43 deletions(-) create mode 100644 phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ConnectionFactory.java create mode 100644 phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/EntityFactory.java diff --git a/phoenix-zipkin/pom.xml b/phoenix-zipkin/pom.xml index 319f1a6dd22..ab978d660d7 100644 --- a/phoenix-zipkin/pom.xml +++ b/phoenix-zipkin/pom.xml @@ -4,7 +4,7 @@ 4.0.0 org.springframework - gs-spring-boot + phoenix-zipkin 0.1.0 diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ConnectionFactory.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ConnectionFactory.java new file mode 100644 index 00000000000..533219a467f --- /dev/null +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ConnectionFactory.java @@ -0,0 +1,43 @@ +/* + * 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.phoenix.zipkin; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +/** +* +* ConnectionFactory is to handle database connection +* +*/ +public class ConnectionFactory { + + private static Connection con; + protected static String PHOENIX_HOST = "localhost"; + protected static int PHOENIX_PORT = 2181; + + public static Connection getConnection() throws SQLException, ClassNotFoundException { + if (con == null || con.isClosed()) { + Class.forName("org.apache.phoenix.jdbc.PhoenixDriver"); + con = DriverManager.getConnection("jdbc:phoenix:"+PHOENIX_HOST+":"+PHOENIX_PORT); + } + return con; + } +} + diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/EntityFactory.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/EntityFactory.java new file mode 100644 index 00000000000..a2a10641118 --- /dev/null +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/EntityFactory.java @@ -0,0 +1,101 @@ +/* + * 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.phoenix.zipkin; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * EntityFactory is used to get results entities For SQL query + */ +public class EntityFactory { + + private String queryString; + protected Connection connection; + + public EntityFactory(Connection connection, String queryString) { + this.queryString = queryString; + this.connection = connection; + } + + public Map findSingle(Object[] params) throws SQLException { + List> objects = this.findMultiple(params); + + if (objects.size() != 1) { + throw new SQLException("Query did not produce one object it produced: " + + objects.size() + " objects."); + } + + Map object = objects.get(0); // get first record; + + return object; + } + + public List> findMultiple(Object[] params) + throws SQLException { + ResultSet rs = null; + PreparedStatement ps = null; + try { + ps = this.connection.prepareStatement(this.queryString); + for (int i = 0; i < params.length; ++i) { + ps.setObject(1, params[i]); + } + + rs = ps.executeQuery(); + return getEntitiesFromResultSet(rs); + } catch (SQLException e) { + throw (e); + } finally { + if (rs != null) { + rs.close(); + } + if (ps != null) { + ps.close(); + } + } + } + + protected static List> getEntitiesFromResultSet( + ResultSet resultSet) throws SQLException { + ArrayList> entities = new ArrayList<>(); + while (resultSet.next()) { + entities.add(getEntityFromResultSet(resultSet)); + } + return entities; + } + + protected static Map getEntityFromResultSet(ResultSet resultSet) + throws SQLException { + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + Map resultsMap = new HashMap<>(); + for (int i = 1; i <= columnCount; ++i) { + String columnName = metaData.getColumnName(i).toLowerCase(); + Object object = resultSet.getObject(i); + resultsMap.put(columnName, object); + } + return resultsMap; + } + +} diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java index 69e277de7ac..061d0f5b1fe 100644 --- a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java @@ -1,50 +1,228 @@ package org.apache.phoenix.zipkin; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jettison.json.JSONArray; +import org.codehaus.jettison.json.JSONException; +import org.codehaus.jettison.json.JSONObject; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.PathVariable; + import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.PreparedStatement; import java.sql.Statement; +import java.util.List; +import java.util.Map; @RestController public class PhoenixZipkinController { - @RequestMapping("/") - public String index() { - return "Greetings from Spring Boot!"; + private static Connection con; + + protected String DEFAULT_LIMIT = "10"; + + protected String DEFAULT_COUNTBY = "hostname"; + + protected String LOGIC_AND = "AND"; + + protected String LOGIC_OR = "OR"; + + protected String TRACING_TABLE = "SYSTEM.TRACING_STATS"; + + @RequestMapping("/") + public String index() { + return "Greetings from Spring Boot!"; + } + + @RequestMapping("/foo") + public String foo() throws SQLException { + Statement stmt = null; + ResultSet rset = null; + String rString = ""; + try { + Connection con = DriverManager.getConnection("jdbc:phoenix:localhost:2181"); + stmt = con.createStatement(); + + PreparedStatement statement = con.prepareStatement("select * from SYSTEM.TRACING_STATS"); + rset = statement.executeQuery(); + while (rset.next()) { + System.out.println(rset.getString("mycolumn")); + rString += rset.getString("mycolumn"); + } + statement.close(); + con.close(); + } catch (SQLException e) { + throw (e); + } + + return "Greetings from foo!, " + rString; + } + + @RequestMapping("/api/v1/services") + public String services() { + return "[\"phoenix-server\"]"; + } + + @RequestMapping("/api/v1/spans") + public String spans() { + return "[]"; + } + + @RequestMapping("/api/v2/traces") + public String traces() { + return "[[{\"traceId\":\"0d73ed4802216e0e\",\"name\":\"bootstrap\",\"id\":\"0d73ed4802216e0e\",\"timestamp\":1469940471944000,\"duration\":12054546,\"annotations\":[{\"endpoint\":{\"serviceName\":\"phoenix-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469940472204546,\"value\":\"ApplicationStarted\"},{\"endpoint\":{\"serviceName\":\"phoenix-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469940472892282,\"value\":\"ApplicationEnvironmentPrepared\"},{\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469940473470234,\"value\":\"ApplicationPrepared\"},{\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469940483864223,\"value\":\"ContextRefreshed\"},{\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469940483993406,\"value\":\"EmbeddedServletContainerInitialized\"},{\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469940483998543,\"value\":\"ApplicationReady\"}],\"binaryAnnotations\":[{\"key\":\"lc\",\"value\":\"spring-boot\",\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411}}]}]]"; + } + + @RequestMapping("/api/v1/traces") + public String tracesv2(@RequestParam(value = "limit", defaultValue = "10") String limit) + throws JSONException { + + String jsonarray = "[]"; + + jsonarray = getTraces(limit); + JSONObject element; + String json = "{ traces: " + jsonarray + "}"; + JSONObject obj = new JSONObject(json); + JSONArray traces = obj.getJSONArray("traces"); + System.out.println(traces.length()); + + JSONObject trace = new JSONObject(); + JSONArray zipkinspans = new JSONArray(); + JSONArray array = new JSONArray(); + long previousTraceid = 0; + for (int i = 0; i < traces.length(); ++i) { + trace = traces.getJSONObject(i); + element = new JSONObject(); + long currentTraceid = (long) trace.get("traceid"); + JSONArray annotation = new JSONArray(); + JSONArray binaryannotation = new JSONArray(); + + // System.out.println(trace.toString()); + element.put("traceId", trace.getString("traceid")); + element.put("name", trace.getString("name")); + element.put("id", trace.getString("id")); + element.put("timestamp", trace.getLong("timestamp") * 1000); + element.put("duration", (trace.getLong("end_time") - trace.getLong("start_time")) * 1000); + element.put("parentId", trace.getString("parent_id")); + element.put("annotations", annotation); + element.put("binaryAnnotations", binaryannotation); + if (previousTraceid == currentTraceid || i == 0) { + zipkinspans.put(element); + previousTraceid = currentTraceid; + if (i == traces.length() - 1) { + array.put(zipkinspans); + } + } else { + previousTraceid = currentTraceid; + array.put(zipkinspans); + zipkinspans = new JSONArray(); + zipkinspans.put(element); + + System.out.print("zipkinspans Cleared"); + } + } - @RequestMapping("/foo") - public String foo() throws SQLException{ - Statement stmt = null; - ResultSet rset = null; -String rString = ""; -try{ - Connection con = DriverManager.getConnection("jdbc:phoenix:localhost:2181"); - stmt = con.createStatement(); - - PreparedStatement statement = con.prepareStatement("select * from SYSTEM.TRACING_STATS"); - rset = statement.executeQuery(); - while (rset.next()) { - System.out.println(rset.getString("mycolumn")); -rString += rset.getString("mycolumn"); - } - statement.close(); - con.close(); -}catch(SQLException e){ -throw (e); -} - - return "Greetings from foo!, "+rString; + // array.put(zipkinspans); + String ss = array.toString(); + return ss; + } + + @RequestMapping("/api/v2/trace/{traceId}") + @ResponseBody + public String tracesV2(@PathVariable String traceId) { + return "[{\"traceId\":\"24baa9cd369ebd80\",\"name\":\"get\",\"id\":\"24baa9cd369ebd80\",\"timestamp\":1469943807711000,\"duration\":16000,\"annotations\":[{\"endpoint\":{\"serviceName\":\"phoenix-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469943807711000,\"value\":\"sr\"},{\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469943807727000,\"value\":\"ss\"}],\"binaryAnnotations\":[{\"key\":\"http.status_code\",\"value\":\"200\",\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411}},{\"key\":\"http.url\",\"value\":\"/api/v1/services\",\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411}}]}]"; + } + + @RequestMapping("/api/v1/trace/{traceId}") + public String traces(@PathVariable String traceId) throws JSONException { + String jsonarray = "[]"; + jsonarray = getTrace(traceId); + JSONArray array = new JSONArray(jsonarray); + JSONArray zipkinspans = new JSONArray(); + for (int i = 0; i < array.length(); ++i) { + JSONObject trace = array.getJSONObject(i); + JSONArray annotation = new JSONArray(); + JSONArray binaryannotation = new JSONArray(); + trace.put("traceId", trace.getString("traceid")); + trace.put("duration", (trace.getLong("duration")) * 1000); + trace.put("annotations", annotation); + trace.put("binaryAnnotations", binaryannotation); + zipkinspans.put(trace); } -@RequestMapping("/api/v1/services") - public String services() { - return "[\"phoenix-server\"]"; + + return zipkinspans.toString(); + } + + // get all trace results with limit count + protected String getAll(String limit) { + String json = null; + if (limit == null) { + limit = DEFAULT_LIMIT; } -@RequestMapping("/api/v1/spans") - public String spans() { - return "[]"; + String sqlQuery = "SELECT * FROM " + TRACING_TABLE + " LIMIT " + limit; + json = getResults(sqlQuery); + return json; + } + + // get trace + protected String getTrace(String traceid) { + String json = null; + System.out.print(traceid); + String sqlQuery = "SELECT trace_id as traceId, description as name," + + " start_time as timestamp, span_id as id, parent_id," + + " ( end_time-start_time ) as duration FROM " + TRACING_TABLE + " WHERE trace_id = " + + traceid; + json = getResults(sqlQuery); + System.out.print(json); + return json; + } + + // get all traces with limit count + protected String getTraces(String limit) { + String json = null; + if (limit == null) { + limit = DEFAULT_LIMIT; } -} + String sqlQuery = "SELECT tablex.trace_id as traceid, tablex.description as name," + + " tablex.start_time as timestamp, tablex.span_id as id, tablex.parent_id," + + " tablex.end_time, tablex.start_time FROM " + TRACING_TABLE + + " as tablex GROUP BY tablex.trace_id, tablex.description, timestamp, id," + + " tablex.parent_id, tablex.end_time, tablex.start_time " + "LIMIT " + limit; + json = getResults(sqlQuery); + return json; + } + + // get results with passing sql query + protected String getResults(String sqlQuery) { + String json = null; + if (sqlQuery == null) { + json = "{error:true,msg:'SQL was null'}"; + } else { + try { + con = ConnectionFactory.getConnection(); + EntityFactory nutrientEntityFactory = new EntityFactory(con, sqlQuery); + List> nutrients = nutrientEntityFactory.findMultiple(new Object[] {}); + System.out.println(nutrients.toString()); + ObjectMapper mapper = new ObjectMapper(); + json = mapper.writeValueAsString(nutrients); + } catch (Exception e) { + json = "{error:true,msg:'Serrver Error:" + e.getMessage() + "'}"; + } finally { + if (con != null) { + try { + con.close(); + } catch (SQLException e) { + json = "{error:true,msg:'SQL Serrver Error:" + e.getMessage() + "'}"; + } + } + } + } + return json; + } + +} \ No newline at end of file diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java index ab246a75140..947cbe2998b 100644 --- a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java @@ -4,7 +4,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ApplicationContext; +//import org.springframework.context.ApplicationContext; //import org.springframework.cloud.sleuth.zipkin.stream.EnableZipkinStreamServer; @@ -24,14 +24,8 @@ public static void main(String[] args) { public class ZipkinApplication { public static void main(String[] args) { - ApplicationContext ctx = SpringApplication.run(ZipkinApplication.class, args); - - System.out.println("Let's inspect the beans provided by Spring Boot:"); + SpringApplication.run(ZipkinApplication.class, args); - String[] beanNames = ctx.getBeanDefinitionNames(); - Arrays.sort(beanNames); - for (String beanName : beanNames) { - System.out.println(beanName); - } + } -} \ No newline at end of file +} From bb243859f04ce9cdbeb9d462085d1487356db0b3 Mon Sep 17 00:00:00 2001 From: ayolajayamaha Date: Mon, 8 Aug 2016 23:04:00 +0530 Subject: [PATCH 05/12] fixing the pom.xml --- phoenix-zipkin/pom.xml | 161 ++++++++++++++++++++++------------------- 1 file changed, 87 insertions(+), 74 deletions(-) diff --git a/phoenix-zipkin/pom.xml b/phoenix-zipkin/pom.xml index ab978d660d7..d76701945e5 100644 --- a/phoenix-zipkin/pom.xml +++ b/phoenix-zipkin/pom.xml @@ -1,89 +1,102 @@ - - - 4.0.0 + + + + + 4.0.0 - org.springframework.boot - spring-boot-starter-parent - 1.3.6.RELEASE + org.apache.phoenix + phoenix + 4.8.0-HBase-1.2-SNAPSHOT + phoenix-zipkin + Phoenix - Zipkin + zipkin visualize the phoenix traces + - - org.springframework.boot - spring-boot-starter-web - - - io.zipkin.java - zipkin-ui - 1.5.1 - - - io.zipkin.java - zipkin-server - 1.5.1 - - - io.zipkin.java - zipkin-autoconfigure-ui - 1.5.1 - - - - - + + org.springframework.boot + spring-boot-starter-web + 1.3.6.RELEASE + + + io.zipkin.java + zipkin-ui + 1.5.1 + + + io.zipkin.java + zipkin-server + 1.5.1 + + + io.zipkin.java + zipkin-autoconfigure-ui + 1.5.1 + + org.apache.phoenix phoenix-core 4.8.0-HBase-1.2-SNAPSHOT - - org.slf4j - slf4j-log4j12 - - - log4j - log4j - - + + org.slf4j + slf4j-log4j12 + + + log4j + log4j + + - - - - 1.8 - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - maven-failsafe-plugin - - - - integration-test - verify - - - - - + + + org.springframework.boot + spring-boot-maven-plugin + 1.3.6.RELEASE + + + + repackage + + + + + + maven-failsafe-plugin + + + + integration-test + verify + + + + + - - - + From 1316050b7e031c26e2c16f6bb5546d5f7d612825 Mon Sep 17 00:00:00 2001 From: ayolajayamaha Date: Mon, 8 Aug 2016 23:27:34 +0530 Subject: [PATCH 06/12] adding licenses --- .../zipkin/PhoenixZipkinController.java | 17 ++++++++ .../phoenix/zipkin/ZipkinApplication.java | 41 +++++++++---------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java index 061d0f5b1fe..c6340764684 100644 --- a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java @@ -1,3 +1,20 @@ +/* + * 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.phoenix.zipkin; import org.codehaus.jackson.map.ObjectMapper; diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java index 947cbe2998b..9325689ec3b 100644 --- a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java @@ -1,31 +1,30 @@ +/* + * 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.phoenix.zipkin; -import java.util.Arrays; - import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -//import org.springframework.context.ApplicationContext; -//import org.springframework.cloud.sleuth.zipkin.stream.EnableZipkinStreamServer; - - -/*@SpringBootApplication -@EnableZipkinStreamServer -public class ZipkinApplication { - public static void main(String[] args) { - SpringApplication.run(ZipkinApplication.class, args); - } - - - -} -*/ @SpringBootApplication public class ZipkinApplication { - public static void main(String[] args) { - SpringApplication.run(ZipkinApplication.class, args); + public static void main(String[] args) { + SpringApplication.run(ZipkinApplication.class, args); - - } + } } From 4b99310ae009bf84f3e298f90942d46dba91c5d3 Mon Sep 17 00:00:00 2001 From: ayolajayamaha Date: Wed, 10 Aug 2016 08:37:57 +0530 Subject: [PATCH 07/12] dependency graph --- .../zipkin/PhoenixZipkinController.java | 103 +++++++++++------- 1 file changed, 65 insertions(+), 38 deletions(-) diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java index c6340764684..934990bf976 100644 --- a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java @@ -56,30 +56,6 @@ public String index() { return "Greetings from Spring Boot!"; } - @RequestMapping("/foo") - public String foo() throws SQLException { - Statement stmt = null; - ResultSet rset = null; - String rString = ""; - try { - Connection con = DriverManager.getConnection("jdbc:phoenix:localhost:2181"); - stmt = con.createStatement(); - - PreparedStatement statement = con.prepareStatement("select * from SYSTEM.TRACING_STATS"); - rset = statement.executeQuery(); - while (rset.next()) { - System.out.println(rset.getString("mycolumn")); - rString += rset.getString("mycolumn"); - } - statement.close(); - con.close(); - } catch (SQLException e) { - throw (e); - } - - return "Greetings from foo!, " + rString; - } - @RequestMapping("/api/v1/services") public String services() { return "[\"phoenix-server\"]"; @@ -98,15 +74,13 @@ public String traces() { @RequestMapping("/api/v1/traces") public String tracesv2(@RequestParam(value = "limit", defaultValue = "10") String limit) throws JSONException { - String jsonarray = "[]"; - jsonarray = getTraces(limit); JSONObject element; String json = "{ traces: " + jsonarray + "}"; JSONObject obj = new JSONObject(json); JSONArray traces = obj.getJSONArray("traces"); - System.out.println(traces.length()); + // System.out.println(traces.length()); JSONObject trace = new JSONObject(); JSONArray zipkinspans = new JSONArray(); @@ -119,7 +93,6 @@ public String tracesv2(@RequestParam(value = "limit", defaultValue = "10") Strin JSONArray annotation = new JSONArray(); JSONArray binaryannotation = new JSONArray(); - // System.out.println(trace.toString()); element.put("traceId", trace.getString("traceid")); element.put("name", trace.getString("name")); element.put("id", trace.getString("id")); @@ -139,20 +112,46 @@ public String tracesv2(@RequestParam(value = "limit", defaultValue = "10") Strin array.put(zipkinspans); zipkinspans = new JSONArray(); zipkinspans.put(element); - System.out.print("zipkinspans Cleared"); } } - // array.put(zipkinspans); - String ss = array.toString(); - return ss; + + String tracesStr = array.toString(); + return tracesStr; } - @RequestMapping("/api/v2/trace/{traceId}") - @ResponseBody - public String tracesV2(@PathVariable String traceId) { - return "[{\"traceId\":\"24baa9cd369ebd80\",\"name\":\"get\",\"id\":\"24baa9cd369ebd80\",\"timestamp\":1469943807711000,\"duration\":16000,\"annotations\":[{\"endpoint\":{\"serviceName\":\"phoenix-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469943807711000,\"value\":\"sr\"},{\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469943807727000,\"value\":\"ss\"}],\"binaryAnnotations\":[{\"key\":\"http.status_code\",\"value\":\"200\",\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411}},{\"key\":\"http.url\",\"value\":\"/api/v1/services\",\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411}}]}]"; + @RequestMapping("/api/v1/dependencies") + public String dependancy(@RequestParam long endTs, @RequestParam long lookback) + throws JSONException { + String jsonarray = "[]"; + long endTime = endTs; + long startTime = endTs - lookback; + jsonarray = getTraceByTime(startTime, endTime); + JSONArray array = new JSONArray(jsonarray); + JSONArray zipkindependancy = new JSONArray(); + + for (int i = 0; i < array.length(); i++) { + int count = 0; + JSONObject trace = array.getJSONObject(i); + String parent = trace.getString("name"); + if (isParentNonExisting(zipkindependancy, parent)) { + long parentId = trace.getLong("parent_id"); + String child = ""; + for (int j = 0; j < array.length(); j++) { + if (parentId == array.getJSONObject(j).getLong("id")) { + count++; + child = array.getJSONObject(j).getString("name"); + } + } + if (count > 0) { + JSONObject element = dependancyElement(parent, child, count); + zipkindependancy.put(element); + System.out.println("adding zipkindependancy element"); + } + } + }// parent level loop end + return zipkindependancy.toString(); } @RequestMapping("/api/v1/trace/{traceId}") @@ -189,16 +188,44 @@ protected String getAll(String limit) { // get trace protected String getTrace(String traceid) { String json = null; - System.out.print(traceid); String sqlQuery = "SELECT trace_id as traceId, description as name," + " start_time as timestamp, span_id as id, parent_id," + " ( end_time-start_time ) as duration FROM " + TRACING_TABLE + " WHERE trace_id = " + traceid; json = getResults(sqlQuery); - System.out.print(json); return json; } + // get trace + protected String getTraceByTime(long start, long end) { + String json = null; + String limit = "100"; + String sqlQuery = "SELECT trace_id as traceId, description as name," + + " span_id as id, parent_id" + " FROM " + TRACING_TABLE + " WHERE start_time > " + + start + " AND end_time < " + end + " LIMIT " + limit; + json = getResults(sqlQuery); + return json; + } + + protected JSONObject dependancyElement(String parent, String child, int callCount) + throws JSONException { + JSONObject element = new JSONObject(); + element.put("parent", parent); + element.put("child", child); + element.put("callCount", callCount); + return element; + } + + protected boolean isParentNonExisting(JSONArray array, String parent) throws JSONException { + boolean out = true; + for (int i = 0; i < array.length(); ++i) { + if (array.getJSONObject(i).getString("parent").equalsIgnoreCase(parent)) { + out = false; + } + } + return out; + } + // get all traces with limit count protected String getTraces(String limit) { String json = null; From 9f229a671fdc497e640086455c76ab3684e21d32 Mon Sep 17 00:00:00 2001 From: ayolajayamaha Date: Fri, 12 Aug 2016 00:19:13 +0530 Subject: [PATCH 08/12] adding annotations and binary annotations --- .../zipkin/PhoenixZipkinController.java | 74 +++++++++++++------ .../phoenix/zipkin/ZipkinApplication.java | 14 +++- 2 files changed, 66 insertions(+), 22 deletions(-) diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java index 934990bf976..7170931939f 100644 --- a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java @@ -23,16 +23,11 @@ import org.codehaus.jettison.json.JSONObject; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.PathVariable; import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.PreparedStatement; -import java.sql.Statement; import java.util.List; import java.util.Map; @@ -80,8 +75,6 @@ public String tracesv2(@RequestParam(value = "limit", defaultValue = "10") Strin String json = "{ traces: " + jsonarray + "}"; JSONObject obj = new JSONObject(json); JSONArray traces = obj.getJSONArray("traces"); - // System.out.println(traces.length()); - JSONObject trace = new JSONObject(); JSONArray zipkinspans = new JSONArray(); JSONArray array = new JSONArray(); @@ -90,9 +83,8 @@ public String tracesv2(@RequestParam(value = "limit", defaultValue = "10") Strin trace = traces.getJSONObject(i); element = new JSONObject(); long currentTraceid = (long) trace.get("traceid"); - JSONArray annotation = new JSONArray(); + JSONArray annotation = getAnnotation(trace.getString("hostname"), trace.getLong("timestamp")); JSONArray binaryannotation = new JSONArray(); - element.put("traceId", trace.getString("traceid")); element.put("name", trace.getString("name")); element.put("id", trace.getString("id")); @@ -112,11 +104,9 @@ public String tracesv2(@RequestParam(value = "limit", defaultValue = "10") Strin array.put(zipkinspans); zipkinspans = new JSONArray(); zipkinspans.put(element); - System.out.print("zipkinspans Cleared"); } } - String tracesStr = array.toString(); return tracesStr; } @@ -133,7 +123,7 @@ public String dependancy(@RequestParam long endTs, @RequestParam long lookback) for (int i = 0; i < array.length(); i++) { int count = 0; - JSONObject trace = array.getJSONObject(i); + JSONObject trace = array.getJSONObject(i); String parent = trace.getString("name"); if (isParentNonExisting(zipkindependancy, parent)) { long parentId = trace.getLong("parent_id"); @@ -147,7 +137,6 @@ public String dependancy(@RequestParam long endTs, @RequestParam long lookback) if (count > 0) { JSONObject element = dependancyElement(parent, child, count); zipkindependancy.put(element); - System.out.println("adding zipkindependancy element"); } } }// parent level loop end @@ -162,15 +151,22 @@ public String traces(@PathVariable String traceId) throws JSONException { JSONArray zipkinspans = new JSONArray(); for (int i = 0; i < array.length(); ++i) { JSONObject trace = array.getJSONObject(i); - JSONArray annotation = new JSONArray(); + JSONArray annotation = getAnnotation(trace.getString("hostname"), trace.getLong("timestamp")); JSONArray binaryannotation = new JSONArray(); - trace.put("traceId", trace.getString("traceid")); + if (trace.getString("name").contains("VALUES ")) { + String description = trace.getString("name"); + trace.put("name", description.split("VALUES")[0]); + binaryannotation = getBinaryAnnotations(trace.getString("hostname"), description); + } + trace.put("traceId", trace.getLong("traceid")); + trace.put("parentId", trace.getLong("parentid")); trace.put("duration", (trace.getLong("duration")) * 1000); trace.put("annotations", annotation); trace.put("binaryAnnotations", binaryannotation); + trace.remove("traceid"); + trace.remove("parentid"); zipkinspans.put(trace); } - return zipkinspans.toString(); } @@ -189,7 +185,7 @@ protected String getAll(String limit) { protected String getTrace(String traceid) { String json = null; String sqlQuery = "SELECT trace_id as traceId, description as name," - + " start_time as timestamp, span_id as id, parent_id," + + " start_time as timestamp, span_id as id, parent_id as parentId, hostname, " + " ( end_time-start_time ) as duration FROM " + TRACING_TABLE + " WHERE trace_id = " + traceid; json = getResults(sqlQuery); @@ -199,7 +195,7 @@ protected String getTrace(String traceid) { // get trace protected String getTraceByTime(long start, long end) { String json = null; - String limit = "100"; + String limit = "10000"; String sqlQuery = "SELECT trace_id as traceId, description as name," + " span_id as id, parent_id" + " FROM " + TRACING_TABLE + " WHERE start_time > " + start + " AND end_time < " + end + " LIMIT " + limit; @@ -207,6 +203,42 @@ protected String getTraceByTime(long start, long end) { return json; } + protected JSONArray getAnnotation(String hostname, long timestamp) throws JSONException { + JSONArray outJsonarray = new JSONArray(); + JSONObject annotation = new JSONObject(); + JSONObject endpoint = new JSONObject(); + // End point support services and port + endpoint.put("ipv4", hostname); + endpoint.put("serviceName", hostname); + endpoint.put("port", ""); + // Mapping annotation values (sr, ss, cs, cr) when tracing support this + annotation.put("value", ""); + annotation.put("timestamp", timestamp * 1000); + annotation.put("endpoint", endpoint); + outJsonarray.put(annotation); + return outJsonarray; + } + + protected JSONArray getBinaryAnnotations(String hostname, String description) + throws JSONException { + String key[] = description.trim().split("VALUES")[0].split("\\(")[1].split("\\)")[0].split(","); + String value[] = description.trim().split("VALUES")[1].split("\\)")[0].split("\\(")[1].split(","); + JSONArray annotations = new JSONArray(); + if (key.length == value.length) { + for (int i = 0; i < key.length; i++) { + JSONObject annotation = new JSONObject(); + JSONObject endpoint = new JSONObject(); + endpoint.put("ipv4", hostname); + endpoint.put("serviceName", hostname); + annotation.put("value", value[i]); + annotation.put("key", key[i]); + annotation.put("endpoint", endpoint); + annotations.put(annotation); + } + } + return annotations; + } + protected JSONObject dependancyElement(String parent, String child, int callCount) throws JSONException { JSONObject element = new JSONObject(); @@ -234,9 +266,10 @@ protected String getTraces(String limit) { } String sqlQuery = "SELECT tablex.trace_id as traceid, tablex.description as name," + " tablex.start_time as timestamp, tablex.span_id as id, tablex.parent_id," - + " tablex.end_time, tablex.start_time FROM " + TRACING_TABLE + + " tablex.end_time, tablex.start_time, tablex.hostname FROM " + TRACING_TABLE + " as tablex GROUP BY tablex.trace_id, tablex.description, timestamp, id," - + " tablex.parent_id, tablex.end_time, tablex.start_time " + "LIMIT " + limit; + + " tablex.parent_id, tablex.end_time, tablex.start_time, tablex.hostname" + " LIMIT " + + limit; json = getResults(sqlQuery); return json; } @@ -251,7 +284,6 @@ protected String getResults(String sqlQuery) { con = ConnectionFactory.getConnection(); EntityFactory nutrientEntityFactory = new EntityFactory(con, sqlQuery); List> nutrients = nutrientEntityFactory.findMultiple(new Object[] {}); - System.out.println(nutrients.toString()); ObjectMapper mapper = new ObjectMapper(); json = mapper.writeValueAsString(nutrients); } catch (Exception e) { diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java index 9325689ec3b..46629a45e81 100644 --- a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java @@ -17,14 +17,26 @@ */ package org.apache.phoenix.zipkin; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; + @SpringBootApplication public class ZipkinApplication { + + public static void main(String[] args) { - SpringApplication.run(ZipkinApplication.class, args); + System.out.println("testing...."); + ApplicationContext ctx = SpringApplication.run(ZipkinApplication.class, args); + //System.out.println(ctx.getBean("value")); } + +// @Bean +// public String value(@Value("#{systemProperties.test}")String value){ +// return value; +// } } From 0be84a9a2c91d5e4a6a40cd05f4886ed7b4a0b7f Mon Sep 17 00:00:00 2001 From: ayolajayamaha Date: Sun, 14 Aug 2016 19:35:39 +0530 Subject: [PATCH 09/12] adding phoenix host and port switch --- phoenix-zipkin/README.md | 27 ++++++ .../phoenix/zipkin/ConnectionFactory.java | 14 ++- .../zipkin/PhoenixZipkinController.java | 96 +++++++++++++++---- .../phoenix/zipkin/ZipkinApplication.java | 27 +++--- .../src/main/resources/application.properties | 3 + 5 files changed, 130 insertions(+), 37 deletions(-) create mode 100755 phoenix-zipkin/README.md create mode 100644 phoenix-zipkin/src/main/resources/application.properties diff --git a/phoenix-zipkin/README.md b/phoenix-zipkin/README.md new file mode 100755 index 00000000000..4e3a401b610 --- /dev/null +++ b/phoenix-zipkin/README.md @@ -0,0 +1,27 @@ +# Phoenix Zipkin TracingWebApp +1. Build the web application- + *mvn clean install* + +2. Start the Phoenix-Zipkin + *java -jar ./target/phoenix-zipkin-4.8.0-HBase-1.2-SNAPSHOT.jar* + +3. View Web application on Web Browser - + *http://localhost:8865/* + + ###Note + You can set the port of the Phoenix-Zipkin by -Dserver.port={portNo} + + eg: + `java -jar -Dserver.port=8088 ./target/phoenix-zipkin-4.8.0-HBase-1.2-SNAPSHOT.jar ` server will start in 8088 + + +To change the zookeeper host + +-Dphoenix.host=pc + +To change the zookeeper port + +-Dphoenix.port=2181 + +eg: + `java -jar -Dphoenix.host=pc -Dphoenix.port=2181 ./target/phoenix-zipkin-4.8.0-HBase-1.2-SNAPSHOT.jar ` \ No newline at end of file diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ConnectionFactory.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ConnectionFactory.java index 533219a467f..40ac80bd5b5 100644 --- a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ConnectionFactory.java +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ConnectionFactory.java @@ -21,6 +21,7 @@ import java.sql.DriverManager; import java.sql.SQLException; + /** * * ConnectionFactory is to handle database connection @@ -28,9 +29,18 @@ */ public class ConnectionFactory { + + private static String PHOENIX_HOST; + private static String PHOENIX_PORT; + + public static void setHost(String host){ + PHOENIX_HOST = host; + } + + public static void setPort(String port){ + PHOENIX_PORT = port; + } private static Connection con; - protected static String PHOENIX_HOST = "localhost"; - protected static int PHOENIX_PORT = 2181; public static Connection getConnection() throws SQLException, ClassNotFoundException { if (con == null || con.isClosed()) { diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java index 7170931939f..80e10157c00 100644 --- a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java @@ -21,6 +21,7 @@ import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.springframework.beans.factory.annotation.*; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -46,31 +47,51 @@ public class PhoenixZipkinController { protected String TRACING_TABLE = "SYSTEM.TRACING_STATS"; - @RequestMapping("/") - public String index() { - return "Greetings from Spring Boot!"; - } + // spring will automatically bind value of property + @Value("${phoenix.host}") + private String PHOENIX_HOSTX; + + @Value("${phoenix.port}") + private String PHOENIX_PORTX; @RequestMapping("/api/v1/services") - public String services() { - return "[\"phoenix-server\"]"; + public String services() throws JSONException { + JSONArray services = new JSONArray(getServices("1")); + JSONArray servicesOut = new JSONArray(); + for (int i = 0; i < services.length(); i++) { + JSONObject obj = services.getJSONObject(i); + servicesOut.put(obj.get("hostname")); + } + return servicesOut.toString(); } @RequestMapping("/api/v1/spans") - public String spans() { - return "[]"; - } - - @RequestMapping("/api/v2/traces") - public String traces() { - return "[[{\"traceId\":\"0d73ed4802216e0e\",\"name\":\"bootstrap\",\"id\":\"0d73ed4802216e0e\",\"timestamp\":1469940471944000,\"duration\":12054546,\"annotations\":[{\"endpoint\":{\"serviceName\":\"phoenix-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469940472204546,\"value\":\"ApplicationStarted\"},{\"endpoint\":{\"serviceName\":\"phoenix-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469940472892282,\"value\":\"ApplicationEnvironmentPrepared\"},{\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469940473470234,\"value\":\"ApplicationPrepared\"},{\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469940483864223,\"value\":\"ContextRefreshed\"},{\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469940483993406,\"value\":\"EmbeddedServletContainerInitialized\"},{\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411},\"timestamp\":1469940483998543,\"value\":\"ApplicationReady\"}],\"binaryAnnotations\":[{\"key\":\"lc\",\"value\":\"spring-boot\",\"endpoint\":{\"serviceName\":\"zipkin-server\",\"ipv4\":\"127.0.0.1\",\"port\":9411}}]}]]"; + public String spans(@RequestParam(value = "serviceName") String hostname) { + JSONArray spanOut = new JSONArray(); + if (!hostname.equalsIgnoreCase("undefined")) { + try { + JSONArray spans = new JSONArray(getSpans("5", hostname)); + for (int i = 0; i < spans.length(); i++) { + JSONObject obj = spans.getJSONObject(i); + spanOut.put(obj.get("spanname")); + } + } catch (Exception JSONException) { + return spanOut.toString(); + } + } + // return spanOut.toString(); + return spanOut.toString(); } @RequestMapping("/api/v1/traces") - public String tracesv2(@RequestParam(value = "limit", defaultValue = "10") String limit) + public String tracesv2(@RequestParam(value = "limit", defaultValue = "10") String limit, + @RequestParam(value = "serviceName") String hostname, + @RequestParam(value = "lookback", defaultValue = "0") long lookback, + @RequestParam(value = "endTs", defaultValue = "0") long endTime, + @RequestParam(value = "minDuration", defaultValue = "0") long minDuration) throws JSONException { String jsonarray = "[]"; - jsonarray = getTraces(limit); + jsonarray = getTraces(limit, hostname, endTime, lookback, minDuration); JSONObject element; String json = "{ traces: " + jsonarray + "}"; JSONObject obj = new JSONObject(json); @@ -181,7 +202,7 @@ protected String getAll(String limit) { return json; } - // get trace + // get trace by trace id protected String getTrace(String traceid) { String json = null; String sqlQuery = "SELECT trace_id as traceId, description as name," @@ -192,7 +213,7 @@ protected String getTrace(String traceid) { return json; } - // get trace + // get trace by time protected String getTraceByTime(long start, long end) { String json = null; String limit = "10000"; @@ -203,6 +224,7 @@ protected String getTraceByTime(long start, long end) { return json; } + // getting span's annotations protected JSONArray getAnnotation(String hostname, long timestamp) throws JSONException { JSONArray outJsonarray = new JSONArray(); JSONObject annotation = new JSONObject(); @@ -219,10 +241,12 @@ protected JSONArray getAnnotation(String hostname, long timestamp) throws JSONEx return outJsonarray; } + // getting binary annotations protected JSONArray getBinaryAnnotations(String hostname, String description) throws JSONException { String key[] = description.trim().split("VALUES")[0].split("\\(")[1].split("\\)")[0].split(","); - String value[] = description.trim().split("VALUES")[1].split("\\)")[0].split("\\(")[1].split(","); + String value[] = description.trim().split("VALUES")[1].split("\\)")[0].split("\\(")[1] + .split(","); JSONArray annotations = new JSONArray(); if (key.length == value.length) { for (int i = 0; i < key.length; i++) { @@ -258,22 +282,52 @@ protected boolean isParentNonExisting(JSONArray array, String parent) throws JSO return out; } - // get all traces with limit count - protected String getTraces(String limit) { + // get all traces with limit count, host and time stamp + protected String getTraces(String limit, String hostname, long endTime, long lookBack, + long minDuration) { String json = null; if (limit == null) { limit = DEFAULT_LIMIT; } + long startTimeStamp = endTime - lookBack; + long minDurationMilSec = minDuration / 1000; String sqlQuery = "SELECT tablex.trace_id as traceid, tablex.description as name," + " tablex.start_time as timestamp, tablex.span_id as id, tablex.parent_id," + " tablex.end_time, tablex.start_time, tablex.hostname FROM " + TRACING_TABLE - + " as tablex GROUP BY tablex.trace_id, tablex.description, timestamp, id," + + " as tablex WHERE hostname = '" + hostname + "' " + " AND end_time < " + endTime + + " AND start_time > " + startTimeStamp + " AND end_time - start_time > " + + minDurationMilSec + " GROUP BY tablex.trace_id, tablex.description, timestamp, id," + " tablex.parent_id, tablex.end_time, tablex.start_time, tablex.hostname" + " LIMIT " + limit; json = getResults(sqlQuery); return json; } + // get all span names + protected String getSpans(String limit, String hostname) { + String json = null; + String strLimit = ""; + if (limit.length() != 0) { + strLimit = " LIMIT " + limit; + } + String sqlQuery = "SELECT distinct REGEXP_SUBSTR(description,'[^(@:\\[-]+') AS SPANNAME" + + " from " + TRACING_TABLE + " where hostname = '" + hostname + "'" + strLimit; + json = getResults(sqlQuery); + return json; + } + + // get all services + protected String getServices(String limit) { + String json = null; + String strLimit = ""; + if (limit.length() != 0) { + strLimit = " LIMIT " + limit; + } + String sqlQuery = "SELECT distinct hostname FROM " + TRACING_TABLE + strLimit; + json = getResults(sqlQuery); + return json; + } + // get results with passing sql query protected String getResults(String sqlQuery) { String json = null; diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java index 46629a45e81..c18084bef01 100644 --- a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/ZipkinApplication.java @@ -17,26 +17,25 @@ */ package org.apache.phoenix.zipkin; - import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ApplicationContext; - +import org.springframework.beans.factory.annotation.*; @SpringBootApplication public class ZipkinApplication { - - - public static void main(String[] args) { - System.out.println("testing...."); - - ApplicationContext ctx = SpringApplication.run(ZipkinApplication.class, args); - //System.out.println(ctx.getBean("value")); + @Value("${phoenix.host}") + public void setHostName(String hostName) { + ConnectionFactory.setHost(hostName); } -// @Bean -// public String value(@Value("#{systemProperties.test}")String value){ -// return value; -// } + @Value("${phoenix.port}") + public void setPortNo(String portNo) { + ConnectionFactory.setPort(portNo); + } + + public static void main(String[] args) { + SpringApplication.run(ZipkinApplication.class, args); + } + } diff --git a/phoenix-zipkin/src/main/resources/application.properties b/phoenix-zipkin/src/main/resources/application.properties new file mode 100644 index 00000000000..8b862253050 --- /dev/null +++ b/phoenix-zipkin/src/main/resources/application.properties @@ -0,0 +1,3 @@ +server.port = 8865 +phoenix.host = localhost +phoenix.port = 2181 \ No newline at end of file From ea1b6b2a3018edfe7c7ef0e562b3024a663586b9 Mon Sep 17 00:00:00 2001 From: ayolajayamaha Date: Sun, 14 Aug 2016 19:43:30 +0530 Subject: [PATCH 10/12] updating readme --- phoenix-zipkin/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/phoenix-zipkin/README.md b/phoenix-zipkin/README.md index 4e3a401b610..8a59549cdca 100755 --- a/phoenix-zipkin/README.md +++ b/phoenix-zipkin/README.md @@ -9,19 +9,19 @@ *http://localhost:8865/* ###Note - You can set the port of the Phoenix-Zipkin by -Dserver.port={portNo} + You can set the port of the Phoenix-Zipkin by `-Dserver.port={portNo}` eg: `java -jar -Dserver.port=8088 ./target/phoenix-zipkin-4.8.0-HBase-1.2-SNAPSHOT.jar ` server will start in 8088 -To change the zookeeper host + To change the zookeeper host --Dphoenix.host=pc + `-Dphoenix.host={hostname}` -To change the zookeeper port + To change the zookeeper port --Dphoenix.port=2181 + `-Dphoenix.port={portNo}` -eg: + eg: `java -jar -Dphoenix.host=pc -Dphoenix.port=2181 ./target/phoenix-zipkin-4.8.0-HBase-1.2-SNAPSHOT.jar ` \ No newline at end of file From 52555dc7a112f3491de0ce1e2d4c3f67d44d3b28 Mon Sep 17 00:00:00 2001 From: ayolajayamaha Date: Sun, 14 Aug 2016 21:17:01 +0530 Subject: [PATCH 11/12] aading span-name filter --- .../zipkin/PhoenixZipkinController.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java index 80e10157c00..c311659ff84 100644 --- a/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java +++ b/phoenix-zipkin/src/main/java/org/apache/phoenix/zipkin/PhoenixZipkinController.java @@ -56,11 +56,17 @@ public class PhoenixZipkinController { @RequestMapping("/api/v1/services") public String services() throws JSONException { - JSONArray services = new JSONArray(getServices("1")); JSONArray servicesOut = new JSONArray(); - for (int i = 0; i < services.length(); i++) { - JSONObject obj = services.getJSONObject(i); - servicesOut.put(obj.get("hostname")); + String json = "{ services: " + getServices("1") + "}"; + JSONObject obj = new JSONObject(json); + if (obj.get("services") instanceof JSONObject) { + return obj.get("services").toString(); + } else { + JSONArray services = (JSONArray) obj.get("services"); + for (int i = 0; i < services.length(); i++) { + JSONObject currentObj = services.getJSONObject(i); + servicesOut.put(currentObj.get("hostname")); + } } return servicesOut.toString(); } @@ -86,12 +92,13 @@ public String spans(@RequestParam(value = "serviceName") String hostname) { @RequestMapping("/api/v1/traces") public String tracesv2(@RequestParam(value = "limit", defaultValue = "10") String limit, @RequestParam(value = "serviceName") String hostname, + @RequestParam(value = "spanName", defaultValue = "all") String spanName, @RequestParam(value = "lookback", defaultValue = "0") long lookback, @RequestParam(value = "endTs", defaultValue = "0") long endTime, @RequestParam(value = "minDuration", defaultValue = "0") long minDuration) throws JSONException { String jsonarray = "[]"; - jsonarray = getTraces(limit, hostname, endTime, lookback, minDuration); + jsonarray = getTraces(limit, hostname, spanName, endTime, lookback, minDuration); JSONObject element; String json = "{ traces: " + jsonarray + "}"; JSONObject obj = new JSONObject(json); @@ -283,12 +290,16 @@ protected boolean isParentNonExisting(JSONArray array, String parent) throws JSO } // get all traces with limit count, host and time stamp - protected String getTraces(String limit, String hostname, long endTime, long lookBack, - long minDuration) { + protected String getTraces(String limit, String hostname, String spanName, long endTime, + long lookBack, long minDuration) { String json = null; + String addtionalQuery = ""; if (limit == null) { limit = DEFAULT_LIMIT; } + if (!spanName.equalsIgnoreCase("all")) { + addtionalQuery = " AND description like '" + spanName + "%'"; + } long startTimeStamp = endTime - lookBack; long minDurationMilSec = minDuration / 1000; String sqlQuery = "SELECT tablex.trace_id as traceid, tablex.description as name," @@ -296,7 +307,8 @@ protected String getTraces(String limit, String hostname, long endTime, long loo + " tablex.end_time, tablex.start_time, tablex.hostname FROM " + TRACING_TABLE + " as tablex WHERE hostname = '" + hostname + "' " + " AND end_time < " + endTime + " AND start_time > " + startTimeStamp + " AND end_time - start_time > " - + minDurationMilSec + " GROUP BY tablex.trace_id, tablex.description, timestamp, id," + + minDurationMilSec + addtionalQuery + + " GROUP BY tablex.trace_id, tablex.description, timestamp, id," + " tablex.parent_id, tablex.end_time, tablex.start_time, tablex.hostname" + " LIMIT " + limit; json = getResults(sqlQuery); From 183c726bc86ad46c2f43ba87f2a8f9d66f443b9f Mon Sep 17 00:00:00 2001 From: Ayola Date: Thu, 1 Sep 2016 13:14:09 +0530 Subject: [PATCH 12/12] Update application.properties --- phoenix-zipkin/src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phoenix-zipkin/src/main/resources/application.properties b/phoenix-zipkin/src/main/resources/application.properties index 8b862253050..9302dea5596 100644 --- a/phoenix-zipkin/src/main/resources/application.properties +++ b/phoenix-zipkin/src/main/resources/application.properties @@ -1,3 +1,3 @@ server.port = 8865 phoenix.host = localhost -phoenix.port = 2181 \ No newline at end of file +phoenix.port = 2181