18
18
19
19
import java .util .Collection ;
20
20
import java .util .Date ;
21
+ import java .util .List ;
22
+ import java .util .stream .Collectors ;
21
23
22
- import io .micrometer .api .instrument .LongTaskTimer ;
23
- import io .micrometer .api .instrument .Tag ;
24
- import io .micrometer .api .instrument .Timer ;
24
+ import io .micrometer .core .instrument .LongTaskTimer ;
25
+ import io .micrometer .core .instrument .Tag ;
26
+ import io .micrometer .core .instrument .observation . Observation ;
25
27
import org .apache .commons .logging .Log ;
26
28
import org .apache .commons .logging .LogFactory ;
27
29
import org .springframework .batch .core .BatchStatus ;
62
64
* @author Mahmoud Ben Hassine
63
65
*/
64
66
public abstract class AbstractJob implements Job , StepLocator , BeanNameAware ,
65
- InitializingBean {
67
+ InitializingBean , Observation . TagsProviderAware < BatchJobTagsProvider > {
66
68
67
69
protected static final Log logger = LogFactory .getLog (AbstractJob .class );
68
70
@@ -80,6 +82,8 @@ public abstract class AbstractJob implements Job, StepLocator, BeanNameAware,
80
82
81
83
private StepHandler stepHandler ;
82
84
85
+ private BatchJobTagsProvider tagsProvider = new DefaultBatchJobTagsProvider ();
86
+
83
87
/**
84
88
* Default constructor.
85
89
*/
@@ -304,8 +308,11 @@ public final void execute(JobExecution execution) {
304
308
LongTaskTimer longTaskTimer = BatchMetrics .createLongTaskTimer ("job.active" , "Active jobs" ,
305
309
Tag .of ("name" , execution .getJobInstance ().getJobName ()));
306
310
LongTaskTimer .Sample longTaskTimerSample = longTaskTimer .start ();
307
- Timer .Sample timerSample = BatchMetrics .createTimerSample ();
308
- try {
311
+ Observation observation = BatchMetrics .createObservation (BatchJobObservation .BATCH_JOB_OBSERVATION .getName (), new BatchJobContext (execution ))
312
+ .contextualName (execution .getJobInstance ().getJobName ())
313
+ .tagsProvider (this .tagsProvider )
314
+ .start ();
315
+ try (Observation .Scope scope = observation .openScope ()) {
309
316
310
317
jobParametersValidator .validate (execution .getJobParameters ());
311
318
@@ -361,11 +368,7 @@ public final void execute(JobExecution execution) {
361
368
ExitStatus .NOOP .addExitDescription ("All steps already completed or no steps configured for this job." );
362
369
execution .setExitStatus (exitStatus .and (newExitStatus ));
363
370
}
364
-
365
- timerSample .stop (BatchMetrics .createTimer ("job" , "Job duration" ,
366
- Tag .of ("name" , execution .getJobInstance ().getJobName ()),
367
- Tag .of ("status" , execution .getExitStatus ().getExitCode ())
368
- ));
371
+ stopObservation (execution , observation );
369
372
longTaskTimerSample .stop ();
370
373
execution .setEndTime (new Date ());
371
374
@@ -384,6 +387,19 @@ public final void execute(JobExecution execution) {
384
387
385
388
}
386
389
390
+ private void stopObservation (JobExecution execution , Observation observation ) {
391
+ List <Throwable > throwables = execution .getFailureExceptions ();
392
+ if (!throwables .isEmpty ()) {
393
+ observation .error (mergedThrowables (throwables ));
394
+ }
395
+ observation .stop ();
396
+ }
397
+
398
+ private IllegalStateException mergedThrowables (List <Throwable > throwables ) {
399
+ return new IllegalStateException (
400
+ throwables .stream ().map (Throwable ::toString ).collect (Collectors .joining ("\n " )));
401
+ }
402
+
387
403
/**
388
404
* Convenience method for subclasses to delegate the handling of a specific
389
405
* step in the context of the current {@link JobExecution}. Clients of this
@@ -443,6 +459,11 @@ private void updateStatus(JobExecution jobExecution, BatchStatus status) {
443
459
jobRepository .update (jobExecution );
444
460
}
445
461
462
+ @ Override
463
+ public void setTagsProvider (BatchJobTagsProvider tagsProvider ) {
464
+ this .tagsProvider = tagsProvider ;
465
+ }
466
+
446
467
@ Override
447
468
public String toString () {
448
469
return ClassUtils .getShortName (getClass ()) + ": [name=" + name + "]" ;
0 commit comments