Search in sources :

Example 1 with Counter

use of io.micrometer.core.instrument.Counter in project eventuate-local by eventuate-local.

the class DbLogBasedCdcKafkaPublisher method handleEvent.

@Override
public void handleEvent(EVENT publishedEvent) throws EventuateLocalPublishingException {
    Objects.requireNonNull(publishedEvent);
    logger.trace("Got record " + publishedEvent.toString());
    String aggregateTopic = publishingStrategy.topicFor(publishedEvent);
    String json = publishingStrategy.toJson(publishedEvent);
    Exception lastException = null;
    for (int i = 0; i < 5; i++) {
        try {
            if (duplicatePublishingDetector.shouldBePublished(publishedEvent.getBinlogFileOffset(), aggregateTopic)) {
                producer.send(aggregateTopic, publishingStrategy.partitionKeyFor(publishedEvent), json).get(10, TimeUnit.SECONDS);
                publishingStrategy.getCreateTime(publishedEvent).ifPresent(time -> histogramEventAge.ifPresent(x -> x.set(System.currentTimeMillis() - time)));
                meterEventsPublished.ifPresent(Counter::increment);
                databaseOffsetKafkaStore.save(publishedEvent.getBinlogFileOffset());
            } else {
                logger.debug("Duplicate event {}", publishedEvent);
                meterEventsDuplicates.ifPresent(Counter::increment);
            }
            return;
        } catch (Exception e) {
            logger.warn("error publishing to " + aggregateTopic, e);
            meterEventsRetries.ifPresent(Counter::increment);
            lastException = e;
            try {
                Thread.sleep((int) Math.pow(2, i) * 1000);
            } catch (InterruptedException ie) {
                throw new RuntimeException(ie);
            }
        }
    }
    throw new EventuateLocalPublishingException("error publishing to " + aggregateTopic, lastException);
}
Also used : EventuateLocalPublishingException(io.eventuate.local.common.exception.EventuateLocalPublishingException) Counter(io.micrometer.core.instrument.Counter) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) BinLogEvent(io.eventuate.local.common.BinLogEvent) Logger(org.slf4j.Logger) CdcKafkaPublisher(io.eventuate.local.common.CdcKafkaPublisher) PublishingStrategy(io.eventuate.local.common.PublishingStrategy) LoggerFactory(org.slf4j.LoggerFactory) EventuateLocalPublishingException(io.eventuate.local.common.exception.EventuateLocalPublishingException) Counter(io.micrometer.core.instrument.Counter) EventuateLocalPublishingException(io.eventuate.local.common.exception.EventuateLocalPublishingException)

Example 2 with Counter

use of io.micrometer.core.instrument.Counter in project vertx-micrometer-metrics by vert-x3.

the class CountersTest method shouldIgnoreCounterLabel.

@Test
public void shouldIgnoreCounterLabel() {
    MeterRegistry registry = new SimpleMeterRegistry();
    BackendRegistries.registerMatchers(registry, Collections.singletonList(new Match().setLabel("address").setType(MatchType.REGEX).setValue(".*").setAlias("_")));
    Counters counters = new Counters("my_counter", "", registry, Label.ADDRESS);
    counters.get("addr1").increment();
    counters.get("addr1").increment();
    counters.get("addr2").increment();
    Counter c = registry.find("my_counter").tags("address", "_").counter();
    assertThat(c.count()).isEqualTo(3d);
    c = registry.find("my_counter").tags("address", "addr1").counter();
    assertThat(c).isNull();
    c = registry.find("my_counter").tags("address", "addr2").counter();
    assertThat(c).isNull();
}
Also used : Counter(io.micrometer.core.instrument.Counter) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Match(io.vertx.micrometer.Match) Test(org.junit.Test)

Example 3 with Counter

use of io.micrometer.core.instrument.Counter in project vertx-micrometer-metrics by vert-x3.

the class CountersTest method shouldAliasCounterLabel.

@Test
public void shouldAliasCounterLabel() {
    MeterRegistry registry = new SimpleMeterRegistry();
    BackendRegistries.registerMatchers(registry, Collections.singletonList(new Match().setLabel("address").setType(MatchType.REGEX).setValue("addr1").setAlias("1")));
    Counters counters = new Counters("my_counter", "", registry, Label.ADDRESS);
    counters.get("addr1").increment();
    counters.get("addr1").increment();
    counters.get("addr2").increment();
    Counter c = registry.find("my_counter").tags("address", "1").counter();
    assertThat(c.count()).isEqualTo(2d);
    c = registry.find("my_counter").tags("address", "addr1").counter();
    assertThat(c).isNull();
    c = registry.find("my_counter").tags("address", "addr2").counter();
    assertThat(c.count()).isEqualTo(1d);
}
Also used : Counter(io.micrometer.core.instrument.Counter) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Match(io.vertx.micrometer.Match) Test(org.junit.Test)

Example 4 with Counter

use of io.micrometer.core.instrument.Counter in project micrometer by micrometer-metrics.

the class CounterSample method main.

public static void main(String[] args) {
    MeterRegistry registry = SampleConfig.myMonitoringSystem();
    Counter counter = registry.counter("counter", "method", "actual");
    AtomicInteger n = new AtomicInteger(0);
    registry.more().counter("counter", Tags.of("method", "function"), n);
    RandomEngine r = new MersenneTwister64(0);
    Normal dist = new Normal(0, 1, r);
    Flux.interval(Duration.ofMillis(10)).doOnEach(d -> {
        if (dist.nextDouble() + 0.1 > 0) {
            counter.increment();
            n.incrementAndGet();
        }
    }).blockLast();
}
Also used : Counter(io.micrometer.core.instrument.Counter) Tags(io.micrometer.core.instrument.Tags) Flux(reactor.core.publisher.Flux) RandomEngine(cern.jet.random.engine.RandomEngine) SampleConfig(io.micrometer.core.samples.utils.SampleConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Normal(cern.jet.random.Normal) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Duration(java.time.Duration) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) Counter(io.micrometer.core.instrument.Counter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) RandomEngine(cern.jet.random.engine.RandomEngine) Normal(cern.jet.random.Normal) MeterRegistry(io.micrometer.core.instrument.MeterRegistry)

Example 5 with Counter

use of io.micrometer.core.instrument.Counter in project spring-cloud-netflix by spring-cloud.

the class DefaultCounterFactoryTests method shouldIncrement.

@Test
public void shouldIncrement() throws Exception {
    MeterRegistry meterRegistry = mock(MeterRegistry.class);
    CounterFactory factory = new DefaultCounterFactory(meterRegistry);
    Counter counter = mock(Counter.class);
    when(meterRegistry.counter(NAME)).thenReturn(counter);
    factory.increment(NAME);
    verify(counter).increment();
}
Also used : Counter(io.micrometer.core.instrument.Counter) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) CounterFactory(com.netflix.zuul.monitoring.CounterFactory) Test(org.junit.Test)

Aggregations

Counter (io.micrometer.core.instrument.Counter)12 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)5 Test (org.junit.Test)5 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)4 Test (org.junit.jupiter.api.Test)3 CdcKafkaPublisher (io.eventuate.local.common.CdcKafkaPublisher)2 PublishingStrategy (io.eventuate.local.common.PublishingStrategy)2 EventuateLocalPublishingException (io.eventuate.local.common.exception.EventuateLocalPublishingException)2 SampleConfig (io.micrometer.core.samples.utils.SampleConfig)2 Match (io.vertx.micrometer.Match)2 Duration (java.time.Duration)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 DisplayName (org.junit.jupiter.api.DisplayName)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Flux (reactor.core.publisher.Flux)2 Normal (cern.jet.random.Normal)1 MersenneTwister64 (cern.jet.random.engine.MersenneTwister64)1 RandomEngine (cern.jet.random.engine.RandomEngine)1