备忘: java使用graphql client,通过SkyWalking GraphQL数据接口查询Trace信息(复杂查询)

java使用graphql client连接Skywalking GraphQL数据接口的简单查询,可以参考 备忘:python和 java graphql client连Sky walking Server查询数据的联通性

现在我们尝试完成复杂查询,通过schema.graphql定义的queryBasicTraces方法查询一段时间trace的信息,方法如下:

query {
    queryBasicTraces(
        condition: {
            serviceId: "Y3XXXXXXXX=.1"
            serviceInstanceId: ""
            endpointId: "Y3XXXXXXXX=.1_YYYYYY=="
            traceState: ALL
            traceId: ""
            queryDuration: {start: "2023-12-24 1140",end: "2023-12-24 1200", step: MINUTE}
            queryOrder: BY_START_TIME
            paging: {
                needTotal: true
                pageNum:1
                pageSize: 1000
            }
        }
    ){
        traces{
            traceIds
            duration
            endpointNames
            start

        }
    }
}

尝试应用java的graphql client的query方法实现,对比简单查询有几个难点
1 查询条件contidition是一个复杂的嵌套结构,不能简单用graphql client所定义的query.addParameter(Key,V)这种方式(addParameter没有嵌套功能),

2 查询条件所设定的值,类型繁多,不能简单都使用字符串

问题1的解法
根据下列材料java后端调用GraphQl接口获取结果

基本思路就是把contidition定义成一个HashMap嵌套结构,然后用addObjectParameter方法关联,因为只有一个参数condition
所以可以用 query.getRequestParameter().addObjectParameter(“condition”,conditions);把query和conditions关联,其中conditions是一个HashMap

问题2 : 涉及类型字符串、数值、布尔,以及枚举类型,其中枚举类型可以参考Sky walking Server schema.graphql定义,包括

public enum STEP {
        SECOND, MINUTE,HOUR,DAY,MONTH
    }

    public enum TraceState {
        ALL,ERROR,SUCCESS
    }

    public enum QueryOrder {
        BY_DURATION,BY_START_TIME
    }

具体代码如下:

public class GraphQLServiceImp {

    public enum STEP {
        SECOND, MINUTE,HOUR,DAY,MONTH
    }

    public enum TraceState {
        ALL,ERROR,SUCCESS
    }

    public enum QueryOrder {
        BY_DURATION,BY_START_TIME
    }

    GraphqlClient graphqlClient;
    ObjectMapper objectMapper = new ObjectMapper();


  
    public void initConnect(String url){
    //初始链接
        try{

            this.graphqlClient = GraphqlClient.buildGraphqlClient(url);

         
            String serviceName="demo3";
            String serviceId=queryServiceId(serviceName);
            System.out.println("serviceId::"+serviceId);

            String endpointName="demo.send";
            String re_epId=queryEndPointId(endpointName,serviceName);
            System.out.println("endpointId::"+re_epId);

        }catch (Exception e){
            e.printStackTrace();
        }

    }



    private JsonNode map2jsNode(Map result){
        JsonNode js_res = objectMapper.convertValue(result, JsonNode.class);
        return js_res;
    }

    public String queryServiceId(String ServiceName){
    //通过SeviceName查ServiceId 非重点,略
       ...
    }

    public String queryEndPointId(String endpointName,String serviceName){
    //通过SeviceName、endpointName查endpointId 非重点,略
       ...

       
    }

    public JsonNode getTotalTracesByEndPoint(String serviceId, String endpointId, String start_time, String end_time) {
        try{
            //生成query
            String queryMethodName = "queryBasicTraces"; // 指定graql的查询接口
            GraphqlQuery query = new DefaultGraphqlQuery(queryMethodName);
            //指定查询条件HasMap
            HashMap conditions=new HashMap();

            conditions.put("serviceId",serviceId);
            conditions.put("serviceInstanceId","");
            conditions.put("endpointId",endpointId);
            conditions.put("traceState",TraceState.ALL);
            conditions.put("traceId","");
            conditions.put("queryOrder",QueryOrder.BY_START_TIME);

            HashMap duration=new HashMap();
            duration.put("start",start_time);
            duration.put("end",end_time);
            duration.put("step",STEP.MINUTE);

            conditions.put("queryDuration",duration);

            HashMap paging=new HashMap();
            paging.put("pageNum",1);
            paging.put("needTotal",true);
            paging.put("pageSize",1000);

            conditions.put("paging",paging);

//关联conditions和query     query.getRequestParameter().addObjectParameter("condition",conditions);

            //指定查询结果
            ResultAttributtes traces = new ResultAttributtes("traces");

            traces.addResultAttributes("traceIds");
            traces.addResultAttributes("duration");
            traces.addResultAttributes("endpointNames");
            traces.addResultAttributes("start");
            query.addResultAttributes(traces);

            System.out.println("query::"+query.toString());


            GraphqlResponse response = this.graphqlClient.doQuery(query);
            System.out.println("res::"+response.toString());
            return map2jsNode(response.getData());
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

测试代码

```bash
public class GraphQLTest {
    public static void main(String[] args) throws Exception {

        String serverUrl = "http://172.19.196.82:8029/graphql";
        GraphQLServiceImp graphQLService=new GraphQLServiceImp();
        graphQLService.initConnect(serverUrl);

        //GraphqlClient graphqlClient = GraphqlClient.buildGraphqlClient(serverUrl);

        String serviceName = "demo3";
        String endpointName="demo.send";

        String serviceId=graphQLService.queryServiceId(serviceName);
        System.out.println("serviceId::"+serviceId);
        String endpointId=graphQLService.queryEndPointId(endpointName,serviceName);
        System.out.println("endpointId::"+endpointId);

        String startTime="2023-12-24 1154";
        String endTime="2023-12-24 1200";
        JsonNode res_traces=graphQLService.getTotalTracesByEndPoint(serviceId,endpointId,startTime,endTime);

        System.out.println("result::"+res_traces.asText());

       
    }
}

执行结果

res::{data={queryBasicTraces={traces=[{traceIds=[bc83f7aa64364f31938c9f22ead14069.1311.17034873369950169], duration=7, endpointNames=[demo.send], start=1703487336995}, {traceIds=[bc83f7aa64364f31938c9f22ead14069.1311.17034873363060165], duration=7, endpointNames=[demo.send], start=1703487336306},

}

你可能感兴趣的:(java,graphql,skywalking)