Skip to content
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

Add ResolutionEventListener#downloadMetadataSuccess to communicate long download times to consumers of MavenPomDownloader #4566

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import org.openrewrite.maven.internal.MavenParsingException;
import org.openrewrite.maven.tree.*;

import java.net.URI;
import java.time.Duration;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -48,6 +50,9 @@ public class MavenExecutionContextView extends DelegatingExecutionContext {
private static final String MAVEN_RESOLUTION_LISTENER = "org.openrewrite.maven.resolutionListener";
private static final String MAVEN_RESOLUTION_TIME = "org.openrewrite.maven.resolutionTime";

@Nullable
private BiConsumer<URI, Duration> onDownloaded;

public MavenExecutionContextView(ExecutionContext delegate) {
super(delegate);
}
Expand Down Expand Up @@ -238,6 +243,15 @@ public MavenExecutionContextView setMavenSettings(@Nullable MavenSettings settin
return getMessage(MAVEN_SETTINGS, null);
}

@Nullable
public BiConsumer<URI, Duration> getOnDownloaded() {
return onDownloaded;
bryceatmoderne marked this conversation as resolved.
Show resolved Hide resolved
}

public void setOnDownloaded(BiConsumer<URI, Duration> onDownloaded) {
this.onDownloaded = onDownloaded;
}

private static List<String> mapActiveProfiles(MavenSettings settings, String... activeProfiles) {
if (settings.getActiveProfiles() == null) {
return Arrays.asList(activeProfiles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;

@SuppressWarnings("OptionalAssignedToNull")
public class MavenPomDownloader {
private static final RetryPolicy<Object> retryPolicy = RetryPolicy.builder()
.handle(SocketTimeoutException.class, TimeoutException.class)
Expand Down Expand Up @@ -333,7 +332,12 @@ public MavenMetadata downloadMetadata(GroupArtifactVersion gav, @Nullable Resolv
.setRepositoryResponses(repositoryResponses);
}

sample.stop(timer.tags("outcome", "success").register(Metrics.globalRegistry));
long nanos = sample.stop(timer.tags("outcome", "success").register(Metrics.globalRegistry));
if (ctx.getOnDownloaded() != null) {
ctx.getOnDownloaded().accept(
URI.create(attemptedUris.get(attemptedUris.size() - 1)),
Duration.ofNanos(nanos));
}
return mavenMetadata;
}

Expand Down Expand Up @@ -604,6 +608,10 @@ public Pom download(GroupArtifactVersion gav,
mavenCache.putPom(resolvedGav, pom);
ctx.getResolutionListener().downloadSuccess(resolvedGav, containingPom);
sample.stop(timer.tags("outcome", "downloaded").register(Metrics.globalRegistry));
long nanos = sample.stop(timer.tags("outcome", "downloaded").register(Metrics.globalRegistry));
if (ctx.getOnDownloaded() != null) {
ctx.getOnDownloaded().accept(uri, Duration.ofNanos(nanos));
}
return pom;
} catch (HttpSenderResponseException e) {
repositoryResponses.put(repo, e.getMessage());
Expand Down
Loading