AI

How to Use the Milvus GetMetrics() Method in Java

Milvus is an open-source vector database that facilitates the similarity search and AI applications.

If you are not familiar with vector databases, check our tutorial on https://linuxhint.com/what-vector-databases.

Milvus supports various programming languages such as Java and offers a suite of APIs to interact with the databases.

One of these valuable APIs is the getMetrics() method in Java. This method is compelling as it allows us to fetch the system-level metrics from a Milvus instance. We can use these metrics for performance monitoring, cluster health checking, and debugging.

In this tutorial, we will walk you through how to configure a Java application to interact with the Milvus instance and use the getMetrics() method to fetch an information about the cluster.

Requirements:

Before diving into the tutorial, you need to have a few things configured on your machine:

  1. Installed Java Development Kit (JDK) 11 or above
  2. Apache Maven for project management and building the Java project
  3. Milvus server that runs locally or remotely
  4. Installed Java Milvus SDK in your project. You can add it as a dependency via Maven.

This tutorial uses a locally hosted Milvus instance that runs on Docker.

Milvus GetMetrics() Method

The getMetrics() method in the Milvus Java SDK provides the detailed insights such as CPU usage, RAM usage, disk usage, and network traffic. In addition, it provides the metrics of Milvus components such as query nodes, index nodes, and data nodes.

The method definition is as follows:

R<GetMetricsResponse> getMetrics(GetMetricsParam requestParam);

The method returns the information about the runtime Milvus metrics in JSON format.

Project Setup

To better understand how the function works, let us learn how to use it in an application. Start by creating a new Maven project using your favorite IDE and tools.

Install the Milvus SDK

Once we created the project, we must install the Milvus SDK as a Maven dependency. First, open the “pom.xml” file and add the following entry in the Maven dependencies block:

<dependency>
  <groupId>io.milvus</groupId>
  <artifactId>milvus-sdk-java</artifactId>
  <version>2.2.5</version>
</dependency>

Save the Maven “pom.xml” file and allow Maven to download and index the dependencies.

Create the Milvus Client

Before we can use the getMetrics() function, we need to create a Milvus client. We use this client to connect to the Milvus server and interact with the database.

In the Java source file, add the source code as follows:

package org.example;

import io.milvus.client.MilvusClient;
import io.milvus.client.MilvusServiceClient;
import io.milvus.param.*;

public class Main {
    public static void main(String[] args) {
        final MilvusServiceClient milvusClient = new MilvusServiceClient(
                ConnectParam.newBuilder()
                        .withHost("localhost")
                        .withPort(19530)
                        .build()
        );
    }
    }

This should allow us to initialize a new Milvus client using the specified host and port. You can modify these values to fit the address and port of your Milvus instances.

Call the GetMetrics() Function

Once we configured the Milvus client, we can call the getMetrics() function to fetch the metrics. The function returns a “GetMetricsResponse” object. The code snippet is as follows:

GetMetricsParam param = GetMetricsParam.newBuilder()
  .withRequest("{"metric_type":"system_info"}")
  .build();
R<GetMetricsResponse> response = milvusClient.getMetrics(param);
if (response.getStatus() != R.Status.Success.getCode()) {
  System.out.println(response.getMessage());
}
System.out.println(response.getData().getResponse());

Once we run the previous code, it should return a string that contains an information about the system. The resulting value is a JSON string that we can parse.

An example output is as follows:

{
    "nodes_info": [
        {
            "identifier": 10,
            "connected": null,
            "infos": {
                "has_error": false,
                "error_reason": "",
                "name": "querynode10",
                "hardware_infos": {
                    "ip": "172.18.0.4:21123",
                    "cpu_core_count": 12,
                    "cpu_core_usage": 0,
                    "memory": 8251953152,
                    "memory_usage": 274948096,
                    "disk": 104857600,
                    "disk_usage": 2097152
                },
                "system_info": {
                    "system_version": "9ffcd53b",
                    "deploy_mode": "STANDALONE",
                    "build_version": "v2.2.9",
                    "build_time": "Fri Jun  2 09:38:35 UTC 2023",
                    "used_go_version": "go version go1.18.3 linux/amd64"
                },
                "created_time": "2023-06-02 15:44:38.8342373 +0000 UTC m=+0.598175001",
                "updated_time": "2023-06-02 15:44:38.8342374 +0000 UTC m=+0.598175101",
                "type": "querynode",
                "id": 10,
                "system_configurations": {
                    "simd_type": "AVX2"
                },
---

It is good to remember that the resulting value is an extensive JSON string. Therefore, you can use the JSON parsing tools such as GSON in Java to parse the object and access the individual values.

Conclusion

You learned how to fetch and parse the system metrics from a Milvus instance using the Java SDK. Monitoring these metrics will help you keep track of your system’s health and optimize its performance.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list