@Override
 public void setRegion(String region) {
   if (region == null || region.isEmpty()) AwsSdkMetrics.setRegion(null);
   else {
     AwsSdkMetrics.setRegion(Regions.fromName(region));
   }
 }
Exemple #2
0
 @Bean
 @ConditionalOnProperty(value = "aws.metrics.enabled", matchIfMissing = true)
 SpectatorMetricCollector spectatorMetricsCollector(Registry registry) {
   SpectatorMetricCollector collector = new SpectatorMetricCollector(registry);
   AwsSdkMetrics.setMetricCollector(collector);
   return collector;
 }
 void reportMetrics() {
   if (getByteCount() > 0) {
     ServiceMetricCollector col = AwsSdkMetrics.getServiceMetricCollector();
     col.collectByteThroughput(this);
     reset();
   }
 }
 /**
  * Returns true if we should wrap the given input stream with a byte counting wrapper; false
  * otherwise.
  */
 private static boolean wrapWithByteCounting(InputStream in) {
   if (!AwsSdkMetrics.isMetricsEnabled()) return false; // metrics is disabled
   if (in instanceof MetricAware) {
     MetricAware aware = (MetricAware) in;
     // wrap only if not already wrapped in one of it's inner chain of input stream
     return !aware.isMetricActivated();
   }
   return true; // this is a raw input stream so metric wrapping is necessary
 }
 @Test
 public void defaultMetricTypes() {
   // Default set of predefined metric types is not empty
   Set<MetricType> set = AwsSdkMetrics.getPredefinedMetrics();
   assertNotNull(set);
   assertTrue(set.size() > 0);
   // Clear out the default set of predefined metric types
   AwsSdkMetrics.set(Collections.<MetricType>emptyList());
   Set<MetricType> empty = AwsSdkMetrics.getPredefinedMetrics();
   assertNotNull(empty);
   assertTrue(empty.size() == 0);
   // Reconfigure the default set of predefined metric types back to the original
   AwsSdkMetrics.set(set);
   Set<MetricType> set2 = AwsSdkMetrics.getPredefinedMetrics();
   assertNotNull(set2);
   assertTrue(set2.size() > 0);
   // Not the same due to ensuring thread-safety
   assertNotSame(set, set2);
 }
 @Test
 public void setPerHostMetricsIncluded() {
   final boolean b = AwsSdkMetrics.isPerHostMetricIncluded();
   AwsSdkMetrics.setPerHostMetricsIncluded(b);
   assertTrue(b == AwsSdkMetrics.isPerHostMetricIncluded());
   AwsSdkMetrics.setPerHostMetricsIncluded(!b);
   assertFalse(b == AwsSdkMetrics.isPerHostMetricIncluded());
   AwsSdkMetrics.setPerHostMetricsIncluded(b);
   assertTrue(b == AwsSdkMetrics.isPerHostMetricIncluded());
 }
 @Test
 public void setJvmMetricsExcluded() {
   final boolean b = AwsSdkMetrics.isMachineMetricExcluded();
   AwsSdkMetrics.setMachineMetricsExcluded(b);
   assertTrue(b == AwsSdkMetrics.isMachineMetricExcluded());
   AwsSdkMetrics.setMachineMetricsExcluded(!b);
   assertFalse(b == AwsSdkMetrics.isMachineMetricExcluded());
   AwsSdkMetrics.setMachineMetricsExcluded(b);
   assertTrue(b == AwsSdkMetrics.isMachineMetricExcluded());
 }
  public Socket connectSocket(
      final int connectTimeout,
      final Socket socket,
      final HttpHost host,
      final InetSocketAddress remoteAddress,
      final InetSocketAddress localAddress,
      final HttpContext context)
      throws IOException {
    if (LOG.isDebugEnabled()) {
      LOG.debug("connecting to " + remoteAddress.getAddress() + ":" + remoteAddress.getPort());
    }
    Socket connectedSocket;
    try {
      connectedSocket =
          super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
      if (!masterSecretValidator.isMasterSecretValid(connectedSocket)) {
        throw log(new IllegalStateException("Invalid SSL master secret"));
      }
    } catch (final SSLException sslEx) {
      if (shouldClearSslSessionsPredicate.test(sslEx)) {
        // clear any related sessions from our cache
        if (LOG.isDebugEnabled()) {
          LOG.debug("connection failed due to SSL error, clearing TLS session cache", sslEx);
        }
        clearSessionCache(sslContext.getClientSessionContext(), remoteAddress);
      }
      throw sslEx;
    }

    if (connectedSocket instanceof SSLSocket) {
      SdkSSLSocket sslSocket = new SdkSSLSocket((SSLSocket) connectedSocket);
      return AwsSdkMetrics.isHttpSocketReadMetricEnabled()
          ? new SdkSSLMetricsSocket(sslSocket)
          : sslSocket;
    }
    SdkSocket sdkSocket = new SdkSocket(connectedSocket);
    return AwsSdkMetrics.isHttpSocketReadMetricEnabled()
        ? new SdkMetricsSocket(sdkSocket)
        : sdkSocket;
  }
 @Test
 public void setNullOrEmpty() {
   Set<MetricType> orig = AwsSdkMetrics.getPredefinedMetrics();
   assertTrue(orig.size() > 0);
   AwsSdkMetrics.set(null);
   Set<MetricType> empty = AwsSdkMetrics.getPredefinedMetrics();
   assertTrue(empty.size() == 0);
   AwsSdkMetrics.set(null);
   Set<MetricType> stillEmpty = AwsSdkMetrics.getPredefinedMetrics();
   assertSame(empty, stillEmpty);
   AwsSdkMetrics.set(Collections.<MetricType>emptySet());
   Set<MetricType> empty3 = AwsSdkMetrics.getPredefinedMetrics();
   assertSame(empty, empty3);
   AwsSdkMetrics.set(orig);
 }
  @Test
  public void isMetricEnabled() {
    // originally disabled
    assertFalse(AwsSdkMetrics.isMetricsEnabled());
    // set to NONE, so still disabled
    AwsSdkMetrics.setMetricCollector(MetricCollector.NONE);
    assertFalse(AwsSdkMetrics.isMetricsEnabled());
    // set to a custom collector, so now considered enabled
    AwsSdkMetrics.setMetricCollector(
        new MetricCollector() {
          @Override
          public boolean start() {
            return true;
          }

          @Override
          public boolean stop() {
            return false;
          }

          @Override
          public boolean isEnabled() {
            return true;
          }

          @Override
          public RequestMetricCollector getRequestMetricCollector() {
            return RequestMetricCollector.NONE;
          }

          @Override
          public ServiceMetricCollector getServiceMetricCollector() {
            return ServiceMetricCollector.NONE;
          }
        });
    assertTrue(AwsSdkMetrics.isMetricsEnabled());
  }
 @Test
 public void test() {
   // by default, it's disabled
   assertFalse(AwsSdkMetrics.isDefaultMetricsEnabled());
   // won't be anble to enable unless the default impl library is on the classpath
   assertFalse(AwsSdkMetrics.enableDefaultMetrics());
   assertFalse(AwsSdkMetrics.isDefaultMetricsEnabled());
   assertSame(RequestMetricCollector.NONE, AwsSdkMetrics.getRequestMetricCollector());
   assertFalse(AwsSdkMetrics.isDefaultMetricsEnabled());
   // effectively no effect
   AwsSdkMetrics.disableMetrics();
   assertFalse(AwsSdkMetrics.isDefaultMetricsEnabled());
 }
 @Override
 public void setMachineMetricsExcluded(boolean excludeJvmMetrics) {
   AwsSdkMetrics.setMachineMetricsExcluded(excludeJvmMetrics);
 }
 @Override
 public boolean isMetricsEnabled() {
   return AwsSdkMetrics.isMetricsEnabled();
 }
 @Override
 public void disableMetrics() {
   AwsSdkMetrics.disableMetrics();
 }
 @Override
 public boolean enableDefaultMetrics() {
   return AwsSdkMetrics.enableDefaultMetrics();
 }
 @Test
 public void removeNull() {
   assertFalse(AwsSdkMetrics.remove(null));
 }
 @Test
 public void addAndRemove() {
   Set<MetricType> orig = AwsSdkMetrics.getPredefinedMetrics();
   AwsSdkMetrics.set(null);
   // Test add and remove
   assertTrue(AwsSdkMetrics.getPredefinedMetrics().isEmpty());
   AwsSdkMetrics.add(Field.ClientExecuteTime);
   assertFalse(AwsSdkMetrics.getPredefinedMetrics().isEmpty());
   AwsSdkMetrics.remove(Field.ClientExecuteTime);
   assertTrue(AwsSdkMetrics.getPredefinedMetrics().isEmpty());
   // Test add more than one entry
   AwsSdkMetrics.add(Field.ClientExecuteTime);
   AwsSdkMetrics.add(Field.Exception);
   assertTrue(AwsSdkMetrics.getPredefinedMetrics().size() == 2);
   AwsSdkMetrics.set(null);
   assertTrue(AwsSdkMetrics.getPredefinedMetrics().isEmpty());
   // Test addAll
   AwsSdkMetrics.addAll(Arrays.asList(Field.Exception, Field.ClientExecuteTime));
   assertTrue(AwsSdkMetrics.getPredefinedMetrics().size() == 2);
   AwsSdkMetrics.set(orig);
   assertTrue(AwsSdkMetrics.getPredefinedMetrics().size() == orig.size());
 }
 @Test
 public void addAllNull() {
   assertFalse(AwsSdkMetrics.addAll(null));
   assertFalse(AwsSdkMetrics.addAll(Collections.<MetricType>emptyList()));
 }
 @Override
 public void setMetricQueueSize(Integer metricQueueSize) {
   AwsSdkMetrics.setMetricQueueSize(metricQueueSize);
 }
 /**
  * By default the AWS SDK metric collection is disabled. Enabling it should fail unless the
  * necessary CloudWatch related jars are on the classpath. Therefore, this test is expected to
  * fail in enabling the default metric collection, but have absolutely no impact otherwise.
  */
 @Test
 public void enableDefaultMetrics() {
   Assert.assertFalse(AwsSdkMetrics.enableDefaultMetrics());
 }
 @Override
 public void setQueuePollTimeoutMilli(Integer timeoutMilli) {
   AwsSdkMetrics.setQueuePollTimeoutMilli(
       timeoutMilli == null ? null : timeoutMilli.longValue());
 }
 @Test
 public void addNull() {
   assertFalse(AwsSdkMetrics.add(null));
 }