【问题标题】:Monitor performance of REST API endpoints using the SPF4J Java Profiler使用 SPF4J Java Profiler 监控 REST API 端点的性能
【发布时间】:2020-07-05 13:28:25
【问题描述】:

我有许多 REST API 端点,我想分别测量每个端点的计时指标。使用 @PerformanceMonitor 注释方法是可行的,但是 recorderSource 字段需要一个类,并且无法为每个方法传递唯一的 forWhat 描述符。我是否需要为每个端点 + HTTP 方法创建一个子类来定义一个唯一的 forWhat 字符串?这似乎不可扩展。我错过了什么吗?

以下是特定记录器源的示例:

import org.spf4j.annotations.RecorderSourceInstance;
import org.spf4j.perf.MeasurementRecorderSource;
import org.spf4j.perf.impl.RecorderFactory;

public static final class GetAllProductsRecorderSource extends RecorderSourceInstance {
    public static final MeasurementRecorderSource INSTANCE;
    static {
        Object forWhat = "GetAllProducts";
        INSTANCE = RecorderFactory.createScalableMinMaxAvgRecorderSource(forWhat, unitOfMeasurement, sampleTimeMillis);
    }
}

这是带有注释的 REST 端点:

import org.spf4j.annotations.PerformanceMonitor;

@PerformanceMonitor(warnThresholdMillis=1, errorThresholdMillis=100, recorderSource=GetAllProductsRecorderSource.class)
@GetMapping("/products")
public List<Product> getAllProducts() throws IOException {
    return productRepository.findAll();
}

【问题讨论】:

    标签: java profiler spf4j


    【解决方案1】:

    如果您使用 recorderSource=RecorderSourceInstance.Rs15m.class

    在您的情况下,forWhat 将是“RecorderSourceInstance.Rs15m, YourClassName.getAllProducts”

    因此您不需要为每个方法创建自定义 RecorderSourceInstance。

    您的另一个选择是在较低级别测量您的指标,在​​像 at 这样的 servlet 过滤器中。这将使您更全面地了解服务器端执行时间(将包括 ser/deser、io...)。

    您可以在atprometheus format 中查看此指标。本次现场演示运行在 GKE 上,源代码为at。有关此演示中展示的一些想法的更多详细信息,请参阅 the wiki's

    【讨论】:

      猜你喜欢
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多