Saturday, August 12, 2017

Write Dropwizard metrics to a log file

Instead of using the Dropwizard URI to view the metrics of the methods that have been annotated by @Timed or @Metered on a need basis, you can also write the output to a log file.

The below changes needs to be made in the Dropwizard configuration xml file.
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:metrics="http://www.ryantenney.com/schema/metrics"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.ryantenney.com/schema/metrics
               http://www.ryantenney.com/schema/metrics/metrics-3.0.xsd">

    <!-- Registry should be defined in only one context XML file -->
    <metrics:metric-registry id="metrics"/>
    <metrics:health-check-registry id="healthCheck"/>
    <!-- annotation-driven must be included in all context files -->
    <metrics:annotation-driven metric-registry="metrics"
                               health-check-registry="healthCheck"/>
 
       <metrics:reporter type="slf4j" metric-registry="metrics" period="15m" logger="com.abc.zyx.metrics"/>

     
    <!-- (Optional) The metrics in this example require the metrics-jvm jar-->
    <metrics:register metric-registry="metrics">
        <bean metrics:name="jvm.gc" class="com.codahale.metrics.jvm.GarbageCollectorMetricSet" />
        <bean metrics:name="jvm.memory" class="com.codahale.metrics.jvm.MemoryUsageGaugeSet"/>
        <bean metrics:name="jvm.thread-states" class="com.codahale.metrics.jvm.ThreadStatesGaugeSet"/>
        <bean metrics:name="jvm.fd.usage" class="com.codahale.metrics.jvm.FileDescriptorRatioGauge"/>
    </metrics:register>

    <bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
        <property name="attributes">
            <map>
                <entry key="com.codahale.metrics.servlets.MetricsServlet.registry" value-ref="metrics"/>
                <entry key="com.codahale.metrics.servlets.HealthCheckServlet.registry" value-ref="healthCheck"/>
            </map>
        </property>
    </bean>

</beans>


And you need to also define a logger in log4j.xml or logback.xml file
logger="com.abc.zyx.metrics"

Monday, July 24, 2017

Jenkins slave node gives error /bin/java": java.io.IOException: error=2 when trying to build project

/bin/java": java.io.IOException: error=2
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.(UNIXProcess.java:164)
    at java.lang.ProcessImpl.start(ProcessImpl.java:81)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:476)


As the error indicates the you need to check the environment variables 'JAVA_HOME' or 'java.home' on the slave node. You need to make sure that 'JAVA_HOME' or 'java.home' is pointing to say in linux /opt/common/vendor/jdk1.7.0_79 and not /opt/common/vendor/jdk1.7.0_79/jre.

To check the configuration you can login to Jenkins as admin and then
select Manage Hudson/Jenkins then choose Manage Nodes.
there should be a Node Properties section.
Check the Tool Locations checkbox.
Click on Add/Update then Select your JDK in the dropdown list and add the path to it.

sonar-maven-plugin Insufficient privileges

When using "sonar-maven-plugin" as part of the jenkins build process, you might encounter this error message "[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.3.0.603:sonar (default-cli) on project service1-data: Insufficient privileges -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.3.0.603:sonar (default-cli) on project service1-data: Insufficient privileges
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
".

To fix this issue make sure that jenkins continuous integration (CI) job is building with correct projects' pom.xml in multi-module project. For example if your project has the following hierarchy
project/pom.xml
project/folder1/pom.xml
project/folder2/pom.xml

then if jenkins CI build is using project/pom.xml then
the sonar jenkins job (using sonar-maven-plugin) should also be using project/pom.xml

If the jenkins CI job is using project/pom.xml but sonar jenkins job is using different pom.xml say project/folder1/pom.xml then you will get above error.