Operations 9 min read

How to Build a Lightweight Java Monitoring System with Netdata and Javamelody

This article explains how to set up both server‑level monitoring using Netdata and business‑level monitoring with Spring AOP and Javamelody in a Java e‑commerce application, covering key metrics, code examples, configuration files, and practical screenshots to illustrate the complete solution.

Programmer DD
Programmer DD
Programmer DD
How to Build a Lightweight Java Monitoring System with Netdata and Javamelody

Monitoring Overview

We discuss two aspects of monitoring: server‑level monitoring (CPU, memory, disk I/O, etc.) and business‑level monitoring (performance, SQL statements, request timeouts, user input, overall request duration, and optimization).

Server Monitoring

Because the Java open‑source fresh‑food e‑commerce platform runs on Alibaba Cloud CentOS, we chose the lightweight Netdata tool over heavier solutions such as Zabbix or Nagios.

Installation and usage steps are omitted for brevity.

Sample screenshots of Netdata in action:

Business Monitoring with Spring AOP

Business monitoring records exceptions, error logs, SMS alerts, and push notifications. The following metrics are tracked:

Java memory usage

Java CPU usage

Number of user sessions

JDBC connection count

HTTP/SQL request counts, average execution time, error percentage, etc.

We use Spring AOP to intercept methods that exceed a 1500 ms threshold and output error logs for further analysis.

import org.apache.commons.lang.time.StopWatch;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {
    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(* com.netcai.admin.controller.*.*.*(..))")
    public void pointcutExpression() {
        logger.debug("配置切入点");
    }

    @Before("pointcutExpression()")
    public void beforeMethod(JoinPoint joinPoint) {
        logger.debug("前置通知执行了");
    }

    @After("pointcutExpression()")
    public void afterMethod(JoinPoint joinPoint) {
        logger.debug("后置通知执行了,有异常也会执行");
    }

    @AfterReturning(value = "pointcutExpression()", returning = "returnValue")
    public void afterRunningMethod(JoinPoint joinPoint, Object returnValue) {
        logger.debug("返回通知执行,执行结果:" + returnValue);
    }

    @AfterThrowing(value = "pointcutExpression()", throwing = "e")
    public void afterThrowingMethod(JoinPoint joinPoint, Exception e) {
        logger.debug("异常通知, 出现异常 " + e);
    }

    @Around("pointcutExpression()")
    public Object aroundMethod(ProceedingJoinPoint pjd) {
        StopWatch clock = new StopWatch();
        Object result = null;
        String className = pjd.getTarget().getClass().getName();
        String methodName = pjd.getSignature().getName();
        try {
            clock.start();
            result = pjd.proceed();
            clock.stop();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        if (!methodName.equalsIgnoreCase("initBinder")) {
            long constTime = clock.getTime();
            logger.info("[" + className + "]-" + "[" + methodName + "] 花费时间:" + constTime + "ms");
            if (constTime > 500) {
                logger.error("[" + className + "]-" + "[" + methodName + "] 花费时间过长,请检查: " + constTime + "ms");
            }
        }
        return result;
    }
}

The aspect records the class and method execution time; if the duration exceeds the configured threshold, an error log is printed for daily review and targeted optimization.

Javamelody Integration

For comprehensive monitoring across the entire business line we also use the open‑source tool Javamelody.

Add the following dependencies to pom.xml:

<!-- System monitoring -->
<dependency>
  <groupId>net.bull.javamelody</groupId>
  <artifactId>javamelody-core</artifactId>
  <version>1.68.1</version>
</dependency>

<dependency>
  <groupId>org.jrobin</groupId>
  <artifactId>jrobin</artifactId>
  <version>1.5.9</version>
</dependency>

Configure the monitoring filter and listener in web.xml:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
    classpath*:config/applicationContext.xml
    classpath*:net/bull/javamelody/monitoring-spring.xml
    classpath*:net/bull/javamelody/monitoring-spring-datasource.xml
    classpath*:net/bull/javamelody/monitoring-spring-aspectj.xml
  </param-value>
</context-param>

<filter>
  <filter-name>monitoring</filter-name>
  <filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
  <async-supported>true</async-supported>
  <init-param>
    <param-name>logEnabled</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>monitoring</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
  <listener-class>net.bull.javamelody.SessionListener</listener-class>
</listener>

Resulting operational dashboards are shown below:

Summary

By combining Netdata for server metrics, Spring AOP for business‑level logging, and Javamelody for overall application monitoring, we built a customized monitoring system; alternatives such as Druid, Zabbix, or CAT can also be integrated, and the system is extensible for further development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaspring-aopJavamelodyNetdata
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.