Skip to content

Instrument Gradle build with metrics. #214

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 4 commits into from
Jan 31, 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
4 changes: 4 additions & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ dependencies {
implementation "com.jaredsburrows:gradle-license-plugin:0.8.1"
implementation 'digital.wup:android-maven-publish:3.6.2'

implementation 'io.opencensus:opencensus-api:0.18.0'
implementation 'io.opencensus:opencensus-exporter-stats-stackdriver:0.18.0'
runtime 'io.opencensus:opencensus-impl:0.18.0'

implementation 'com.android.tools.build:gradle:3.2.1'
testImplementation 'junit:junit:4.12'
testImplementation 'org.json:json:20180813'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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

import org.gradle.BuildAdapter;
import org.gradle.BuildResult;
import org.gradle.api.logging.Logger;

/**
* Build listener that waits for Stackdriver to export metrics before exiting.
*
* <p>Stackdriver exporter is implemented in such a way that it exports metrics on a periodic basis,
* with period being configurable. This means that, when the build finishes and exits, it is highly
* likely that there are unexported metrics in memory. For this reason we have this build listener
* that makes the gradle process sleep for the duration of the configured export period to make sure
* metrics get exported.
*
* @see <a
* href="https://opencensus.io/exporters/supported-exporters/java/stackdriver-stats/">Opencensus
* docs</a>
*/
class DrainingBuildListener extends BuildAdapter {
private final long sleepDuration;
private final Logger logger;

DrainingBuildListener(long sleepDuration, Logger logger) {
this.sleepDuration = sleepDuration;
this.logger = logger;
}

@Override
public void buildFinished(BuildResult result) {
try {
logger.lifecycle("Draining metrics to Stackdriver.");
Thread.sleep(sleepDuration);
} catch (InterruptedException e) {
// Restore the interrupted status
Thread.currentThread().interrupt();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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

import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
import org.gradle.api.Task;
import org.gradle.api.execution.TaskExecutionGraph;
import org.gradle.api.execution.TaskExecutionListener;
import org.gradle.api.tasks.TaskState;

class MeasuringTaskExecutionListener implements TaskExecutionListener {
private static final String METRICS_START_TIME = "metricsStartTime";
private static final String METRICS_ELAPSED_TIME = "metricsElapsedTime";

private final Metrics metrics;
private final TaskExecutionGraph taskGraph;

MeasuringTaskExecutionListener(Metrics metrics, TaskExecutionGraph taskGraph) {
this.metrics = metrics;
this.taskGraph = taskGraph;
}

@Override
public void beforeExecute(Task task) {
recordStart(task);
}

@Override
public void afterExecute(Task task, TaskState taskState) {
recordElapsed(task);
long elapsedTime = getTotalElapsed(task);

if (taskState.getFailure() != null) {
metrics.measureFailure(task);
return;
}
metrics.measureSuccess(task, elapsedTime);
}

private static void recordStart(Task task) {
task.getExtensions().add(METRICS_START_TIME, System.currentTimeMillis());
}

private static void recordElapsed(Task task) {
long startTime = (long) task.getExtensions().getByName(METRICS_START_TIME);
task.getExtensions().add(METRICS_ELAPSED_TIME, System.currentTimeMillis() - startTime);
}

private static long getElapsed(Task task) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: We could either have an equivalent getStart() or write this inline as well

Copy link
Member Author

Choose a reason for hiding this comment

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

fwiw I did not add it because getStart() is needed only in recordElapsed() which is a pretty simple function, otoh getElapsed() is used in a graph traversal so I wanted to keep things simple and readable there.

return (long) task.getExtensions().getByName(METRICS_ELAPSED_TIME);
}

// a tasks elapsed time does not include how long it took for its dependencies took to execute,
// so we walk the dependency graph to get the total elapsed time.
private long getTotalElapsed(Task task) {
Queue<Task> queue = new ArrayDeque<>();
queue.add(task);
Set<Task> visited = new HashSet<>();

long totalElapsed = 0;
while (!queue.isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not often you see graph traversal in a code review 🤣

Copy link
Member Author

Choose a reason for hiding this comment

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

I know, right? :)

Task currentTask = queue.remove();
if (!visited.add(currentTask)) {
continue;
}

totalElapsed += getElapsed(currentTask);

for (Task dep : taskGraph.getDependencies(currentTask)) {
if (!visited.contains(dep)) {
queue.add(dep);
}
}
}
return totalElapsed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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

import org.gradle.api.Task;

/** Provides methods for measuring various parts of the build. */
interface Metrics {
/** Measure successful execution of a task. */
void measureSuccess(Task task, long elapsedTime);

/** Measure task execution failure. */
void measureFailure(Task task);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.execution.TaskExecutionGraph;

/** 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());

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

private static boolean isCollectionEnabled() {
String enabled = System.getenv("FIREBASE_ENABLE_METRICS");
return enabled != null && enabled.equals("1");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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

import io.opencensus.common.Duration;
import io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration;
import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter;
import io.opencensus.stats.Aggregation;
import io.opencensus.stats.Measure;
import io.opencensus.stats.Stats;
import io.opencensus.stats.View;
import io.opencensus.tags.TagContext;
import io.opencensus.tags.TagKey;
import io.opencensus.tags.TagValue;
import io.opencensus.tags.Tags;
import io.opencensus.tags.propagation.TagContextBinarySerializer;
import io.opencensus.tags.propagation.TagContextDeserializationException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.gradle.api.GradleException;
import org.gradle.api.Task;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.logging.Logger;

/**
* Object used to record measurements via {@link #measureSuccess(Task, long)} and {@link
* #measureFailure(Task)}.
*/
class StackdriverMetrics implements Metrics {
private static final AtomicBoolean STACKDRIVER_INITIALIZED = new AtomicBoolean();
private static final long STACKDRIVER_UPLOAD_PERIOD_MS = 5000;

private final TagContext globalContext;
private final Logger logger;

private static Measure.MeasureDouble M_LATENCY =
Measure.MeasureDouble.create("latency", "", "ms");
private static Measure.MeasureLong M_SUCCESS = Measure.MeasureLong.create("success", "", "1");

private static final TagKey STAGE = TagKey.create("stage");
private static final TagKey GRADLE_PROJECT = TagKey.create("gradle_project");

private static final List<TagKey> TAG_KEYS =
Arrays.asList(
STAGE,
GRADLE_PROJECT,
TagKey.create("repo_owner"),
TagKey.create("repo_name"),
TagKey.create("pull_number"),
TagKey.create("job_name"),
TagKey.create("build_id"),
TagKey.create("job_type"));

StackdriverMetrics(Gradle gradle, Logger logger) {
this.logger = logger;
globalContext = deserializeContext();

ensureStackdriver(gradle);

Stats.getViewManager()
.registerView(
View.create(
View.Name.create("fireci/tasklatency"),
"The latency in milliseconds",
M_LATENCY,
Aggregation.LastValue.create(),
TAG_KEYS));

Stats.getViewManager()
.registerView(
View.create(
View.Name.create("fireci/tasksuccess"),
"Indicated success or failure.",
M_SUCCESS,
Aggregation.LastValue.create(),
TAG_KEYS));
}

/** Records failure of the execution stage named {@code name}. */
public void measureFailure(Task task) {

TagContext ctx =
Tags.getTagger()
.toBuilder(globalContext)
.put(STAGE, TagValue.create(task.getName()))
.put(GRADLE_PROJECT, TagValue.create(task.getProject().getPath()))
.build();
Stats.getStatsRecorder().newMeasureMap().put(M_SUCCESS, 0).record(ctx);
}

/** Records success and latency of the execution stage named {@code name}. */
public void measureSuccess(Task task, long elapsedTime) {

TagContext ctx =
Tags.getTagger()
.toBuilder(globalContext)
.put(STAGE, TagValue.create(task.getName()))
.put(GRADLE_PROJECT, TagValue.create(task.getProject().getPath()))
.build();
Stats.getStatsRecorder()
.newMeasureMap()
.put(M_SUCCESS, 1)
.put(M_LATENCY, elapsedTime)
.record(ctx);
}

private void ensureStackdriver(Gradle gradle) {
// make sure we only initialize stackdriver once as gradle daemon is not guaranteed to restart
// across gradle invocations.
if (!STACKDRIVER_INITIALIZED.compareAndSet(false, true)) {
logger.lifecycle("Stackdriver exporter already initialized.");
return;
}
logger.lifecycle("Initializing Stackdriver exporter.");

try {
StackdriverStatsExporter.createAndRegister(
StackdriverStatsConfiguration.builder()
.setExportInterval(Duration.fromMillis(STACKDRIVER_UPLOAD_PERIOD_MS))
.build());

// make sure gradle does not exit before metrics get uploaded to stackdriver.
gradle.addBuildListener(new DrainingBuildListener(STACKDRIVER_UPLOAD_PERIOD_MS, logger));
} catch (IOException e) {
throw new GradleException("Could not configure metrics exporter", e);
}
}

/** Extract opencensus context(if any) from environment. */
private static TagContext deserializeContext() {
String serializedContext = System.getenv("OPENCENSUS_STATS_CONTEXT");
if (serializedContext == null) {
return Tags.getTagger().empty();
}

TagContextBinarySerializer serializer = Tags.getTagPropagationComponent().getBinarySerializer();

try {
return serializer.fromByteArray(Base64.getDecoder().decode(serializedContext));
} catch (TagContextDeserializationException e) {
return Tags.getTagger().empty();
}
}
}
1 change: 1 addition & 0 deletions ci/fireci/fireci/gradle.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def P(name, value):
return '-P{}={}'.format(name, value)


@stats.measure_call('gradle')
def run(*args, gradle_opts='', workdir=None):
"""Invokes gradle with specified args and gradle_opts."""
new_env = dict(os.environ)
Expand Down
Loading