Move Spring Data repository metrics into spring-boot-data-commons

This commit is contained in:
Andy Wilkinson
2025-05-30 14:32:51 +01:00
committed by Phillip Webb
parent 3b58215093
commit b9aa01c1c4
24 changed files with 56 additions and 55 deletions

View File

@@ -0,0 +1,114 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.Timer.Builder;
import org.springframework.util.CollectionUtils;
/**
* Strategy that can be used to apply {@link Timer Timers} automatically instead of using
* {@link Timed @Timed}.
*
* @author Tadaya Tsuyukubo
* @author Stephane Nicoll
* @author Phillip Webb
* @since 4.0.0
*/
@FunctionalInterface
public interface AutoTimer {
/**
* An {@link AutoTimer} implementation that is enabled but applies no additional
* customizations.
*/
AutoTimer ENABLED = (builder) -> {
};
/**
* An {@link AutoTimer} implementation that is disabled and will not record metrics.
*/
AutoTimer DISABLED = new AutoTimer() {
@Override
public boolean isEnabled() {
return false;
}
@Override
public void apply(Builder builder) {
throw new IllegalStateException("AutoTimer is disabled");
}
};
/**
* Return if the auto-timer is enabled and metrics should be recorded.
* @return if the auto-timer is enabled
*/
default boolean isEnabled() {
return true;
}
/**
* Factory method to create a new {@link Builder Timer.Builder} with auto-timer
* settings {@link #apply(Timer.Builder) applied}.
* @param name the name of the timer
* @return a new builder instance with auto-settings applied
*/
default Timer.Builder builder(String name) {
return builder(() -> Timer.builder(name));
}
/**
* Factory method to create a new {@link Builder Timer.Builder} with auto-timer
* settings {@link #apply(Timer.Builder) applied}.
* @param supplier the builder supplier
* @return a new builder instance with auto-settings applied
*/
default Timer.Builder builder(Supplier<Timer.Builder> supplier) {
Timer.Builder builder = supplier.get();
apply(builder);
return builder;
}
/**
* Called to apply any auto-timer settings to the given {@link Builder Timer.Builder}.
* @param builder the builder to apply settings to
*/
void apply(Timer.Builder builder);
static void apply(AutoTimer autoTimer, String metricName, Set<Timed> annotations, Consumer<Timer.Builder> action) {
if (!CollectionUtils.isEmpty(annotations)) {
for (Timed annotation : annotations) {
action.accept(Timer.builder(annotation, metricName));
}
}
else {
if (autoTimer != null && autoTimer.isEnabled()) {
action.accept(autoTimer.builder(metricName));
}
}
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics;
import java.lang.reflect.Method;
import java.util.function.Function;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener.RepositoryMethodInvocation;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener.RepositoryMethodInvocationResult.State;
import org.springframework.util.StringUtils;
/**
* Default {@link RepositoryTagsProvider} implementation.
*
* @author Phillip Webb
* @since 4.0.0
*/
public class DefaultRepositoryTagsProvider implements RepositoryTagsProvider {
private static final Tag EXCEPTION_NONE = Tag.of("exception", "None");
@Override
public Iterable<Tag> repositoryTags(RepositoryMethodInvocation invocation) {
Tags tags = Tags.empty();
tags = and(tags, invocation.getRepositoryInterface(), "repository", this::getSimpleClassName);
tags = and(tags, invocation.getMethod(), "method", Method::getName);
tags = and(tags, invocation.getResult().getState(), "state", State::name);
tags = and(tags, invocation.getResult().getError(), "exception", this::getExceptionName, EXCEPTION_NONE);
return tags;
}
private <T> Tags and(Tags tags, T instance, String key, Function<T, String> value) {
return and(tags, instance, key, value, null);
}
private <T> Tags and(Tags tags, T instance, String key, Function<T, String> value, Tag fallback) {
if (instance != null) {
return tags.and(key, value.apply(instance));
}
return (fallback != null) ? tags.and(fallback) : tags;
}
private String getExceptionName(Throwable error) {
return getSimpleClassName(error.getClass());
}
private String getSimpleClassName(Class<?> type) {
String simpleName = type.getSimpleName();
return (!StringUtils.hasText(simpleName)) ? type.getName() : simpleName;
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener;
import org.springframework.util.function.SingletonSupplier;
/**
* Intercepts Spring Data {@code Repository} invocations and records metrics about
* execution time and results.
*
* @author Phillip Webb
* @since 4.0.0
*/
public class MetricsRepositoryMethodInvocationListener implements RepositoryMethodInvocationListener {
private final SingletonSupplier<MeterRegistry> registrySupplier;
private final RepositoryTagsProvider tagsProvider;
private final String metricName;
private final AutoTimer autoTimer;
/**
* Create a new {@code MetricsRepositoryMethodInvocationListener}.
* @param registrySupplier a supplier for the registry to which metrics are recorded
* @param tagsProvider provider for metrics tags
* @param metricName name of the metric to record
* @param autoTimer the auto-timers to apply or {@code null} to disable auto-timing
* @since 2.5.4
*/
public MetricsRepositoryMethodInvocationListener(Supplier<MeterRegistry> registrySupplier,
RepositoryTagsProvider tagsProvider, String metricName, AutoTimer autoTimer) {
this.registrySupplier = (registrySupplier instanceof SingletonSupplier)
? (SingletonSupplier<MeterRegistry>) registrySupplier : SingletonSupplier.of(registrySupplier);
this.tagsProvider = tagsProvider;
this.metricName = metricName;
this.autoTimer = (autoTimer != null) ? autoTimer : AutoTimer.DISABLED;
}
@Override
public void afterInvocation(RepositoryMethodInvocation invocation) {
Set<Timed> annotations = TimedAnnotations.get(invocation.getMethod(), invocation.getRepositoryInterface());
Iterable<Tag> tags = this.tagsProvider.repositoryTags(invocation);
long duration = invocation.getDuration(TimeUnit.NANOSECONDS);
AutoTimer.apply(this.autoTimer, this.metricName, annotations,
(builder) -> builder.description("Duration of repository invocations")
.tags(tags)
.register(this.registrySupplier.get())
.record(duration, TimeUnit.NANOSECONDS));
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics;
import io.micrometer.core.instrument.Tag;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener.RepositoryMethodInvocation;
/**
* Provides {@link Tag Tags} for Spring Data {@link RepositoryMethodInvocation Repository
* invocations}.
*
* @author Phillip Webb
* @since 4.0.0
*/
@FunctionalInterface
public interface RepositoryTagsProvider {
/**
* Provides tags to be associated with metrics for the given {@code invocation}.
* @param invocation the repository invocation
* @return tags to associate with metrics for the invocation
*/
Iterable<Tag> repositoryTags(RepositoryMethodInvocation invocation);
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import io.micrometer.core.annotation.Timed;
import org.springframework.core.annotation.MergedAnnotationCollectors;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.util.ConcurrentReferenceHashMap;
/**
* Utility used to obtain {@link Timed @Timed} annotations from bean methods.
*
* @author Phillip Webb
* @since 4.0.0
*/
public final class TimedAnnotations {
private static final Map<AnnotatedElement, Set<Timed>> cache = new ConcurrentReferenceHashMap<>();
private TimedAnnotations() {
}
/**
* Return {@link Timed} annotations that should be used for the given {@code method}
* and {@code type}.
* @param method the source method
* @param type the source type
* @return the {@link Timed} annotations to use or an empty set
*/
public static Set<Timed> get(Method method, Class<?> type) {
Set<Timed> methodAnnotations = findTimedAnnotations(method);
if (!methodAnnotations.isEmpty()) {
return methodAnnotations;
}
return findTimedAnnotations(type);
}
private static Set<Timed> findTimedAnnotations(AnnotatedElement element) {
if (element == null) {
return Collections.emptySet();
}
Set<Timed> result = cache.get(element);
if (result != null) {
return result;
}
MergedAnnotations annotations = MergedAnnotations.from(element);
result = (!annotations.isPresent(Timed.class)) ? Collections.emptySet()
: annotations.stream(Timed.class).collect(MergedAnnotationCollectors.toAnnotationSet());
cache.put(element, result);
return result;
}
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* {@link ConfigurationProperties @ConfigurationProperties} for configuring
* Micrometer-based Spring Data metrics.
*
* @author Andy Wilkinson
* @since 4.0.0
*/
@ConfigurationProperties("management.metrics.data")
public class DataMetricsProperties {
private final Repository repository = new Repository();
public Repository getRepository() {
return this.repository;
}
public static class Repository {
/**
* Name of the metric for sent requests.
*/
private String metricName = "spring.data.repository.invocations";
/**
* Auto-timed request settings.
*/
private final Autotime autotime = new Autotime();
public String getMetricName() {
return this.metricName;
}
public void setMetricName(String metricName) {
this.metricName = metricName;
}
public Autotime getAutotime() {
return this.autotime;
}
public static class Autotime {
/**
* Whether to enable auto-timing.
*/
private boolean enabled = true;
/**
* Whether to publish percentile histograms.
*/
private boolean percentilesHistogram;
/**
* Percentiles for which additional time series should be published.
*/
private double[] percentiles;
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isPercentilesHistogram() {
return this.percentilesHistogram;
}
public void setPercentilesHistogram(boolean percentilesHistogram) {
this.percentilesHistogram = percentilesHistogram;
}
public double[] getPercentiles() {
return this.percentiles;
}
public void setPercentiles(double[] percentiles) {
this.percentiles = percentiles;
}
}
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics.autoconfigure;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.data.metrics.MetricsRepositoryMethodInvocationListener;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.core.support.RepositoryFactoryCustomizer;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.util.function.SingletonSupplier;
/**
* {@link BeanPostProcessor} to apply a {@link MetricsRepositoryMethodInvocationListener}
* to all {@link RepositoryFactorySupport repository factories}.
*
* @author Phillip Webb
*/
class MetricsRepositoryMethodInvocationListenerBeanPostProcessor implements BeanPostProcessor {
private final RepositoryFactoryCustomizer customizer;
MetricsRepositoryMethodInvocationListenerBeanPostProcessor(
SingletonSupplier<MetricsRepositoryMethodInvocationListener> listener) {
this.customizer = new MetricsRepositoryFactoryCustomizer(listener);
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof RepositoryFactoryBeanSupport) {
((RepositoryFactoryBeanSupport<?, ?, ?>) bean).addRepositoryFactoryCustomizer(this.customizer);
}
return bean;
}
private static final class MetricsRepositoryFactoryCustomizer implements RepositoryFactoryCustomizer {
private final SingletonSupplier<MetricsRepositoryMethodInvocationListener> listenerSupplier;
private MetricsRepositoryFactoryCustomizer(
SingletonSupplier<MetricsRepositoryMethodInvocationListener> listenerSupplier) {
this.listenerSupplier = listenerSupplier;
}
@Override
public void customize(RepositoryFactorySupport repositoryFactory) {
repositoryFactory.addInvocationListener(this.listenerSupplier.get());
}
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics.autoconfigure;
import io.micrometer.core.instrument.Timer.Builder;
import org.springframework.boot.data.metrics.AutoTimer;
import org.springframework.boot.data.metrics.autoconfigure.DataMetricsProperties.Repository.Autotime;
/**
* {@link AutoTimer} whose behavior is configured by {@link Autotime} properties.
*
* @author Andy Wilkinson
* @since 4.0.0
*/
public class PropertiesAutoTimer implements AutoTimer {
private final Autotime properties;
/**
* Create a new {@link PropertiesAutoTimer} configured using the given
* {@code properties}.
* @param properties the properties to configure auto-timing
*/
public PropertiesAutoTimer(Autotime properties) {
this.properties = properties;
}
@Override
public void apply(Builder builder) {
builder.publishPercentileHistogram(this.properties.isPercentilesHistogram())
.publishPercentiles(this.properties.getPercentiles());
}
@Override
public boolean isEnabled() {
return this.properties.isEnabled();
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics.autoconfigure;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.data.metrics.DefaultRepositoryTagsProvider;
import org.springframework.boot.data.metrics.MetricsRepositoryMethodInvocationListener;
import org.springframework.boot.data.metrics.RepositoryTagsProvider;
import org.springframework.boot.data.metrics.autoconfigure.DataMetricsProperties.Repository;
import org.springframework.context.annotation.Bean;
import org.springframework.util.function.SingletonSupplier;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data Repository metrics.
*
* @author Phillip Webb
* @since 4.0.0
*/
@AutoConfiguration(
afterName = { "org.springframework.boot.metrics.autoconfigure.CompositeMeterRegistryAutoConfiguration",
"org.springframework.boot.metrics.autoconfigure.MetricsAutoConfiguration",
"org.springframework.boot.metrics.autoconfigure.export.simple.SimpleMetricsExportAutoConfiguration" })
@ConditionalOnClass(org.springframework.data.repository.Repository.class)
@ConditionalOnBean(MeterRegistry.class)
@EnableConfigurationProperties(DataMetricsProperties.class)
public class RepositoryMetricsAutoConfiguration {
private final DataMetricsProperties properties;
public RepositoryMetricsAutoConfiguration(DataMetricsProperties properties) {
this.properties = properties;
}
@Bean
@ConditionalOnMissingBean(RepositoryTagsProvider.class)
public DefaultRepositoryTagsProvider repositoryTagsProvider() {
return new DefaultRepositoryTagsProvider();
}
@Bean
@ConditionalOnMissingBean
public MetricsRepositoryMethodInvocationListener metricsRepositoryMethodInvocationListener(
ObjectProvider<MeterRegistry> registry, RepositoryTagsProvider tagsProvider) {
Repository properties = this.properties.getRepository();
return new MetricsRepositoryMethodInvocationListener(registry::getObject, tagsProvider,
properties.getMetricName(), new PropertiesAutoTimer(properties.getAutotime()));
}
@Bean
public static MetricsRepositoryMethodInvocationListenerBeanPostProcessor metricsRepositoryMethodInvocationListenerBeanPostProcessor(
ObjectProvider<MetricsRepositoryMethodInvocationListener> metricsRepositoryMethodInvocationListener) {
return new MetricsRepositoryMethodInvocationListenerBeanPostProcessor(
SingletonSupplier.of(metricsRepositoryMethodInvocationListener::getObject));
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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.
*/
/**
* Auto-configuration for Spring Data repository metrics.
*/
package org.springframework.boot.data.metrics.autoconfigure;

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* 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
*
* https://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.
*/
/**
* Spring Data repository metrics.
*/
package org.springframework.boot.data.metrics;

View File

@@ -1 +1,2 @@
org.springframework.boot.data.metrics.autoconfigure.RepositoryMetricsAutoConfiguration
org.springframework.boot.data.web.autoconfigure.SpringDataWebAutoConfiguration

View File

@@ -0,0 +1,102 @@
/*
* Copyright 2012-2022 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics;
import java.io.IOException;
import java.lang.reflect.Method;
import io.micrometer.core.instrument.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener.RepositoryMethodInvocation;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener.RepositoryMethodInvocationResult;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener.RepositoryMethodInvocationResult.State;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link DefaultRepositoryTagsProvider}.
*
* @author Phillip Webb
*/
class DefaultRepositoryTagsProviderTests {
private final DefaultRepositoryTagsProvider provider = new DefaultRepositoryTagsProvider();
@Test
void repositoryTagsIncludesRepository() {
RepositoryMethodInvocation invocation = createInvocation();
Iterable<Tag> tags = this.provider.repositoryTags(invocation);
assertThat(tags).contains(Tag.of("repository", "ExampleRepository"));
}
@Test
void repositoryTagsIncludesMethod() {
RepositoryMethodInvocation invocation = createInvocation();
Iterable<Tag> tags = this.provider.repositoryTags(invocation);
assertThat(tags).contains(Tag.of("method", "findById"));
}
@Test
void repositoryTagsIncludesState() {
RepositoryMethodInvocation invocation = createInvocation();
Iterable<Tag> tags = this.provider.repositoryTags(invocation);
assertThat(tags).contains(Tag.of("state", "SUCCESS"));
}
@Test
void repositoryTagsIncludesException() {
RepositoryMethodInvocation invocation = createInvocation(new IOException());
Iterable<Tag> tags = this.provider.repositoryTags(invocation);
assertThat(tags).contains(Tag.of("exception", "IOException"));
}
@Test
void repositoryTagsWhenNoExceptionIncludesExceptionTagWithNone() {
RepositoryMethodInvocation invocation = createInvocation();
Iterable<Tag> tags = this.provider.repositoryTags(invocation);
assertThat(tags).contains(Tag.of("exception", "None"));
}
private RepositoryMethodInvocation createInvocation() {
return createInvocation(null);
}
private RepositoryMethodInvocation createInvocation(Throwable error) {
Class<?> repositoryInterface = ExampleRepository.class;
Method method = ReflectionUtils.findMethod(repositoryInterface, "findById", long.class);
RepositoryMethodInvocationResult result = mock(RepositoryMethodInvocationResult.class);
given(result.getState()).willReturn((error != null) ? State.ERROR : State.SUCCESS);
given(result.getError()).willReturn(error);
return new RepositoryMethodInvocation(repositoryInterface, method, result, 0);
}
interface ExampleRepository extends Repository<Example, Long> {
Example findById(long id);
}
static class Example {
}
}

View File

@@ -0,0 +1,122 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics;
import java.lang.reflect.Method;
import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.MockClock;
import io.micrometer.core.instrument.simple.SimpleConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener.RepositoryMethodInvocation;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener.RepositoryMethodInvocationResult;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener.RepositoryMethodInvocationResult.State;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link MetricsRepositoryMethodInvocationListener}.
*
* @author Phillip Webb
*/
class MetricsRepositoryMethodInvocationListenerTests {
private static final String REQUEST_METRICS_NAME = "repository.invocations";
private SimpleMeterRegistry registry;
private MetricsRepositoryMethodInvocationListener listener;
@BeforeEach
void setup() {
MockClock clock = new MockClock();
this.registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock);
this.listener = new MetricsRepositoryMethodInvocationListener(() -> this.registry,
new DefaultRepositoryTagsProvider(), REQUEST_METRICS_NAME, AutoTimer.ENABLED);
}
@Test
void afterInvocationWhenNoTimerAnnotationsAndNoAutoTimerDoesNothing() {
this.listener = new MetricsRepositoryMethodInvocationListener(() -> this.registry,
new DefaultRepositoryTagsProvider(), REQUEST_METRICS_NAME, null);
this.listener.afterInvocation(createInvocation(NoAnnotationsRepository.class));
assertThat(this.registry.find(REQUEST_METRICS_NAME).timers()).isEmpty();
}
@Test
void afterInvocationWhenTimedMethodRecordsMetrics() {
this.listener.afterInvocation(createInvocation(TimedMethodRepository.class));
assertMetricsContainsTag("state", "SUCCESS");
assertMetricsContainsTag("tag1", "value1");
}
@Test
void afterInvocationWhenTimedClassRecordsMetrics() {
this.listener.afterInvocation(createInvocation(TimedClassRepository.class));
assertMetricsContainsTag("state", "SUCCESS");
assertMetricsContainsTag("taga", "valuea");
}
@Test
void afterInvocationWhenAutoTimedRecordsMetrics() {
this.listener.afterInvocation(createInvocation(NoAnnotationsRepository.class));
assertMetricsContainsTag("state", "SUCCESS");
}
private void assertMetricsContainsTag(String tagKey, String tagValue) {
assertThat(this.registry.get(REQUEST_METRICS_NAME).tag(tagKey, tagValue).timer().count()).isOne();
}
private RepositoryMethodInvocation createInvocation(Class<?> repositoryInterface) {
Method method = ReflectionUtils.findMethod(repositoryInterface, "findById", long.class);
RepositoryMethodInvocationResult result = mock(RepositoryMethodInvocationResult.class);
given(result.getState()).willReturn(State.SUCCESS);
return new RepositoryMethodInvocation(repositoryInterface, method, result, 0);
}
interface NoAnnotationsRepository extends Repository<Example, Long> {
Example findById(long id);
}
interface TimedMethodRepository extends Repository<Example, Long> {
@Timed(extraTags = { "tag1", "value1" })
Example findById(long id);
}
@Timed(extraTags = { "taga", "valuea" })
interface TimedClassRepository extends Repository<Example, Long> {
Example findById(long id);
}
static class Example {
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics;
import java.lang.reflect.Method;
import java.util.Set;
import io.micrometer.core.annotation.Timed;
import org.junit.jupiter.api.Test;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link TimedAnnotations}.
*
* @author Phillip Webb
*/
class TimedAnnotationsTests {
@Test
void getWhenNoneReturnsEmptySet() {
Object bean = new None();
Method method = ReflectionUtils.findMethod(bean.getClass(), "handle");
Set<Timed> annotations = TimedAnnotations.get(method, bean.getClass());
assertThat(annotations).isEmpty();
}
@Test
void getWhenOnMethodReturnsMethodAnnotations() {
Object bean = new OnMethod();
Method method = ReflectionUtils.findMethod(bean.getClass(), "handle");
Set<Timed> annotations = TimedAnnotations.get(method, bean.getClass());
assertThat(annotations).extracting(Timed::value).containsOnly("y", "z");
}
@Test
void getWhenNonOnMethodReturnsBeanAnnotations() {
Object bean = new OnBean();
Method method = ReflectionUtils.findMethod(bean.getClass(), "handle");
Set<Timed> annotations = TimedAnnotations.get(method, bean.getClass());
assertThat(annotations).extracting(Timed::value).containsOnly("y", "z");
}
static class None {
void handle() {
}
}
@Timed("x")
static class OnMethod {
@Timed("y")
@Timed("z")
void handle() {
}
}
@Timed("y")
@Timed("z")
static class OnBean {
void handle() {
}
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics.autoconfigure;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.boot.data.metrics.MetricsRepositoryMethodInvocationListener;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.core.support.RepositoryFactoryCustomizer;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.util.function.SingletonSupplier;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link MetricsRepositoryMethodInvocationListenerBeanPostProcessor} .
*
* @author Phillip Webb
*/
class MetricsRepositoryMethodInvocationListenerBeanPostProcessorTests {
private final MetricsRepositoryMethodInvocationListener listener = mock(
MetricsRepositoryMethodInvocationListener.class);
private final MetricsRepositoryMethodInvocationListenerBeanPostProcessor postProcessor = new MetricsRepositoryMethodInvocationListenerBeanPostProcessor(
SingletonSupplier.of(this.listener));
@Test
@SuppressWarnings("rawtypes")
void postProcessBeforeInitializationWhenRepositoryFactoryBeanSupportAddsListener() {
RepositoryFactoryBeanSupport bean = mock(RepositoryFactoryBeanSupport.class);
Object result = this.postProcessor.postProcessBeforeInitialization(bean, "name");
assertThat(result).isSameAs(bean);
ArgumentCaptor<RepositoryFactoryCustomizer> customizer = ArgumentCaptor
.forClass(RepositoryFactoryCustomizer.class);
then(bean).should().addRepositoryFactoryCustomizer(customizer.capture());
RepositoryFactorySupport repositoryFactory = mock(RepositoryFactorySupport.class);
customizer.getValue().customize(repositoryFactory);
then(repositoryFactory).should().addInvocationListener(this.listener);
}
@Test
void postProcessBeforeInitializationWhenOtherBeanDoesNothing() {
Object bean = new Object();
Object result = this.postProcessor.postProcessBeforeInitialization(bean, "name");
assertThat(result).isSameAs(bean);
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics.autoconfigure;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.data.jpa.autoconfigure.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.data.metrics.autoconfigure.city.CityRepository;
import org.springframework.boot.jdbc.autoconfigure.EmbeddedDataSourceConfiguration;
import org.springframework.boot.jpa.autoconfigure.hibernate.HibernateJpaAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link RepositoryMetricsAutoConfiguration}.
*
* @author Phillip Webb
*/
class RepositoryMetricsAutoConfigurationIntegrationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withBean(SimpleMeterRegistry.class)
.withConfiguration(
AutoConfigurations.of(HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, RepositoryMetricsAutoConfiguration.class))
.withUserConfiguration(EmbeddedDataSourceConfiguration.class, TestConfig.class);
@Test
void repositoryMethodCallRecordsMetrics() {
this.contextRunner.run((context) -> {
context.getBean(CityRepository.class).count();
MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.get("spring.data.repository.invocations")
.tag("repository", "CityRepository")
.timer()
.count()).isOne();
});
}
@Test
void doesNotPreventMeterBindersFromDependingUponSpringDataRepositories() {
this.contextRunner.withUserConfiguration(SpringDataRepositoryMeterBinderConfiguration.class)
.run((context) -> assertThat(context).hasNotFailed());
}
@Configuration(proxyBeanMethods = false)
@AutoConfigurationPackage
static class TestConfig {
}
@Configuration(proxyBeanMethods = false)
static class SpringDataRepositoryMeterBinderConfiguration {
@Bean
MeterBinder meterBinder(CityRepository repository) {
return (registry) -> Gauge.builder("city.count", repository::count);
}
}
}

View File

@@ -0,0 +1,226 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics.autoconfigure;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Supplier;
import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.distribution.HistogramSnapshot;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.data.metrics.AutoTimer;
import org.springframework.boot.data.metrics.DefaultRepositoryTagsProvider;
import org.springframework.boot.data.metrics.MetricsRepositoryMethodInvocationListener;
import org.springframework.boot.data.metrics.RepositoryTagsProvider;
import org.springframework.boot.metrics.autoconfigure.MetricsAutoConfiguration;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener.RepositoryMethodInvocation;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener.RepositoryMethodInvocationResult;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener.RepositoryMethodInvocationResult.State;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link RepositoryMetricsAutoConfiguration}.
*
* @author Phillip Webb
*/
class RepositoryMetricsAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withBean(SimpleMeterRegistry.class)
.withConfiguration(
AutoConfigurations.of(MetricsAutoConfiguration.class, RepositoryMetricsAutoConfiguration.class))
.withPropertyValues("management.metrics.use-global-registry=false");
@Test
void backsOffWhenMeterRegistryIsMissing() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(RepositoryMetricsAutoConfiguration.class))
.run((context) -> assertThat(context).doesNotHaveBean(RepositoryTagsProvider.class));
}
@Test
void definesTagsProviderAndListenerWhenMeterRegistryIsPresent() {
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(DefaultRepositoryTagsProvider.class);
assertThat(context).hasSingleBean(MetricsRepositoryMethodInvocationListener.class);
assertThat(context).hasSingleBean(MetricsRepositoryMethodInvocationListenerBeanPostProcessor.class);
});
}
@Test
void tagsProviderBacksOff() {
this.contextRunner.withUserConfiguration(TagsProviderConfiguration.class).run((context) -> {
assertThat(context).doesNotHaveBean(DefaultRepositoryTagsProvider.class);
assertThat(context).hasSingleBean(TestRepositoryTagsProvider.class);
});
}
@Test
void metricsRepositoryMethodInvocationListenerBacksOff() {
this.contextRunner.withUserConfiguration(MetricsRepositoryMethodInvocationListenerConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(MetricsRepositoryMethodInvocationListener.class);
assertThat(context).hasSingleBean(TestMetricsRepositoryMethodInvocationListener.class);
});
}
@Test
void metricNameCanBeConfigured() {
this.contextRunner.withPropertyValues("management.metrics.data.repository.metric-name=datarepo")
.run((context) -> {
MeterRegistry registry = getInitializedMeterRegistry(context, ExampleRepository.class);
Timer timer = registry.get("datarepo").timer();
assertThat(timer).isNotNull();
});
}
@Test
void autoTimeRequestsCanBeConfigured() {
this.contextRunner
.withPropertyValues("management.metrics.data.repository.autotime.enabled=true",
"management.metrics.data.repository.autotime.percentiles=0.5,0.7")
.run((context) -> {
MeterRegistry registry = getInitializedMeterRegistry(context, ExampleRepository.class);
Timer timer = registry.get("spring.data.repository.invocations").timer();
HistogramSnapshot snapshot = timer.takeSnapshot();
assertThat(snapshot.percentileValues()).hasSize(2);
assertThat(snapshot.percentileValues()[0].percentile()).isEqualTo(0.5);
assertThat(snapshot.percentileValues()[1].percentile()).isEqualTo(0.7);
});
}
@Test
void timerWorksWithTimedAnnotationsWhenAutoTimeRequestsIsFalse() {
this.contextRunner.withPropertyValues("management.metrics.data.repository.autotime.enabled=false")
.run((context) -> {
MeterRegistry registry = getInitializedMeterRegistry(context, ExampleAnnotatedRepository.class);
Collection<Meter> meters = registry.get("spring.data.repository.invocations").meters();
assertThat(meters).hasSize(1);
Meter meter = meters.iterator().next();
assertThat(meter.getId().getTag("method")).isEqualTo("count");
});
}
@Test
void doesNotTriggerEarlyInitializationThatPreventsMeterBindersFromBindingMeters() {
this.contextRunner.withUserConfiguration(MeterBinderConfiguration.class)
.run((context) -> assertThat(context.getBean(MeterRegistry.class).find("binder.test").counter())
.isNotNull());
}
private MeterRegistry getInitializedMeterRegistry(AssertableApplicationContext context,
Class<?> repositoryInterface) {
MetricsRepositoryMethodInvocationListener listener = context
.getBean(MetricsRepositoryMethodInvocationListener.class);
ReflectionUtils.doWithLocalMethods(repositoryInterface, (method) -> {
RepositoryMethodInvocationResult result = mock(RepositoryMethodInvocationResult.class);
given(result.getState()).willReturn(State.SUCCESS);
RepositoryMethodInvocation invocation = new RepositoryMethodInvocation(repositoryInterface, method, result,
10);
listener.afterInvocation(invocation);
});
return context.getBean(MeterRegistry.class);
}
@Configuration(proxyBeanMethods = false)
static class TagsProviderConfiguration {
@Bean
TestRepositoryTagsProvider tagsProvider() {
return new TestRepositoryTagsProvider();
}
}
private static final class TestRepositoryTagsProvider implements RepositoryTagsProvider {
@Override
public Iterable<Tag> repositoryTags(RepositoryMethodInvocation invocation) {
return Collections.emptyList();
}
}
@Configuration(proxyBeanMethods = false)
static class MeterBinderConfiguration {
@Bean
MeterBinder meterBinder() {
return (registry) -> registry.counter("binder.test");
}
}
@Configuration(proxyBeanMethods = false)
static class MetricsRepositoryMethodInvocationListenerConfiguration {
@Bean
MetricsRepositoryMethodInvocationListener metricsRepositoryMethodInvocationListener(
ObjectFactory<MeterRegistry> registry, RepositoryTagsProvider tagsProvider) {
return new TestMetricsRepositoryMethodInvocationListener(registry::getObject, tagsProvider);
}
}
static class TestMetricsRepositoryMethodInvocationListener extends MetricsRepositoryMethodInvocationListener {
TestMetricsRepositoryMethodInvocationListener(Supplier<MeterRegistry> registrySupplier,
RepositoryTagsProvider tagsProvider) {
super(registrySupplier, tagsProvider, "test", AutoTimer.DISABLED);
}
}
interface ExampleRepository extends Repository<Example, Long> {
long count();
}
interface ExampleAnnotatedRepository extends Repository<Example, Long> {
@Timed
long count();
long delete();
}
static class Example {
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics.autoconfigure.city;
import java.io.Serializable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
@Entity
public class City implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String state;
@Column(nullable = false)
private String country;
@Column(nullable = false)
private String map;
protected City() {
}
public City(String name, String country) {
this.name = name;
this.country = country;
}
public String getName() {
return this.name;
}
public String getState() {
return this.state;
}
public String getCountry() {
return this.country;
}
public String getMap() {
return this.map;
}
@Override
public String toString() {
return getName() + "," + getState() + "," + getCountry();
}
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.boot.data.metrics.autoconfigure.city;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CityRepository extends JpaRepository<City, Long> {
@Override
Page<City> findAll(Pageable pageable);
Page<City> findByNameLikeAndCountryLikeAllIgnoringCase(String name, String country, Pageable pageable);
City findByNameAndCountryAllIgnoringCase(String name, String country);
}