可观测性

引入依赖

修改应用根目录下的pom.xml,为 polaris-java 添加 dependencyManagement:

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>com.tencent.polaris</groupId>
  5. <artifactId>polaris-dependencies</artifactId>
  6. <version>${version}</version>
  7. <type>pom</type>
  8. <scope>import</scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>

然后只需要在 标签中在添加 polaris-all 即可

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.tencent.polaris</groupId>
  4. <artifactId>polaris-all</artifactId>
  5. </dependency>
  6. </dependencies>

通过配置文件 polaris.yaml 开启监控上报

你需要在项目的 main/resources 下创建一个 polaris.yaml 文件用于初始化 polaris-java SDK。polaris.yaml配置详细

通过 prometheus pull 模式上报监控数据
  1. global:
  2. #描述: 监控及日志数据上报相关配置
  3. statReporter:
  4. #描述: 是否启用上报
  5. enable: true
  6. plugin:
  7. prometheus:
  8. #描述: 设置 prometheus http-server 的监听端口
  9. #类型:int
  10. #默认值: 28080
  11. #如果设置为负数,则不会开启默认的http-server,如果设置为0,则随机选择一个可用端口进行启动 http-server
  12. port: 28080
  13. #描述: 设置 prometheus http-server 的拉取path
  14. #类型:string
  15. #默认值: /metric
  16. path: /metric
通过 pushgateway push 模式上报监控数据
  1. global:
  2. #描述: 监控及日志数据上报相关配置
  3. statReporter:
  4. #描述: 是否启用上报
  5. enable: true
  6. plugin:
  7. prometheus-pushgateway:
  8. #描述: 上报给prometheus pushgateway的地址
  9. pushgatewayAddress: 127.0.0.1:9090
  10. #描述: 上报给pushgateway的时间间隔
  11. pushInterval: 10s

通过代码开启监控上报

通过 prometheus pull 模式上报监控数据
  1. ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory
  2. .defaultConfig(ConfigProvider.DEFAULT_CONFIG);
  3. configuration.getGlobal().getStatReporter().setEnable(true);
  4. PrometheusHandlerConfig prometheusHandlerConfig = configuration.getGlobal().getStatReporter()
  5. .getPluginConfig(DEFAULT_REPORTER_PROMETHEUS, PrometheusHandlerConfig.class);
  6. prometheusHandlerConfig.setPort(28080);
  7. prometheusHandlerConfig.setPath("/metrics");
  8. configuration.getGlobal().getStatReporter()
  9. .setPluginConfig("prometheus", prometheusHandlerConfig);
通过 pushgateway push 模式上报监控数据
  1. ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory
  2. .defaultConfig(ConfigProvider.DEFAULT_CONFIG);
  3. configuration.getGlobal().getStatReporter().setEnable(true);
  4. PrometheusPushHandlerConfig prometheusHandlerConfig = configuration.getGlobal().getStatReporter()
  5. .getPluginConfig("prometheus-pushgateway", PrometheusPushHandlerConfig.class);
  6. prometheusHandlerConfig.setPushgatewayAddress("127.0.0.1:9091");
  7. // default 30 second
  8. prometheusHandlerConfig.setPushInterval(30 * 1000L);
  9. configuration.getGlobal().getStatReporter()
  10. .setPluginConfig("prometheus-pushgateway", prometheusHandlerConfig);

SDK实例构建

当初始化好 polaris.yaml 文件之后,你可以直接 import com.tencent.polaris.factory.api.DiscoveryAPIFactory, 使用 DiscoveryAPIFactory 中的方法进行构造一个 ConsumerAPI SDK 实例

  1. import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
  2. public static void main(String[] args) throws Exception {
  3. ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPI();
  4. }

服务调用结果

  1. public enum RetStatus {
  2. // 服务调用成功
  3. RetSuccess,
  4. // 服务调用失败
  5. RetFail,
  6. // 服务调用超时
  7. RetTimeout,
  8. }
  9. ServiceCallResult result = new ServiceCallResult();
  10. // 设置被调服务所在命名空间
  11. result.setNamespace(String namespace);
  12. // 设置被调服务的服务信息
  13. result.setService(String service);
  14. // 设置被调实例
  15. result.setInstance(Instance instance);
  16. // 设置本次请求的响应码
  17. result.setRetCode(String code);
  18. // 设置本次请求的耗时
  19. result.setDelay(String delay);
  20. // 设置本次请求的结果状态
  21. result.setRetStatus(RetStatus status);
  22. // 设置本次请求的请求标签,格式为 key=value;key=value;
  23. result.setLabels(String labels);
  24. // 设置本次请求调用的方法
  25. result.setMethod(String method);

上报请求调用结果

你在根据请求调用情况对 ServiceCallResult 结构体完成初始化后,只需要调用 ConsumerAPI.updateServiceCallResult 方法即可完成请求调用结果上报。SDK 内部会根据上报的调用结果信息,将其转换为相应的流量调用指标数据,上报至 prometheus。

  1. consumerAPI.updateServiceCallResult(ServiceCallResult)