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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.appsmith.external.constants.spans;

import static com.appsmith.external.constants.spans.BaseSpan.APPSMITH_SPAN_PREFIX;

public class TenantSpan {
public static final String FETCH_DEFAULT_TENANT_SPAN = APPSMITH_SPAN_PREFIX + "fetch_default_tenant";
public static final String FETCH_TENANT_CACHE_POST_DESERIALIZATION_ERROR_SPAN =
APPSMITH_SPAN_PREFIX + "fetch_tenant_cache_post_deserialization_error";
public static final String FETCH_TENANT_FROM_DB_SPAN = APPSMITH_SPAN_PREFIX + "fetch_tenant_from_db";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.appsmith.server.helpers.InMemoryCacheableRepositoryHelper;
import com.appsmith.server.repositories.ce_compatible.CacheableRepositoryHelperCECompatibleImpl;
import io.micrometer.observation.ObservationRegistry;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.stereotype.Component;

Expand All @@ -11,7 +12,8 @@ public class CacheableRepositoryHelperImpl extends CacheableRepositoryHelperCECo

public CacheableRepositoryHelperImpl(
ReactiveMongoOperations mongoOperations,
InMemoryCacheableRepositoryHelper inMemoryCacheableRepositoryHelper) {
super(mongoOperations, inMemoryCacheableRepositoryHelper);
InMemoryCacheableRepositoryHelper inMemoryCacheableRepositoryHelper,
ObservationRegistry observationRegistry) {
super(mongoOperations, inMemoryCacheableRepositoryHelper, observationRegistry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
import com.appsmith.server.helpers.InMemoryCacheableRepositoryHelper;
import com.appsmith.server.helpers.ce.bridge.Bridge;
import com.appsmith.server.helpers.ce.bridge.BridgeQuery;
import io.micrometer.observation.ObservationRegistry;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import reactor.core.observability.micrometer.Micrometer;
import reactor.core.publisher.Mono;

import java.util.Set;
import java.util.stream.Collectors;

import static com.appsmith.external.constants.spans.TenantSpan.FETCH_TENANT_FROM_DB_SPAN;
import static com.appsmith.server.constants.FieldName.PERMISSION_GROUP_ID;
import static com.appsmith.server.constants.ce.FieldNameCE.ANONYMOUS_USER;
import static com.appsmith.server.constants.ce.FieldNameCE.DEFAULT_PERMISSION_GROUP;
Expand All @@ -35,12 +38,15 @@
public class CacheableRepositoryHelperCEImpl implements CacheableRepositoryHelperCE {
private final ReactiveMongoOperations mongoOperations;
private final InMemoryCacheableRepositoryHelper inMemoryCacheableRepositoryHelper;
private final ObservationRegistry observationRegistry;

public CacheableRepositoryHelperCEImpl(
ReactiveMongoOperations mongoOperations,
InMemoryCacheableRepositoryHelper inMemoryCacheableRepositoryHelper) {
InMemoryCacheableRepositoryHelper inMemoryCacheableRepositoryHelper,
ObservationRegistry observationRegistry) {
this.mongoOperations = mongoOperations;
this.inMemoryCacheableRepositoryHelper = inMemoryCacheableRepositoryHelper;
this.observationRegistry = observationRegistry;
}

@Cache(cacheName = "permissionGroupsForUser", key = "{#user.email + #user.tenantId}")
Expand Down Expand Up @@ -182,13 +188,17 @@ public Mono<Tenant> fetchDefaultTenant(String tenantId) {
BridgeQuery<Tenant> andCriteria = Bridge.and(defaultTenantCriteria, notDeletedCriteria);
Query query = new Query();
query.addCriteria(andCriteria);

return mongoOperations.findOne(query, Tenant.class).map(tenant -> {
if (tenant.getTenantConfiguration() == null) {
tenant.setTenantConfiguration(new TenantConfiguration());
}
return tenant;
});
log.info("FETCHING TENANT FROM DATABASE AS NOT PRESENT IN CACHE!");
return mongoOperations
.findOne(query, Tenant.class)
.map(tenant -> {
if (tenant.getTenantConfiguration() == null) {
tenant.setTenantConfiguration(new TenantConfiguration());
}
return tenant;
})
.name(FETCH_TENANT_FROM_DB_SPAN)
.tap(Micrometer.observation(observationRegistry));
}

@CacheEvict(cacheName = "tenant", key = "{#tenantId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.appsmith.server.helpers.InMemoryCacheableRepositoryHelper;
import com.appsmith.server.repositories.ce.CacheableRepositoryHelperCEImpl;
import io.micrometer.observation.ObservationRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.stereotype.Component;
Expand All @@ -12,7 +13,8 @@ public class CacheableRepositoryHelperCECompatibleImpl extends CacheableReposito
implements CacheableRepositoryHelperCECompatible {
public CacheableRepositoryHelperCECompatibleImpl(
ReactiveMongoOperations mongoOperations,
InMemoryCacheableRepositoryHelper inMemoryCacheableRepositoryHelper) {
super(mongoOperations, inMemoryCacheableRepositoryHelper);
InMemoryCacheableRepositoryHelper inMemoryCacheableRepositoryHelper,
ObservationRegistry observationRegistry) {
super(mongoOperations, inMemoryCacheableRepositoryHelper, observationRegistry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.appsmith.server.repositories.TenantRepository;
import com.appsmith.server.services.ce.TenantServiceCEImpl;
import com.appsmith.server.solutions.EnvManager;
import io.micrometer.observation.ObservationRegistry;
import jakarta.validation.Validator;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
Expand All @@ -23,7 +24,8 @@ public TenantServiceImpl(
@Lazy EnvManager envManager,
FeatureFlagMigrationHelper featureFlagMigrationHelper,
CacheableRepositoryHelper cacheableRepositoryHelper,
CommonConfig commonConfig) {
CommonConfig commonConfig,
ObservationRegistry observationRegistry) {
super(
validator,
repository,
Expand All @@ -32,6 +34,7 @@ public TenantServiceImpl(
envManager,
featureFlagMigrationHelper,
cacheableRepositoryHelper,
commonConfig);
commonConfig,
observationRegistry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
import com.appsmith.server.services.BaseService;
import com.appsmith.server.services.ConfigService;
import com.appsmith.server.solutions.EnvManager;
import io.micrometer.observation.ObservationRegistry;
import jakarta.validation.Validator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.StringUtils;
import reactor.core.observability.micrometer.Micrometer;
import reactor.core.publisher.Mono;

import java.util.Map;

import static com.appsmith.external.constants.spans.TenantSpan.FETCH_DEFAULT_TENANT_SPAN;
import static com.appsmith.external.constants.spans.TenantSpan.FETCH_TENANT_CACHE_POST_DESERIALIZATION_ERROR_SPAN;
import static com.appsmith.server.acl.AclPermission.MANAGE_TENANT;
import static java.lang.Boolean.TRUE;

Expand All @@ -44,6 +48,7 @@ public class TenantServiceCEImpl extends BaseService<TenantRepository, Tenant, S
private final CacheableRepositoryHelper cacheableRepositoryHelper;

private final CommonConfig commonConfig;
private final ObservationRegistry observationRegistry;

public TenantServiceCEImpl(
Validator validator,
Expand All @@ -53,13 +58,15 @@ public TenantServiceCEImpl(
@Lazy EnvManager envManager,
FeatureFlagMigrationHelper featureFlagMigrationHelper,
CacheableRepositoryHelper cacheableRepositoryHelper,
CommonConfig commonConfig) {
CommonConfig commonConfig,
ObservationRegistry observationRegistry) {
super(validator, repository, analyticsService);
this.configService = configService;
this.envManager = envManager;
this.featureFlagMigrationHelper = featureFlagMigrationHelper;
this.cacheableRepositoryHelper = cacheableRepositoryHelper;
this.commonConfig = commonConfig;
this.observationRegistry = observationRegistry;
}

@Override
Expand Down Expand Up @@ -175,6 +182,8 @@ public Mono<Tenant> getDefaultTenant() {
// Fetching Tenant from redis cache
return getDefaultTenantId()
.flatMap(tenantId -> cacheableRepositoryHelper.fetchDefaultTenant(tenantId))
.name(FETCH_DEFAULT_TENANT_SPAN)
.tap(Micrometer.observation(observationRegistry))
.flatMap(tenant -> repository.setUserPermissionsInObject(tenant).switchIfEmpty(Mono.just(tenant)))
.onErrorResume(e -> {
log.error("Error fetching default tenant from redis!", e);
Expand All @@ -191,6 +200,8 @@ public Mono<Tenant> getDefaultTenant() {
}
return tenant;
}))
.name(FETCH_TENANT_CACHE_POST_DESERIALIZATION_ERROR_SPAN)
.tap(Micrometer.observation(observationRegistry))
.flatMap(tenant -> repository
.setUserPermissionsInObject(tenant)
.switchIfEmpty(Mono.just(tenant)));
Expand Down