mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-14 11:35:03 +00:00
![pixeebot[bot]](/assets/img/avatar_default.png)
This change defensively switches the order of literals in comparison expressions to ensure that no null pointer exceptions are unexpectedly thrown. Runtime exceptions especially can cause exceptional and unexpected code paths to be taken, and this can result in unexpected behavior. Both simple vulnerabilities (like information disclosure) and complex vulnerabilities (like business logic flaws) can take advantage of these unexpected code paths. Our changes look something like this: ```diff String fieldName = header.getFieldName(); String fieldValue = header.getFieldValue(); - if(fieldName.equals("requestId")) { + if("requestId".equals(fieldName)) { logRequest(fieldValue); } ``` <details> <summary>More reading</summary> * [https://cwe.mitre.org/data/definitions/476.html](https://cwe.mitre.org/data/definitions/476.html) * [https://en.wikibooks.org/wiki/Java_Programming/Preventing_NullPointerException](https://en.wikibooks.org/wiki/Java_Programming/Preventing_NullPointerException) * [https://rules.sonarsource.com/java/RSPEC-1132/](https://rules.sonarsource.com/java/RSPEC-1132/) </details> 🧚🤖 Powered by Pixeebot [Feedback](https://ask.pixee.ai/feedback) | [Community](https://pixee-community.slack.com/signup#/domain-signup) | [Docs](https://docs.pixee.ai/) | Codemod ID: pixee:java/switch-literal-first  <!--{"type":"DRIP","codemod":"pixee:java/switch-literal-first"}--> Co-authored-by: pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com>
70 lines
2.6 KiB
Java
70 lines
2.6 KiB
Java
package stirling.software.SPDF.service;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import io.micrometer.core.instrument.MeterRegistry;
|
|
import io.micrometer.core.instrument.search.Search;
|
|
|
|
@Service
|
|
public class MetricsAggregatorService {
|
|
|
|
private final MeterRegistry meterRegistry;
|
|
private final PostHogService postHogService;
|
|
private final Map<String, Double> lastSentMetrics = new ConcurrentHashMap<>();
|
|
|
|
@Autowired
|
|
public MetricsAggregatorService(MeterRegistry meterRegistry, PostHogService postHogService) {
|
|
this.meterRegistry = meterRegistry;
|
|
this.postHogService = postHogService;
|
|
}
|
|
|
|
@Scheduled(fixedRate = 7200000) // Run every 2 hours
|
|
public void aggregateAndSendMetrics() {
|
|
Map<String, Object> metrics = new HashMap<>();
|
|
Search.in(meterRegistry)
|
|
.name("http.requests")
|
|
.counters()
|
|
.forEach(
|
|
counter -> {
|
|
String method = counter.getId().getTag("method");
|
|
String uri = counter.getId().getTag("uri");
|
|
|
|
// Skip if either method or uri is null
|
|
if (method == null || uri == null) {
|
|
return;
|
|
}
|
|
if (!"GET".equals(method) && !"POST".equals(method)) {
|
|
return;
|
|
}
|
|
// Skip URIs that are 2 characters or shorter
|
|
if (uri.length() <= 2) {
|
|
return;
|
|
}
|
|
|
|
String key =
|
|
String.format(
|
|
"http_requests_%s_%s", method, uri.replace("/", "_"));
|
|
|
|
double currentCount = counter.count();
|
|
double lastCount = lastSentMetrics.getOrDefault(key, 0.0);
|
|
double difference = currentCount - lastCount;
|
|
|
|
if (difference > 0) {
|
|
metrics.put(key, difference);
|
|
lastSentMetrics.put(key, currentCount);
|
|
}
|
|
});
|
|
|
|
// Send aggregated metrics to PostHog
|
|
if (!metrics.isEmpty()) {
|
|
postHogService.captureEvent("aggregated_metrics", metrics);
|
|
}
|
|
}
|
|
}
|