Search in sources :

Example 6 with MetricIdGenerator

use of com.datastax.oss.driver.internal.core.metrics.MetricIdGenerator in project java-driver by datastax.

the class MicroProfileMetricsIT method assertMetricsPresent.

@Override
protected void assertMetricsPresent(CqlSession session) {
    MetricRegistry registry = (MetricRegistry) ((InternalDriverContext) session.getContext()).getMetricRegistry();
    assertThat(registry).isNotNull();
    assertThat(registry.getMetrics()).hasSize(ENABLED_SESSION_METRICS.size() + ENABLED_NODE_METRICS.size() * 3);
    MetricIdGenerator metricIdGenerator = ((InternalDriverContext) session.getContext()).getMetricIdGenerator();
    for (DefaultSessionMetric metric : ENABLED_SESSION_METRICS) {
        MetricId metricId = metricIdGenerator.sessionMetricId(metric);
        Tag[] tags = MicroProfileTags.toMicroProfileTags(metricId.getTags());
        MetricID id = new MetricID(metricId.getName(), tags);
        Metric m = registry.getMetrics().get(id);
        assertThat(m).isNotNull();
        switch(metric) {
            case CONNECTED_NODES:
                assertThat(m).isInstanceOf(Gauge.class);
                assertThat((Integer) ((Gauge<?>) m).getValue()).isEqualTo(3);
                break;
            case CQL_REQUESTS:
                assertThat(m).isInstanceOf(Timer.class);
                await().untilAsserted(() -> assertThat(((Timer) m).getCount()).isEqualTo(30));
                break;
            case CQL_PREPARED_CACHE_SIZE:
                assertThat(m).isInstanceOf(Gauge.class);
                assertThat((Long) ((Gauge<?>) m).getValue()).isOne();
                break;
            case BYTES_SENT:
            case BYTES_RECEIVED:
                assertThat(m).isInstanceOf(Meter.class);
                assertThat(((Meter) m).getCount()).isGreaterThan(0);
                break;
            case CQL_CLIENT_TIMEOUTS:
            case THROTTLING_ERRORS:
                assertThat(m).isInstanceOf(Counter.class);
                assertThat(((Counter) m).getCount()).isZero();
                break;
            case THROTTLING_DELAY:
                assertThat(m).isInstanceOf(Timer.class);
                assertThat(((Timer) m).getCount()).isZero();
                break;
            case THROTTLING_QUEUE_SIZE:
                assertThat(m).isInstanceOf(Gauge.class);
                assertThat((Integer) ((Gauge<?>) m).getValue()).isZero();
                break;
        }
    }
    for (Node node : session.getMetadata().getNodes().values()) {
        for (DefaultNodeMetric metric : ENABLED_NODE_METRICS) {
            MetricId description = metricIdGenerator.nodeMetricId(node, metric);
            Tag[] tags = MicroProfileTags.toMicroProfileTags(description.getTags());
            MetricID id = new MetricID(description.getName(), tags);
            Metric m = registry.getMetrics().get(id);
            assertThat(m).isNotNull();
            switch(metric) {
                case OPEN_CONNECTIONS:
                    assertThat(m).isInstanceOf(Gauge.class);
                    // control node has 2 connections
                    assertThat((Integer) ((Gauge<?>) m).getValue()).isBetween(1, 2);
                    break;
                case CQL_MESSAGES:
                    assertThat(m).isInstanceOf(Timer.class);
                    await().untilAsserted(() -> assertThat(((Timer) m).getCount()).isEqualTo(10));
                    break;
                case READ_TIMEOUTS:
                case WRITE_TIMEOUTS:
                case UNAVAILABLES:
                case OTHER_ERRORS:
                case ABORTED_REQUESTS:
                case UNSENT_REQUESTS:
                case RETRIES:
                case IGNORES:
                case RETRIES_ON_READ_TIMEOUT:
                case RETRIES_ON_WRITE_TIMEOUT:
                case RETRIES_ON_UNAVAILABLE:
                case RETRIES_ON_OTHER_ERROR:
                case RETRIES_ON_ABORTED:
                case IGNORES_ON_READ_TIMEOUT:
                case IGNORES_ON_WRITE_TIMEOUT:
                case IGNORES_ON_UNAVAILABLE:
                case IGNORES_ON_OTHER_ERROR:
                case IGNORES_ON_ABORTED:
                case SPECULATIVE_EXECUTIONS:
                case CONNECTION_INIT_ERRORS:
                case AUTHENTICATION_ERRORS:
                    assertThat(m).isInstanceOf(Counter.class);
                    assertThat(((Counter) m).getCount()).isZero();
                    break;
                case BYTES_SENT:
                case BYTES_RECEIVED:
                    assertThat(m).isInstanceOf(Meter.class);
                    assertThat(((Meter) m).getCount()).isGreaterThan(0L);
                    break;
                case AVAILABLE_STREAMS:
                case IN_FLIGHT:
                case ORPHANED_STREAMS:
                    assertThat(m).isInstanceOf(Gauge.class);
                    break;
            }
        }
    }
}
Also used : DefaultSessionMetric(com.datastax.oss.driver.api.core.metrics.DefaultSessionMetric) Meter(org.eclipse.microprofile.metrics.Meter) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) Node(com.datastax.oss.driver.api.core.metadata.Node) DefaultNodeMetric(com.datastax.oss.driver.api.core.metrics.DefaultNodeMetric) MetricIdGenerator(com.datastax.oss.driver.internal.core.metrics.MetricIdGenerator) MetricId(com.datastax.oss.driver.internal.core.metrics.MetricId) MetricID(org.eclipse.microprofile.metrics.MetricID) Counter(org.eclipse.microprofile.metrics.Counter) Timer(org.eclipse.microprofile.metrics.Timer) DefaultNodeMetric(com.datastax.oss.driver.api.core.metrics.DefaultNodeMetric) Metric(org.eclipse.microprofile.metrics.Metric) DefaultSessionMetric(com.datastax.oss.driver.api.core.metrics.DefaultSessionMetric) Tag(org.eclipse.microprofile.metrics.Tag) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext)

Example 7 with MetricIdGenerator

use of com.datastax.oss.driver.internal.core.metrics.MetricIdGenerator in project java-driver by datastax.

the class MicrometerNodeMetricUpdaterTest method should_log_warning_when_provided_eviction_time_setting_is_too_low.

@Test
public void should_log_warning_when_provided_eviction_time_setting_is_too_low() {
    // given
    LoggerTest.LoggerSetup logger = LoggerTest.setupTestLogger(AbstractMetricUpdater.class, Level.WARN);
    Node node = mock(Node.class);
    InternalDriverContext context = mock(InternalDriverContext.class);
    DriverExecutionProfile profile = mock(DriverExecutionProfile.class);
    DriverConfig config = mock(DriverConfig.class);
    MetricIdGenerator generator = mock(MetricIdGenerator.class);
    Set<NodeMetric> enabledMetrics = Collections.singleton(DefaultNodeMetric.CQL_MESSAGES);
    Duration expireAfter = AbstractMetricUpdater.MIN_EXPIRE_AFTER.minusMinutes(1);
    // when
    when(context.getSessionName()).thenReturn("prefix");
    when(context.getConfig()).thenReturn(config);
    when(config.getDefaultProfile()).thenReturn(profile);
    when(context.getMetricIdGenerator()).thenReturn(generator);
    when(profile.getDuration(DefaultDriverOption.METRICS_NODE_EXPIRE_AFTER)).thenReturn(expireAfter);
    when(generator.nodeMetricId(node, DefaultNodeMetric.CQL_MESSAGES)).thenReturn(METRIC_ID);
    when(profile.getDuration(DefaultDriverOption.METRICS_NODE_CQL_MESSAGES_HIGHEST)).thenReturn(Duration.ofSeconds(10));
    when(profile.getDuration(DefaultDriverOption.METRICS_NODE_CQL_MESSAGES_LOWEST)).thenReturn(Duration.ofMillis(1));
    when(profile.getInt(DefaultDriverOption.METRICS_NODE_CQL_MESSAGES_DIGITS)).thenReturn(5);
    MicrometerNodeMetricUpdater updater = new MicrometerNodeMetricUpdater(node, context, enabledMetrics, new SimpleMeterRegistry());
    // then
    assertThat(updater.getExpireAfter()).isEqualTo(AbstractMetricUpdater.MIN_EXPIRE_AFTER);
    verify(logger.appender, timeout(500).times(1)).doAppend(logger.loggingEventCaptor.capture());
    assertThat(logger.loggingEventCaptor.getValue().getMessage()).isNotNull();
    assertThat(logger.loggingEventCaptor.getValue().getFormattedMessage()).contains(String.format("[prefix] Value too low for %s: %s. Forcing to %s instead.", DefaultDriverOption.METRICS_NODE_EXPIRE_AFTER.getPath(), expireAfter, AbstractMetricUpdater.MIN_EXPIRE_AFTER));
}
Also used : MetricIdGenerator(com.datastax.oss.driver.internal.core.metrics.MetricIdGenerator) DefaultNodeMetric(com.datastax.oss.driver.api.core.metrics.DefaultNodeMetric) DseNodeMetric(com.datastax.dse.driver.api.core.metrics.DseNodeMetric) NodeMetric(com.datastax.oss.driver.api.core.metrics.NodeMetric) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) Node(com.datastax.oss.driver.api.core.metadata.Node) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) DriverConfig(com.datastax.oss.driver.api.core.config.DriverConfig) Duration(java.time.Duration) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) LoggerTest(com.datastax.oss.driver.internal.core.util.LoggerTest) LoggerTest(com.datastax.oss.driver.internal.core.util.LoggerTest) Test(org.junit.Test)

Example 8 with MetricIdGenerator

use of com.datastax.oss.driver.internal.core.metrics.MetricIdGenerator in project java-driver by datastax.

the class MicrometerNodeMetricUpdaterTest method should_create_timer.

@Test
@UseDataProvider(value = "timerMetrics")
public void should_create_timer(NodeMetric metric, DriverOption lowest, DriverOption highest, DriverOption digits, DriverOption sla) {
    // given
    Node node = mock(Node.class);
    InternalDriverContext context = mock(InternalDriverContext.class);
    DriverExecutionProfile profile = mock(DriverExecutionProfile.class);
    DriverConfig config = mock(DriverConfig.class);
    MetricIdGenerator generator = mock(MetricIdGenerator.class);
    Set<NodeMetric> enabledMetrics = Collections.singleton(metric);
    // when
    when(context.getSessionName()).thenReturn("prefix");
    when(context.getConfig()).thenReturn(config);
    when(config.getDefaultProfile()).thenReturn(profile);
    when(context.getMetricIdGenerator()).thenReturn(generator);
    when(profile.getDuration(DefaultDriverOption.METRICS_NODE_EXPIRE_AFTER)).thenReturn(Duration.ofHours(1));
    when(profile.getDuration(lowest)).thenReturn(Duration.ofMillis(10));
    when(profile.getDuration(highest)).thenReturn(Duration.ofSeconds(1));
    when(profile.getInt(digits)).thenReturn(5);
    when(profile.isDefined(sla)).thenReturn(true);
    when(profile.getDurationList(sla)).thenReturn(Arrays.asList(Duration.ofMillis(100), Duration.ofMillis(500)));
    when(generator.nodeMetricId(node, metric)).thenReturn(METRIC_ID);
    SimpleMeterRegistry registry = spy(new SimpleMeterRegistry());
    MicrometerNodeMetricUpdater updater = new MicrometerNodeMetricUpdater(node, context, enabledMetrics, registry);
    for (int i = 0; i < 10; i++) {
        updater.updateTimer(metric, null, 100, TimeUnit.MILLISECONDS);
    }
    // then
    Timer timer = registry.find(METRIC_ID.getName()).timer();
    assertThat(timer).isNotNull();
    assertThat(timer.count()).isEqualTo(10);
    HistogramSnapshot snapshot = timer.takeSnapshot();
    assertThat(snapshot.histogramCounts()).hasSize(2);
}
Also used : MetricIdGenerator(com.datastax.oss.driver.internal.core.metrics.MetricIdGenerator) DefaultNodeMetric(com.datastax.oss.driver.api.core.metrics.DefaultNodeMetric) DseNodeMetric(com.datastax.dse.driver.api.core.metrics.DseNodeMetric) NodeMetric(com.datastax.oss.driver.api.core.metrics.NodeMetric) Timer(io.micrometer.core.instrument.Timer) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) Node(com.datastax.oss.driver.api.core.metadata.Node) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) DriverConfig(com.datastax.oss.driver.api.core.config.DriverConfig) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) HistogramSnapshot(io.micrometer.core.instrument.distribution.HistogramSnapshot) LoggerTest(com.datastax.oss.driver.internal.core.util.LoggerTest) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 9 with MetricIdGenerator

use of com.datastax.oss.driver.internal.core.metrics.MetricIdGenerator in project java-driver by datastax.

the class MicrometerSessionMetricUpdaterTest method should_create_timer.

@Test
@UseDataProvider(value = "timerMetrics")
public void should_create_timer(SessionMetric metric, DriverOption lowest, DriverOption highest, DriverOption digits, DriverOption sla) {
    // given
    InternalDriverContext context = mock(InternalDriverContext.class);
    DriverExecutionProfile profile = mock(DriverExecutionProfile.class);
    DriverConfig config = mock(DriverConfig.class);
    MetricIdGenerator generator = mock(MetricIdGenerator.class);
    Set<SessionMetric> enabledMetrics = Collections.singleton(metric);
    // when
    when(context.getSessionName()).thenReturn("prefix");
    when(context.getConfig()).thenReturn(config);
    when(config.getDefaultProfile()).thenReturn(profile);
    when(context.getMetricIdGenerator()).thenReturn(generator);
    when(profile.getDuration(DefaultDriverOption.METRICS_NODE_EXPIRE_AFTER)).thenReturn(Duration.ofHours(1));
    when(profile.getDuration(lowest)).thenReturn(Duration.ofMillis(10));
    when(profile.getDuration(highest)).thenReturn(Duration.ofSeconds(1));
    when(profile.getInt(digits)).thenReturn(5);
    when(profile.isDefined(sla)).thenReturn(true);
    when(profile.getDurationList(sla)).thenReturn(Arrays.asList(Duration.ofMillis(100), Duration.ofMillis(500)));
    when(generator.sessionMetricId(metric)).thenReturn(METRIC_ID);
    SimpleMeterRegistry registry = spy(new SimpleMeterRegistry());
    MicrometerSessionMetricUpdater updater = new MicrometerSessionMetricUpdater(context, enabledMetrics, registry);
    for (int i = 0; i < 10; i++) {
        updater.updateTimer(metric, null, 100, TimeUnit.MILLISECONDS);
    }
    // then
    Timer timer = registry.find(METRIC_ID.getName()).timer();
    assertThat(timer).isNotNull();
    assertThat(timer.count()).isEqualTo(10);
    HistogramSnapshot snapshot = timer.takeSnapshot();
    assertThat(snapshot.histogramCounts()).hasSize(2);
}
Also used : MetricIdGenerator(com.datastax.oss.driver.internal.core.metrics.MetricIdGenerator) DseSessionMetric(com.datastax.dse.driver.api.core.metrics.DseSessionMetric) SessionMetric(com.datastax.oss.driver.api.core.metrics.SessionMetric) DefaultSessionMetric(com.datastax.oss.driver.api.core.metrics.DefaultSessionMetric) Timer(io.micrometer.core.instrument.Timer) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) DriverConfig(com.datastax.oss.driver.api.core.config.DriverConfig) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) HistogramSnapshot(io.micrometer.core.instrument.distribution.HistogramSnapshot) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)9 MetricIdGenerator (com.datastax.oss.driver.internal.core.metrics.MetricIdGenerator)9 DefaultNodeMetric (com.datastax.oss.driver.api.core.metrics.DefaultNodeMetric)8 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)7 Node (com.datastax.oss.driver.api.core.metadata.Node)6 MetricId (com.datastax.oss.driver.internal.core.metrics.MetricId)5 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)4 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)4 DefaultSessionMetric (com.datastax.oss.driver.api.core.metrics.DefaultSessionMetric)4 Test (org.junit.Test)4 DseNodeMetric (com.datastax.dse.driver.api.core.metrics.DseNodeMetric)3 NodeMetric (com.datastax.oss.driver.api.core.metrics.NodeMetric)3 LoggerTest (com.datastax.oss.driver.internal.core.util.LoggerTest)3 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)3 Meter (io.micrometer.core.instrument.Meter)3 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)3 Tag (io.micrometer.core.instrument.Tag)3 Timer (io.micrometer.core.instrument.Timer)3 HistogramSnapshot (io.micrometer.core.instrument.distribution.HistogramSnapshot)2 Counter (com.codahale.metrics.Counter)1