I am using custom timers to instrument a ton of fields via Micrometer. Ideally I do not want the metrics reported for this specific meter that have a count of zero between the configured step interval. This is not crucial but would love to potentially reduce noise of what is getting sent to NR every x seconds.
I've created an extension off NewRelicMeterRegistry that overrides the publish()
method to add the functionality before the default behavior.
public class FilteringNewRelicMeterRegistry extends NewRelicMeterRegistry {
public FilteringNewRelicMeterRegistry(NewRelicConfig config, Clock clock) {
super(config, clock);
}
/**
* Remove field metrics that have not been used since the last publish.
*/
@Override
protected void publish() {
getMeters().stream()
.filter(filterByMeterId(...)))
.filter(meter -> ((Timer) meter).count() == 0)
.forEach(this::remove);
super.publish();
}
}
But for the life of me, I can't figure out how to get the AutoConfiguration to prefer this implementation over the default NewRelicMeterRegistry
.
How do I get spring-boot or micrometer to honor my implementation and use that as the designated bean in the application context for autowiring purposes?
Also if there is an out of the box way to override this behavior via micrometer abstractions or utility, awesome that would be even better! Please let me know. I've tried using MeterRegistryCustomizer
but that didn't seem to have what I needed.
I want to avoid using Spring's scheduling functionality via @Scheduled
, would like to do this on an "on publish" basis.
if you don't want to default auto configuration disable default with this
@SpringBootApplication(exclude = { NewRelicMetricsExportAutoConfiguration.class })
and extends your FilteringNewRelicMeterRegistry class with StepMeterRegistry and configure with your responsibilities, because StepMeterRegistry is subclasses to MeterRegistry and micrometer detect your configuration
after register your custom configuration with this configuration class same as NewRelicMetricsExportAutoConfiguration StepMeterRegistry is need StepRegistryConfig and Clock is use default NewRelicConfig and clock and register like this, I read NewRelicMetricsExportAutoConfiguration and simplify configuration like this
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnProperty(prefix = "management.metrics.export.newrelic", name = "enabled", havingValue = "true",
matchIfMissing = true)
@EnableConfigurationProperties(NewRelicProperties.class)
public class FilteringNewRelicConfiguration {
private final NewRelicProperties properties;
public FilteringNewRelicConfiguration(NewRelicProperties properties) {
this.properties = properties;
}
@Bean
public NewRelicConfig newRelicConfig() {
return new NewRelicPropertiesConfigAdapter(this.properties);
}
@Bean
public FilteringNewRelicMeterRegistry filteringNewRelicMeterRegistry(NewRelicConfig newRelicConfig, Clock clock) {
return new FilteringNewRelicMeterRegistry(newRelicConfig, clock)
}
}