Search in sources :

Example 1 with ElasticLoadBalancingV2Client

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;
}
Also used : LambdaFunction(ai.asserts.aws.resource.ResourceType.LambdaFunction) CollectionUtils.isEmpty(org.springframework.util.CollectionUtils.isEmpty) DescribeTargetHealthResponse(software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeTargetHealthResponse) ResourceRelation(ai.asserts.aws.resource.ResourceRelation) SCRAPE_ACCOUNT_ID_LABEL(ai.asserts.aws.MetricNameUtil.SCRAPE_ACCOUNT_ID_LABEL) AWSClientProvider(ai.asserts.aws.AWSClientProvider) ScrapeConfigProvider(ai.asserts.aws.cloudwatch.config.ScrapeConfigProvider) Set(java.util.Set) ResourceMapper(ai.asserts.aws.resource.ResourceMapper) Resource(ai.asserts.aws.resource.Resource) ElasticLoadBalancingV2Client(software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client) SCRAPE_REGION_LABEL(ai.asserts.aws.MetricNameUtil.SCRAPE_REGION_LABEL) HashSet(java.util.HashSet) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) RateLimiter(ai.asserts.aws.RateLimiter) Map(java.util.Map) SCRAPE_OPERATION_LABEL(ai.asserts.aws.MetricNameUtil.SCRAPE_OPERATION_LABEL) Optional(java.util.Optional) DescribeTargetHealthRequest(software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest) AllArgsConstructor(lombok.AllArgsConstructor) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ResourceRelation(ai.asserts.aws.resource.ResourceRelation) Resource(ai.asserts.aws.resource.Resource) ElasticLoadBalancingV2Client(software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client) DescribeTargetHealthResponse(software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeTargetHealthResponse) HashSet(java.util.HashSet)

Example 2 with ElasticLoadBalancingV2Client

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));
    }
}
Also used : CollectionUtils.isEmpty(org.springframework.util.CollectionUtils.isEmpty) DescribeRulesRequest(software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeRulesRequest) Getter(lombok.Getter) AWSClientProvider(ai.asserts.aws.AWSClientProvider) ScrapeConfigProvider(ai.asserts.aws.cloudwatch.config.ScrapeConfigProvider) DescribeRulesResponse(software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeRulesResponse) DescribeListenersResponse(software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeListenersResponse) ResourceMapper(ai.asserts.aws.resource.ResourceMapper) SCRAPE_REGION_LABEL(ai.asserts.aws.MetricNameUtil.SCRAPE_REGION_LABEL) ScrapeConfig(ai.asserts.aws.cloudwatch.config.ScrapeConfig) LoadBalancer(software.amazon.awssdk.services.elasticloadbalancingv2.model.LoadBalancer) RateLimiter(ai.asserts.aws.RateLimiter) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) DescribeLoadBalancersResponse(software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeLoadBalancersResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Resource(ai.asserts.aws.resource.Resource) ElasticLoadBalancingV2Client(software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) TreeMap(java.util.TreeMap) Listener(software.amazon.awssdk.services.elasticloadbalancingv2.model.Listener) SCRAPE_OPERATION_LABEL(ai.asserts.aws.MetricNameUtil.SCRAPE_OPERATION_LABEL) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AllArgsConstructor(lombok.AllArgsConstructor) DescribeListenersRequest(software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeListenersRequest) SortedMap(java.util.SortedMap) Optional(java.util.Optional) DescribeRulesResponse(software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeRulesResponse) TreeMap(java.util.TreeMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with ElasticLoadBalancingV2Client

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);
    }
}
Also used : ElasticLoadBalancingV2Client(software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client) DescribeLoadBalancersResponse(software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeLoadBalancersResponse) ScrapeConfig(ai.asserts.aws.cloudwatch.config.ScrapeConfig)

Example 4 with ElasticLoadBalancingV2Client

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) SortedMap(java.util.SortedMap) LoadBalancer(software.amazon.awssdk.services.elasticloadbalancingv2.model.LoadBalancer) ElasticLoadBalancingV2Client(software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client) RateLimiter(ai.asserts.aws.RateLimiter) Test(org.junit.jupiter.api.Test)

Example 5 with ElasticLoadBalancingV2Client

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");
    }
}
Also used : ArrayList(java.util.ArrayList) ElasticLoadBalancingV2Client(software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client)

Aggregations

ElasticLoadBalancingV2Client (software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client)7 RateLimiter (ai.asserts.aws.RateLimiter)4 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)4 AWSClientProvider (ai.asserts.aws.AWSClientProvider)3 ScrapeConfig (ai.asserts.aws.cloudwatch.config.ScrapeConfig)3 ScrapeConfigProvider (ai.asserts.aws.cloudwatch.config.ScrapeConfigProvider)3 Resource (ai.asserts.aws.resource.Resource)3 ResourceMapper (ai.asserts.aws.resource.ResourceMapper)3 SortedMap (java.util.SortedMap)3 DescribeListenersRequest (software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeListenersRequest)3 DescribeListenersResponse (software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeListenersResponse)3 LoadBalancer (software.amazon.awssdk.services.elasticloadbalancingv2.model.LoadBalancer)3 SCRAPE_OPERATION_LABEL (ai.asserts.aws.MetricNameUtil.SCRAPE_OPERATION_LABEL)2 SCRAPE_REGION_LABEL (ai.asserts.aws.MetricNameUtil.SCRAPE_REGION_LABEL)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Map (java.util.Map)2 Optional (java.util.Optional)2 TreeMap (java.util.TreeMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AllArgsConstructor (lombok.AllArgsConstructor)2