Skip to content

Commit

Permalink
do not clear a subset of MDC keys when scope exits (DAT-16500) (#5416)
Browse files Browse the repository at this point in the history
* do not clear a subset of MDC keys when scope exits

* move method to util class

---------

Co-authored-by: obovsunivskyii <baqaua@gmail.com>
  • Loading branch information
StevenMassaro and obovsunivskyii committed Jan 11, 2024
1 parent 8743812 commit de976e5
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package liquibase.integration.commandline;

import static liquibase.integration.commandline.LiquibaseLauncherSettings.LiquibaseLauncherSetting.LIQUIBASE_HOME;
import static liquibase.integration.commandline.LiquibaseLauncherSettings.getSetting;

import liquibase.GlobalConfiguration;
import liquibase.Scope;
import liquibase.command.CommandArgumentDefinition;
Expand Down Expand Up @@ -58,6 +55,8 @@

import static java.util.ResourceBundle.getBundle;
import static liquibase.configuration.LiquibaseConfiguration.REGISTERED_VALUE_PROVIDERS_KEY;
import static liquibase.integration.commandline.LiquibaseLauncherSettings.LiquibaseLauncherSetting.LIQUIBASE_HOME;
import static liquibase.integration.commandline.LiquibaseLauncherSettings.getSetting;
import static liquibase.util.SystemUtil.isWindows;


Expand Down Expand Up @@ -417,6 +416,8 @@ public int execute(String[] args) {
for (ConfigurationValueProvider provider : valueProviders) {
liquibaseConfiguration.unregisterProvider(provider);
}

LogUtil.setPersistedMdcKeysToEmptyString();
}
});
} catch (Throwable e) {
Expand All @@ -434,14 +435,13 @@ private void addEmptyMdcValues() {
Scope.getCurrentScope().addMdcValue(MdcKey.DEPLOYMENT_OUTCOME_COUNT, "0");
Scope.getCurrentScope().addMdcValue(MdcKey.ROWS_AFFECTED, "0");
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGELOG_FILE, "");
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_ID, "");
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_AUTHOR, "");
LogUtil.setPersistedMdcKeysToEmptyString();
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_OUTCOME, "NOOP");
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESETS_UPDATED, new ChangesetsUpdated());
Scope.getCurrentScope().addMdcValue(MdcKey.OPERATION_START_TIME, "");
Scope.getCurrentScope().addMdcValue(MdcKey.OPERATION_STOP_TIME, "");
Scope.getCurrentScope().addMdcValue(MdcKey.LIQUIBASE_SYSTEM_NAME, "");
Scope.getCurrentScope().addMdcValue(MdcKey.LIQUIBASE_SYSTEM_USER, "");
Scope.getCurrentScope().addMdcValue(MdcKey.LIQUIBASE_SYSTEM_USER, "", false);
Scope.getCurrentScope().addMdcValue(MdcKey.LIQUIBASE_TARGET_URL, "");
Scope.getCurrentScope().addMdcValue(MdcKey.LIQUIBASE_VERSION, "");
Scope.getCurrentScope().addMdcValue(MdcKey.LIQUIBASE_SCHEMA_NAME, "");
Expand All @@ -455,8 +455,8 @@ private void addEmptyMdcValues() {
private void logMdcData() throws IOException {
MdcManager mdcManager = Scope.getCurrentScope().getMdcManager();
String localHostName = NetUtil.getLocalHostName();
Scope.getCurrentScope().addMdcValue(MdcKey.LIQUIBASE_SYSTEM_USER, System.getProperty("user.name"), false);
try (MdcObject version = mdcManager.put(MdcKey.LIQUIBASE_VERSION, LiquibaseUtil.getBuildVersion());
MdcObject systemUser = mdcManager.put(MdcKey.LIQUIBASE_SYSTEM_USER, System.getProperty("user.name"));
MdcObject systemName = mdcManager.put(MdcKey.LIQUIBASE_SYSTEM_NAME, localHostName);
// The host name here is purposefully the same as the system name. The system name is retained for backwards compatibility.
MdcObject hostName = mdcManager.put(MdcKey.LIQUIBASE_HOST_NAME, localHostName)) {
Expand Down
6 changes: 3 additions & 3 deletions liquibase-standard/src/main/java/liquibase/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public MdcObject addMdcValue(String key, String value) {
* then it should be set to true.
*/
public MdcObject addMdcValue(String key, String value, boolean removeWhenScopeExits) {
MdcObject mdcObject = getMdcManager().put(key, value);
MdcObject mdcObject = getMdcManager().put(key, value, removeWhenScopeExits);
removeMdcObjectWhenScopeExits(removeWhenScopeExits, mdcObject);

return mdcObject;
Expand Down Expand Up @@ -449,7 +449,7 @@ public MdcObject addMdcValue(String key, Map<String, Object> value) {
* then it should be set to true.
*/
public MdcObject addMdcValue(String key, Map<String, Object> value, boolean removeWhenScopeExits) {
MdcObject mdcObject = getMdcManager().put(key, value);
MdcObject mdcObject = getMdcManager().put(key, value, removeWhenScopeExits);
removeMdcObjectWhenScopeExits(removeWhenScopeExits, mdcObject);

return mdcObject;
Expand All @@ -470,7 +470,7 @@ public MdcObject addMdcValue(String key, CustomMdcObject customMdcObject) {
* then it should be set to true.
*/
public MdcObject addMdcValue(String key, CustomMdcObject customMdcObject, boolean removeWhenScopeExits) {
MdcObject mdcObject = getMdcManager().put(key, customMdcObject);
MdcObject mdcObject = getMdcManager().put(key, customMdcObject, removeWhenScopeExits);
removeMdcObjectWhenScopeExits(removeWhenScopeExits, mdcObject);

return mdcObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ enum Direction {

default void logMdcData(ChangeSet changeSet) {
Scope scope = Scope.getCurrentScope();
scope.addMdcValue(MdcKey.CHANGESET_ID, changeSet.getId());
scope.addMdcValue(MdcKey.CHANGESET_AUTHOR, changeSet.getAuthor());
scope.addMdcValue(MdcKey.CHANGESET_ID, changeSet.getId(), false);
scope.addMdcValue(MdcKey.CHANGESET_AUTHOR, changeSet.getAuthor(), false);
scope.addMdcValue(MdcKey.CHANGESET_FILEPATH, changeSet.getFilePath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,50 @@ public interface MdcManager extends Plugin {
@Beta
MdcObject put(String key, String value);

/**
* Puts a context value (the <code>value</code> parameter) as identified with the <code>key</code> parameter into
* the MDC. The caller is responsible for cleaning up this entry at an appropriate time.
* @param removeWhenScopeExits if true, this key value pair will be automatically removed from the MDC when this
* scope exits. If there is not a demonstrable reason for setting this parameter to false
* then it should be set to true.
*/
@Beta
MdcObject put(String key, String value, boolean removeWhenScopeExits);

/**
* Puts a context value (the <code>values</code> parameter) as identified with the <code>key</code> parameter into
* the MDC. The caller is responsible for cleaning up this entry at an appropriate time.
*/
@Beta
MdcObject put(String key, Map<String, Object> values);

/**
* Puts a context value (the <code>values</code> parameter) as identified with the <code>key</code> parameter into
* the MDC. The caller is responsible for cleaning up this entry at an appropriate time.
* @param removeWhenScopeExits if true, this key value pair will be automatically removed from the MDC when this
* scope exits. If there is not a demonstrable reason for setting this parameter to false
* then it should be set to true.
*/
@Beta
MdcObject put(String key, Map<String, Object> values, boolean removeWhenScopeExits);

/**
* Puts a context value (the <code>customMdcObject</code> parameter) as identified with the <code>key</code> parameter into
* the MDC. The caller is responsible for cleaning up this entry at an appropriate time.
*/
@Beta
MdcObject put(String key, CustomMdcObject customMdcObject);

/**
* Puts a context value (the <code>customMdcObject</code> parameter) as identified with the <code>key</code> parameter into
* the MDC. The caller is responsible for cleaning up this entry at an appropriate time.
* @param removeWhenScopeExits if true, this key value pair will be automatically removed from the MDC when this
* scope exits. If there is not a demonstrable reason for setting this parameter to false
* then it should be set to true.
*/
@Beta
MdcObject put(String key, CustomMdcObject customMdcObject, boolean removeWhenScopeExits);

/**
* Removes the context value identified by the <code>key</code> parameter.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,31 @@ public MdcObject put(String key, String value) {
return new MdcObject(key, value);
}

@Override
public MdcObject put(String key, String value, boolean removeWhenScopeExits) {
return new MdcObject(key, value);
}

@Override
public MdcObject put(String key, Map<String, Object> values) {
return new MdcObject(key, values);
}

@Override
public MdcObject put(String key, Map<String, Object> values, boolean removeWhenScopeExits) {
return new MdcObject(key, values);
}

@Override
public MdcObject put(String key, CustomMdcObject customMdcObject) {
return new MdcObject(key, customMdcObject);
}

@Override
public MdcObject put(String key, CustomMdcObject customMdcObject, boolean removeWhenScopeExits) {
return new MdcObject(key, customMdcObject);
}

@Override
public void remove(String key) {

Expand Down
22 changes: 22 additions & 0 deletions liquibase-standard/src/main/java/liquibase/util/LogUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package liquibase.util;

import liquibase.Scope;
import liquibase.logging.mdc.MdcKey;

public class LogUtil {


/**
* In {@link LiquibaseCommandLine#addEmptyMdcValues()}, baseline values are added to the MDC with empty strings.
* It is desired that {@link MdcKey#CHANGESET_ID}, {@link MdcKey#CHANGESET_AUTHOR} and {@link MdcKey#LIQUIBASE_SYSTEM_USER}
* are not ever cleared from the scope, except when the command finishes executing (or they are replaced with a new
* value). When the command finishes executing, these keys must be set back to an empty string so that incorrect
* values do not persist outside of command execution (like would occur during flow). {@link MdcKey#LIQUIBASE_SYSTEM_USER}
* is purposefully excluded from this method because it does not need to be reset when command execution finishes
* because it should be constant throughout the entire Liquibase execution.
*/
public static void setPersistedMdcKeysToEmptyString() {
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_ID, "", false);
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_AUTHOR, "", false);
}
}
17 changes: 16 additions & 1 deletion liquibase-standard/src/test/groovy/liquibase/ScopeTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,17 @@ class ScopeTest extends Specification {
}

@Override
MdcObject put(String key, Map<String, String> values) {
MdcObject put(String key, String value, boolean removeWhenScopeExits) {
return put(key, (Object) value)
}

@Override
MdcObject put(String key, Map<String, Object> values, boolean removeWhenScopeExits) {
return put(key, (Object) values)
}

@Override
MdcObject put(String key, Map<String, Object> values) {
return put(key, (Object) values)
}

Expand All @@ -144,6 +154,11 @@ class ScopeTest extends Specification {
return put(key, (Object) customMdcObject)
}

@Override
MdcObject put(String key, CustomMdcObject customMdcObject, boolean removeWhenScopeExits) {
return put(key, (Object) customMdcObject)
}

MdcObject put(String key, Object object) {
this.values.put(key, object)
return new MdcObject(key, object)
Expand Down

0 comments on commit de976e5

Please sign in to comment.