0

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.

GoldFlsh
  • 1,095
  • 2
  • 11
  • 26

1 Answers1

0

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)

    }

}
kakashi hatake
  • 1,175
  • 2
  • 10
  • 18
  • This looks good, I tried something similar but had issues maybe because I did not exclude the `NewRelicMetricsExportAutoConfiguration.class`. I'll give it a shot and get back to you. One question, how come `extends NewRelicMeterRegistry` doesn't count as being an extension of `MeterRegistry` and you think I should extend `StepMeterRegistry` directly? That would necessitate me copying a lot of code from the `micrometer.newrelic.NewRelicMeterRegistry` into my override. – GoldFlsh Nov 04 '19 at 01:57
  • @GoldFlsh if you dont want to copy some code , I think your extension class is also work.I just wanted to do more detail and customization – kakashi hatake Nov 04 '19 at 06:13