All Products
Search
Document Center

Simple Log Service:Collect metric data from Java applications or Tomcat servers

Last Updated:Aug 25, 2023

You can use Telegraf to collect metric data from Java applications or Tomcat servers. Then, you can use Logtail to send the metric data to a Metricstore in Simple Log Service. This way, you can monitor the metric data of Java applications and Tomcat servers in a visualized manner. This topic describes how to collect metric data from Java applications by using Simple Log Service and visualize the data.

Prerequisites

Limit

Only Linux Logtail V0.16.48 and later can collect metric data from Java applications and Tomcat servers. If an earlier version of Logtail is installed on your server, you must update Logtail to a supported version. For more information, see Update Logtail online.

Step 1: Create a Logtail configuration

  1. Log on to the Log Service console.

  2. On the Monitoring Data tab in the Import Data section, click Java Application Monitoring Data.

    If you want to collect metric data from Tomcat servers, click Tomcat Monitoring Data.

  3. Select the project and Metricstore and click Next.
  4. Create a machine group.
    • If a machine group is available, click Use Existing Machine Groups.
    • If no machine groups are available, perform the following steps to create a machine group. In this example, an Elastic Compute Service (ECS) instance is used.
      1. On the ECS Instances tab, select Manually Select Instances. Then, select the ECS instance that you want to use and click Create.

        For more information, see Install Logtail on ECS instances.

        Important If you want to collect logs from an ECS instance that belongs to a different Alibaba Cloud account, a server in an on-premises data center, or a server of a third-party cloud service provider, you must manually install Logtail. For more information, see Install Logtail on a Linux server. After you manually install Logtail, you must configure a user identifier for the server. For more information, see Configure a user identifier.
      2. After Logtail is installed, click Complete Installation.
      3. In the Create Machine Group step, configure the Name parameter and click Next.

        Log Service allows you to create IP address-based machine groups and custom identifier-based machine groups. For more information, see Create an IP address-based machine group and Create a custom identifier-based machine group.

  5. Select the new machine group from Source Server Groups and move the machine group to Applied Server Groups. Then, click Next.
    Important If you apply a machine group immediately after you create the machine group, the heartbeat status of the machine group may be FAIL. This issue occurs because the machine group is not connected to Log Service. To resolve this issue, you can click Automatic Retry. If the issue persists, see What do I do if no heartbeat connections are detected on Logtail?
  6. In the Specify Data Source step, configure the parameters. The following table describes the parameters.

    ParameterDescription
    Configuration NameThe name of the Logtail configuration. You can enter a custom name.
    Cluster NameThe name of the Tomcat cluster. You can enter a custom name.

    After you configure this parameter, Log Service adds a cluster=Cluster name tag to the Tomcat monitoring data that is collected by using the Logtail configuration.

    Important Make sure that the cluster name is unique. Otherwise, data conflicts may occur.
    Application NameThe name of the Java application. You can enter a custom name.
    Server ListThe information about the Tomcat server. The information includes the following configuration items:
    • Address: the address of the Tomcat server.
    • Port: the port number of the Tomcat server. Default value: 7777.

    You can add information about multiple Tomcat servers based on your business requirements.

    Custom TagsThe custom tags that are added to the collected Tomcat monitoring data. The tags are key-value pairs.

    After you configure this parameter, Log Service adds the custom tags to the Tomcat monitoring data that is collected by using the Logtail configuration.

Step 2: Configure JavaAgent

After the Logtail configuration is created, you must enable access to JMX data over HTTP. Simple Log Service allows you to use Jolokia to access JMX data over HTTP. You can download and load Jolokia. If you need help, you can refer to the official documentation of Jolokia. You can also use Jolokia JavaAgent that is provided by Logtail. Jolokia JavaAgent is stored in /etc/ilogtail/telegraf/javaagent/jolokia-jvm.jar.

  • If you want to collect metric data from Java applications, you must add the script -javaagent:/etc/ilogtail/telegraf/javaagent/jolokia-jvm.jar=port=7777 to the Java startup parameters.

  • If you want to collect metric data from Tomcat servers, you must configure the JAVA_OPTS environment variable. For example, specify export JAVA_OPTS="-javaagent:/etc/ilogtail/telegraf/jolokia-jvm.jar=port=7777". In this example, 7777 indicates the port number of the application server. This port number must be the same as the port number that you specify in Step 1: Create a Logtail configuration.

Note

By default, Jolokia JavaAgent listens only on the IP address 127.0.0.1 and allows requests only from the local host. If Logtail and your Java application are installed on different servers, you can add the host= field to the added script. This way, Jolokia JavaAgent can listen on other IP addresses. If you add host=0.0.0.0, Jolokia JavaAgent listens on all IP addresses. Example:

-javaagent:/tmp/jolokia-jvm.jar=port=7777,host=0.0.0.0

After you configure the settings, you must restart your Java application. If your Java application fails to restart, run the following command to connect Jolokia JavaAgent to a specified Java process. This way, the configuration immediately takes effect. Replace PID with the actual value.

Note

This command is used only for testing. You must complete the settings based on the preceding steps. Otherwise, the configuration becomes invalid after your application restarts.

java -jar /etc/ilogtail/telegraf/javaagent/jolokia-jvm.jar --port 7777 start PID

If the following output is returned, the connection is successful.

Jolokia is already attached to PID 752
http://127.0.0.1:7777/jolokia/

After the connection is established, you can access the following URL to verify the connection:

curl http://127.0.0.1:7777/jolokia/
# Sample response
{"request":{"type":"version"},"value":{"agent":"1.6.2","protocol":"7.2","config":{"listenForHttpService":"true","maxCollectionSize":"0","authIgnoreCerts":"false","agentId":"30.**.**.186-752-5b091b5d-jvm","debug":"false","agentType":"jvm","policyLocation":"classpath:\/jolokia-access.xml","agentContext":"\/jolokia","serializeException":"false","mimeType":"text\/plain","maxDepth":"15","authMode":"basic","authMatch":"any","discoveryEnabled":"true","streaming":"true","canonicalNaming":"true","historyMaxEntries":"10","allowErrorDetails":"true","allowDnsReverseLookup":"true","realm":"jolokia","includeStackTrace":"true","maxObjects":"0","useRestrictorService":"false","debugMaxEntries":"100"},"info":{"product":"tomcat","vendor":"Apache","version":"8.5.57"}},"timestamp":1602663330,"status":200}⏎

FAQ

How can I check whether Telegraf collects data as expected?

You can check the logs of the /etc/ilogtail/telegraf/telegraf.log file on your server. You can also collect this log file to Log Service and search for the required information.

What to do next

  • Search and analyze

    After you complete the settings, Telegraf uses the Logtail to upload collected metrics to the Metricstore of Log Service. You can query the data on the Search & Analysis page of the Metricstore. For more information, see Query and analyze metric data.

  • Visualize query and analysis results

    • After you configure the settings to collect metric data from your Java application, Simple Log Service automatically creates a dashboard named Java Application Monitoring Data_<cluster name> in the related project. You can visualize query and analysis results on the dashboard. You can also configure alert rules for the dashboard.

    • After you configure the settings to collect metric data from your Tomcat server, Simple Log Service automatically creates a dashboard named Tomcat Monitoring Data_<cluster name> in the related project. You can visualize query and analysis results on the dashboard. You can also configure alert rules for the dashboard.