Skip to content

Commit

Permalink
Add application ID in the JSON logs
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Aug 15, 2023
1 parent f4c071f commit 1956a96
Show file tree
Hide file tree
Showing 103 changed files with 674 additions and 238 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ configure(subprojects.findAll { ['core', 'examples'].contains(it.name) }) {
tasks.register('violations', ViolationsTask) {
minSeverity = 'INFO'
detailLevel = 'VERBOSE' // PER_FILE_COMPACT, COMPACT or VERBOSE
maxViolations = 1064
maxViolations = 1066
printViolations = true
violations = [
["FINDBUGS", ".", ".*/reports/spotbugsReports/.*\\.xml", "Spotbugs"],
Expand Down
15 changes: 12 additions & 3 deletions core/src/main/java/org/mapfish/print/MapPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.annotation.Nonnull;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
Expand Down Expand Up @@ -116,18 +117,26 @@ public final OutputFormat getOutputFormat(final PJsonObject specJson) {
/**
* Start a print.
*
* @param jobId the job ID
* @param mdcContext the MDC context for the current print job.
* @param specJson the client json request.
* @param out the stream to write to.
*/
public final Processor.ExecutionContext print(
final String jobId, final PJsonObject specJson, final OutputStream out) throws Exception {
@Nonnull final Map<String, String> mdcContext,
final PJsonObject specJson,
final OutputStream out)
throws Exception {
final OutputFormat format = getOutputFormat(specJson);
final File taskDirectory = this.workingDirectories.getTaskDirectory();

try {
return format.print(
jobId, specJson, getConfiguration(), this.configFile.getParentFile(), taskDirectory, out);
mdcContext,
specJson,
getConfiguration(),
this.configFile.getParentFile(),
taskDirectory,
out);
} finally {
this.workingDirectories.removeDirectory(taskDirectory);
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/mapfish/print/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.json.JSONWriter;
Expand Down Expand Up @@ -196,7 +197,7 @@ private void run(final CliDefinition cli) throws Exception {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Request Data: \n{}", jsonSpec.getInternalObj().toString(2));
}
this.mapPrinter.print("main", jsonSpec, outFile);
this.mapPrinter.print(new HashMap<String, String>(), jsonSpec, outFile);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.URI;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -35,7 +36,7 @@ public final class ConfigFileResolvingHttpRequestFactory implements MfClientHttp
private static final Logger LOGGER =
LoggerFactory.getLogger(ConfigFileResolvingHttpRequestFactory.class);
private final Configuration config;
private final String jobId;
@Nonnull private final Map<String, String> mdcContext;
private final MfClientHttpRequestFactoryImpl httpRequestFactory;
private final List<RequestConfigurator> callbacks = new CopyOnWriteArrayList<>();

Expand All @@ -44,15 +45,15 @@ public final class ConfigFileResolvingHttpRequestFactory implements MfClientHttp
*
* @param httpRequestFactory basic request factory
* @param config the template for the current print job.
* @param jobId the job ID
* @param mdcContext the mdc context for the current print job.
*/
public ConfigFileResolvingHttpRequestFactory(
final MfClientHttpRequestFactoryImpl httpRequestFactory,
final Configuration config,
final String jobId) {
@Nonnull final Map<String, String> mdcContext) {
this.httpRequestFactory = httpRequestFactory;
this.config = config;
this.jobId = jobId;
this.mdcContext = mdcContext;
}

@Override
Expand Down Expand Up @@ -91,19 +92,30 @@ private synchronized ClientHttpRequest createRequestFromWrapped(final HttpHeader
httpRequest.setConfiguration(ConfigFileResolvingHttpRequestFactory.this.config);

httpRequest.getHeaders().putAll(headers);
httpRequest
.getHeaders()
.set("X-Request-ID", ConfigFileResolvingHttpRequestFactory.this.jobId);
if (ConfigFileResolvingHttpRequestFactory.this.mdcContext.containsKey(
Processor.MDC_JOB_ID_KEY)) {
String jobId =
ConfigFileResolvingHttpRequestFactory.this.mdcContext.get(Processor.MDC_JOB_ID_KEY);
httpRequest.getHeaders().set("X-Request-ID", jobId);
httpRequest.getHeaders().set("X-Job-ID", jobId);
}
if (ConfigFileResolvingHttpRequestFactory.this.mdcContext.containsKey(
Processor.MDC_APPLICATION_ID_KEY)) {
String applicationId =
ConfigFileResolvingHttpRequestFactory.this.mdcContext.get(
Processor.MDC_APPLICATION_ID_KEY);
httpRequest.getHeaders().set("X-Application-ID", applicationId);
}
return httpRequest;
}

@Override
protected synchronized ClientHttpResponse executeInternal(final HttpHeaders headers)
throws IOException {
final String prev = MDC.get(Processor.MDC_JOB_ID_KEY);
boolean mdcChanged = prev == null || jobId.equals(prev);
final Map<String, String> prev = MDC.getCopyOfContextMap();
boolean mdcChanged = mdcContext.equals(prev);
if (mdcChanged) {
MDC.put(Processor.MDC_JOB_ID_KEY, ConfigFileResolvingHttpRequestFactory.this.jobId);
MDC.setContextMap(ConfigFileResolvingHttpRequestFactory.this.mdcContext);
}
try {
if (this.request != null) {
Expand Down Expand Up @@ -144,11 +156,7 @@ protected synchronized ClientHttpResponse executeInternal(final HttpHeaders head
return executeCallbacksAndRequest(createRequestFromWrapped(headers));
} finally {
if (mdcChanged) {
if (prev != null) {
MDC.put(Processor.MDC_JOB_ID_KEY, prev);
} else {
MDC.remove(Processor.MDC_JOB_ID_KEY);
}
MDC.setContextMap(prev);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ protected abstract void doExport(OutputStream outputStream, Print print)

@Override
public final Processor.ExecutionContext print(
final String jobId,
@Nonnull final Map<String, String> mdcContext,
final PJsonObject requestData,
final Configuration config,
final File configDir,
final File taskDirectory,
final OutputStream outputStream)
throws Exception {
final Print print = getJasperPrint(jobId, requestData, config, configDir, taskDirectory);
final Print print = getJasperPrint(mdcContext, requestData, config, configDir, taskDirectory);

if (Thread.currentThread().isInterrupted()) {
throw new CancellationException();
Expand All @@ -108,7 +108,7 @@ private JasperFillManager getJasperFillManager(
/**
* Renders the jasper report.
*
* @param jobId the job ID
* @param mdcContext the MDC context for the current print job.
* @param requestData the data from the client, required for writing.
* @param config the configuration object representing the server side configuration.
* @param configDir the directory that contains the configuration, used for resolving resources
Expand All @@ -119,7 +119,7 @@ private JasperFillManager getJasperFillManager(
*/
@VisibleForTesting
public final Print getJasperPrint(
final String jobId,
@Nonnull final Map<String, String> mdcContext,
final PJsonObject requestData,
final Configuration config,
final File configDir,
Expand All @@ -138,7 +138,7 @@ public final Print getJasperPrint(

final Values values =
new Values(
jobId,
mdcContext,
requestData,
template,
taskDirectory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import javax.annotation.Nonnull;
import org.apache.commons.io.IOUtils;
import org.mapfish.print.Constants;
import org.mapfish.print.config.Configuration;
Expand Down Expand Up @@ -73,7 +75,7 @@ private String getMapSubReportVariable(final Template template) {

@Override
public final Processor.ExecutionContext print(
final String jobId,
@Nonnull final Map<String, String> mdcContext,
final PJsonObject spec,
final Configuration config,
final File configDir,
Expand All @@ -86,7 +88,13 @@ public final Processor.ExecutionContext print(

final Values values =
new Values(
jobId, spec, template, taskDirectory, this.httpRequestFactory, null, this.fileSuffix);
mdcContext,
spec,
template,
taskDirectory,
this.httpRequestFactory,
null,
this.fileSuffix);

final ProcessorDependencyGraph.ProcessorGraphForkJoinTask task =
template.getProcessorGraph().createTask(values);
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/org/mapfish/print/output/OutputFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.File;
import java.io.OutputStream;
import java.util.Map;
import javax.annotation.Nonnull;
import org.mapfish.print.config.Configuration;
import org.mapfish.print.processor.Processor;
import org.mapfish.print.wrapper.json.PJsonObject;
Expand All @@ -21,7 +23,7 @@ public interface OutputFormat {
/**
* Performs the print and writes to the report in the correct format to the outputStream.
*
* @param jobId the job ID
* @param mdcContext the MDC context for the current print job.
* @param spec the data from the client, required for writing.
* @param config the configuration object representing the server side configuration.
* @param configDir the directory that contains the configuration, used for resolving resources
Expand All @@ -30,7 +32,7 @@ public interface OutputFormat {
* @param outputStream the stream to write the result to
*/
Processor.ExecutionContext print(
String jobId,
@Nonnull Map<String, String> mdcContext,
PJsonObject spec,
Configuration config,
File configDir,
Expand Down
36 changes: 25 additions & 11 deletions core/src/main/java/org/mapfish/print/output/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public final class Values {
/** The key for the values object for the subreport directory. */
public static final String SUBREPORT_DIR_KEY = "SUBREPORT_DIR";

/** The key for the reference ID. */
public static final String JOB_ID_KEY = "jobId";
/** The key for the MDC context of the current print job. */
public static final String MDC_CONTEXT_KEY = "mdcContext";

/** The key for the values object of it self. */
public static final String VALUES_KEY = "values";
Expand All @@ -79,28 +79,34 @@ public Values() {
/**
* Construct from the json request body and the associated template.
*
* @param jobId the job ID
* @param mdcContext the MDC context
* @param requestData the json request data
* @param template the template
* @param taskDirectory the temporary directory for this printing task.
* @param httpRequestFactory a factory for making http requests.
* @param jasperTemplateBuild the directory where the jasper templates are compiled to
*/
public Values(
final String jobId,
@Nonnull final Map<String, String> mdcContext,
final PJsonObject requestData,
final Template template,
final File taskDirectory,
final MfClientHttpRequestFactoryImpl httpRequestFactory,
final File jasperTemplateBuild) {
this(
jobId, requestData, template, taskDirectory, httpRequestFactory, jasperTemplateBuild, null);
mdcContext,
requestData,
template,
taskDirectory,
httpRequestFactory,
jasperTemplateBuild,
null);
}

/**
* Construct from the json request body and the associated template.
*
* @param jobId the job ID
* @param mdcContext the MDC context
* @param requestData the json request data
* @param template the template
* @param taskDirectory the temporary directory for this printing task.
Expand All @@ -110,7 +116,7 @@ public Values(
*/
// CHECKSTYLE:OFF
public Values(
final String jobId,
@Nonnull final Map<String, String> mdcContext,
final PJsonObject requestData,
final Template template,
final File taskDirectory,
Expand All @@ -126,7 +132,7 @@ public Values(
CLIENT_HTTP_REQUEST_FACTORY_KEY,
new MfClientHttpRequestFactoryProvider(
new ConfigFileResolvingHttpRequestFactory(
httpRequestFactory, template.getConfiguration(), jobId)));
httpRequestFactory, template.getConfiguration(), mdcContext)));
this.values.put(TEMPLATE_KEY, template);
this.values.put(PDF_CONFIG_KEY, template.getPdfConfig());
if (jasperTemplateBuild != null) {
Expand All @@ -141,7 +147,7 @@ public Values(
Map<String, Attribute> attributes = new HashMap<>(template.getAttributes());
populateFromAttributes(template, attributes, jsonAttributes);

this.values.put(JOB_ID_KEY, jobId);
this.values.put(MDC_CONTEXT_KEY, mdcContext);

this.values.put(VALUES_KEY, this);

Expand Down Expand Up @@ -263,7 +269,7 @@ public void addRequiredValues(@Nonnull final Values sourceValues) {
this.values.put(PDF_CONFIG_KEY, pdfConfig);
this.values.put(SUBREPORT_DIR_KEY, subReportDir);
this.values.put(VALUES_KEY, this);
this.values.put(JOB_ID_KEY, sourceValues.getString(JOB_ID_KEY));
this.values.put(MDC_CONTEXT_KEY, sourceValues.getStringMap(MDC_CONTEXT_KEY));
this.values.put(LOCALE_KEY, sourceValues.getObject(LOCALE_KEY, Locale.class));
}

Expand Down Expand Up @@ -319,7 +325,7 @@ public Integer getInteger(final String key) {
}

/**
* Get a value as a string.
* Get a value as a object.
*
* @param key the key for looking up the value.
* @param type the type of the object
Expand All @@ -330,6 +336,14 @@ public <V> V getObject(final String key, final Class<V> type) {
return type.cast(obj);
}

/**
* Get a value as a Map from String to String.
*
* @param key the key for looking up the value.
*/
public Map<String, String> getStringMap(final String key) {
return (Map<String, String>) this.values.get(key);
}
/**
* Return true if the identified value is present in this values.
*
Expand Down
Loading

0 comments on commit 1956a96

Please sign in to comment.