You can’t extend DefaultModulithObservationConvention, it’s package-private by design.
The proper way is to define your own ObservationConvention<ModulithObservationContext> bean.
Spring Modulith will automatically use it instead of the internal default.
@Bean
ObservationConvention<ModulithObservationContext> modulithObservationConvention() {
return new ObservationConvention<>() {
@Override
public boolean supportsContext(Observation.Context context) {
return context instanceof ModulithObservationContext;
}
@Override
public String getName() {
return "modulith.observation";
}
@Override
public KeyValues getLowCardinalityKeyValues(ModulithObservationContext context) {
return KeyValues.of(
"module.key", context.getModule().getName(),
"module.method", context.getMethod().getName()
);
}
@Override
public KeyValues getHighCardinalityKeyValues(ModulithObservationContext context) {
return KeyValues.of(
"bean.name", context.getBean().getClass().getSimpleName(),
"class.name", context.getMethod().getDeclaringClass().getName()
);
}
};
}
This replaces the built-in convention and lets you add any extra tags (bean.name, class.name, etc.)
No internal classes need to be copied or modified.
In short: register your own ObservationConvention<ModulithObservationContext> bean : it safely overrides the default and gives you full control over trace tags.