Skywalking-10:Skywalking查询协议——GraphQL

GraphQL

GraphQL 基础

参照Getting started with GraphQL Java and Spring Boot这篇文章学习即可

PS:可以使用 brew install --cask graphql-playground 安装 graphql for mac 客户端。

IDEA 怎么调试 GraphQL 应用

安装 JS GraphQL 插件

点击JS GraphQL安装插件

GraphQL 定义

schema.graphqls

type Query {
    bookById(id: ID): Book
}

type Book {
    id: ID
    name: String
    pageCount: Int
    author: Author
}

type Author {
    id: ID
    firstName: String
    lastName: String
}

GraphQL 配置文件

.graphqlconfig

{
  "name": "book-details",
  "schemaPath": "schema.graphqls",
  "extensions": {
    "endpoints": {
      "Default GraphQL Endpoint": {
        "url": "http://localhost:8080/graphql", // 请求路径
        "headers": {
          "user-agent": "JS GraphQL"
        },
        "introspect": false
      }
    }
  }
}

创建一个查询文件

query.graphql

# {"id": "book-1"}
query queryData($id: ID) {
    bookById(id: $id) {
        id name pageCount author {
            id firstName lastName
        }
    }
}

GraphQL 脚本目录结构

resources
├── .graphqlconfig  # 配置文件
├── query.graphql   # 查询文件
└── schema.graphqls # 定义文件

执行结果

GraphQL 在 Skywalking 中的应用

graphql 协议文件路径: oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol

GraphQL 配置文件

.graphqlconfig

{
  "name": "skywalking",
  "schemaPath": "schema.graphql",
  "extensions": {
    "endpoints": {
      "Default GraphQL Endpoint": {
        "url": "http://localhost:8080/graphql",
        "headers": {
          "user-agent": "JS GraphQL"
        },
        "introspect": true
      }
    }
  }
}

创建一个查询文件

query.graphql

query queryData {
    readMetricsValues(
        duration: {start: "2021-07-03 1400",end: "2021-07-03 1401", step: MINUTE},
        condition: {
            name: "instance_jvm_thread_runnable_thread_count",
            entity: {
                scope: ServiceInstance,
                serviceName: "business-zone::projectA",
                serviceInstanceName: "e8cf34a1d54a4058a8c98505877770e2@192.168.50.113",
                normal: true
            }
        }
    ) { 
        label values{ values{ id value }}
    }
}

执行结果

{
  "data": {
    "readMetricsValues": {
      "values": {
        "values": [
          {
            "id": "202107031400_YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1_ZThjZjM0YTFkNTRhNDA1OGE4Yzk4NTA1ODc3NzcwZTJAMTkyLjE2OC41MC4xMTM=",
            "value": 22
          },
          {
            "id": "202107031401_YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1_ZThjZjM0YTFkNTRhNDA1OGE4Yzk4NTA1ODc3NzcwZTJAMTkyLjE2OC41MC4xMTM=",
            "value": 22
          }
        ]
      }
    }
  }
}

参考文档

posted @ 2021-10-07 11:31  switchvov  阅读(609)  评论(0编辑  收藏  举报