Skip to content

Commit

Permalink
RHCLOUD-31503 Log Unleash toggles changes in all apps
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenneg committed Mar 17, 2024
1 parent 4e91135 commit 8773870
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.redhat.cloud.notifications.config;

import com.redhat.cloud.notifications.unleash.ToggleRegistry;
import io.getunleash.Unleash;
import io.quarkus.logging.Log;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.event.Startup;
Expand All @@ -22,7 +24,7 @@ public class AggregatorConfig {
/*
* Unleash configuration
*/
private static final String SINGLE_DAILY_DIGEST = toggleName("single-daily-digest");
private String singleDailyDigestToggle;

private static String toggleName(String feature) {
return String.format("notifications-aggregator.%s.enabled", feature);
Expand All @@ -36,13 +38,21 @@ private static String toggleName(String feature) {
@Deprecated(forRemoval = true, since = "To be removed when we're done migrating to Unleash in all environments")
boolean singleDailyDigestEnabled;

@Inject
ToggleRegistry toggleRegistry;

@Inject
Unleash unleash;

@PostConstruct
void postConstruct() {
singleDailyDigestToggle = toggleRegistry.register("single-daily-digest", true);
}

void logConfigAtStartup(@Observes Startup event) {

Map<String, Object> config = new TreeMap<>();
config.put(SINGLE_DAILY_DIGEST, isSingleDailyDigestEnabled());
config.put(singleDailyDigestToggle, isSingleDailyDigestEnabled());
config.put(UNLEASH, unleashEnabled);

Log.info("=== Startup configuration ===");
Expand All @@ -53,7 +63,7 @@ void logConfigAtStartup(@Observes Startup event) {

public boolean isSingleDailyDigestEnabled() {
if (unleashEnabled) {
return unleash.isEnabled(SINGLE_DAILY_DIGEST, false);
return unleash.isEnabled(singleDailyDigestToggle, false);
} else {
return singleDailyDigestEnabled;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.redhat.cloud.notifications.config;

import com.redhat.cloud.notifications.unleash.ToggleRegistry;
import io.getunleash.Unleash;
import io.quarkus.logging.Log;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.event.Startup;
Expand All @@ -25,10 +27,10 @@ public class BackendConfig {
/*
* Unleash configuration
*/
private static final String DRAWER = toggleName("drawer");
private static final String FORBID_SLACK_CHANNEL_USAGE = toggleName("forbid-slack-channel-usage");
private static final String UNIQUE_BG_NAME = toggleName("unique-bg-name");
private static final String UNIQUE_INTEGRATION_NAME = toggleName("unique-integration-name");
private String drawerToggle;
private String forbidSlackChannelUsageToggle;
private String uniqueBgNameToggle;
private String uniqueIntegrationNameToggle;

private static String toggleName(String feature) {
return String.format("notifications-backend.%s.enabled", feature);
Expand Down Expand Up @@ -66,19 +68,30 @@ private static String toggleName(String feature) {
@ConfigProperty(name = INSTANT_EMAILS, defaultValue = "false")
boolean instantEmailsEnabled;

@Inject
ToggleRegistry toggleRegistry;

@Inject
Unleash unleash;

@PostConstruct
void postConstruct() {
drawerToggle = toggleRegistry.register("drawer", true);
forbidSlackChannelUsageToggle = toggleRegistry.register("forbid-slack-channel-usage", true);
uniqueBgNameToggle = toggleRegistry.register("unique-bg-name", true);
uniqueIntegrationNameToggle = toggleRegistry.register("unique-integration-name", true);
}

void logConfigAtStartup(@Observes Startup event) {

Map<String, Object> config = new TreeMap<>();
config.put(DEFAULT_TEMPLATE, isDefaultTemplateEnabled());
config.put(DRAWER, isDrawerEnabled());
config.put(drawerToggle, isDrawerEnabled());
config.put(EMAILS_ONLY_MODE, isEmailsOnlyModeEnabled());
config.put(INSTANT_EMAILS, isInstantEmailsEnabled());
config.put(FORBID_SLACK_CHANNEL_USAGE, isForbidSlackChannelUsage());
config.put(UNIQUE_BG_NAME, isUniqueBgNameEnabled());
config.put(UNIQUE_INTEGRATION_NAME, isUniqueIntegrationNameEnabled());
config.put(forbidSlackChannelUsageToggle, isForbidSlackChannelUsage());
config.put(uniqueBgNameToggle, isUniqueBgNameEnabled());
config.put(uniqueIntegrationNameToggle, isUniqueIntegrationNameEnabled());
config.put(UNLEASH, unleashEnabled);

Log.info("=== Startup configuration ===");
Expand All @@ -93,7 +106,7 @@ public boolean isDefaultTemplateEnabled() {

public boolean isDrawerEnabled() {
if (unleashEnabled) {
return unleash.isEnabled(DRAWER, false);
return unleash.isEnabled(drawerToggle, false);
} else {
return drawerEnabled;
}
Expand All @@ -105,7 +118,7 @@ public boolean isEmailsOnlyModeEnabled() {

public boolean isForbidSlackChannelUsage() {
if (unleashEnabled) {
return unleash.isEnabled(FORBID_SLACK_CHANNEL_USAGE, false);
return unleash.isEnabled(forbidSlackChannelUsageToggle, false);
} else {
return slackForbidChannelUsageEnabled;
}
Expand All @@ -117,15 +130,15 @@ public boolean isInstantEmailsEnabled() {

public boolean isUniqueBgNameEnabled() {
if (unleashEnabled) {
return unleash.isEnabled(UNIQUE_BG_NAME, false);
return unleash.isEnabled(uniqueBgNameToggle, false);
} else {
return enforceBehaviorGroupNameUnicity;
}
}

public boolean isUniqueIntegrationNameEnabled() {
if (unleashEnabled) {
return unleash.isEnabled(UNIQUE_INTEGRATION_NAME, false);
return unleash.isEnabled(uniqueIntegrationNameToggle, false);
} else {
return enforceIntegrationNameUnicity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@
import io.getunleash.FeatureToggle;
import io.getunleash.repository.ToggleCollection;
import io.quarkus.logging.Log;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public abstract class ToggleChangedLogger {
@ApplicationScoped
public class ToggleChangedLogger {

@ConfigProperty(name = "notifications.unleash.toggle-changed-logger.enabled", defaultValue = "true")
boolean enabled;

@Inject
ToggleRegistry toggleRegistry;

private final Map<String, Boolean> toggleValues = new HashMap<>();

void process(@Observes ToggleCollection toggleCollection) {
if (enabled) {
for (String toggleName : getLoggedToggles()) {
for (String toggleName : toggleRegistry.getLoggedToggles()) {
FeatureToggle toggle = toggleCollection.getToggle(toggleName);
if (toggle != null) {
Boolean toggleEnabled = toggleValues.put(toggle.getName(), toggle.isEnabled());
Expand All @@ -32,6 +37,4 @@ void process(@Observes ToggleCollection toggleCollection) {
}
}
}

protected abstract Set<String> getLoggedToggles();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.redhat.cloud.notifications.unleash;

import io.quarkus.logging.Log;
import io.quarkus.runtime.ApplicationConfig;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import java.util.HashSet;
import java.util.Set;

@ApplicationScoped
public class ToggleRegistry {

private static final String DEFAULT_APP_NAME = "notifications-default-app-name";

@Inject
ApplicationConfig applicationConfig;

private final Set<String> loggedToggles = new HashSet<>();

public String register(String feature, boolean logChanges) {

if (applicationConfig == null) {
throw new IllegalStateException("This method must be called after CDI is done initializing");
}

String appName = applicationConfig.name.orElseGet(() -> {
// This should only happen when tests are executed.
Log.warnf("Application name not found in the Quarkus config, defaulting to %s", DEFAULT_APP_NAME);
return DEFAULT_APP_NAME;
});

String toggleName = String.format("%s.%s.enabled", appName, feature);

if (logChanges) {
loggedToggles.add(toggleName);
}

return toggleName;
}

public Set<String> getLoggedToggles() {
return loggedToggles;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.redhat.cloud.notifications.connector.http;

import com.redhat.cloud.notifications.connector.ConnectorConfig;
import com.redhat.cloud.notifications.unleash.ToggleRegistry;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import java.util.List;
Expand All @@ -26,6 +28,11 @@ public class HttpConnectorConfig extends ConnectorConfig {
private static final String SERVER_ERROR_LOG_LEVEL = "notifications.connector.http.server-error.log-level";
private static final String SOCKET_TIMEOUT_MS = "notifications.connector.http.socket-timeout-ms";

/*
* Unleash configuration
*/
private String disableFaultyEndpointsToggle;

@ConfigProperty(name = CLIENT_ERROR_LOG_LEVEL, defaultValue = "DEBUG")
Level clientErrorLogLevel;

Expand Down Expand Up @@ -54,14 +61,12 @@ public class HttpConnectorConfig extends ConnectorConfig {
@ConfigProperty(name = SOCKET_TIMEOUT_MS, defaultValue = "2500")
int httpSocketTimeout;

/*
* Unleash configuration
*/
private String disableFaultyEndpointsToggleName;
@Inject
ToggleRegistry toggleRegistry;

@PostConstruct
void postConstruct() {
disableFaultyEndpointsToggleName = toggleName("disable-faulty-endpoints");
disableFaultyEndpointsToggle = toggleRegistry.register("disable-faulty-endpoints", true);
}

@Override
Expand All @@ -71,7 +76,7 @@ protected Map<String, Object> getLoggedConfiguration() {
config.put(COMPONENTS, httpComponents);
config.put(CONNECT_TIMEOUT_MS, httpConnectTimeout);
config.put(CONNECTIONS_PER_ROUTE, httpConnectionsPerRoute);
config.put(disableFaultyEndpointsToggleName, isDisableFaultyEndpoints());
config.put(disableFaultyEndpointsToggle, isDisableFaultyEndpoints());
config.put(FOLLOW_REDIRECTS, followRedirects);
config.put(MAX_TOTAL_CONNECTIONS, httpMaxTotalConnections);
config.put(SERVER_ERROR_LOG_LEVEL, serverErrorLogLevel);
Expand All @@ -97,7 +102,7 @@ public int getHttpConnectionsPerRoute() {

public boolean isDisableFaultyEndpoints() {
if (unleashEnabled) {
return unleash.isEnabled(disableFaultyEndpointsToggleName, true);
return unleash.isEnabled(disableFaultyEndpointsToggle, true);
} else {
return disableFaultyEndpoints;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ public void log() {
});
}

protected String toggleName(String feature) {
return String.format("notifications-connector-%s.%s.enabled", getConnectorName(), feature);
}

protected Map<String, Object> getLoggedConfiguration() {
Map<String, Object> config = new TreeMap<>();
config.put(ENDPOINT_CACHE_MAX_SIZE, endpointCacheMaxSize);
Expand Down
Loading

0 comments on commit 8773870

Please sign in to comment.