Skip to content

Commit 2f0f78a

Browse files
committed
Updating task metrics to micrometer 1.10
Changes include: * Use Observable instead of metrics * Update the setting of lowCardinalityTag to the correct events. * Update the docs * Remove EXTERNAL_EXECUTION_ID * Utilize registry provided by boot instead of globalregistry * Set the handler until Boot does this as shown on line 80 of TaskLifecycleConfiguration. * Uses Observability long task timer Updated to latest changes in Micrometer Observability Updated based on code review and api change Finished removing the metrics registry Updated based on code review Readded cloud foundry keys to observations Renamed metrics to observations where applicable Updated metric sample to be observation sample Updated to set defaults and migrate tests to infrastructure Updated based on code review Remove duplicate keyvalues from task observation Added DocumentedObservation to create the observation Migrated code to use the LowerCardinality KeyNames from the DocumentedObservation Test cleanup Updated code based on code review Exception is handled by the error in the observation vs logging it as a task metric key value Updated to use ObservationConvention Updated to handle user defined convention vs default Removed the snapshot versions of observations Removed unnecessary dependencies Checkpoint tests fail after re-adding 1.10-SNAPSHOT Replaced TimeObservationHandler with DefaultMeterObservationHandler Updated to set micrometer to latest milestone Updated to rename TaskValuesProvider variables to ObservationConvention
1 parent ceb504b commit 2f0f78a

38 files changed

+1031
-825
lines changed

docs/src/main/asciidoc/features.adoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,21 @@ In these cases the context is held open because a thread has been allocated
448448
set the `spring.cloud.task.closecontextEnabled` property to `true` when launching your task.
449449
This will close the application's context once the task is complete.
450450
Thus allowing the application to terminate.
451+
452+
[[enable-task-metrics]]
453+
=== Enable Task Metrics
454+
Spring Cloud Task integrates with Micrometer and creates observations for the Tasks it executes.
455+
To enable Task Observability integration, you must add `spring-boot-starter-actuator`, your preferred registry implementation (if you want to publish metrics), and micrometer-tracing (if you want to publish tracing data) to your task application.
456+
An example maven set of dependencies to enable task observability and metrics using Influx would be:
457+
458+
[source,xml]
459+
<dependency>
460+
<groupId>org.springframework.boot</groupId>
461+
<artifactId>spring-boot-starter-actuator</artifactId>
462+
</dependency>
463+
<dependency>
464+
<groupId>io.micrometer</groupId>
465+
<artifactId>micrometer-registry-influx</artifactId>
466+
<scope>runtime</scope>
467+
</dependency>
468+

spring-cloud-task-core/pom.xml

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
<packaging>jar</packaging>
1414
<name>Spring Cloud Task Core</name>
1515
<description>Spring Cloud Task</description>
16-
<properties>
17-
<jmock-version>1.49</jmock-version>
18-
</properties>
1916

2017
<dependencies>
2118
<dependency>
@@ -123,29 +120,23 @@
123120
<artifactId>micrometer-observation</artifactId>
124121
</dependency>
125122
<dependency>
126-
<groupId>org.junit.jupiter</groupId>
127-
<artifactId>junit-jupiter-params</artifactId>
128-
<scope>test</scope>
129-
</dependency>
130-
<dependency>
131-
<groupId>org.junit.jupiter</groupId>
132-
<artifactId>junit-jupiter-engine</artifactId>
123+
<groupId>io.micrometer</groupId>
124+
<artifactId>micrometer-test</artifactId>
133125
<scope>test</scope>
134126
</dependency>
135127
<dependency>
136-
<groupId>org.jmockit</groupId>
137-
<artifactId>jmockit</artifactId>
138-
<version>${jmock-version}</version>
128+
<groupId>io.micrometer</groupId>
129+
<artifactId>micrometer-observation-test</artifactId>
139130
<scope>test</scope>
140131
</dependency>
141132
<dependency>
142-
<groupId>io.micrometer</groupId>
143-
<artifactId>micrometer-observation-test</artifactId>
133+
<groupId>org.junit.jupiter</groupId>
134+
<artifactId>junit-jupiter-params</artifactId>
144135
<scope>test</scope>
145136
</dependency>
146137
<dependency>
147-
<groupId>io.micrometer</groupId>
148-
<artifactId>micrometer-test</artifactId>
138+
<groupId>org.junit.jupiter</groupId>
139+
<artifactId>junit-jupiter-engine</artifactId>
149140
<scope>test</scope>
150141
</dependency>
151142
<dependency>

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/MetricsAutoConfiguration.java

Lines changed: 0 additions & 136 deletions
This file was deleted.

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/SimpleTaskAutoConfiguration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.context.annotation.Bean;
3939
import org.springframework.context.annotation.Configuration;
4040
import org.springframework.context.annotation.Lazy;
41+
import org.springframework.context.annotation.Profile;
4142
import org.springframework.transaction.PlatformTransactionManager;
4243
import org.springframework.transaction.annotation.EnableTransactionManagement;
4344
import org.springframework.util.CollectionUtils;
@@ -113,6 +114,13 @@ public TaskRepositoryInitializer taskRepositoryInitializer() {
113114
return taskRepositoryInitializer;
114115
}
115116

117+
118+
@Bean
119+
@Profile("cloud")
120+
TaskObservationCloudKeyValues taskObservationCloudKeyValues() {
121+
return new TaskObservationCloudKeyValues();
122+
}
123+
116124
/**
117125
* Determines the {@link TaskConfigurer} to use.
118126
*/

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/TaskLifecycleConfiguration.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.cloud.task.configuration;
1818

19+
import io.micrometer.observation.ObservationRegistry;
1920
import jakarta.annotation.PostConstruct;
2021
import org.apache.commons.logging.Log;
2122
import org.apache.commons.logging.LogFactory;
@@ -61,17 +62,27 @@ public class TaskLifecycleConfiguration {
6162

6263
private boolean initialized = false;
6364

65+
private ObservationRegistry observationRegistry;
66+
67+
private TaskObservationCloudKeyValues taskObservationCloudKeyValues;
68+
6469
@Autowired
6570
public TaskLifecycleConfiguration(TaskProperties taskProperties,
66-
ConfigurableApplicationContext context, TaskRepository taskRepository,
67-
TaskExplorer taskExplorer, TaskNameResolver taskNameResolver,
68-
ObjectProvider<ApplicationArguments> applicationArguments) {
71+
ConfigurableApplicationContext context, TaskRepository taskRepository,
72+
TaskExplorer taskExplorer, TaskNameResolver taskNameResolver,
73+
ObjectProvider<ApplicationArguments> applicationArguments,
74+
@Autowired(required = false) ObservationRegistry observationRegistry,
75+
@Autowired(required = false) TaskObservationCloudKeyValues taskObservationCloudKeyValues) {
76+
6977
this.taskProperties = taskProperties;
7078
this.context = context;
7179
this.taskRepository = taskRepository;
7280
this.taskExplorer = taskExplorer;
7381
this.taskNameResolver = taskNameResolver;
7482
this.applicationArguments = applicationArguments.getIfAvailable();
83+
this.observationRegistry = observationRegistry == null ? ObservationRegistry.NOOP : observationRegistry;
84+
this.taskObservationCloudKeyValues = taskObservationCloudKeyValues;
85+
7586
}
7687

7788
@Bean
@@ -88,7 +99,8 @@ protected void initialize() {
8899
this.taskLifecycleListener = new TaskLifecycleListener(this.taskRepository,
89100
this.taskNameResolver, this.applicationArguments, this.taskExplorer,
90101
this.taskProperties,
91-
new TaskListenerExecutorObjectFactory(this.context));
102+
new TaskListenerExecutorObjectFactory(this.context),
103+
this.observationRegistry, taskObservationCloudKeyValues);
92104

93105
this.initialized = true;
94106
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2022-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.task.configuration;
18+
19+
import org.springframework.beans.factory.annotation.Value;
20+
21+
/**
22+
* Provides values for the {@link io.micrometer.common.KeyValues} for the task
23+
* {@link io.micrometer.observation.Observation} when the cloud profile is active.
24+
*
25+
* @author Glenn Renfro
26+
* @since 3.0
27+
*/
28+
public class TaskObservationCloudKeyValues {
29+
30+
@Value("${vcap.application.org_name:default}")
31+
private String organizationName;
32+
33+
@Value("${vcap.application.space_id:unknown}")
34+
private String spaceId;
35+
36+
@Value("${vcap.application.space_name:unknown}")
37+
private String spaceName;
38+
39+
@Value("${vcap.application.application_name:unknown}")
40+
private String applicationName;
41+
42+
@Value("${vcap.application.application_id:unknown}")
43+
private String applicationId;
44+
45+
@Value("${vcap.application.application_version:unknown}")
46+
private String applicationVersion;
47+
48+
@Value("${vcap.application.instance_index:0}")
49+
private String instanceIndex;
50+
51+
public String getOrganizationName() {
52+
return organizationName;
53+
}
54+
55+
public void setOrganizationName(String organizationName) {
56+
this.organizationName = organizationName;
57+
}
58+
59+
public String getSpaceId() {
60+
return spaceId;
61+
}
62+
63+
public void setSpaceId(String spaceId) {
64+
this.spaceId = spaceId;
65+
}
66+
67+
public String getSpaceName() {
68+
return spaceName;
69+
}
70+
71+
public void setSpaceName(String spaceName) {
72+
this.spaceName = spaceName;
73+
}
74+
75+
public String getApplicationName() {
76+
return applicationName;
77+
}
78+
79+
public void setApplicationName(String applicationName) {
80+
this.applicationName = applicationName;
81+
}
82+
83+
public String getApplicationId() {
84+
return applicationId;
85+
}
86+
87+
public void setApplicationId(String applicationId) {
88+
this.applicationId = applicationId;
89+
}
90+
91+
public String getApplicationVersion() {
92+
return applicationVersion;
93+
}
94+
95+
public void setApplicationVersion(String applicationVersion) {
96+
this.applicationVersion = applicationVersion;
97+
}
98+
99+
public String getInstanceIndex() {
100+
return instanceIndex;
101+
}
102+
103+
public void setInstanceIndex(String instanceIndex) {
104+
this.instanceIndex = instanceIndex;
105+
}
106+
}

spring-cloud-task-core/src/main/java/org/springframework/cloud/task/configuration/observation/DefaultTaskObservationConvention.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import io.micrometer.observation.Observation;
2121

2222
/**
23-
* {@link Observation.KeyValuesProvider} for Spring Cloud Task.
23+
* {@link Observation.ObservationConvention} for Spring Cloud Task.
2424
*
2525
* @author Marcin Grzejszczak
2626
* @since 3.0.0

0 commit comments

Comments
 (0)