简体   繁体   中英

metrics-spring configuration via .properties file

I'm trying to configure metrics-spring via configuration file

In my spring.xml I've added

<bean id="propertyPlaceholderConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>
                classpath:metrics.properties
            </value>
        </list>
    </property>
    <property name="systemPropertiesModeName"
              value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="searchSystemEnvironment" value="true"/>
</bean>

filled with something like

metrics.reporter.type=console

and then I'm setting it in the spring config accessing that property via ${metrics.reporter.type}

<metrics:reporter metric-registry="metrics" type="${metrics.reporter.type}" period="1m"/>

During the startup of the web application, spring throws a BeanDefinitionParsingException due to the unresolved variable above

Configuration problem: No ReporterElementParser found for reporter type '${metrics.reporter.type}'

I'm using this configuration method (via properties file) for mongo host and port and it works like a charm.

I'm running in Tomcat7, Spring 4.0.5.RELEASE, metrics framework 3.1.0-SNAPSHOT (I need jersey 2 support) and metrics-spring 3.0.1. I also tried with a self-compiled metrics-spring 3.1.0-SNAPSHOT but doesn't solve my problem.

[EDIT]

Found this issue which explain that SpEL is not supported by the ElementParser.

I'm afraid it isn't possible to use a property placeholder in the type attribute. Spring does not resolve property placeholders or SpEL until the phase after metrics-spring reads the type attribute and parses the reporter element (which is necessary to allow placeholders and bean references to be used in all the other attributes).

A possible solution would be to configure all the reporters you might want to use, and use a placeholder in the enabled attribute:

<metrics:reporter metric-registry="metrics" type="console" period="1m"
                  enabled="${metrics.reporter.console.enabled}" />

<metrics:reporter metric-registry="metrics" type="slf4j" period="1m"
                  enabled="${metrics.reporter.slf4j.enabled}" />

And the properties file:

metrics.reporter.console.enabled=true
metrics.reporter.slf4j.enabled=false

I hope this makes sense, I've had a very long week!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM