Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mdc to update command (DAT-12602) #3647

Merged
merged 37 commits into from Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
65eb573
add MDC interfaces and base classes (DAT-12601) (#3567)
StevenMassaro Dec 14, 2022
c327475
Merge branch 'master' into DAT-11693
StevenMassaro Dec 20, 2022
053e02d
set log format in log service
StevenMassaro Dec 19, 2022
a06f822
add deprecated annotation
StevenMassaro Dec 19, 2022
8e248cc
Revert "set log format in log service"
StevenMassaro Dec 19, 2022
4dabdf9
use log formatter for JSON logging
StevenMassaro Dec 20, 2022
dfa312d
add MDC to structured log output
StevenMassaro Dec 20, 2022
5533ed0
fix test
StevenMassaro Dec 20, 2022
2c3fa40
Include "path" in databasechangelog's description column for all chan…
MichaelKern-IVV Dec 20, 2022
92182fd
#1466: Add ignore:true changeset attribute to Formatted SQL changeLog…
skrivenko Dec 20, 2022
2077751
Merge branch 'DAT-11693' into DAT-12652
StevenMassaro Dec 21, 2022
598755a
[DAT-12602] Initial update mdc implementation.
abrackx Jan 4, 2023
1faba36
add configuration option for log format
StevenMassaro Jan 5, 2023
4652f25
[DAT-12602] Adds remaining mdc values. Reworks to use static strings …
abrackx Jan 5, 2023
441fa91
Merge branch 'DAT-11693' into DAT-12727
StevenMassaro Jan 5, 2023
fb0a2f1
[DAT-12602] Remove unused keys, fixed formatting.
abrackx Jan 5, 2023
d64500e
Merge branch 'DAT-12727' into DAT-12602
abrackx Jan 5, 2023
07b67ff
[DAT-12602] Adds system information to mdc.
abrackx Jan 6, 2023
c601452
[DAT-12602] Fixes formatting for mdckey.
abrackx Jan 6, 2023
1ceec55
[DAT-12602] Simplifies getting mdc for command name.
abrackx Jan 6, 2023
f04e4a2
updated logFormat description
StevenMassaro Jan 6, 2023
6e9163d
Merge remote-tracking branch 'origin/DAT-11693' into DAT-12602
abrackx Jan 11, 2023
718ca6e
warn about invalid values for log format
StevenMassaro Jan 11, 2023
9691bec
[DAT-12602] Add changeset outcome to info log, fix deployment outcome…
abrackx Jan 11, 2023
57c287b
Merge branch 'DAT-11693' into DAT-12727
abrackx Jan 11, 2023
10d540b
Revert "Merge remote-tracking branch 'origin/DAT-11693' into DAT-12602"
abrackx Jan 11, 2023
8554e29
Merge remote-tracking branch 'origin/DAT-12727' into DAT-12602
abrackx Jan 11, 2023
12f6539
Revert "Merge remote-tracking branch 'origin/DAT-12727' into DAT-12602"
abrackx Jan 11, 2023
ddfe3f3
Revert "Revert "Merge remote-tracking branch 'origin/DAT-11693' into …
abrackx Jan 11, 2023
459428e
Revert "Revert "Merge remote-tracking branch 'origin/DAT-12727' into …
abrackx Jan 11, 2023
4c6a740
[DAT-12602] Fix liquibase system information mdc.
abrackx Jan 11, 2023
d7dea0c
[DAT-12602] Fix logged liquibase version.
abrackx Jan 11, 2023
af19e97
Revert change to build.properties
abrackx Jan 11, 2023
cdf7898
safely cast logformat - fixes using the default value
StevenMassaro Jan 12, 2023
039c58e
[DAT-12602] Adds mdc for failed changeset outcome.
abrackx Jan 12, 2023
c0b6ef5
Merge branch 'DAT-12727' into DAT-12602
abrackx Jan 13, 2023
b1cd72b
Merge branch 'DAT-11693' into DAT-12602
abrackx Jan 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions liquibase-core/src/main/java/liquibase/Liquibase.java
Expand Up @@ -31,6 +31,8 @@
import liquibase.logging.Logger;
import liquibase.logging.core.BufferedLogService;
import liquibase.logging.core.CompositeLogService;
import liquibase.logging.mdc.MdcKey;
import liquibase.logging.mdc.MdcValue;
import liquibase.parser.ChangeLogParser;
import liquibase.parser.ChangeLogParserFactory;
import liquibase.parser.core.xml.XMLChangeLogSAXParser;
Expand Down Expand Up @@ -235,6 +237,8 @@ public void update(Contexts contexts, LabelExpression labelExpression, boolean c
runInScope(() -> {
LockService lockService = LockServiceFactory.getInstance().getLockService(database);
lockService.waitForLock();
Scope.getCurrentScope().addMdcValue(MdcKey.OPERATION_TARGET_VALUE, database.getConnection().getURL());
Scope.getCurrentScope().addMdcValue(MdcKey.OPERATION_TARGET_TYPE, MdcValue.URL_DATABASE_TARGET);

changeLogParameters.setContexts(contexts);
changeLogParameters.setLabels(labelExpression);
Expand All @@ -252,6 +256,8 @@ public void update(Contexts contexts, LabelExpression labelExpression, boolean c
ChangeLogHistoryService changelogService = ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
changelogService.generateDeploymentId();

Scope.getCurrentScope().addMdcValue(MdcKey.DEPLOYMENT_ID, changelogService.getDeploymentId());

changeLog.validate(database, contexts, labelExpression);

//
Expand Down
Expand Up @@ -16,6 +16,7 @@
import liquibase.executor.ExecutorService;
import liquibase.executor.LoggingExecutor;
import liquibase.logging.Logger;
import liquibase.logging.mdc.MdcKey;
import liquibase.parser.ChangeLogParserConfiguration;
import liquibase.parser.core.ParsedNode;
import liquibase.parser.core.ParsedNodeException;
Expand All @@ -27,6 +28,7 @@
import liquibase.sql.visitor.SqlVisitor;
import liquibase.sql.visitor.SqlVisitorFactory;
import liquibase.statement.SqlStatement;
import liquibase.util.ISODateFormat;
import liquibase.util.StreamUtil;
import liquibase.util.StringUtil;

Expand Down Expand Up @@ -569,6 +571,7 @@ public ExecType execute(DatabaseChangeLog databaseChangeLog, ChangeExecListener
}

long startTime = new Date().getTime();
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_OPERATION_START_TIME, new ISODateFormat().format(new Date()));

ExecType execType = null;

Expand Down Expand Up @@ -697,7 +700,8 @@ public ExecType execute(DatabaseChangeLog databaseChangeLog, ChangeExecListener
if (runInTransaction) {
database.commit();
}
log.info("ChangeSet " + toString(false) + " ran successfully in " + (new Date().getTime() - startTime + "ms"));
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_OPERATION_STOP_TIME, new ISODateFormat().format(new Date()));
log.info("ChangeSet " + toString(false) + " ran successfully in " + (new Date().getTime() - startTime) + "ms");
if (execType == null) {
execType = ExecType.EXECUTED;
}
Expand Down
Expand Up @@ -13,6 +13,7 @@
import liquibase.executor.Executor;
import liquibase.executor.ExecutorService;
import liquibase.executor.LoggingExecutor;
import liquibase.logging.mdc.MdcKey;

import java.util.Set;

Expand Down Expand Up @@ -43,6 +44,10 @@ public Direction getDirection() {
@Override
public void visit(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database,
Set<ChangeSetFilterResult> filterResults) throws LiquibaseException {
Scope scope = Scope.getCurrentScope();
scope.addMdcValue(MdcKey.CHANGESET_ID, changeSet.getId());
scope.addMdcValue(MdcKey.CHANGESET_AUTHOR, changeSet.getAuthor());
scope.addMdcValue(MdcKey.CHANGESET_FILEPATH, changeSet.getFilePath());
Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
if (! (executor instanceof LoggingExecutor)) {
Scope.getCurrentScope().getUI().sendMessage("Running Changeset: " + changeSet);
Expand All @@ -61,6 +66,7 @@ public void visit(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Data
if (!runStatus.equals(ChangeSet.RunStatus.NOT_RAN)) {
execType = ChangeSet.ExecType.RERAN;
}
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_OUTCOME, execType.value.toLowerCase());
fireRan(changeSet, databaseChangeLog, database, execType);
// reset object quoting strategy after running changeset
this.database.setObjectQuotingStrategy(previousStr);
Expand Down
Expand Up @@ -11,6 +11,7 @@
import liquibase.executor.AbstractExecutor;
import liquibase.listener.SqlListener;
import liquibase.logging.Logger;
import liquibase.logging.mdc.MdcKey;
import liquibase.servicelocator.PrioritizedService;
import liquibase.sql.CallableSql;
import liquibase.sql.Sql;
Expand Down Expand Up @@ -425,6 +426,7 @@ public Object doInStatement(Statement stmt) throws SQLException, DatabaseExcepti
statement = statement.replaceFirst("[\\s\\r\\n]*[^*]/[\\s\\r\\n]*$", "");
}
}
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_SQL, statement);

for (SqlListener listener : Scope.getCurrentScope().getListeners(SqlListener.class)) {
listener.writeSqlWillRun(String.format("%s", statement));
Expand Down
Expand Up @@ -3,6 +3,7 @@
import liquibase.configuration.AutoloadedConfigurations;
import liquibase.configuration.ConfigurationDefinition;
import liquibase.configuration.ConfigurationValueConverter;
import liquibase.logging.LogFormat;

import java.util.logging.Level;

Expand All @@ -22,6 +23,7 @@ public class LiquibaseCommandLineConfiguration implements AutoloadedConfiguratio
public static final ConfigurationDefinition<Level> LOG_LEVEL;
public static final ConfigurationDefinition<String> LOG_CHANNELS;
public static final ConfigurationDefinition<String> LOG_FILE;
public static final ConfigurationDefinition<LogFormat> LOG_FORMAT;
public static final ConfigurationDefinition<String> OUTPUT_FILE;
public static final ConfigurationDefinition<Boolean> SHOULD_RUN;
public static final ConfigurationDefinition<ArgumentConverter> ARGUMENT_CONVERTER;
Expand Down Expand Up @@ -79,6 +81,13 @@ public class LiquibaseCommandLineConfiguration implements AutoloadedConfiguratio
.setDefaultValue(argument -> argument)
.build();

LOG_FORMAT = builder.define("logFormat", LogFormat.class)
.setDescription("Set the format of log output to console or log files\n" +
"[OSS] Defaults to \"TEXT\" logs to the console or output log files\n" +
"[PRO] Can be set to \"JSON\" to enable json-structured log files to the console or output log files")
.setDefaultValue(LogFormat.TEXT)
.build();

}

public interface ArgumentConverter {
Expand Down
Expand Up @@ -26,12 +26,15 @@
import liquibase.logging.LogService;
import liquibase.logging.Logger;
import liquibase.logging.core.JavaLogService;
import liquibase.logging.mdc.MdcKey;
import liquibase.logging.mdc.MdcValue;
import liquibase.resource.*;
import liquibase.ui.ConsoleUIService;
import liquibase.util.*;

import java.io.*;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
Expand Down Expand Up @@ -324,6 +327,10 @@ public Integer run() throws Exception {
}
}

Scope.getCurrentScope().getMdcManager().put(MdcKey.LIQUIBASE_VERSION, LiquibaseUtil.getBuildVersionInfo());
Scope.getCurrentScope().getMdcManager().put(MdcKey.LIQUIBASE_SYSTEM_USER, System.getProperty("user.name"));
Scope.getCurrentScope().getMdcManager().put(MdcKey.LIQUIBASE_SYSTEM_NAME, InetAddress.getLocalHost().getHostName());

LicenseService licenseService = Scope.getCurrentScope().getSingleton(LicenseServiceFactory.class).getLicenseService();
if (licenseService != null) {
if (main.liquibaseProLicenseKey == null) {
Expand Down Expand Up @@ -393,9 +400,13 @@ public Integer run() throws Exception {
if (!Main.runningFromNewCli) {
innerScopeObjects.put(Scope.Attr.resourceAccessor.name(), new ClassLoaderResourceAccessor(main.configureClassLoader()));
}
Scope.getCurrentScope().getMdcManager().remove(MdcKey.LIQUIBASE_VERSION);
Scope.getCurrentScope().getMdcManager().remove(MdcKey.LIQUIBASE_SYSTEM_NAME);
Scope.getCurrentScope().getMdcManager().remove(MdcKey.LIQUIBASE_SYSTEM_USER);

Scope.child(innerScopeObjects, () -> {
main.doMigration();

Scope.getCurrentScope().addMdcValue(MdcKey.DEPLOYMENT_OUTCOME, MdcValue.COMMAND_SUCCESSFUL);
if (!Main.runningFromNewCli) {
if (COMMANDS.UPDATE.equals(main.command)) {
Scope.getCurrentScope().getUI().sendMessage(coreBundle.getString("update.successful"));
Expand All @@ -407,6 +418,7 @@ public Integer run() throws Exception {
}
});
} catch (Throwable e) {
Scope.getCurrentScope().addMdcValue(MdcKey.DEPLOYMENT_OUTCOME, MdcValue.COMMAND_FAILED);
String message = e.getMessage();
if (e.getCause() != null) {
message = e.getCause().getMessage();
Expand Down Expand Up @@ -1831,7 +1843,9 @@ protected void doMigration() throws Exception {
}

try {
Scope.getCurrentScope().addMdcValue(MdcKey.LIQUIBASE_COMMAND_NAME, command);
if (COMMANDS.UPDATE.equalsIgnoreCase(command)) {
Scope.getCurrentScope().addMdcValue(MdcKey.OPERATION_TYPE, COMMANDS.UPDATE);
try {
liquibase.update(new Contexts(contexts), new LabelExpression(getLabelFilter()));
} catch (LiquibaseException updateException) {
Expand Down
6 changes: 6 additions & 0 deletions liquibase-core/src/main/java/liquibase/logging/LogFormat.java
@@ -0,0 +1,6 @@
package liquibase.logging;

public enum LogFormat {
TEXT,
JSON
}
20 changes: 20 additions & 0 deletions liquibase-core/src/main/java/liquibase/logging/mdc/MdcKey.java
@@ -0,0 +1,20 @@
package liquibase.logging.mdc;

public class MdcKey {
public static final String DEPLOYMENT_ID = "deploymentId";
public static final String CHANGESET_ID = "changesetId";
public static final String CHANGESET_AUTHOR = "changesetAuthor";
public static final String CHANGESET_FILEPATH = "changesetFilepath";
public static final String CHANGESET_OUTCOME = "changesetOutcome";
public static final String OPERATION_TYPE = "liquibaseOperation";
public static final String OPERATION_TARGET_TYPE = "liquibaseTargetType";
public static final String OPERATION_TARGET_VALUE = "liquibaseTarget";
public static final String CHANGESET_OPERATION_START_TIME = "changesetOperationStart";
public static final String CHANGESET_OPERATION_STOP_TIME = "changesetOperationStop";
public static final String CHANGESET_SQL = "changesetSql";
public static final String DEPLOYMENT_OUTCOME = "deploymentOutcome";
public static final String LIQUIBASE_COMMAND_NAME = "liquibaseCommandName";
public static final String LIQUIBASE_VERSION = "liquibaseVersion";
public static final String LIQUIBASE_SYSTEM_NAME = "liquibaseSystemName";
public static final String LIQUIBASE_SYSTEM_USER = "liquibaseSystemUser";
}
@@ -0,0 +1,7 @@
package liquibase.logging.mdc;

public class MdcValue {
public static final String COMMAND_SUCCESSFUL = "success";
public static final String COMMAND_FAILED = "fail";
public static final String URL_DATABASE_TARGET = "url";
}