Skip to content

Commit

Permalink
Refactoring metrics register logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Gretchen-z committed Jul 26, 2024
1 parent 957fd3a commit 62892f3
Showing 1 changed file with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,8 @@ public void registerMetrics() {
break;
}

String id = String.format("%s:%s:%s:%s",
withdrawalData.getWithdrawalId(),
withdrawalData.getProviderId(),
withdrawalData.getProviderName(),
withdrawalData.getCurrencyCode());

machinesFailedCountMap.put(id, machinesFailedCountMap.getOrDefault(id, 0.0) + 1);

Tags tags = Tags.of(
Tag.of("withdrawalId", withdrawalData.getWithdrawalId()),
Tag.of("providerId", withdrawalData.getProviderId()),
Tag.of("providerName", withdrawalData.getProviderName()),
Tag.of("currencyCode", withdrawalData.getCurrencyCode())
);

var gauge = Gauge.builder(Metric.MACHINES_FAILED_COUNT.getName(), machinesFailedCountMap, map -> map.get(id))
.description(Metric.MACHINES_FAILED_COUNT.getDescription())
.tags(tags);

meterRegistryService.registry(gauge);
gauge(machinesFailedCountMap, Metric.MACHINES_FAILED_COUNT, getWithdrawalMetricId(withdrawalData),
getWithdrawalTags(withdrawalData));
}

var invoiceIds = machinesFailedData.stream()
Expand All @@ -96,4 +78,32 @@ public void registerMetrics() {

}

private void gauge(Map<String, Double> storage, Metric metric, String id, Tags tags) {
if (!storage.containsKey(id)) {
var gauge = Gauge.builder(metric.getName(), storage, map -> map.get(id))
.description(metric.getDescription())
.tags(tags);
meterRegistryService.registry(gauge);
}
storage.put(id, storage.getOrDefault(id, 0.0) + 1);

}

private Tags getWithdrawalTags(WithdrawalsAggregatedMetricDto withdrawalData) {
return Tags.of(
Tag.of("withdrawalId", withdrawalData.getWithdrawalId()),
Tag.of("providerId", withdrawalData.getProviderId()),
Tag.of("providerName", withdrawalData.getProviderName()),
Tag.of("currencyCode", withdrawalData.getCurrencyCode())
);
}

private String getWithdrawalMetricId (WithdrawalsAggregatedMetricDto withdrawalData) {
return String.format("%s:%s:%s:%s",
withdrawalData.getWithdrawalId(),
withdrawalData.getProviderId(),
withdrawalData.getProviderName(),
withdrawalData.getCurrencyCode());
}

}

0 comments on commit 62892f3

Please sign in to comment.