Ejemplo n.º 1
0
 @Override
 public void setRegion(String region) {
   if (region == null || region.isEmpty()) AwsSdkMetrics.setRegion(null);
   else {
     AwsSdkMetrics.setRegion(Regions.fromName(region));
   }
 }
Ejemplo n.º 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
 }
Ejemplo n.º 5
0
 @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);
 }
Ejemplo n.º 6
0
 @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());
 }
Ejemplo n.º 7
0
 @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;
  }
Ejemplo n.º 9
0
 @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);
 }
Ejemplo n.º 10
0
  @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());
  }
Ejemplo n.º 11
0
 @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());
 }
Ejemplo n.º 12
0
 @Override
 public void setMachineMetricsExcluded(boolean excludeJvmMetrics) {
   AwsSdkMetrics.setMachineMetricsExcluded(excludeJvmMetrics);
 }
Ejemplo n.º 13
0
 @Override
 public boolean isMetricsEnabled() {
   return AwsSdkMetrics.isMetricsEnabled();
 }
Ejemplo n.º 14
0
 @Override
 public void disableMetrics() {
   AwsSdkMetrics.disableMetrics();
 }
Ejemplo n.º 15
0
 @Override
 public boolean enableDefaultMetrics() {
   return AwsSdkMetrics.enableDefaultMetrics();
 }
Ejemplo n.º 16
0
 @Test
 public void removeNull() {
   assertFalse(AwsSdkMetrics.remove(null));
 }
Ejemplo n.º 17
0
 @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());
 }
Ejemplo n.º 18
0
 @Test
 public void addAllNull() {
   assertFalse(AwsSdkMetrics.addAll(null));
   assertFalse(AwsSdkMetrics.addAll(Collections.<MetricType>emptyList()));
 }
Ejemplo n.º 19
0
 @Override
 public void setMetricQueueSize(Integer metricQueueSize) {
   AwsSdkMetrics.setMetricQueueSize(metricQueueSize);
 }
Ejemplo n.º 20
0
 /**
  * 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());
 }
Ejemplo n.º 21
0
 @Override
 public void setQueuePollTimeoutMilli(Integer timeoutMilli) {
   AwsSdkMetrics.setQueuePollTimeoutMilli(
       timeoutMilli == null ? null : timeoutMilli.longValue());
 }
Ejemplo n.º 22
0
 @Test
 public void addNull() {
   assertFalse(AwsSdkMetrics.add(null));
 }