use of ai.asserts.aws.resource.Resource in project aws-cloudwatch-exporter by asserts.
the class LBToECSRoutingBuilder method getRoutings.
public Set<ResourceRelation> getRoutings(EcsClient ecsClient, Resource cluster, List<Resource> services) {
Set<ResourceRelation> routing = new HashSet<>();
log.info("Updating Resource Relation for ECS Cluster {}", cluster);
try {
String api = "EcsClient/describeServices";
DescribeServicesResponse response = rateLimiter.doWithRateLimit(api, ImmutableSortedMap.of(SCRAPE_REGION_LABEL, cluster.getRegion(), SCRAPE_ACCOUNT_ID_LABEL, cluster.getAccount(), SCRAPE_OPERATION_LABEL, api), () -> ecsClient.describeServices(DescribeServicesRequest.builder().cluster(cluster.getArn()).services(services.stream().map(Resource::getArn).collect(Collectors.toList())).build()));
if (response.hasServices()) {
response.services().stream().filter(service -> resourceMapper.map(service.serviceArn()).isPresent()).forEach(service -> {
Optional<Resource> servResOpt = resourceMapper.map(service.serviceArn());
servResOpt.ifPresent(servRes -> routing.addAll(service.loadBalancers().stream().map(loadBalancer -> resourceMapper.map(loadBalancer.targetGroupArn())).filter(Optional::isPresent).map(Optional::get).map(tg -> targetGroupLBMapProvider.getTgToLB().get(tg)).filter(Objects::nonNull).map(lb -> ResourceRelation.builder().from(lb).to(servRes).name("ROUTES_TO").build()).collect(Collectors.toSet())));
});
}
} catch (Exception e) {
log.error("Failed to build resource relations", e);
}
return routing;
}
use of ai.asserts.aws.resource.Resource 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 ai.asserts.aws.resource.Resource in project aws-cloudwatch-exporter by asserts.
the class LambdaCapacityExporter method getMetrics.
private List<MetricFamilySamples> getMetrics() {
ScrapeConfig scrapeConfig = scrapeConfigProvider.getScrapeConfig();
Optional<NamespaceConfig> optional = scrapeConfig.getLambdaConfig();
String availableMetric = metricNameUtil.getLambdaMetric("available_concurrency");
String requestedMetric = metricNameUtil.getLambdaMetric("requested_concurrency");
String allocatedMetric = metricNameUtil.getLambdaMetric("allocated_concurrency");
String reservedMetric = metricNameUtil.getLambdaMetric("reserved_concurrency");
String timeoutMetric = metricNameUtil.getLambdaMetric("timeout_seconds");
String memoryLimit = metricNameUtil.getLambdaMetric("memory_limit_mb");
String accountLimitMetric = metricNameUtil.getLambdaMetric("account_limit");
Map<String, List<MetricFamilySamples.Sample>> samples = new TreeMap<>();
optional.ifPresent(lambdaConfig -> functionScraper.getFunctions().forEach((region, functions) -> {
log.info(" - Getting Lambda account and provisioned concurrency for region {}", region);
try (LambdaClient lambdaClient = awsClientProvider.getLambdaClient(region)) {
GetAccountSettingsResponse accountSettings = rateLimiter.doWithRateLimit("LambdaClient/getAccountSettings", ImmutableSortedMap.of(SCRAPE_REGION_LABEL, region, SCRAPE_OPERATION_LABEL, "getAccountSettings", SCRAPE_NAMESPACE_LABEL, "AWS/Lambda"), lambdaClient::getAccountSettings);
MetricFamilySamples.Sample sample = sampleBuilder.buildSingleSample(accountLimitMetric, ImmutableMap.of("region", region, "cw_namespace", lambda.getNormalizedNamespace(), "type", "concurrent_executions"), accountSettings.accountLimit().concurrentExecutions() * 1.0D);
samples.computeIfAbsent(accountLimitMetric, k -> new ArrayList<>()).add(sample);
sample = sampleBuilder.buildSingleSample(accountLimitMetric, ImmutableMap.of("region", region, "cw_namespace", lambda.getNormalizedNamespace(), "type", "unreserved_concurrent_executions"), accountSettings.accountLimit().unreservedConcurrentExecutions() * 1.0D);
samples.computeIfAbsent(accountLimitMetric, k -> new ArrayList<>()).add(sample);
Set<Resource> fnResources = resourceTagHelper.getFilteredResources(region, lambdaConfig);
functions.forEach((functionArn, lambdaFunction) -> {
GetFunctionConcurrencyResponse fCResponse = rateLimiter.doWithRateLimit("LambdaClient/getFunctionConcurrency", ImmutableSortedMap.of(SCRAPE_REGION_LABEL, region, SCRAPE_OPERATION_LABEL, "getFunctionConcurrency", SCRAPE_NAMESPACE_LABEL, "AWS/Lambda"), () -> lambdaClient.getFunctionConcurrency(GetFunctionConcurrencyRequest.builder().functionName(lambdaFunction.getArn()).build()));
if (fCResponse.reservedConcurrentExecutions() != null) {
MetricFamilySamples.Sample reserved = sampleBuilder.buildSingleSample(reservedMetric, ImmutableMap.of("region", region, "cw_namespace", lambda.getNormalizedNamespace(), "d_function_name", lambdaFunction.getName(), "job", lambdaFunction.getName(), SCRAPE_ACCOUNT_ID_LABEL, lambdaFunction.getAccount()), fCResponse.reservedConcurrentExecutions().doubleValue());
samples.computeIfAbsent(reservedMetric, k -> new ArrayList<>()).add(reserved);
}
Optional<Resource> fnResourceOpt = fnResources.stream().filter(resource -> functionArn.equals(resource.getArn())).findFirst();
Map<String, String> labels = new TreeMap<>();
fnResourceOpt.ifPresent(fnResource -> fnResource.addEnvLabel(labels, metricNameUtil));
labels.put("region", region);
labels.put("cw_namespace", lambda.getNormalizedNamespace());
labels.put("d_function_name", lambdaFunction.getName());
labels.put("job", lambdaFunction.getName());
labels.put(SCRAPE_ACCOUNT_ID_LABEL, lambdaFunction.getAccount());
// Export timeout
double timeout = lambdaFunction.getTimeoutSeconds() * 1.0D;
samples.computeIfAbsent(timeoutMetric, k -> new ArrayList<>()).add(sampleBuilder.buildSingleSample(timeoutMetric, labels, timeout));
samples.computeIfAbsent(memoryLimit, k -> new ArrayList<>()).add(sampleBuilder.buildSingleSample(memoryLimit, labels, lambdaFunction.getMemoryMB() * 1.0D));
ListProvisionedConcurrencyConfigsRequest request = ListProvisionedConcurrencyConfigsRequest.builder().functionName(lambdaFunction.getName()).build();
ListProvisionedConcurrencyConfigsResponse response = rateLimiter.doWithRateLimit("LambdaClient/listProvisionedConcurrencyConfigs", ImmutableSortedMap.of(SCRAPE_REGION_LABEL, region, SCRAPE_OPERATION_LABEL, "listProvisionedConcurrencyConfigs", SCRAPE_NAMESPACE_LABEL, "AWS/Lambda"), () -> lambdaClient.listProvisionedConcurrencyConfigs(request));
if (response.hasProvisionedConcurrencyConfigs()) {
response.provisionedConcurrencyConfigs().forEach(config -> {
// Capacity is always provisioned at alias or version level
String[] parts = config.functionArn().split(":");
String level = Character.isDigit(parts[parts.length - 1].charAt(0)) ? "d_executed_version" : "d_resource";
labels.put(level, parts[parts.length - 1]);
Integer available = config.availableProvisionedConcurrentExecutions();
samples.computeIfAbsent(availableMetric, k -> new ArrayList<>()).add(sampleBuilder.buildSingleSample(availableMetric, labels, available.doubleValue()));
Integer requested = config.requestedProvisionedConcurrentExecutions();
samples.computeIfAbsent(requestedMetric, k -> new ArrayList<>()).add(sampleBuilder.buildSingleSample(requestedMetric, labels, requested.doubleValue()));
Integer allocated = config.allocatedProvisionedConcurrentExecutions();
samples.computeIfAbsent(allocatedMetric, k -> new ArrayList<>()).add(sampleBuilder.buildSingleSample(allocatedMetric, labels, allocated.doubleValue()));
});
}
});
} catch (Exception e) {
log.error("Failed to get lambda provisioned capacity for region " + region, e);
}
}));
return samples.values().stream().map(sampleBuilder::buildFamily).collect(Collectors.toList());
}
use of ai.asserts.aws.resource.Resource in project aws-cloudwatch-exporter by asserts.
the class LambdaEventSourceExporter method getMappings.
private List<MetricFamilySamples> getMappings() {
Map<String, List<Sample>> samples = new TreeMap<>();
Map<String, List<EventSourceMappingConfiguration>> byRegion = new TreeMap<>();
ScrapeConfig scrapeConfig = scrapeConfigProvider.getScrapeConfig();
scrapeConfig.getLambdaConfig().ifPresent(namespaceConfig -> scrapeConfig.getRegions().forEach(region -> {
try (LambdaClient client = awsClientProvider.getLambdaClient(region)) {
// Get all event source mappings
log.info("Discovering Lambda event source mappings for region={}", region);
String nextToken = null;
do {
ListEventSourceMappingsRequest req = ListEventSourceMappingsRequest.builder().marker(nextToken).build();
ListEventSourceMappingsResponse response = rateLimiter.doWithRateLimit("LambdaClient/listEventSourceMappings", ImmutableSortedMap.of(SCRAPE_REGION_LABEL, region, SCRAPE_OPERATION_LABEL, "listEventSourceMappings", SCRAPE_NAMESPACE_LABEL, "AWS/Lambda"), () -> client.listEventSourceMappings(req));
if (response.hasEventSourceMappings()) {
byRegion.computeIfAbsent(region, k -> new ArrayList<>()).addAll(response.eventSourceMappings().stream().filter(mapping -> resourceMapper.map(mapping.functionArn()).isPresent()).collect(Collectors.toList()));
}
nextToken = response.nextMarker();
} while (StringUtils.hasText(nextToken));
Set<Resource> fnResources = resourceTagHelper.getFilteredResources(region, namespaceConfig);
byRegion.computeIfAbsent(region, k -> new ArrayList<>()).forEach(mappingConfiguration -> {
Optional<Resource> fnResource = Optional.ofNullable(fnResources.stream().filter(r -> r.getArn().equals(mappingConfiguration.functionArn())).findFirst().orElse(resourceMapper.map(mappingConfiguration.functionArn()).orElse(null)));
Optional<Resource> eventResourceOpt = resourceMapper.map(mappingConfiguration.eventSourceArn());
eventResourceOpt.ifPresent(eventResource -> fnResource.ifPresent(fn -> buildSample(region, fn, eventResource, samples)));
});
} catch (Exception e) {
log.info("Failed to discover event source mappings", e);
}
}));
return samples.values().stream().map(sampleBuilder::buildFamily).collect(Collectors.toList());
}
use of ai.asserts.aws.resource.Resource in project aws-cloudwatch-exporter by asserts.
the class ECSServiceDiscoveryExporter method buildTargetsInCluster.
@VisibleForTesting
List<StaticConfig> buildTargetsInCluster(ScrapeConfig scrapeConfig, EcsClient ecsClient, Resource cluster, Set<ResourceRelation> newRouting) {
List<StaticConfig> targets = new ArrayList<>();
// List services just returns the service ARN. There is no need to paginate
ListServicesRequest serviceReq = ListServicesRequest.builder().cluster(cluster.getName()).build();
ListServicesResponse serviceResp = rateLimiter.doWithRateLimit("EcsClient/listServices", ImmutableSortedMap.of(SCRAPE_REGION_LABEL, cluster.getRegion(), SCRAPE_OPERATION_LABEL, "listServices", SCRAPE_NAMESPACE_LABEL, "AWS/ECS"), () -> ecsClient.listServices(serviceReq));
if (serviceResp.hasServiceArns()) {
List<Resource> services = new ArrayList<>();
serviceResp.serviceArns().stream().map(resourceMapper::map).filter(Optional::isPresent).map(Optional::get).forEach(service -> {
if (scrapeConfig.isDiscoverECSTasks()) {
targets.addAll(buildTargetsInService(scrapeConfig, ecsClient, cluster, service));
}
services.add(service);
});
newRouting.addAll(lbToECSRoutingBuilder.getRoutings(ecsClient, cluster, services));
}
return targets;
}
Aggregations