fix for update gauge value (#7)

This commit is contained in:
Anatolii Karlov 2023-08-15 12:28:09 +03:00 committed by GitHub
parent 21443fb8f2
commit f60c8ec652
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 12 deletions

View File

@ -6,9 +6,17 @@ import io.micrometer.core.instrument.MultiGauge;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Configuration
public class GaugeConfig {
@Bean
public Map<String, Double> walletBalancesAggregatesMap() {
return new ConcurrentHashMap<>();
}
@Bean
public MultiGauge multiGaugeWalletBalancesAmount(MeterRegistry meterRegistry) {
return MultiGauge.builder(Metric.WALLET_BALANCES_AMOUNT.getName())

View File

@ -16,6 +16,6 @@ public enum Metric {
private final String description;
private static String formatWithPrefix(String name) {
return String.format("ewm_%s", name);
return String.format("ewb_%s", name);
}
}

View File

@ -37,8 +37,9 @@ public class OpenSearchService {
private String intervalTime;
@SneakyThrows
public List<WalletBalanceData> getWalletBalanceDataByInterval() {
public List<WalletBalanceData> getWalletsBalancesDataByInterval() {
return openSearchClient.search(s -> s
.size(10000)
.index(openSearchProperties.getIndex())
.sort(builder -> builder
.field(builder1 -> builder1

View File

@ -9,6 +9,8 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
@RequiredArgsConstructor
@Slf4j
@ -17,20 +19,26 @@ public class WalletBalancesService {
private final OpenSearchService openSearchService;
private final MeterRegistryService meterRegistryService;
private final Map<String, Double> walletBalancesAggregatesMap;
public void registerMetrics() {
var walletBalanceData = openSearchService.getWalletBalanceDataByInterval();
walletBalanceData
.forEach(dto -> {
final var amount = Double.parseDouble(dto.getWallet().getBalance().getAmount());
var gauge = Gauge.builder(Metric.WALLET_BALANCES_AMOUNT.getName(), this, o -> amount)
var walletsBalancesDataByInterval = openSearchService.getWalletsBalancesDataByInterval();
for (WalletBalanceData walletBalanceData : walletsBalancesDataByInterval) {
var id = walletBalanceData.getWallet().getId();
if (!walletBalancesAggregatesMap.containsKey(id)) {
var gauge = Gauge.builder(
Metric.WALLET_BALANCES_AMOUNT.getName(),
walletBalancesAggregatesMap,
map -> map.get(id))
.description(Metric.WALLET_BALANCES_AMOUNT.getDescription())
.tags(getTags(dto));
.tags(getTags(walletBalanceData));
meterRegistryService.registry(gauge);
});
}
walletBalancesAggregatesMap.put(id, Double.parseDouble(walletBalanceData.getWallet().getBalance().getAmount()));
}
var registeredMetricsSize = meterRegistryService.getRegisteredMetricsSize(Metric.WALLET_BALANCES_AMOUNT.getName());
log.info("Payments with final statuses metrics have been registered to 'prometheus', " +
"registeredMetricsSize = {}, clientSize = {}", registeredMetricsSize, walletBalanceData.size());
"registeredMetricsSize = {}, clientSize = {}", registeredMetricsSize, walletsBalancesDataByInterval.size());
}
private Tags getTags(WalletBalanceData dto) {