使用 SPF4J Java Profiler 监控 REST API 端点的性能

Monitor performance of REST API endpoints using the SPF4J Java Profiler

我有许多 REST API 端点,我想分别测量每个端点的计时指标。使用 @PerformanceMonitor 注释方法是可行的,但是 recorderSource 字段采用 class 并且无法为每个方法传递唯一的 forWhat 描述符。我是否需要为每个端点 + HTTP 方法创建一个子 class 来定义一个唯一的 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 endpoing:

import org.spf4j.annotations.PerformanceMonitor;

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

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

你的 forWhat 将是“RecorderSourceInstance.Rs15m,YourClassName.getAllProducts”

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

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

您可以查看此指标的实际效果at or in prometheus format. This live demo is running on GKE, and the source code is at. See the wiki's,了解有关此演示中演示的一些想法的更多详细信息。