-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd328d0
commit c10a7c4
Showing
14 changed files
with
528 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
# analysis-quality-extractor | ||
|
||
Basic instructions: | ||
|
||
The main task is task.AnalysisQualityGenerator.main, which will extract all information. | ||
|
||
You need to have valid AWS credentials (in order to extract the metrics), plus export the following env variables: AWS_PROFILE and AWS_REGION. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package extractor; | ||
|
||
import com.amazonaws.services.logs.AWSLogs; | ||
import com.amazonaws.services.logs.model.FilterLogEventsRequest; | ||
import com.amazonaws.services.logs.model.FilteredLogEvent; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonObject; | ||
import java.util.Arrays; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
import model.ProjectAnalysisMetrics; | ||
import model.logs.CloudWatchEntry; | ||
import model.logs.CloudWatchMetricDefinition; | ||
|
||
public class MetricsConnector { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(MetricsConnector.class.getName()); | ||
private static final Gson GSON = new Gson(); | ||
|
||
private static final String LOG_GROUP = "/SonarCloud/Autoscan/WorkerForJava/Fargate/Task/squad-1"; | ||
private static final int START_TIME_MARGIN_HOUR = -3; | ||
private static final int END_TIME_MARGIN_HOUR = 3; | ||
|
||
private static final List<String> TARGET_METRICS = Arrays.asList( | ||
"JavaAnalysisFinishedCount", | ||
"WorkerForJavaCloneDuration", | ||
"WorkerForJavaTaskDuration", | ||
"DownloadedArtifactsPercentage", | ||
"ParsedArtifactsCount", | ||
"ConstructDependencyGraphDuration", | ||
"DownloadDependenciesDuration", | ||
"ResolveDependenciesDuration", | ||
"WorkerForJavaQueueLatency" | ||
); | ||
|
||
private final AWSLogs logsClient; | ||
private ProjectAnalysisMetrics metrics; | ||
|
||
public MetricsConnector(AWSLogs logsClient) { | ||
this.logsClient = logsClient; | ||
} | ||
|
||
public ProjectAnalysisMetrics getMetrics(String projectKey, String branch, Date analysisDate) { | ||
metrics = new ProjectAnalysisMetrics(); | ||
FilterLogEventsRequest filterRequest = new FilterLogEventsRequest() | ||
.withStartTime(getTimeWithMargin(analysisDate, START_TIME_MARGIN_HOUR)) | ||
.withEndTime(getTimeWithMargin(analysisDate, END_TIME_MARGIN_HOUR)) | ||
.withFilterPattern("{($.log_type = \"METRICS\") && ($.project=\"" + projectKey + "\") && ($.branch=\"" + branch + "\")}") | ||
.withLogGroupName(LOG_GROUP); | ||
|
||
logsClient.filterLogEvents(filterRequest).getEvents().forEach(this::extractMetricsFromEvent); | ||
return metrics; | ||
} | ||
|
||
private long getTimeWithMargin(Date current, int amount) { | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.setTime(current); | ||
calendar.add(Calendar.HOUR, amount); | ||
return calendar.getTimeInMillis(); | ||
} | ||
|
||
private void extractMetricsFromEvent(FilteredLogEvent event) { | ||
CloudWatchEntry cw = GSON.fromJson(event.getMessage(), CloudWatchEntry.class); | ||
cw.getCloudWatchMetrics().getMetrics().forEach(cwMetrics -> | ||
cwMetrics.getMetrics().stream() | ||
.filter(this::isTargetMetric) | ||
.forEach(metricDefinition -> populateMetric(event, metricDefinition)) | ||
); | ||
} | ||
|
||
private boolean isTargetMetric(CloudWatchMetricDefinition metricDefinition) { | ||
return TARGET_METRICS.contains(metricDefinition.getName()); | ||
} | ||
|
||
private void populateMetric(FilteredLogEvent event, CloudWatchMetricDefinition metricDefinition) { | ||
JsonObject json = GSON.fromJson(event.getMessage(), JsonObject.class); | ||
ProjectAnalysisMetrics.PopulateMetricCommand.populateValue( | ||
metrics, metricDefinition.getName(), json.get(metricDefinition.getName()).getAsString()); | ||
LOGGER.info(metricDefinition.getName() + ": " + json.get(metricDefinition.getName()).getAsString()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package model; | ||
|
||
import java.util.Map; | ||
|
||
import static java.lang.Float.parseFloat; | ||
import static java.lang.Integer.parseInt; | ||
|
||
public class ProjectAnalysisMetrics { | ||
|
||
private int JavaAnalysisFinishedCount; | ||
private int WorkerForJavaCloneDuration; | ||
private int WorkerForJavaTaskDuration; | ||
private float DownloadedArtifactsPercentage; | ||
private int ParsedArtifactsCount; | ||
private int ConstructDependencyGraphDuration; | ||
private int DownloadDependenciesDuration; | ||
private int ResolveDependenciesDuration; | ||
private int WorkerForJavaQueueLatency; | ||
|
||
public int getJavaAnalysisFinishedCount() { | ||
return JavaAnalysisFinishedCount; | ||
} | ||
|
||
public void setJavaAnalysisFinishedCount(int javaAnalysisFinishedCount) { | ||
JavaAnalysisFinishedCount = javaAnalysisFinishedCount; | ||
} | ||
|
||
public int getWorkerForJavaCloneDuration() { | ||
return WorkerForJavaCloneDuration; | ||
} | ||
|
||
public void setWorkerForJavaCloneDuration(int workerForJavaCloneDuration) { | ||
WorkerForJavaCloneDuration = workerForJavaCloneDuration; | ||
} | ||
|
||
public int getWorkerForJavaTaskDuration() { | ||
return WorkerForJavaTaskDuration; | ||
} | ||
|
||
public void setWorkerForJavaTaskDuration(int workerForJavaTaskDuration) { | ||
WorkerForJavaTaskDuration = workerForJavaTaskDuration; | ||
} | ||
|
||
public float getDownloadedArtifactsPercentage() { | ||
return DownloadedArtifactsPercentage; | ||
} | ||
|
||
public void setDownloadedArtifactsPercentage(float downloadedArtifactsPercentage) { | ||
DownloadedArtifactsPercentage = downloadedArtifactsPercentage; | ||
} | ||
|
||
public int getParsedArtifactsCount() { | ||
return ParsedArtifactsCount; | ||
} | ||
|
||
public void setParsedArtifactsCount(int parsedArtifactsCount) { | ||
ParsedArtifactsCount = parsedArtifactsCount; | ||
} | ||
|
||
public int getConstructDependencyGraphDuration() { | ||
return ConstructDependencyGraphDuration; | ||
} | ||
|
||
public void setConstructDependencyGraphDuration(int constructDependencyGraphDuration) { | ||
ConstructDependencyGraphDuration = constructDependencyGraphDuration; | ||
} | ||
|
||
public int getDownloadDependenciesDuration() { | ||
return DownloadDependenciesDuration; | ||
} | ||
|
||
public void setDownloadDependenciesDuration(int downloadDependenciesDuration) { | ||
DownloadDependenciesDuration = downloadDependenciesDuration; | ||
} | ||
|
||
public int getResolveDependenciesDuration() { | ||
return ResolveDependenciesDuration; | ||
} | ||
|
||
public void setResolveDependenciesDuration(int resolveDependenciesDuration) { | ||
ResolveDependenciesDuration = resolveDependenciesDuration; | ||
} | ||
|
||
public int getWorkerForJavaQueueLatency() { | ||
return WorkerForJavaQueueLatency; | ||
} | ||
|
||
public void setWorkerForJavaQueueLatency(int workerForJavaQueueLatency) { | ||
WorkerForJavaQueueLatency = workerForJavaQueueLatency; | ||
} | ||
|
||
private interface PopulateCommand { | ||
void populate(ProjectAnalysisMetrics metrics, String value); | ||
} | ||
|
||
public static class PopulateMetricCommand { | ||
|
||
private PopulateMetricCommand() { | ||
throw new IllegalStateException("Utility class"); | ||
} | ||
|
||
private static Map<String, PopulateCommand> populateCommandProvider() { | ||
return Map.of( | ||
"JavaAnalysisFinishedCount", | ||
(metrics, value) -> metrics.setJavaAnalysisFinishedCount(parseInt(value)), | ||
"WorkerForJavaCloneDuration", | ||
(metrics, value) -> metrics.setWorkerForJavaCloneDuration(parseInt(value)), | ||
"WorkerForJavaTaskDuration", | ||
(metrics, value) -> metrics.setWorkerForJavaTaskDuration(parseInt(value)), | ||
"DownloadedArtifactsPercentage", | ||
(metrics, value) -> metrics.setDownloadedArtifactsPercentage(parseFloat(value)), | ||
"ParsedArtifactsCount", | ||
(metrics, value) -> metrics.setParsedArtifactsCount(parseInt(value)), | ||
"ConstructDependencyGraphDuration", | ||
(metrics, value) -> metrics.setConstructDependencyGraphDuration(parseInt(value)), | ||
"DownloadDependenciesDuration", | ||
(metrics, value) -> metrics.setDownloadDependenciesDuration(parseInt(value)), | ||
"ResolveDependenciesDuration", | ||
(metrics, value) -> metrics.setResolveDependenciesDuration(parseInt(value)), | ||
"WorkerForJavaQueueLatency", | ||
(metrics, value) -> metrics.setWorkerForJavaQueueLatency(parseInt(value)) | ||
); | ||
} | ||
|
||
public static void populateValue(ProjectAnalysisMetrics metrics, String metric, String value) { | ||
PopulateCommand command = populateCommandProvider().get(metric); | ||
command.populate(metrics, value); | ||
} | ||
|
||
} | ||
|
||
|
||
} |
Oops, something went wrong.