com.vladmihalcea.flexypool.metric.MetricsFactory Java Examples

The following examples show how to use com.vladmihalcea.flexypool.metric.MetricsFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MicrometerMetricsFactoryServiceTest.java    From flexy-pool with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadFailure() {
    ClassLoader currentClassLoader = ClassLoaderUtils.getClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(new ClassLoader() {
            @Override
            protected Class loadClass(String class_name, boolean resolve) throws ClassNotFoundException {
                if(class_name.equals(METRICS_CLASS_NAME)) {
                    return null;
                }
                return super.loadClass(class_name, resolve);
            }
        });
        MetricsFactory metricsFactory = new MicrometerMetricsFactoryService().load();
        assertNull(metricsFactory);
    } finally {
        Thread.currentThread().setContextClassLoader(currentClassLoader);
    }
}
 
Example #2
Source File: CodahaleMetricsFactoryServiceTest.java    From flexy-pool with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadFailure() {
    ClassLoader currentClassLoader = ClassLoaderUtils.getClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(new ClassLoader() {
            @Override
            protected Class loadClass(String class_name, boolean resolve) throws ClassNotFoundException {
                if(class_name.equals(METRICS_CLASS_NAME)) {
                    return null;
                }
                return super.loadClass(class_name, resolve);
            }
        });
        MetricsFactory metricsFactory = new CodahaleMetricsFactoryService().load();
        assertNull(metricsFactory);
    } finally {
        Thread.currentThread().setContextClassLoader(currentClassLoader);
    }
}
 
Example #3
Source File: DropwizardMetricsFactoryServiceTest.java    From flexy-pool with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadFailure() {
    ClassLoader currentClassLoader = ClassLoaderUtils.getClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(new ClassLoader() {
            @Override
            protected Class loadClass(String class_name, boolean resolve) throws ClassNotFoundException {
                if(class_name.equals(METRICS_CLASS_NAME)) {
                    return null;
                }
                return super.loadClass(class_name, resolve);
            }
        });
        MetricsFactory metricsFactory = new DropwizardMetricsFactoryService().load();
        assertNull(metricsFactory);
    } finally {
        Thread.currentThread().setContextClassLoader(currentClassLoader);
    }
}
 
Example #4
Source File: FlexyPoolWrapper.java    From hammock with Apache License 2.0 5 votes vote down vote up
private Configuration<HikariDataSource> createConfiguration(String name, Config config, HikariDataSource dataSource) {
    String uniqueId = config.getOptionalValue(format(UNIQUE_ID_PROPERTY_FORMAT, name), String.class).orElse(name);
    Configuration.Builder<HikariDataSource> builder = new Configuration.Builder<>(uniqueId, dataSource, HikariCPPoolAdapter.FACTORY);
    if(metricsFactoryInstance.isResolvable()) {
        MetricsFactory metricsFactory = metricsFactoryInstance.get();
        builder.setMetricsFactory(metricsFactory);
    }
    return builder.build();
}
 
Example #5
Source File: ConfigurationTest.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuilder() {
    final MeterRegistry meterRegistry = Mockito.mock(MeterRegistry.class);

    DataSource dataSource = Mockito.mock(DataSource.class);
    PoolAdapterFactory<DataSource> poolAdapterFactory = Mockito.mock(PoolAdapterFactory.class);
    ConnectionProxyFactory connectionProxyFactory = Mockito.mock(ConnectionProxyFactory.class);
    Metrics metrics = Mockito.mock(Metrics.class);
    PoolAdapter poolAdapter = Mockito.mock(PoolAdapter.class);
    when(poolAdapterFactory.newInstance(any(ConfigurationProperties.class))).thenReturn(poolAdapter);
    Configuration<DataSource> configuration = new Configuration.Builder<DataSource>(
            "unique", dataSource, poolAdapterFactory)
            .setConnectionProxyFactory(connectionProxyFactory)
            .setJmxAutoStart(true)
            .setJmxEnabled(true)
            .setMetricLogReporterMillis(120)
            .setMetricsFactory(new MetricsFactory() {
                @Override public Metrics newInstance(ConfigurationProperties configurationProperties) {
                    return new MicrometerMetrics(configurationProperties, meterRegistry);
                }
            })
            .build();
    assertSame("unique", configuration.getUniqueName());
    assertSame(connectionProxyFactory, configuration.getConnectionProxyFactory());
    assertTrue(configuration.isJmxAutoStart());
    assertTrue(configuration.isJmxEnabled());
    assertEquals(120, configuration.getMetricLogReporterMillis());
    assertSame(poolAdapter, configuration.getPoolAdapter());
    assertSame(dataSource, configuration.getTargetDataSource());
}
 
Example #6
Source File: ConfigurationTest.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuilder() {
    DataSource dataSource = Mockito.mock(DataSource.class);
    PoolAdapterFactory<DataSource> poolAdapterFactory = Mockito.mock(PoolAdapterFactory.class);
    ConnectionProxyFactory connectionProxyFactory = Mockito.mock(ConnectionProxyFactory.class);
    MetricsFactory metricsFactory = Mockito.mock(MetricsFactory.class);
    Metrics metrics = Mockito.mock(Metrics.class);
    PoolAdapter poolAdapter = Mockito.mock(PoolAdapter.class);
    when(metricsFactory.newInstance(any(ConfigurationProperties.class))).thenReturn(metrics);
    when(poolAdapterFactory.newInstance(any(ConfigurationProperties.class))).thenReturn(poolAdapter);
    Configuration<DataSource> configuration = new Configuration.Builder<DataSource>(
        "unique", dataSource, poolAdapterFactory)
    .setConnectionProxyFactory(connectionProxyFactory)
    .setJmxAutoStart(true)
    .setJmxEnabled(true)
    .setMetricLogReporterMillis(120)
    .setMetricsFactory(metricsFactory)
    .build();
    assertSame("unique", configuration.getUniqueName());
    assertSame(connectionProxyFactory, configuration.getConnectionProxyFactory());
    assertTrue(configuration.isJmxAutoStart());
    assertTrue(configuration.isJmxEnabled());
    assertEquals(120, configuration.getMetricLogReporterMillis());
    assertSame(metrics, configuration.getMetrics());
    assertSame(poolAdapter, configuration.getPoolAdapter());
    assertSame(dataSource, configuration.getTargetDataSource());
}
 
Example #7
Source File: ConfigurationTest.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuilder() {
    final MetricRegistry metricRegistry = Mockito.mock(MetricRegistry.class);

    DataSource dataSource = Mockito.mock(DataSource.class);
    PoolAdapterFactory<DataSource> poolAdapterFactory = Mockito.mock(PoolAdapterFactory.class);
    ConnectionProxyFactory connectionProxyFactory = Mockito.mock(ConnectionProxyFactory.class);
    Metrics metrics = Mockito.mock(Metrics.class);
    PoolAdapter poolAdapter = Mockito.mock(PoolAdapter.class);
    when(poolAdapterFactory.newInstance(any(ConfigurationProperties.class))).thenReturn(poolAdapter);
    Configuration<DataSource> configuration = new Configuration.Builder<DataSource>(
            "unique", dataSource, poolAdapterFactory)
            .setConnectionProxyFactory(connectionProxyFactory)
            .setJmxAutoStart(true)
            .setJmxEnabled(true)
            .setMetricLogReporterMillis(120)
            .setMetricsFactory(new MetricsFactory() {
                @Override
                public Metrics newInstance(ConfigurationProperties configurationProperties) {
                    return new DropwizardMetrics(configurationProperties, metricRegistry, new ReservoirFactory() {
                        @Override
                        public Reservoir newInstance(Class<? extends Metric> metricClass, String metricName) {
                            return new ExponentiallyDecayingReservoir();
                        }
                    });
                }
            })
            .build();
    assertSame("unique", configuration.getUniqueName());
    assertSame(connectionProxyFactory, configuration.getConnectionProxyFactory());
    assertTrue(configuration.isJmxAutoStart());
    assertTrue(configuration.isJmxEnabled());
    assertEquals(120, configuration.getMetricLogReporterMillis());
    assertSame(poolAdapter, configuration.getPoolAdapter());
    assertSame(dataSource, configuration.getTargetDataSource());
}
 
Example #8
Source File: ConfigurationTest.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuilder() {
    final MetricRegistry metricRegistry = Mockito.mock(MetricRegistry.class);

    DataSource dataSource = Mockito.mock(DataSource.class);
    PoolAdapterFactory<DataSource> poolAdapterFactory = Mockito.mock(PoolAdapterFactory.class);
    ConnectionProxyFactory connectionProxyFactory = Mockito.mock(ConnectionProxyFactory.class);
    Metrics metrics = Mockito.mock(Metrics.class);
    PoolAdapter poolAdapter = Mockito.mock(PoolAdapter.class);
    when(poolAdapterFactory.newInstance(any(ConfigurationProperties.class))).thenReturn(poolAdapter);
    Configuration<DataSource> configuration = new Configuration.Builder<DataSource>(
            "unique", dataSource, poolAdapterFactory)
            .setConnectionProxyFactory(connectionProxyFactory)
            .setJmxAutoStart(true)
            .setJmxEnabled(true)
            .setMetricLogReporterMillis(120)
            .setMetricsFactory(new MetricsFactory() {
                @Override
                public Metrics newInstance(ConfigurationProperties configurationProperties) {
                    return new CodahaleMetrics(configurationProperties, metricRegistry, new ReservoirFactory() {
                        @Override
                        public Reservoir newInstance(Class<? extends Metric> metricClass, String metricName) {
                            return new ExponentiallyDecayingReservoir();
                        }
                    });
                }
            })
            .build();
    assertSame("unique", configuration.getUniqueName());
    assertSame(connectionProxyFactory, configuration.getConnectionProxyFactory());
    assertTrue(configuration.isJmxAutoStart());
    assertTrue(configuration.isJmxEnabled());
    assertEquals(120, configuration.getMetricLogReporterMillis());
    assertSame(poolAdapter, configuration.getPoolAdapter());
    assertSame(dataSource, configuration.getTargetDataSource());
}
 
Example #9
Source File: MicrometerMetricsFactoryServiceTest.java    From flexy-pool with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadSuccess() {
    MetricsFactory metricsFactory = new MicrometerMetricsFactoryService().load();
    assertNotNull(metricsFactory);
    assertSame(MicrometerMetrics.FACTORY, metricsFactory);
}
 
Example #10
Source File: FlexyPoolDataSource.java    From flexy-pool with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private Configuration<D> configuration(D dataSource) {
    String uniqueName = propertyLoader.getUniqueName();
    PoolAdapterFactory<D> poolAdapterFactory = propertyLoader.getPoolAdapterFactory();
    MetricsFactory metricsFactory = propertyLoader.getMetricsFactory();
    ConnectionProxyFactory connectionProxyFactory = propertyLoader.getConnectionProxyFactory();
    Integer metricLogReporterMillis = propertyLoader.getMetricLogReporterMillis();
    Boolean jmxEnabled = propertyLoader.isJmxEnabled();
    Boolean jmxAutoStart = propertyLoader.isJmxAutoStart();
    EventListenerResolver eventListenerResolver = propertyLoader.getEventListenerResolver();
    Long connectionAcquireTimeThresholdMillis = propertyLoader.getConnectionAcquireTimeThresholdMillis();
    Long connectionLeaseTimeThresholdMillis = propertyLoader.getConnectionLeaseTimeThresholdMillis();

    if (poolAdapterFactory == null) {
        poolAdapterFactory = (PoolAdapterFactory<D>) DataSourcePoolAdapter.FACTORY;
    }

    Configuration.Builder<D> configurationBuilder = new Configuration.Builder<D>(
            uniqueName, dataSource, poolAdapterFactory
    );
    if (metricsFactory != null) {
        configurationBuilder.setMetricsFactory(metricsFactory);
    }
    if (connectionProxyFactory != null) {
        configurationBuilder.setConnectionProxyFactory(connectionProxyFactory);
    }
    if (metricLogReporterMillis != null) {
        configurationBuilder.setMetricLogReporterMillis(metricLogReporterMillis);
    }
    if (jmxEnabled != null) {
        configurationBuilder.setJmxEnabled(jmxEnabled);
    }
    if (jmxAutoStart != null) {
        configurationBuilder.setJmxAutoStart(jmxAutoStart);
    }
    if (eventListenerResolver != null) {
        configurationBuilder.setEventListenerResolver(eventListenerResolver);
    }
    if (connectionAcquireTimeThresholdMillis != null) {
        configurationBuilder.setConnectionAcquireTimeThresholdMillis(connectionAcquireTimeThresholdMillis);
    }
    if (connectionLeaseTimeThresholdMillis != null) {
        configurationBuilder.setConnectionLeaseTimeThresholdMillis(connectionLeaseTimeThresholdMillis);
    }
    return configurationBuilder.build();
}
 
Example #11
Source File: CodahaleMetricsFactoryServiceTest.java    From flexy-pool with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadSuccess() {
    MetricsFactory metricsFactory = new CodahaleMetricsFactoryService().load();
    assertNotNull(metricsFactory);
    assertSame(CodahaleMetrics.FACTORY, metricsFactory);
}
 
Example #12
Source File: FlexyPoolConfiguration.java    From spring-boot-data-source-decorator with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public MetricsFactory metricsFactory() {
    return MicrometerMetrics.FACTORY;
}
 
Example #13
Source File: DropwizardMetricsFactoryServiceTest.java    From flexy-pool with Apache License 2.0 4 votes vote down vote up
@Test
public void testLoadSuccess() {
    MetricsFactory metricsFactory = new DropwizardMetricsFactoryService().load();
    assertNotNull(metricsFactory);
    assertSame(DropwizardMetrics.FACTORY, metricsFactory);
}
 
Example #14
Source File: DropwizardMetricsFactoryService.java    From flexy-pool with Apache License 2.0 2 votes vote down vote up
/**
 * Load DropwizardMetrics Factory if the Dropwizard Metrics is available at runtime
 *
 * @return DropwizardMetrics Factory
 */
@Override
public MetricsFactory load() {
    return ClassLoaderUtils.findClass(METRICS_CLASS_NAME) ? DropwizardMetrics.FACTORY : null;
}
 
Example #15
Source File: CodahaleMetricsFactoryService.java    From flexy-pool with Apache License 2.0 2 votes vote down vote up
/**
 * Load CodahaleMetrics Factory if the Codahale Metrics is available at runtime
 *
 * @return CodahaleMetrics Factory
 */
@Override
public MetricsFactory load() {
    return ClassLoaderUtils.findClass(METRICS_CLASS_NAME) ? CodahaleMetrics.FACTORY : null;
}
 
Example #16
Source File: Configuration.java    From flexy-pool with Apache License 2.0 2 votes vote down vote up
/**
 * Set metrics factory
 *
 * @param metricsFactory metrics factory
 * @return this {@link com.vladmihalcea.flexypool.config.Configuration.Builder}
 */
public Builder<T> setMetricsFactory(MetricsFactory metricsFactory) {
    this.metricsFactory = metricsFactory;
    return this;
}
 
Example #17
Source File: PropertyLoader.java    From flexy-pool with Apache License 2.0 2 votes vote down vote up
/**
 * Get the {@link MetricsFactory}
 *
 * @return {@link MetricsFactory}
 */
public MetricsFactory getMetricsFactory() {
    return instantiateClass(PropertyKey.POOL_METRICS_FACTORY);
}
 
Example #18
Source File: MicrometerMetricsFactoryService.java    From flexy-pool with Apache License 2.0 2 votes vote down vote up
/**
 * Load {@link MicrometerMetrics} if the Micrometer Metrics is available at runtime.
 *
 * @return Micrometer {@link MetricsFactory}
 */
@Override
public MetricsFactory load() {
    return ClassLoaderUtils.findClass(METRICS_CLASS_NAME) ? MicrometerMetrics.FACTORY : null;
}