@Inject from Jakarta failing to provide MetricRegistry Microprofile

113 views Asked by At

I have a problem that I've been trying to solve for a while. I am trying to measure the performance in the app, and for that I want to use the microprofile metrics library from eclipse (more exactly, I am using the version 1.1.5). The application is using Spring Boot 2.7.2, Maven and Websphere/Liberty 22.0.0.6. Seems like everything worked fine til some point, since when I am starting the app, the endpoint /metrics is working and I can see metrics of type vendor and base, but no application ones (added in the code). It seems like the problem appears due to the fact that @Inject is providing a **null **MetricRegistry to which I am trying to add the custom made metrics. Here is the @Configuration for the Metrics module where the MetricRegistry is injected:

import jakarta.inject.Inject;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MetricsConfiguration {
    
    @Inject
    MetricRegistry metricRegistry;

    @Bean
    public MetricsPort metricsPort() {
        return new MetricsPortImpl(metricRegistry);
    }
}

And here is the dependency I added in order to be able to use the inject annotation from Jakarta:

<dependency>
    <groupId>jakarta.inject</groupId>
    <artifactId>jakarta.inject-api</artifactId>
    <version>2.0.0</version>
    <scope>provided</scope>
</dependency>

I tried to change the @Inject to @Autowired from Spring but it seems like MetricRegistry can be injected only using CDI. I tried changing the version of Jakarta but nothing improved. Initially I was using the version 9.1.0 for jakarta.jakartaee-api, which let me start the app but was providing the null MetricRegistry. I also tried the annotation @DependsOn to create the MetricsPort after the MetricRegistry was injected. I tried a bunch of different dependencies and options for @Inject annotation but nothing changed.

0

There are 0 answers