Skip to content

Commit 4a71e50

Browse files
omidtavakoliomid.tavakoli
andauthored
Logging improvement
* parameterized logging defined * execution time logged * sauron buildID logged * pipeline service logging and metrics exposing updated * comments updated * removed atomic reference * removed spare variable --------- Co-authored-by: omid.tavakoli <[email protected]>
1 parent c4b255b commit 4a71e50

File tree

6 files changed

+182
-70
lines changed

6 files changed

+182
-70
lines changed

sauron-core/src/main/java/com/freenow/sauron/model/DataSet.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ public Map<String, Object> copyAdditionalInformation()
9494
}
9595

9696

97+
@Override
98+
public String toString()
99+
{
100+
try
101+
{
102+
return toJson();
103+
}
104+
catch (JsonProcessingException e)
105+
{
106+
return "DataSet{" +
107+
"serviceName='" + serviceName + '\'' +
108+
", commitId='" + commitId + '\'' +
109+
", additionalInformation=" + additionalInformation +
110+
'}';
111+
}
112+
}
113+
114+
97115
public String toJson() throws JsonProcessingException
98116
{
99117
ObjectMapper jsonMapper = new ObjectMapper();

sauron-service/src/main/java/com/freenow/sauron/controller/PipelineController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ public ResponseEntity<Void> build(@Valid @RequestBody BuildRequest request)
3939
pipelineService.publish(request);
4040
return ResponseEntity.ok().build();
4141
}
42-
}
42+
}
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
package com.freenow.sauron.properties;
22

33
import java.util.Collections;
4-
import java.util.HashMap;
54
import java.util.List;
5+
import java.util.Map;
6+
import lombok.Getter;
7+
import lombok.Setter;
68
import org.springframework.boot.context.properties.ConfigurationProperties;
79

8-
@ConfigurationProperties("sauron.pipelines")
9-
public class PipelineConfigurationProperties extends HashMap<String, List<String>>
10+
@Getter
11+
@Setter
12+
@ConfigurationProperties(prefix = "sauron")
13+
public class PipelineConfigurationProperties
1014
{
15+
//A map of pipeline names to the list of plugin IDs that comprise them.Spring Boot will bind properties like `sauron.pipelines.default` into this map.
16+
private Map<String, List<String>> pipelines = Collections.emptyMap();
17+
18+
//The ID of the plugin that must be executed at the end of a user-defined pipeline run.
19+
private String mandatoryOutputPlugin = "elasticsearch-output";
20+
1121
public List<String> getDefaultPipeline()
1222
{
1323
return getPipeline("default");
1424
}
1525

16-
1726
public List<String> getPipeline(String pipeline)
1827
{
19-
return this.getOrDefault(pipeline, Collections.emptyList());
28+
return pipelines.getOrDefault(pipeline, Collections.emptyList());
2029
}
2130
}

sauron-service/src/main/java/com/freenow/sauron/service/PipelineService.java

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.freenow.sauron.plugins.SauronExtension;
88
import com.freenow.sauron.properties.PipelineConfigurationProperties;
99
import com.freenow.sauron.properties.PluginsConfigurationProperties;
10+
import io.micrometer.core.instrument.MeterRegistry;
11+
import io.micrometer.core.instrument.Timer;
1012
import java.util.List;
1113
import lombok.extern.slf4j.Slf4j;
1214
import org.apache.commons.lang3.StringUtils;
@@ -21,8 +23,6 @@
2123
@EnableConfigurationProperties({PipelineConfigurationProperties.class, PluginsConfigurationProperties.class})
2224
public class PipelineService
2325
{
24-
private static final String ELASTICSEARCH_OUTPUT_PLUGIN = "elasticsearch-output";
25-
2626
private final PipelineConfigurationProperties pipelineProperties;
2727

2828
private final PluginsConfigurationProperties pluginsProperties;
@@ -31,18 +31,22 @@ public class PipelineService
3131

3232
private final RequestHandler handler;
3333

34+
private final MeterRegistry meterRegistry;
35+
3436

3537
@Autowired
3638
public PipelineService(
3739
PluginManager pluginManager,
3840
PipelineConfigurationProperties pipelineProperties,
3941
PluginsConfigurationProperties pluginsProperties,
40-
RequestHandler handler)
42+
RequestHandler handler,
43+
MeterRegistry meterRegistry)
4144
{
4245
this.pluginManager = pluginManager;
4346
this.pipelineProperties = pipelineProperties;
4447
this.pluginsProperties = pluginsProperties;
4548
this.handler = handler;
49+
this.meterRegistry = meterRegistry;
4650
handler.setConsumer(this::process);
4751
}
4852

@@ -51,6 +55,7 @@ public void publish(BuildRequest request)
5155
{
5256
try
5357
{
58+
log.info("Received request to publish: serviceName={}, commitId={}", request.getServiceName(), request.getCommitId());
5459
handler.handle(request);
5560
}
5661
catch (Exception ex)
@@ -64,68 +69,103 @@ void process(BuildRequest request)
6469
{
6570
try
6671
{
67-
final DataSet dataSet = BuildMapper.makeDataSet(request);
72+
log.info("Starting processing for request: serviceName={}, commitId={}", request.getServiceName(), request.getCommitId());
73+
DataSet dataSet = BuildMapper.makeDataSet(request);
74+
log.debug("Initial DataSet created from request: {}", dataSet);
6875
String plugin = request.getPlugin();
6976

7077
if (StringUtils.isNotBlank(plugin))
7178
{
7279
plugin = StringUtils.lowerCase(request.getPlugin());
7380
final List<String> defaultPipeline = pipelineProperties.getDefaultPipeline();
7481

75-
log.debug("Running user defined pipeline.");
82+
log.debug("User-defined plugin specified: {}. Running user defined pipeline. Default pipeline plugins: {}", plugin, defaultPipeline);
7683

7784
if (defaultPipeline.contains(plugin))
7885
{
79-
runDependencies(request, dataSet, plugin, defaultPipeline);
86+
log.debug("User-defined plugin '{}' is part of the default pipeline. Running dependencies first.", plugin);
87+
runDependencies(dataSet, plugin, defaultPipeline);
8088
}
8189

82-
runPlugin(plugin, request, dataSet);
83-
runPlugin(ELASTICSEARCH_OUTPUT_PLUGIN, request, dataSet);
90+
log.debug("Executing user-defined plugin: {}", plugin);
91+
runPlugin(plugin, dataSet);
92+
93+
String mandatoryOutputPlugin = pipelineProperties.getMandatoryOutputPlugin();
94+
if (StringUtils.isNotBlank(mandatoryOutputPlugin)) {
95+
log.debug("Executing mandatory output plugin: {}", mandatoryOutputPlugin);
96+
runPlugin(mandatoryOutputPlugin, dataSet);
97+
}
8498
}
8599
else
86100
{
87-
log.debug("Running default pipeline.");
88-
pipelineProperties.getDefaultPipeline().forEach(pluginId -> runPlugin(pluginId, request, dataSet));
101+
log.debug("No user-defined plugin. Running default pipeline. Default pipeline plugins: {}", pipelineProperties.getDefaultPipeline());
102+
pipelineProperties.getDefaultPipeline().forEach(pluginId -> runPlugin(pluginId, dataSet));
89103
}
90104
}
91105
catch (final Exception ex)
92106
{
93-
log.error(String.format("Error loading plugins: %s", ex.getMessage()), ex);
107+
log.error("Error processing request for serviceName={}, commitId={}", request.getServiceName(), request.getCommitId(), ex);
94108
}
95109
}
96110

97111

98112
private void runDependencies(
99-
final BuildRequest request, final DataSet dataSet,
100-
final String plugin, final List<String> defaultPipeline)
113+
DataSet dataSet, final String plugin, final List<String> defaultPipeline)
101114
{
102115
for (final String defaultPipelinePlugin : defaultPipeline)
103116
{
104117
if (StringUtils.equals(plugin, defaultPipelinePlugin))
105118
{
119+
log.debug("Dependency plugin '{}' is the main plugin '{}', skipping further dependencies.", defaultPipelinePlugin, plugin);
106120
return;
107121
}
108122

109-
runPlugin(defaultPipelinePlugin, request, dataSet);
123+
log.debug("Running dependency plugin: {} for main plugin: {}", defaultPipelinePlugin, plugin);
124+
runPlugin(defaultPipelinePlugin, dataSet);
110125
}
111126
}
112127

113128

114-
void runPlugin(String plugin, BuildRequest request, DataSet dataSet)
129+
void runPlugin(String plugin, DataSet dataSet)
115130
{
116-
pluginManager.getExtensions(SauronExtension.class, plugin).forEach(pluginExtension -> {
131+
for (SauronExtension pluginExtension : pluginManager.getExtensions(SauronExtension.class, plugin))
132+
{
117133
try
118134
{
119-
log.debug(String.format("Applying pluginId: %s. Processing service %s - %s", plugin, request.getServiceName(), request.getCommitId()));
120135
MDC.put("sauron.pluginId", plugin);
121-
MDC.put("sauron.serviceName", request.getServiceName());
122-
MDC.put("sauron.commitId", request.getCommitId());
123-
pluginExtension.apply(pluginsProperties, dataSet);
136+
MDC.put("sauron.serviceName", dataSet.getServiceName());
137+
MDC.put("sauron.commitId", dataSet.getCommitId());
138+
MDC.put("sauron.buildId", dataSet.getBuildId());
139+
log.debug("Applying pluginId: {}. Processing service {} - {}. DataSet BEFORE plugin execution: {}", plugin, dataSet.getServiceName(), dataSet.getCommitId(),
140+
dataSet);
141+
142+
getTimerBuilder("sauron.plugin.execution.time")
143+
.tag("plugin", plugin)
144+
.tag("service", dataSet.getServiceName())
145+
.tag("commit", dataSet.getCommitId())
146+
.register(meterRegistry).record(() -> pluginExtension.apply(pluginsProperties, dataSet));
147+
148+
meterRegistry.counter("sauron.plugin.executions.total", "plugin", plugin, "result", "success").increment();
149+
log.debug("PluginId: {} applied. Processing service {} - {}. DataSet AFTER plugin execution: {}", plugin, dataSet.getServiceName(), dataSet.getCommitId(), dataSet);
124150
}
125151
catch (final Exception ex)
126152
{
127-
log.error(String.format("Error processing pipeline: %s:%s. %s", request.getServiceName(), request.getCommitId(), ex.getMessage()), ex);
153+
meterRegistry.counter("sauron.plugin.executions.total", "plugin", plugin, "result", "failure").increment();
154+
log.error("Error in plugin '{}' for serviceName={}, commitId={}. DataSet at time of failure: {}", plugin, dataSet.getServiceName(), dataSet.getCommitId(), dataSet, ex);
155+
}
156+
finally
157+
{
158+
MDC.remove("sauron.pluginId");
159+
MDC.remove("sauron.serviceName");
160+
MDC.remove("sauron.commitId");
161+
MDC.remove("sauron.buildId");
128162
}
129-
});
163+
}
164+
}
165+
166+
167+
Timer.Builder getTimerBuilder(String name)
168+
{
169+
return Timer.builder(name);
130170
}
131171
}

0 commit comments

Comments
 (0)