Skip to content

Commit fba660b

Browse files
committed
GH-1567 - Streamline observability infrastructure.
Restructured the metrics we publish for domain application events. We now consistently use module.… as prefix for all keys as well as module.identifier instead of module.key. Also, we now register an invocation type "bean" in symmetry with "event-listener". The calculation of all keys and values has been consolidated into ModulithContext. The existing accessors have been kept around for extensibility purposes. See the adapted reference documentation for details. We now propagate the module identifier and name of the first module invocation on a span to the parent request span. Removed local service renaming in favor of better span labels. Made ModulithObservationConventions fully customizable as we now pick up an instance of it from the ApplicationContext and use it in the infrastructure creating metrics and traces.
1 parent 13c3d9f commit fba660b

26 files changed

+549
-306
lines changed

spring-modulith-observability/spring-modulith-observability-api/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@
2828
<artifactId>micrometer-core</artifactId>
2929
</dependency>
3030

31+
<dependency>
32+
<groupId>org.springframework.modulith</groupId>
33+
<artifactId>spring-modulith-core</artifactId>
34+
<version>${project.version}</version>
35+
<optional>true</optional>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework</groupId>
40+
<artifactId>spring-aop</artifactId>
41+
<optional>true</optional>
42+
</dependency>
43+
3144
</dependencies>
3245

3346
</project>

spring-modulith-observability/spring-modulith-observability-core/src/main/java/org/springframework/modulith/observability/support/ModulithContext.java renamed to spring-modulith-observability/spring-modulith-observability-api/src/main/java/org/springframework/modulith/observability/ModulithContext.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.modulith.observability.support;
16+
package org.springframework.modulith.observability;
1717

18+
import io.micrometer.common.KeyValue;
19+
import io.micrometer.common.KeyValues;
1820
import io.micrometer.observation.Observation.Context;
1921

2022
import org.aopalliance.intercept.MethodInvocation;
2123
import org.jspecify.annotations.Nullable;
2224
import org.springframework.core.env.Environment;
25+
import org.springframework.modulith.observability.ModulithObservations.HighKeys;
26+
import org.springframework.modulith.observability.ModulithObservations.LowKeys;
2327
import org.springframework.util.Assert;
2428

2529
/**
@@ -31,6 +35,8 @@
3135
*/
3236
public class ModulithContext extends Context {
3337

38+
public static final String DEFAULT_CONVENTION_NAME = "module.invocations";
39+
3440
private final ObservedModule module;
3541
private final MethodInvocation invocation;
3642
private final @Nullable String applicationName;
@@ -65,4 +71,29 @@ public MethodInvocation getInvocation() {
6571
public @Nullable String getApplicationName() {
6672
return applicationName;
6773
}
74+
75+
public KeyValues getLowCardinalityValues() {
76+
77+
return KeyValues.of(KeyValue.of(LowKeys.MODULE_IDENTIFIER, module.getIdentifier().toString()))
78+
.and(KeyValue.of(LowKeys.MODULE_NAME, module.getDisplayName()))
79+
.and(KeyValue.of(LowKeys.INVOCATION_TYPE, getInvocationType()));
80+
}
81+
82+
public KeyValues getHighCardinalityValues() {
83+
return KeyValues.of(HighKeys.MODULE_METHOD.withValue(invocation.getMethod().getName()));
84+
}
85+
86+
/*
87+
*
88+
* (non-Javadoc)
89+
* @see io.micrometer.observation.Observation.Context#getContextualName()
90+
*/
91+
@Override
92+
public String getContextualName() {
93+
return module.getDisplayName() + " > " + module.format(invocation);
94+
}
95+
96+
private String getInvocationType() {
97+
return module.isEventListenerInvocation(invocation) ? "event-listener" : "bean";
98+
}
6899
}

spring-modulith-observability/spring-modulith-observability-core/src/main/java/org/springframework/modulith/observability/support/ModulithMetrics.java renamed to spring-modulith-observability/spring-modulith-observability-api/src/main/java/org/springframework/modulith/observability/ModulithMetrics.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.modulith.observability.support;
16+
package org.springframework.modulith.observability;
1717

1818
import io.micrometer.common.docs.KeyName;
1919
import io.micrometer.core.instrument.Meter;
@@ -24,7 +24,7 @@
2424
* @author Oliver Drotbohm
2525
* @since 1.4
2626
*/
27-
enum ModulithMetrics implements MeterDocumentation {
27+
public enum ModulithMetrics implements MeterDocumentation {
2828

2929
/**
3030
* Counter for the events.
@@ -37,7 +37,7 @@ enum ModulithMetrics implements MeterDocumentation {
3737
*/
3838
@Override
3939
public String getName() {
40-
return "modulith.events.processed";
40+
return "module.events.published";
4141
}
4242

4343
/*
@@ -59,7 +59,8 @@ public KeyName[] getKeyNames() {
5959
}
6060
};
6161

62-
enum LowKeys implements KeyName {
62+
public enum LowKeys implements KeyName {
63+
6364
/**
6465
* Type of the emitted event.
6566
*/
@@ -71,22 +72,22 @@ enum LowKeys implements KeyName {
7172
*/
7273
@Override
7374
public String asString() {
74-
return "event.type";
75+
return "module.event.type";
7576
}
7677
},
7778

7879
/**
79-
* Name of the module.
80+
* The identifier of the module.
8081
*/
81-
MODULE_KEY {
82+
MODULE_IDENTIFIER {
8283

8384
/*
8485
* (non-Javadoc)
8586
* @see io.micrometer.common.docs.KeyName#asString()
8687
*/
8788
@Override
8889
public String asString() {
89-
return "module.key";
90+
return "module.identifier";
9091
}
9192
},
9293

spring-modulith-observability/spring-modulith-observability-core/src/main/java/org/springframework/modulith/observability/support/ModulithObservationConvention.java renamed to spring-modulith-observability/spring-modulith-observability-api/src/main/java/org/springframework/modulith/observability/ModulithObservationConvention.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.modulith.observability.support;
16+
package org.springframework.modulith.observability;
1717

1818
import io.micrometer.observation.Observation.Context;
1919
import io.micrometer.observation.ObservationConvention;

spring-modulith-observability/spring-modulith-observability-core/src/main/java/org/springframework/modulith/observability/support/ModulithObservations.java renamed to spring-modulith-observability/spring-modulith-observability-api/src/main/java/org/springframework/modulith/observability/ModulithObservations.java

Lines changed: 23 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,19 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.modulith.observability.support;
16+
package org.springframework.modulith.observability;
1717

1818
import io.micrometer.common.docs.KeyName;
19-
import io.micrometer.observation.Observation;
20-
import io.micrometer.observation.Observation.Context;
2119
import io.micrometer.observation.Observation.Event;
22-
import io.micrometer.observation.ObservationConvention;
2320
import io.micrometer.observation.docs.ObservationDocumentation;
2421

25-
enum ModulithObservations implements ObservationDocumentation {
22+
public enum ModulithObservations implements ObservationDocumentation {
2623

2724
/**
2825
* Observation related to entering a module.
2926
*/
3027
MODULE_ENTRY {
3128

32-
/*
33-
* (non-Javadoc)
34-
* @see io.micrometer.observation.docs.ObservationDocumentation#getDefaultConvention()
35-
*/
36-
@Override
37-
public Class<? extends ObservationConvention<? extends Context>> getDefaultConvention() {
38-
return DefaultModulithObservationConvention.class;
39-
}
40-
4129
/*
4230
* (non-Javadoc)
4331
* @see io.micrometer.observation.docs.ObservationDocumentation#getLowCardinalityKeyNames()
@@ -61,25 +49,40 @@ public KeyName[] getHighCardinalityKeyNames() {
6149
* @see io.micrometer.observation.docs.ObservationDocumentation#getEvents()
6250
*/
6351
@Override
64-
public Observation.Event[] getEvents() {
65-
return Events.values();
52+
public Event[] getEvents() {
53+
return new Event[0];
6654
}
6755
};
6856

69-
enum LowKeys implements KeyName {
57+
public enum LowKeys implements KeyName {
58+
59+
/**
60+
* The identifier of the module.
61+
*/
62+
MODULE_IDENTIFIER {
63+
64+
/*
65+
* (non-Javadoc)
66+
* @see io.micrometer.common.docs.KeyName#asString()
67+
*/
68+
@Override
69+
public String asString() {
70+
return "module.identifier";
71+
}
72+
},
7073

7174
/**
7275
* Name of the module.
7376
*/
74-
MODULE_KEY {
77+
MODULE_NAME {
7578

7679
/*
7780
* (non-Javadoc)
7881
* @see io.micrometer.common.docs.KeyName#asString()
7982
*/
8083
@Override
8184
public String asString() {
82-
return "module.key";
85+
return "module.name";
8386
}
8487
},
8588

@@ -99,7 +102,7 @@ public String asString() {
99102
}
100103
}
101104

102-
enum HighKeys implements KeyName {
105+
public enum HighKeys implements KeyName {
103106
/**
104107
* Method executed on a module.
105108
*/
@@ -110,55 +113,4 @@ public String asString() {
110113
}
111114
}
112115
}
113-
114-
enum Events implements Event {
115-
116-
/**
117-
* Published when an event is sent successfully.
118-
*/
119-
EVENT_PUBLICATION_SUCCESS {
120-
121-
/*
122-
* (non-Javadoc)
123-
* @see io.micrometer.observation.Observation.Event#getName()
124-
*/
125-
@Override
126-
public String getName() {
127-
return "event.publication.success";
128-
}
129-
130-
/*
131-
* (non-Javadoc)
132-
* @see io.micrometer.observation.Observation.Event#getContextualName()
133-
*/
134-
@Override
135-
public String getContextualName() {
136-
return "event.publication.success";
137-
}
138-
},
139-
140-
/**
141-
* Published when an event is sent with a failure.
142-
*/
143-
EVENT_PUBLICATION_FAILURE {
144-
145-
/*
146-
* (non-Javadoc)
147-
* @see io.micrometer.observation.Observation.Event#getName()
148-
*/
149-
@Override
150-
public String getName() {
151-
return "event.publication.failure";
152-
}
153-
154-
/*
155-
* (non-Javadoc)
156-
* @see io.micrometer.observation.Observation.Event#getContextualName()
157-
*/
158-
@Override
159-
public String getContextualName() {
160-
return "event.publication.failure";
161-
}
162-
}
163-
}
164116
}

spring-modulith-observability/spring-modulith-observability-core/src/main/java/org/springframework/modulith/observability/support/ObservedModule.java renamed to spring-modulith-observability/spring-modulith-observability-api/src/main/java/org/springframework/modulith/observability/ObservedModule.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.modulith.observability.support;
17-
18-
import java.lang.reflect.Method;
16+
package org.springframework.modulith.observability;
1917

2018
import org.aopalliance.intercept.MethodInvocation;
2119
import org.jspecify.annotations.Nullable;
@@ -24,7 +22,6 @@
2422
import org.springframework.modulith.core.ApplicationModules;
2523

2624
import com.tngtech.archunit.core.domain.JavaClass;
27-
import org.springframework.modulith.core.ArchitecturallyEvidentType;
2825

2926
/**
3027
* Information about observed module.
@@ -56,6 +53,15 @@ public interface ObservedModule {
5653
*/
5754
String getInvokedMethod(MethodInvocation invocation);
5855

56+
/**
57+
* Produces a formatted {@link String} for the given {@link MethodInvocation}.
58+
*
59+
* @param invocation must not be {@literal null}.
60+
* @return will never be {@literal null}.
61+
* @since 2.1
62+
*/
63+
String format(MethodInvocation invocation);
64+
5965
/**
6066
* Returns whether the {@link ObservedModule} exposes the given {@link JavaClass}.
6167
*

spring-modulith-observability/spring-modulith-observability-core/src/main/java/org/springframework/modulith/observability/support/ObservedModuleType.java renamed to spring-modulith-observability/spring-modulith-observability-api/src/main/java/org/springframework/modulith/observability/ObservedModuleType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.modulith.observability.support;
16+
package org.springframework.modulith.observability;
1717

1818
import java.lang.reflect.Method;
1919
import java.lang.reflect.Modifier;
@@ -59,7 +59,7 @@ public class ObservedModuleType {
5959
* @param module must not be {@literal null}.
6060
* @param type must not be {@literal null}.
6161
*/
62-
ObservedModuleType(ApplicationModules modules, ObservedModule module, ArchitecturallyEvidentType type) {
62+
public ObservedModuleType(ApplicationModules modules, ObservedModule module, ArchitecturallyEvidentType type) {
6363

6464
Assert.notNull(modules, "ApplicationModules must not be null!");
6565
Assert.notNull(module, "ObservedModule must not be null!");

spring-modulith-observability/spring-modulith-observability-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@
6767
<artifactId>spring-data-rest-webmvc</artifactId>
6868
<optional>true</optional>
6969
</dependency>
70+
71+
<dependency>
72+
<groupId>jakarta.servlet</groupId>
73+
<artifactId>jakarta.servlet-api</artifactId>
74+
<optional>true</optional>
75+
</dependency>
7076

7177
<dependency>
7278
<groupId>org.springframework.boot</groupId>

0 commit comments

Comments
 (0)