[jboss-jira] [JBoss JIRA] (WFLY-12828) Can not inject MP Config property to a custom MP metric

Ivan Straka (Jira) issues at jboss.org
Tue Nov 26 06:50:00 EST 2019


Ivan Straka created WFLY-12828:
----------------------------------

             Summary: Can not inject MP Config property to a custom MP metric
                 Key: WFLY-12828
                 URL: https://issues.jboss.org/browse/WFLY-12828
             Project: WildFly
          Issue Type: Bug
          Components: CDI / Weld, MP Config, MP Health
    Affects Versions: 18.0.1.Final
            Reporter: Ivan Straka
            Assignee: Jeff Mesnil


Injecting MP Config property into a custom metric results to NPE.

*web servlet*
{code:java}
    @Inject
    @RegistryType(type = MetricRegistry.Type.APPLICATION)
    MetricRegistry metricRegistry;
    
    @Inject
    private CustomMetricService hello;

    @Override
    public void init() throws ServletException {
        Metadata metadata = new MetadataBuilder()
                .withName("custom-metric")
                .withType(MetricType.COUNTER)
                .withUnit(MetricUnits.NONE)
                .build();
        metricRegistry.register(metadata, new CustomCounterMetric());
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.getWriter().print(hello.hello());
    }
{code}

*CDI bean*

{code:java}
    @Inject
    @ConfigProperty(name="dummy_increment")
    int increment;

    @Inject
    @RegistryType(type = MetricRegistry.Type.APPLICATION)
    MetricRegistry metricRegistry;

    @Counted
    public String hello() {
        metricRegistry.counter("custom-metric").inc();
        return "Hello! " + metricRegistry.counter("custom-metric").getCount() + "\n";
    }
{code}

*custom metric*

{code:java}
    @Inject
    @ConfigProperty(name="dummy_increment")
    Provider<Long> increment;

    private long counter = 0;

    @Override
    public void inc() {
        counter = counter + increment.get();
    }

    @Override
    public void inc(long l) {
        counter = counter + l + increment.get() ;
    }

    @Override
    public long getCount() {
        return counter;
    }
{code}

I have tested environment propety _-Ddummy_increment=1_ and _props config source_

Sample source: https://github.com/istraka/microprofile-samples/tree/master/metrics-config



--
This message was sent by Atlassian Jira
(v7.13.8#713008)


More information about the jboss-jira mailing list