Skip to content

Reduce the number of metrics exported to stackdriver. #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package com.google.firebase.gradle.plugins.ci.metrics;

import java.util.function.Predicate;
import org.gradle.api.Task;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.logging.Logger;

/** Provides methods for measuring various parts of the build. */
interface Metrics {
Expand All @@ -23,4 +26,41 @@ interface Metrics {

/** Measure task execution failure. */
void measureFailure(Task task);

/**
* Creates a {@link Metrics} implementation that uses a {@code predicate} to determine whether to
* emit measurements for it.
*/
static Metrics filtered(Metrics metrics, Predicate<Task> predicate) {
return new Metrics() {
@Override
public void measureSuccess(Task task, long elapsedTime) {
if (predicate.test(task)) {
metrics.measureSuccess(task, elapsedTime);
}
}

@Override
public void measureFailure(Task task) {
if (predicate.test(task)) {
metrics.measureFailure(task);
}
}
};
}

/** Creates a {@link Metrics} implementation that logs results at the specified level. */
static Metrics toLog(Logger toLogger, LogLevel level) {
return new Metrics() {
@Override
public void measureSuccess(Task task, long elapsedTime) {
toLogger.log(level, "[METRICS] Task {} took {}ms.", task.getPath(), elapsedTime);
}

@Override
public void measureFailure(Task task) {
toLogger.log(level, "[METRICS] Task {} failed.", task.getPath());
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,39 @@

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.execution.TaskExecutionGraph;
import org.gradle.api.logging.LogLevel;

/** Instruments Gradle to measure latency and success rate of all executed tasks. */
public class MetricsPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
if (!isCollectionEnabled()) {
project.getLogger().lifecycle("Metrics collection is disabled.");
return;
}
project.getLogger().lifecycle("Metrics collection is enabled.");

Metrics metrics = new StackdriverMetrics(project.getGradle(), project.getLogger());
Metrics metrics = Metrics.filtered(initializeMetrics(project), MetricsPlugin::isTaskPublic);

TaskExecutionGraph taskGraph = project.getGradle().getTaskGraph();
taskGraph.addTaskExecutionListener(new MeasuringTaskExecutionListener(metrics, taskGraph));
}

private static Metrics initializeMetrics(Project project) {
if (!isCollectionEnabled()) {
project.getLogger().lifecycle("Metrics collection is disabled. Logging to stdout...");
return Metrics.toLog(project.getLogger(), LogLevel.LIFECYCLE);
}
project.getLogger().lifecycle("Metrics collection is enabled.");
return new StackdriverMetrics(project.getGradle(), project.getLogger());
}

private static boolean isCollectionEnabled() {
String enabled = System.getenv("FIREBASE_ENABLE_METRICS");
return enabled != null && enabled.equals("1");
}

/**
* Determines whether a given gradle {@link Task} is "public", e.g. visible with `gradle tasks`.
*/
private static boolean isTaskPublic(Task task) {
return task.getGroup() != null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you link to some document that shows that this hack is the right way?

}
}