Move default TenantResolver registration in hawkbit-core (#2778)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-10-28 08:33:50 +02:00
committed by GitHub
parent 948ce408f2
commit 46c0c4aeaf
7 changed files with 32 additions and 53 deletions

View File

@@ -58,12 +58,6 @@ import org.springframework.util.CollectionUtils;
@Import(RepositoryConfiguration.class)
public class SecurityAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public TenantResolver tenantResolver() {
return new DefaultTenantResolver();
}
/**
* Creates a {@link ContextAware} (hence {@link TenantAware}) bean based on the given {@link UserAuthoritiesResolver},
* {@link SecurityContextSerializer} and {@link TenantResolver}.

View File

@@ -17,10 +17,7 @@ import io.micrometer.common.KeyValue;
import io.micrometer.common.KeyValues;
import io.micrometer.core.instrument.Tag;
import io.micrometer.observation.ObservationRegistry;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.eclipse.hawkbit.tenancy.TenantAware.TenantResolver;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.autoconfigure.observation.ObservationProperties;
import org.springframework.boot.actuate.autoconfigure.observation.web.servlet.WebMvcObservationAutoConfiguration;
@@ -29,35 +26,48 @@ import org.springframework.boot.actuate.metrics.data.RepositoryTagsProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
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.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener.RepositoryMethodInvocation;
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener;
import org.springframework.http.server.observation.DefaultServerRequestObservationConvention;
import org.springframework.http.server.observation.ServerRequestObservationContext;
import org.springframework.http.server.observation.ServerRequestObservationConvention;
import org.springframework.web.filter.ServerHttpObservationFilter;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TenantMetricsConfiguration {
@AutoConfiguration
public class DefaultTenantConfiguration {
public static final String TENANT_TAG = "tenant";
@Bean
@ConditionalOnMissingBean
TenantAware.TenantResolver tenantResolver() {
return new TenantAware.DefaultTenantResolver();
}
@Bean
@ConditionalOnMissingBean
TenantAwareCacheManager cacheManager() {
return TenantAwareCacheManager.getInstance();
}
@AutoConfiguration(afterName = {
"org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration",
"org.eclipse.hawkbit.autoconfigure.security.SecurityAutoConfiguration" })
@ConditionalOnProperty(name = "hawkbit.metrics.tenancy.web.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass(name = { "org.springframework.web.servlet.DispatcherServlet", "io.micrometer.observation.Observation" })
@ConditionalOnBean({ ObservationRegistry.class, TenantResolver.class })
@ConditionalOnBean({ ObservationRegistry.class, TenantAware.TenantResolver.class })
public static class WebConfig {
@Bean
@Primary
public DefaultServerRequestObservationConvention serverRequestObservationConvention(final TenantResolver tenantResolver) {
public DefaultServerRequestObservationConvention serverRequestObservationConvention(final TenantAware.TenantResolver tenantResolver) {
return new DefaultServerRequestObservationConvention() {
@NonNull
@@ -94,15 +104,15 @@ public class TenantMetricsConfiguration {
@ConditionalOnClass(name = {
"io.micrometer.core.instrument.Tag",
"org.springframework.data.repository.core.support.RepositoryMethodInvocationListener" })
@ConditionalOnBean(TenantResolver.class)
@ConditionalOnBean(TenantAware.TenantResolver.class)
public static class RepositoryConfig {
@Bean
public RepositoryTagsProvider repositoryTagsProvider(final TenantResolver tenantResolver) {
public RepositoryTagsProvider repositoryTagsProvider(final TenantAware.TenantResolver tenantResolver) {
return new DefaultRepositoryTagsProvider() {
@Override
public Iterable<Tag> repositoryTags(final RepositoryMethodInvocation invocation) {
public Iterable<Tag> repositoryTags(final RepositoryMethodInvocationListener.RepositoryMethodInvocation invocation) {
final Iterable<Tag> defaultTags = super.repositoryTags(invocation);
final String tenant = Optional.ofNullable(tenantResolver.resolveTenant()).orElse("n/a");
return () -> {

View File

@@ -1,24 +0,0 @@
/**
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.tenancy;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TenantCacheConfiguration {
@Bean
TenantAwareCacheManager cacheManager() {
return TenantAwareCacheManager.getInstance();
}
}

View File

@@ -1,3 +1,3 @@
org.eclipse.hawkbit.tenancy.TenantCacheConfiguration
org.eclipse.hawkbit.tenancy.TenantMetricsConfiguration.WebConfig
org.eclipse.hawkbit.tenancy.TenantMetricsConfiguration.RepositoryConfig
org.eclipse.hawkbit.tenancy.DefaultTenantConfiguration
org.eclipse.hawkbit.tenancy.DefaultTenantConfiguration.WebConfig
org.eclipse.hawkbit.tenancy.DefaultTenantConfiguration.RepositoryConfig

View File

@@ -18,7 +18,7 @@ import lombok.extern.slf4j.Slf4j;
import org.eclipse.hawkbit.repository.AutoAssignExecutor;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.security.SystemSecurityContext;
import org.eclipse.hawkbit.tenancy.TenantMetricsConfiguration;
import org.eclipse.hawkbit.tenancy.DefaultTenantConfiguration;
import org.springframework.integration.support.locks.LockRegistry;
import org.springframework.scheduling.annotation.Scheduled;
@@ -86,7 +86,7 @@ public class AutoAssignScheduler {
meterRegistry
.map(mReg -> mReg.timer(
"hawkbit.scheduler.executor",
TenantMetricsConfiguration.TENANT_TAG, tenant))
DefaultTenantConfiguration.TENANT_TAG, tenant))
.ifPresent(timer -> timer.record(System.nanoTime() - startNanoT, TimeUnit.NANOSECONDS));
});
} finally {

View File

@@ -20,8 +20,8 @@ import org.eclipse.hawkbit.repository.RolloutExecutor;
import org.eclipse.hawkbit.repository.RolloutHandler;
import org.eclipse.hawkbit.repository.RolloutManagement;
import org.eclipse.hawkbit.repository.jpa.utils.DeploymentHelper;
import org.eclipse.hawkbit.tenancy.DefaultTenantConfiguration;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.eclipse.hawkbit.tenancy.TenantMetricsConfiguration;
import org.springframework.integration.support.locks.LockRegistry;
import org.springframework.transaction.PlatformTransactionManager;
@@ -86,7 +86,7 @@ public class JpaRolloutHandler implements RolloutHandler {
}
});
meterRegistry
.map(mReg -> mReg.timer("hawkbit.rollout.handler.all", TenantMetricsConfiguration.TENANT_TAG, tenantAware.getCurrentTenant()))
.map(mReg -> mReg.timer("hawkbit.rollout.handler.all", DefaultTenantConfiguration.TENANT_TAG, tenantAware.getCurrentTenant()))
.ifPresent(timer -> timer.record(System.nanoTime() - startNano, TimeUnit.NANOSECONDS));
log.debug("Finished handling of the rollouts.");
@@ -116,9 +116,8 @@ public class JpaRolloutHandler implements RolloutHandler {
meterRegistry
.map(mReg -> mReg.timer(
"hawkbit.rollout.handler",
TenantMetricsConfiguration.TENANT_TAG, tenantAware.getCurrentTenant(),
DefaultTenantConfiguration.TENANT_TAG, tenantAware.getCurrentTenant(),
"rollout", String.valueOf(rolloutId)))
.ifPresent(timer -> timer.record(System.nanoTime() - startNano, TimeUnit.NANOSECONDS));
}
}
}

View File

@@ -18,7 +18,7 @@ import org.eclipse.hawkbit.repository.RolloutHandler;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.repository.jpa.rollout.BlockWhenFullPolicy;
import org.eclipse.hawkbit.security.SystemSecurityContext;
import org.eclipse.hawkbit.tenancy.TenantMetricsConfiguration;
import org.eclipse.hawkbit.tenancy.DefaultTenantConfiguration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@@ -94,7 +94,7 @@ public class RolloutScheduler {
meterRegistry
.map(mReg -> mReg.timer(
"hawkbit.rollout.scheduler",
TenantMetricsConfiguration.TENANT_TAG, tenant))
DefaultTenantConfiguration.TENANT_TAG, tenant))
.ifPresent(timer -> timer.record(System.nanoTime() - startNano, TimeUnit.NANOSECONDS));
}