All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.finn.unleash.metric.UnleashMetricServiceImpl Maven / Gradle / Ivy

There is a newer version: 4.4.1
Show newest version
package no.finn.unleash.metric;

import no.finn.unleash.util.UnleashConfig;
import no.finn.unleash.util.UnleashScheduledExecutor;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Set;

public class UnleashMetricServiceImpl implements UnleashMetricService {
    private final LocalDateTime started;
    private final UnleashConfig unleashConfig;
    private final long metricsInterval;
    private final UnleashMetricsSender unleashMetricsSender;

    //mutable
    private volatile MetricsBucket currentMetricsBucket;

    public UnleashMetricServiceImpl(UnleashConfig unleashConfig, UnleashScheduledExecutor executor) {
        this(unleashConfig, new UnleashMetricsSender(unleashConfig), executor);
    }

    public UnleashMetricServiceImpl(UnleashConfig unleashConfig,
                                    UnleashMetricsSender unleashMetricsSender,
                                    UnleashScheduledExecutor executor) {
        this.currentMetricsBucket = new MetricsBucket();
        this.started = LocalDateTime.now(ZoneId.of("UTC"));
        this.unleashConfig = unleashConfig;
        this.metricsInterval = unleashConfig.getSendMetricsInterval();
        this.unleashMetricsSender = unleashMetricsSender;

        executor.setInterval(sendMetrics(), metricsInterval, metricsInterval);
    }

    @Override
    public void register(Set strategies) {
        ClientRegistration registration = new ClientRegistration(unleashConfig, started, strategies);
        unleashMetricsSender.registerClient(registration);
    }

    @Override
    public void count(String toggleName, boolean active) {
        currentMetricsBucket.registerCount(toggleName, active);
    }

    @Override
    public void countVariant(String toggleName, String variantName) {
        currentMetricsBucket.registerCount(toggleName, variantName);
    }

    private Runnable sendMetrics() {
        return () -> {
            MetricsBucket metricsBucket = this.currentMetricsBucket;
            this.currentMetricsBucket = new MetricsBucket();
            metricsBucket.end();
            ClientMetrics metrics = new ClientMetrics(unleashConfig, metricsBucket);
            unleashMetricsSender.sendMetrics(metrics);
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy