use of software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client in project aws-cloudwatch-exporter by asserts.
the class LBToLambdaRoutingBuilder method getRoutings.
public Set<ResourceRelation> getRoutings() {
log.info("LB To Lambda routing relation builder about to build relations");
Set<ResourceRelation> routing = new HashSet<>();
scrapeConfigProvider.getScrapeConfig().getRegions().forEach(region -> {
try (ElasticLoadBalancingV2Client elbV2Client = awsClientProvider.getELBV2Client(region)) {
Map<Resource, Resource> tgToLB = targetGroupLBMapProvider.getTgToLB();
tgToLB.keySet().forEach(tg -> {
try {
String api = "ElasticLoadBalancingV2Client/describeTargetHealth";
DescribeTargetHealthResponse response = rateLimiter.doWithRateLimit(api, ImmutableSortedMap.of(SCRAPE_REGION_LABEL, region, SCRAPE_ACCOUNT_ID_LABEL, accountIDProvider.getAccountId(), SCRAPE_OPERATION_LABEL, api), () -> elbV2Client.describeTargetHealth(DescribeTargetHealthRequest.builder().targetGroupArn(tg.getArn()).build()));
if (!isEmpty(response.targetHealthDescriptions())) {
response.targetHealthDescriptions().stream().map(tH -> resourceMapper.map(tH.target().id())).filter(opt -> opt.isPresent() && opt.get().getType().equals(LambdaFunction)).map(Optional::get).forEach(lambda -> routing.add(ResourceRelation.builder().from(tgToLB.get(tg)).to(lambda).name("ROUTES_TO").build()));
}
} catch (Exception e) {
log.error("Failed to build resource relations", e);
}
});
}
});
return routing;
}
use of software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client in project aws-cloudwatch-exporter by asserts.
the class TargetGroupLBMapProvider method mapListener.
@VisibleForTesting
void mapListener(ElasticLoadBalancingV2Client lbClient, SortedMap<String, String> labels, Resource lbResource, Listener listener) {
SortedMap<String, String> telemetryLabels = new TreeMap<>(labels);
telemetryLabels.put(SCRAPE_OPERATION_LABEL, "ElasticLoadBalancingClientV2/describeRules");
DescribeRulesResponse dLR = rateLimiter.doWithRateLimit("ElasticLoadBalancingClientV2/describeRules", telemetryLabels, () -> lbClient.describeRules(DescribeRulesRequest.builder().listenerArn(listener.listenerArn()).build()));
if (dLR.hasRules()) {
dLR.rules().stream().filter(rule -> !isEmpty(rule.actions())).flatMap(rule -> rule.actions().stream()).filter(action -> action.targetGroupArn() != null).map(action -> resourceMapper.map(action.targetGroupArn())).filter(Optional::isPresent).map(Optional::get).forEach(tg -> tgToLB.put(tg, lbResource));
}
}
use of software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client in project aws-cloudwatch-exporter by asserts.
the class TargetGroupLBMapProvider method update.
public void update() {
try {
ScrapeConfig scrapeConfig = scrapeConfigProvider.getScrapeConfig();
scrapeConfig.getRegions().forEach(region -> {
try (ElasticLoadBalancingV2Client lbClient = awsClientProvider.getELBV2Client(region)) {
String api = "ElasticLoadBalancingV2Client/describeLoadBalancers";
ImmutableSortedMap<String, String> labels = ImmutableSortedMap.of(SCRAPE_REGION_LABEL, region, SCRAPE_OPERATION_LABEL, api);
DescribeLoadBalancersResponse resp = rateLimiter.doWithRateLimit(api, labels, lbClient::describeLoadBalancers);
if (resp.hasLoadBalancers()) {
resp.loadBalancers().forEach(lb -> mapLB(lbClient, labels, lb));
}
}
});
} catch (Exception e) {
log.error("Failed to build LB Target Group map", e);
}
}
use of software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client in project aws-cloudwatch-exporter by asserts.
the class TargetGroupLBMapProviderTest method update.
@Test
public void update() {
LoadBalancer loadBalancer = LoadBalancer.builder().loadBalancerArn("lb-arn").build();
expect(awsClientProvider.getELBV2Client("region")).andReturn(lbClient);
expect(lbClient.describeLoadBalancers()).andReturn(DescribeLoadBalancersResponse.builder().loadBalancers(loadBalancer).build());
metricCollector.recordLatency(eq(SCRAPE_LATENCY_METRIC), anyObject(SortedMap.class), anyLong());
AtomicInteger sideEffect = new AtomicInteger();
TargetGroupLBMapProvider testClass = new TargetGroupLBMapProvider(scrapeConfigProvider, awsClientProvider, resourceMapper, new RateLimiter(metricCollector)) {
@Override
void mapLB(ElasticLoadBalancingV2Client client, SortedMap<String, String> theLabels, LoadBalancer theLb) {
assertEquals(lbClient, client);
assertEquals(labels, theLabels);
assertEquals(loadBalancer, theLb);
sideEffect.incrementAndGet();
}
};
lbClient.close();
replayAll();
testClass.update();
assertEquals(1, sideEffect.get());
verifyAll();
}
use of software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client in project adama-lang by mathgladiator.
the class Fleet method fleetDeployUpdateELB.
private static void fleetDeployUpdateELB(Config config, HashSet<String> frontendInstances) {
ElasticLoadBalancingV2Client elb = getELB(config);
String loadBalancerArn = config.get_string("loadbalancer-arn", null);
if (loadBalancerArn == null) {
throw new NullPointerException("config has no 'loadbalancer-arn'");
}
ArrayList<TargetDescription> toRemove = new ArrayList<>();
ArrayList<TargetDescription> toAdd = new ArrayList<>();
for (TargetHealthDescription health : elb.describeTargetHealth(DescribeTargetHealthRequest.builder().targetGroupArn(loadBalancerArn).build()).targetHealthDescriptions()) {
if (frontendInstances.contains(health.target().id())) {
frontendInstances.remove(health.target().id());
} else {
toRemove.add(health.target());
}
}
for (String idToAdd : frontendInstances) {
toAdd.add(TargetDescription.builder().id(idToAdd).port(8080).build());
}
if (toAdd.size() > 0) {
elb.registerTargets(RegisterTargetsRequest.builder().targetGroupArn(loadBalancerArn).targets(toAdd).build());
System.err.println("registered " + toAdd.size() + " targets");
}
if (toRemove.size() > 0) {
elb.deregisterTargets(DeregisterTargetsRequest.builder().targetGroupArn(loadBalancerArn).targets(toRemove).build());
System.err.println("deregistered " + toRemove.size() + " targets");
}
}
Aggregations