use of ai.asserts.aws.cloudwatch.config.ScrapeConfig in project aws-cloudwatch-exporter by asserts.
the class MetricTaskManager method afterPropertiesSet.
public void afterPropertiesSet() {
ScrapeConfig scrapeConfig = scrapeConfigProvider.getScrapeConfig();
Set<String> regions = scrapeConfig.getRegions();
scrapeConfig.getNamespaces().stream().filter(nc -> !CollectionUtils.isEmpty(nc.getMetrics())).flatMap(nc -> nc.getMetrics().stream().map(MetricConfig::getScrapeInterval)).forEach(interval -> regions.forEach(region -> addScrapeTask(scrapeConfig, interval, region)));
alarmMetricExporter.register(collectorRegistry);
// After AlarmMetricExporter is register call one time alarm fetcher
alarmFetcher.sendAlarmsForRegions();
}
use of ai.asserts.aws.cloudwatch.config.ScrapeConfig 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.cloudwatch.config.ScrapeConfig 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.cloudwatch.config.ScrapeConfig in project aws-cloudwatch-exporter by asserts.
the class LambdaLogMetricScrapeTask method scrapeLogEvents.
private Map<FunctionLogScrapeConfig, FilteredLogEvent> scrapeLogEvents() {
Map<FunctionLogScrapeConfig, FilteredLogEvent> map = new HashMap<>();
ScrapeConfig scrapeConfig = scrapeConfigProvider.getScrapeConfig();
scrapeConfig.getLambdaConfig().ifPresent(nc -> {
log.info("BEGIN lambda log scrape for region {}", region);
if (!CollectionUtils.isEmpty(nc.getLogs()) && lambdaFunctionScraper.getFunctions().containsKey(region)) {
try (CloudWatchLogsClient cloudWatchLogsClient = awsClientProvider.getCloudWatchLogsClient(region)) {
lambdaFunctionScraper.getFunctions().get(region).forEach((arn, functionConfig) -> nc.getLogs().stream().filter(logScrapeConfig -> logScrapeConfig.shouldScrapeLogsFor(functionConfig.getName())).findFirst().ifPresent(logScrapeConfig -> {
sleep(scrapeConfig.getLogScrapeDelaySeconds() * 1000);
Optional<FilteredLogEvent> logEventOpt = logEventScraper.findLogEvent(cloudWatchLogsClient, functionConfig, logScrapeConfig);
logEventOpt.ifPresent(logEvent -> map.put(new FunctionLogScrapeConfig(functionConfig, logScrapeConfig), logEvent));
}));
} catch (Exception e) {
log.error("Failed to scrape lambda logs", e);
}
} else {
log.info("No functions found for region {}", region);
}
log.info("END lambda log scrape for region {}", region);
});
return map;
}
use of ai.asserts.aws.cloudwatch.config.ScrapeConfig in project aws-cloudwatch-exporter by asserts.
the class LambdaLogMetricScrapeTask method collect.
public List<MetricFamilySamples> collect() {
List<Collector.MetricFamilySamples.Sample> samples = new ArrayList<>();
ScrapeConfig scrapeConfig = scrapeConfigProvider.getScrapeConfig();
scrapeConfig.getLambdaConfig().ifPresent(namespaceConfig -> {
Map<FunctionLogScrapeConfig, FilteredLogEvent> copy = this.cache;
copy.forEach((config, event) -> logEventMetricEmitter.getSample(namespaceConfig, config, event).ifPresent(samples::add));
});
return Collections.singletonList(new MetricFamilySamples("aws_lambda_logs", GAUGE, "", samples));
}
Aggregations