77import com .freenow .sauron .plugins .SauronExtension ;
88import com .freenow .sauron .properties .PipelineConfigurationProperties ;
99import com .freenow .sauron .properties .PluginsConfigurationProperties ;
10+ import io .micrometer .core .instrument .MeterRegistry ;
11+ import io .micrometer .core .instrument .Timer ;
1012import java .util .List ;
1113import lombok .extern .slf4j .Slf4j ;
1214import org .apache .commons .lang3 .StringUtils ;
2123@ EnableConfigurationProperties ({PipelineConfigurationProperties .class , PluginsConfigurationProperties .class })
2224public 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