Search in sources :

Example 1 with ResourceMapper

use of ai.asserts.aws.resource.ResourceMapper in project aws-cloudwatch-exporter by asserts.

the class LBToASGRelationBuilder method updateRouting.

public void updateRouting() {
    log.info("Updating LB to ASG Routing relations");
    Set<ResourceRelation> newConfigs = new HashSet<>();
    scrapeConfigProvider.getScrapeConfig().getRegions().forEach(region -> {
        String api = "AutoScalingClient/describeAutoScalingGroups";
        try (AutoScalingClient autoScalingClient = rateLimiter.doWithRateLimit(api, ImmutableSortedMap.of(SCRAPE_REGION_LABEL, region, SCRAPE_OPERATION_LABEL, api), () -> awsClientProvider.getAutoScalingClient(region))) {
            DescribeAutoScalingGroupsResponse resp = autoScalingClient.describeAutoScalingGroups();
            List<AutoScalingGroup> groups = resp.autoScalingGroups();
            if (!isEmpty(groups)) {
                groups.forEach(asg -> resourceMapper.map(asg.autoScalingGroupARN()).ifPresent(asgRes -> {
                    if (!isEmpty(asg.targetGroupARNs())) {
                        asg.targetGroupARNs().stream().map(resourceMapper::map).filter(Optional::isPresent).map(Optional::get).filter(tg -> targetGroupLBMapProvider.getTgToLB().containsKey(tg)).map(tg -> targetGroupLBMapProvider.getTgToLB().get(tg)).forEach(lb -> newConfigs.add(ResourceRelation.builder().from(lb).to(asgRes).name("ROUTES_TO").build()));
                    }
                }));
            }
        } catch (Exception e) {
            log.error("Failed to build LB to ASG relationship", e);
        }
    });
    routingConfigs = newConfigs;
}
Also used : CollectionUtils.isEmpty(org.springframework.util.CollectionUtils.isEmpty) ResourceRelation(ai.asserts.aws.resource.ResourceRelation) Getter(lombok.Getter) AWSClientProvider(ai.asserts.aws.AWSClientProvider) ScrapeConfigProvider(ai.asserts.aws.cloudwatch.config.ScrapeConfigProvider) Set(java.util.Set) ResourceMapper(ai.asserts.aws.resource.ResourceMapper) AutoScalingClient(software.amazon.awssdk.services.autoscaling.AutoScalingClient) SCRAPE_REGION_LABEL(ai.asserts.aws.MetricNameUtil.SCRAPE_REGION_LABEL) HashSet(java.util.HashSet) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) DescribeAutoScalingGroupsResponse(software.amazon.awssdk.services.autoscaling.model.DescribeAutoScalingGroupsResponse) List(java.util.List) RateLimiter(ai.asserts.aws.RateLimiter) SCRAPE_OPERATION_LABEL(ai.asserts.aws.MetricNameUtil.SCRAPE_OPERATION_LABEL) Optional(java.util.Optional) AutoScalingGroup(software.amazon.awssdk.services.autoscaling.model.AutoScalingGroup) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) AutoScalingGroup(software.amazon.awssdk.services.autoscaling.model.AutoScalingGroup) ResourceRelation(ai.asserts.aws.resource.ResourceRelation) AutoScalingClient(software.amazon.awssdk.services.autoscaling.AutoScalingClient) DescribeAutoScalingGroupsResponse(software.amazon.awssdk.services.autoscaling.model.DescribeAutoScalingGroupsResponse) Optional(java.util.Optional) HashSet(java.util.HashSet)

Example 2 with ResourceMapper

use of ai.asserts.aws.resource.ResourceMapper in project aws-cloudwatch-exporter by asserts.

the class LambdaInvokeConfigExporterTest method setup.

@BeforeEach
public void setup() {
    fnScraper = mock(LambdaFunctionScraper.class);
    awsClientProvider = mock(AWSClientProvider.class);
    lambdaClient = mock(LambdaClient.class);
    metricNameUtil = mock(MetricNameUtil.class);
    ScrapeConfigProvider scrapeConfigProvider = mock(ScrapeConfigProvider.class);
    ScrapeConfig scrapeConfig = mock(ScrapeConfig.class);
    NamespaceConfig namespaceConfig = mock(NamespaceConfig.class);
    resourceMapper = mock(ResourceMapper.class);
    resource = mock(Resource.class);
    metricSampleBuilder = mock(MetricSampleBuilder.class);
    sample = mock(Collector.MetricFamilySamples.Sample.class);
    metricCollector = mock(BasicMetricCollector.class);
    testClass = new LambdaInvokeConfigExporter(fnScraper, awsClientProvider, metricNameUtil, scrapeConfigProvider, resourceMapper, metricSampleBuilder, new RateLimiter(metricCollector));
    expect(scrapeConfig.getLambdaConfig()).andReturn(Optional.of(namespaceConfig));
    expect(scrapeConfigProvider.getScrapeConfig()).andReturn(scrapeConfig);
    expect(metricNameUtil.getMetricPrefix(CWNamespace.lambda.getNamespace())).andReturn("prefix");
}
Also used : NamespaceConfig(ai.asserts.aws.cloudwatch.config.NamespaceConfig) Resource(ai.asserts.aws.resource.Resource) AWSClientProvider(ai.asserts.aws.AWSClientProvider) LambdaFunctionScraper(ai.asserts.aws.lambda.LambdaFunctionScraper) RateLimiter(ai.asserts.aws.RateLimiter) ScrapeConfigProvider(ai.asserts.aws.cloudwatch.config.ScrapeConfigProvider) LambdaClient(software.amazon.awssdk.services.lambda.LambdaClient) ResourceMapper(ai.asserts.aws.resource.ResourceMapper) Collector(io.prometheus.client.Collector) MetricNameUtil(ai.asserts.aws.MetricNameUtil) ScrapeConfig(ai.asserts.aws.cloudwatch.config.ScrapeConfig) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ResourceMapper

use of ai.asserts.aws.resource.ResourceMapper in project aws-cloudwatch-exporter by asserts.

the class LBToLambdaRoutingBuilderTest method setup.

@BeforeEach
public void setup() {
    AccountIDProvider accountIDProvider = mock(AccountIDProvider.class);
    ScrapeConfigProvider scrapeConfigProvider = mock(ScrapeConfigProvider.class);
    ScrapeConfig scrapeConfig = mock(ScrapeConfig.class);
    AWSClientProvider awsClientProvider = mock(AWSClientProvider.class);
    TargetGroupLBMapProvider targetGroupLBMapProvider = mock(TargetGroupLBMapProvider.class);
    metricCollector = mock(BasicMetricCollector.class);
    elbV2Client = mock(ElasticLoadBalancingV2Client.class);
    resourceMapper = mock(ResourceMapper.class);
    targetResource = mock(Resource.class);
    lbRsource = mock(Resource.class);
    lambdaResource = mock(Resource.class);
    testClass = new LBToLambdaRoutingBuilder(accountIDProvider, scrapeConfigProvider, awsClientProvider, new RateLimiter(metricCollector), resourceMapper, targetGroupLBMapProvider);
    expect(accountIDProvider.getAccountId()).andReturn("account").anyTimes();
    expect(scrapeConfigProvider.getScrapeConfig()).andReturn(scrapeConfig).anyTimes();
    expect(scrapeConfig.getRegions()).andReturn(ImmutableSet.of("region")).anyTimes();
    expect(awsClientProvider.getELBV2Client("region")).andReturn(elbV2Client).anyTimes();
    expect(targetGroupLBMapProvider.getTgToLB()).andReturn(ImmutableMap.of(targetResource, lbRsource));
}
Also used : ResourceMapper(ai.asserts.aws.resource.ResourceMapper) Resource(ai.asserts.aws.resource.Resource) AWSClientProvider(ai.asserts.aws.AWSClientProvider) ElasticLoadBalancingV2Client(software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client) RateLimiter(ai.asserts.aws.RateLimiter) ScrapeConfigProvider(ai.asserts.aws.cloudwatch.config.ScrapeConfigProvider) ScrapeConfig(ai.asserts.aws.cloudwatch.config.ScrapeConfig) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ResourceMapper

use of ai.asserts.aws.resource.ResourceMapper in project aws-cloudwatch-exporter by asserts.

the class LambdaEventSourceExporterTest method setup.

@BeforeEach
public void setup() {
    metricNameUtil = mock(MetricNameUtil.class);
    lambdaClient = mock(LambdaClient.class);
    resourceMapper = mock(ResourceMapper.class);
    fnResource = mock(Resource.class);
    sourceResource = mock(Resource.class);
    resourceTagHelper = mock(ResourceTagHelper.class);
    sampleBuilder = mock(MetricSampleBuilder.class);
    sample = mock(Sample.class);
    familySamples = mock(Collector.MetricFamilySamples.class);
    metricCollector = mock(BasicMetricCollector.class);
    namespaceConfig = mock(NamespaceConfig.class);
    ScrapeConfigProvider scrapeConfigProvider = mock(ScrapeConfigProvider.class);
    expect(namespaceConfig.getName()).andReturn("AWS/Lambda").anyTimes();
    expect(scrapeConfigProvider.getScrapeConfig()).andReturn(ScrapeConfig.builder().regions(ImmutableSet.of("region1")).namespaces(ImmutableList.of(namespaceConfig)).build()).anyTimes();
    AWSClientProvider awsClientProvider = mock(AWSClientProvider.class);
    expect(awsClientProvider.getLambdaClient("region1")).andReturn(lambdaClient).anyTimes();
    testClass = new LambdaEventSourceExporter(scrapeConfigProvider, awsClientProvider, metricNameUtil, resourceMapper, resourceTagHelper, sampleBuilder, new RateLimiter(metricCollector));
}
Also used : NamespaceConfig(ai.asserts.aws.cloudwatch.config.NamespaceConfig) Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) Resource(ai.asserts.aws.resource.Resource) AWSClientProvider(ai.asserts.aws.AWSClientProvider) RateLimiter(ai.asserts.aws.RateLimiter) ScrapeConfigProvider(ai.asserts.aws.cloudwatch.config.ScrapeConfigProvider) LambdaClient(software.amazon.awssdk.services.lambda.LambdaClient) ResourceMapper(ai.asserts.aws.resource.ResourceMapper) MetricNameUtil(ai.asserts.aws.MetricNameUtil) ResourceTagHelper(ai.asserts.aws.resource.ResourceTagHelper) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with ResourceMapper

use of ai.asserts.aws.resource.ResourceMapper in project aws-cloudwatch-exporter by asserts.

the class ECSServiceDiscoveryExporterTest method setup.

@BeforeEach
public void setup() {
    scrapeConfigProvider = mock(ScrapeConfigProvider.class);
    scrapeConfig = mock(ScrapeConfig.class);
    awsClientProvider = mock(AWSClientProvider.class);
    ecsClient = mock(EcsClient.class);
    resourceMapper = mock(ResourceMapper.class);
    resource = mock(Resource.class);
    ecsTaskUtil = mock(ECSTaskUtil.class);
    metricCollector = mock(BasicMetricCollector.class);
    objectMapperFactory = mock(ObjectMapperFactory.class);
    objectMapper = mock(ObjectMapper.class);
    objectWriter = mock(ObjectWriter.class);
    mockStaticConfig = mock(StaticConfig.class);
    lbToECSRoutingBuilder = mock(LBToECSRoutingBuilder.class);
    mockRelation = mock(ResourceRelation.class);
    rateLimiter = new RateLimiter(metricCollector);
    resetAll();
}
Also used : Resource(ai.asserts.aws.resource.Resource) StaticConfig(ai.asserts.aws.exporter.ECSServiceDiscoveryExporter.StaticConfig) AWSClientProvider(ai.asserts.aws.AWSClientProvider) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) EcsClient(software.amazon.awssdk.services.ecs.EcsClient) RateLimiter(ai.asserts.aws.RateLimiter) ScrapeConfigProvider(ai.asserts.aws.cloudwatch.config.ScrapeConfigProvider) ObjectMapperFactory(ai.asserts.aws.ObjectMapperFactory) ResourceRelation(ai.asserts.aws.resource.ResourceRelation) ResourceMapper(ai.asserts.aws.resource.ResourceMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ScrapeConfig(ai.asserts.aws.cloudwatch.config.ScrapeConfig) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ResourceMapper (ai.asserts.aws.resource.ResourceMapper)8 AWSClientProvider (ai.asserts.aws.AWSClientProvider)7 RateLimiter (ai.asserts.aws.RateLimiter)7 ScrapeConfigProvider (ai.asserts.aws.cloudwatch.config.ScrapeConfigProvider)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ScrapeConfig (ai.asserts.aws.cloudwatch.config.ScrapeConfig)5 Resource (ai.asserts.aws.resource.Resource)5 MetricNameUtil (ai.asserts.aws.MetricNameUtil)2 NamespaceConfig (ai.asserts.aws.cloudwatch.config.NamespaceConfig)2 ResourceRelation (ai.asserts.aws.resource.ResourceRelation)2 AutoScalingClient (software.amazon.awssdk.services.autoscaling.AutoScalingClient)2 EcsClient (software.amazon.awssdk.services.ecs.EcsClient)2 ElasticLoadBalancingV2Client (software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client)2 LambdaClient (software.amazon.awssdk.services.lambda.LambdaClient)2 SCRAPE_OPERATION_LABEL (ai.asserts.aws.MetricNameUtil.SCRAPE_OPERATION_LABEL)1 SCRAPE_REGION_LABEL (ai.asserts.aws.MetricNameUtil.SCRAPE_REGION_LABEL)1 ObjectMapperFactory (ai.asserts.aws.ObjectMapperFactory)1 StaticConfig (ai.asserts.aws.exporter.ECSServiceDiscoveryExporter.StaticConfig)1 LambdaFunctionScraper (ai.asserts.aws.lambda.LambdaFunctionScraper)1 ResourceTagHelper (ai.asserts.aws.resource.ResourceTagHelper)1