|
16 | 16 |
|
17 | 17 | import org.gradle.api.Plugin;
|
18 | 18 | import org.gradle.api.Project;
|
| 19 | +import org.gradle.api.Task; |
19 | 20 | import org.gradle.api.execution.TaskExecutionGraph;
|
| 21 | +import org.gradle.api.logging.LogLevel; |
20 | 22 |
|
21 | 23 | /** Instruments Gradle to measure latency and success rate of all executed tasks. */
|
22 | 24 | public class MetricsPlugin implements Plugin<Project> {
|
23 | 25 | @Override
|
24 | 26 | public void apply(Project project) {
|
25 |
| - if (!isCollectionEnabled()) { |
26 |
| - project.getLogger().lifecycle("Metrics collection is disabled."); |
27 |
| - return; |
28 |
| - } |
29 |
| - project.getLogger().lifecycle("Metrics collection is enabled."); |
30 | 27 |
|
31 |
| - Metrics metrics = new StackdriverMetrics(project.getGradle(), project.getLogger()); |
| 28 | + Metrics metrics = Metrics.filtered(initializeMetrics(project), MetricsPlugin::isTaskPublic); |
32 | 29 |
|
33 | 30 | TaskExecutionGraph taskGraph = project.getGradle().getTaskGraph();
|
34 | 31 | taskGraph.addTaskExecutionListener(new MeasuringTaskExecutionListener(metrics, taskGraph));
|
35 | 32 | }
|
36 | 33 |
|
| 34 | + private static Metrics initializeMetrics(Project project) { |
| 35 | + if (!isCollectionEnabled()) { |
| 36 | + project.getLogger().lifecycle("Metrics collection is disabled. Logging to stdout..."); |
| 37 | + return Metrics.toLog(project.getLogger(), LogLevel.LIFECYCLE); |
| 38 | + } |
| 39 | + project.getLogger().lifecycle("Metrics collection is enabled."); |
| 40 | + return new StackdriverMetrics(project.getGradle(), project.getLogger()); |
| 41 | + } |
| 42 | + |
37 | 43 | private static boolean isCollectionEnabled() {
|
38 | 44 | String enabled = System.getenv("FIREBASE_ENABLE_METRICS");
|
39 | 45 | return enabled != null && enabled.equals("1");
|
40 | 46 | }
|
| 47 | + |
| 48 | + /** |
| 49 | + * Determines whether a given gradle {@link Task} is "public", e.g. visible with `gradle tasks`. |
| 50 | + */ |
| 51 | + private static boolean isTaskPublic(Task task) { |
| 52 | + return task.getGroup() != null; |
| 53 | + } |
41 | 54 | }
|
0 commit comments