Skip to content
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
12 changes: 7 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-bom</artifactId>
<version>${micrometer.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
Expand Down Expand Up @@ -137,11 +144,6 @@
<artifactId>classgraph</artifactId>
<version>${classgraph.version}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<version>${micrometer.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/land/oras/auth/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package land.oras.auth;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -68,11 +67,6 @@ public final class HttpClient {
*/
private static final Logger LOG = LoggerFactory.getLogger(HttpClient.class);

/**
* Metric name for token refresh counter
*/
public static final String TOKEN_REFRESH_METRIC = "oras.auth.token.refresh";

/**
* The pattern for the WWW-Authenticate header value
*/
Expand Down Expand Up @@ -103,12 +97,6 @@ public final class HttpClient {
* The meter registry for metrics
*/
private MeterRegistry meterRegistry;

/**
* Counter for token refreshes
*/
private Counter tokenRefreshCounter;

/**
* Hidden constructor
*/
Expand Down Expand Up @@ -157,9 +145,6 @@ private void setTlsVerify(boolean skipTlsVerify) {
*/
public HttpClient build() {
this.client = this.builder.build();
this.tokenRefreshCounter = Counter.builder(TOKEN_REFRESH_METRIC)
.description("Number of token refreshes performed against the registry")
.register(meterRegistry);
return this;
}

Expand Down Expand Up @@ -463,7 +448,9 @@ public <T> TokenResponse refreshToken(
TokenResponse token = JsonUtils.fromJson(responseWrapper.response(), TokenResponse.class)
.forService(service);
TokenCache.put(newScopes, token);
tokenRefreshCounter.increment();
meterRegistry
.counter(Const.METRIC_TOKEN_REFRESH, Const.METRIC_TAG_SERVICE, service, Const.METRIC_TAG_REALM, realm)
.increment();
return token;
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/land/oras/utils/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,19 @@ public static String currentTimestamp() {
* OCI Chunk Minimum Length header
*/
public static final String OCI_CHUNK_MIN_LENGTH_HEADER = "OCI-Chunk-Min-Length";

/**
* Metric name for token refresh counter
*/
public static final String METRIC_TOKEN_REFRESH = "land_oras_auth_token_refresh_total";

/**
* Metric name for token refresh duration
*/
public static final String METRIC_TAG_SERVICE = "service";

/**
* Metric name for token refresh duration
*/
public static final String METRIC_TAG_REALM = "realm";
}
18 changes: 16 additions & 2 deletions src/test/java/land/oras/DockerIoITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@

import static org.junit.jupiter.api.Assertions.*;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import java.nio.file.Path;
import land.oras.utils.Const;
import land.oras.utils.ZotUnsecureContainer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -106,8 +110,12 @@ void shouldPullOneBlob() {
void shouldCopyTagToInternalRegistry() {

// Source registry
Registry sourceRegistry =
Registry.Builder.builder().withParallelism(3).defaults().build();
MeterRegistry meterRegistry = new SimpleMeterRegistry();
Registry sourceRegistry = Registry.Builder.builder()
.withMeterRegistry(meterRegistry)
.withParallelism(3)
.defaults()
.build();

// Copy to this internal registry
Registry targetRegistry = Registry.Builder.builder()
Expand All @@ -122,6 +130,12 @@ void shouldCopyTagToInternalRegistry() {

CopyUtils.copy(sourceRegistry, containerSource, targetRegistry, containerTarget, CopyUtils.CopyOptions.deep());
assertTrue(targetRegistry.exists(containerTarget));

assertEquals(
1.0,
meterRegistry.find(Const.METRIC_TOKEN_REFRESH).counters().stream()
.mapToDouble(Counter::count)
.sum());
}

@Test
Expand Down
20 changes: 17 additions & 3 deletions src/test/java/land/oras/RegistryWireMockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import com.github.tomakehurst.wiremock.stubbing.Scenario;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand Down Expand Up @@ -584,8 +586,7 @@ void shouldRefreshExpiredToken(WireMockRuntimeInfo wmRuntimeInfo) {
WireMock.ok().withBody("blob-data").withHeader(Const.DOCKER_CONTENT_DIGEST_HEADER, digest)));

// Insecure registry with a custom meter registry to track metrics
io.micrometer.core.instrument.simple.SimpleMeterRegistry meterRegistry =
new io.micrometer.core.instrument.simple.SimpleMeterRegistry();
SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry();
Registry registry = Registry.Builder.builder()
.withAuthProvider(new BearerTokenProvider()) // Already bearer token
.withInsecure(true)
Expand All @@ -600,8 +601,21 @@ void shouldRefreshExpiredToken(WireMockRuntimeInfo wmRuntimeInfo) {
// Verify that exactly one token refresh was performed
assertEquals(
1.0,
meterRegistry.counter(HttpClient.TOKEN_REFRESH_METRIC).count(),
meterRegistry
.counter(
Const.METRIC_TOKEN_REFRESH,
Const.METRIC_TAG_SERVICE,
"localhost",
Const.METRIC_TAG_REALM,
"http://localhost:%d/token".formatted(wmRuntimeInfo.getHttpPort()))
.count(),
"Token refresh counter should be 1 after one token refresh");
assertEquals(
1.0,
meterRegistry.find(Const.METRIC_TOKEN_REFRESH).counters().stream()
.mapToDouble(Counter::count)
.sum());
TestUtils.dumpMetrics(meterRegistry);
}

@Test
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/land/oras/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,38 @@

package land.oras;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;

/**
* Several tests utils
*/
public final class TestUtils {

/**
* Logger
*/
private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);

/**
* Dump current metrics to console for debug purpose
* @param meterRegistry the meter registry to dump
*/
public static void dumpMetrics(MeterRegistry meterRegistry) {
meterRegistry.getMeters().forEach(meter -> {
Meter.Id id = meter.getId();
LOG.info("{} {}", id.getName(), id.getTags());

meter.measure().forEach(ms -> LOG.info(" {}={}", ms.getStatistic(), ms.getValue()));
});
}

/**
* Create a registries.conf file in the given home directory with the given content.
* @param homeDir the home directory where the .config/containers/registries.conf file will be created
Expand Down