-
Notifications
You must be signed in to change notification settings - Fork 620
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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; | ||
|
||
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.warn("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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fwiw I did not add it because |
||
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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not often you see graph traversal in a code review 🤣 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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().warn("Metrics collection is disabled."); | ||
return; | ||
} | ||
project.getLogger().warn("Metrics collection is enabled."); | ||
vkryachko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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,158 @@ | ||
// 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")); | ||
|
||
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/success"), | ||
"Indicated success or failure.", | ||
M_SUCCESS, | ||
Aggregation.LastValue.create(), | ||
TAG_KEYS)); | ||
} | ||
|
||
/** Records failure of the execution stage named {@code name}. */ | ||
allisonbm92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.warn("Stackdriver exporter already initialized."); | ||
return; | ||
} | ||
logger.warn("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(); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.