Example usage for com.amazonaws.util EC2MetadataUtils getInstanceId

List of usage examples for com.amazonaws.util EC2MetadataUtils getInstanceId

Introduction

In this page you can find the example usage for com.amazonaws.util EC2MetadataUtils getInstanceId.

Prototype

public static String getInstanceId() 

Source Link

Document

Get the ID of this instance.

Usage

From source file:com.basistech.metrics.TryThisOut.java

License:Open Source License

public TryThisOut() {
    registry = new MetricRegistry();
    gauge = new Gauge<Integer>() {
        @Override//  w w w  .  ja  v a  2s  .  com
        public Integer getValue() {
            return gaugeValue;
        }
    };
    registry.register("gauge", gauge);
    counter = new Counter();
    registry.register("counter", counter);
    histogram = new Histogram(new SlidingTimeWindowReservoir(10, TimeUnit.SECONDS));
    registry.register("histogram", histogram);
    meter = new Meter();
    registry.register("meter", meter);
    timer = new Timer();
    registry.register("timer", timer);

    String instanceId = EC2MetadataUtils.getInstanceId();
    // try out the default constructor.
    AmazonCloudWatchClient client = new AmazonCloudWatchClient();
    reporter = new CloudWatchReporter.Builder(registry, client).dimension("instanceId", instanceId)
            .namespace("some-namespace").build();
    reporter.start(5, TimeUnit.SECONDS);

    executorService = Executors.newSingleThreadScheduledExecutor();
    executorService.scheduleAtFixedRate(new Runnable() {
        private int counterVal;
        private Random random = new Random();

        @Override
        public void run() {
            gaugeValue = counterVal++;
            counter.inc();
            Timer.Context context = timer.time();
            meter.mark();
            histogram.update((int) (random.nextGaussian() * 10));
            context.stop();
        }
    }, 0, 1, TimeUnit.SECONDS);
}

From source file:com.blacklocus.qs.worker.aws.AmazonEC2WorkerIdService.java

License:Apache License

@Override
public String getWorkerId() {
    String id = ec2id.get();/*  w  w  w  .  ja v a2 s.  c  om*/

    if (null == id) {
        if (!Boolean.valueOf(System.getProperty(PROP_SKIP_EC2_META, "false"))) {
            id = EC2MetadataUtils.getInstanceId();
            ec2id.set(id);
        }
    }

    // Maybe ec2 meta was disabled, maybe it failed. Whatever: fall back.
    if (StringUtils.isBlank(id)) {
        id = fallback.getWorkerId();
    }

    return id;
}

From source file:com.boundlessgeo.suite.geoserver.cloudwatch.aws.MetricDatumEncoder.java

License:Open Source License

private void setUp() {
    try {//from w  ww  .j av a2s.  c o  m
        if (enablePerInstanceMetrics) {
            amiId = EC2MetadataUtils.getAmiId();
            logger.info("Detected AMI ID of {}", amiId);
            instanceType = EC2MetadataUtils.getInstanceType();
            logger.info("Detected Instance Type = {}", instanceType);
            if (StringUtils.isBlank(instanceId)) {
                instanceId = EC2MetadataUtils.getInstanceId();
                logger.info("Detected Instance Id of {}", instanceId);
            } else
                logger.info("Instance Id overridden with {}", instanceId);
        }
        setup = true;
    } catch (Exception ex) {
        logger.error("Unable to determine AMI ID or Instance type...are we on AWS?");
        logger.error(ex.getMessage());
        //only try this once....the strings can just be empty if it fails.
        //the sender will catch it anyways
        setup = true;
    }
}

From source file:com.kixeye.chassis.bootstrap.aws.ServerInstanceContext.java

License:Apache License

private ServerInstanceContext() {
    amazonElasticLoadBalancing = new AmazonElasticLoadBalancingClient();
    amazonEC2 = new AmazonEC2Client();

    ec2MetadataClient = new Ec2MetadataClient() {
        @Override// w ww.j a va 2  s  .com
        public String getAvailabilityZone() {
            return EC2MetadataUtils.getAvailabilityZone();
        }

        @Override
        public String getInstanceId() {
            return EC2MetadataUtils.getInstanceId();
        }

        @Override
        public String getUserData() {
            return EC2MetadataUtils.getUserData();
        }

        @Override
        public String getPrivateIpAddress() {
            return EC2MetadataUtils.getPrivateIpAddress();
        }

        @Override
        public String getPublicIpAddress() {
            for (EC2MetadataUtils.NetworkInterface net : EC2MetadataUtils.getNetworkInterfaces()) {
                List<String> ips = net.getPublicIPv4s();
                if (ips != null && ips.size() > 0) {
                    return ips.get(0);
                }
            }
            return null;
        }
    };

    init();
}

From source file:org.springframework.cloud.aws.core.env.ec2.AmazonEc2InstanceIdProvider.java

License:Apache License

@Override
public String getCurrentInstanceId() {
    return EC2MetadataUtils.getInstanceId();
}

From source file:org.web.online.cloudwatch.tomcat.valve.ElapsedTimeAggregator.java

License:Apache License

/**
 * Construct the instance querying EC2 meta data to get the InstanceId
 * and querying the tags of this instance to get the
 * AutoScalingGroupName (tag key == "aws:autoscaling:groupName").
 * /*from  w w  w. j  ava  2  s .  c o m*/
 * Additionally sets up the Cloud Watch metric with the given namespace,
 * a metric name of "ElapsedTime" and dimensions
 * of InstanceId and AutoScalingGroupName.
 * 
 * @param namespace namespace value to use to push data to CloudWatch
 * @throws IllegalStateException if instance/ASG details not able to be
 * retrieved
 */
public ElapsedTimeAggregator(String namespace) {
    this(namespace, Regions.getCurrentRegion(), EC2MetadataUtils.getInstanceId(), null, new AmazonEC2Client(),
            new AmazonCloudWatchClient());
}