Java

Host metrics refer to the metrics collected from the operating system of the host where your applications are running. These metrics include CPU, memory, disk, and network usage. Understanding host metrics is crucial as it helps you identify potential problems or bottlenecks that could affect the overall performance of your applications.

In this tutorial, we will show you how to collect host metrics, send them to GreptimeDB and visualize them.

Create Service

To experience the full power of GreptimeCloud, you need to create a service which contains a database with authentication. Open the GreptimeCloud console, signup and login. Then click the New Service button and config the following:

  • Service Name: The name you want to describe your service.
  • Description: More information about your service.
  • Region: Select the region where the database is located.
  • Plan: Select the pricing plan you want to use.

Now create the service and we are ready to write some metrics to it.

Write Data

Prerequisites

Example Application

In this section, we will create a quick start demo and showcase the core code to collect JVM runtime metrics and send them to GreptimeDB. The demo is based on OTLP/HTTP. For reference, you can obtain the entire demo on GitHub.

To begin, create a new directory named quick-start-java to host our project, then use IDEA to create a new project in the directory. Choose Java as the Language, Gradle as the Build System, JDK 17 as the Project SDK, Groovy as the Gradle DSL, then click Create button.

Install the required packages in build.gradle:

groovy

  1. dependencies {
  2. implementation 'io.opentelemetry:opentelemetry-api:1.28.0'
  3. implementation 'io.opentelemetry:opentelemetry-sdk:1.28.0'
  4. implementation 'io.opentelemetry:opentelemetry-exporter-otlp:1.28.0'
  5. implementation 'io.opentelemetry:opentelemetry-semconv:1.28.0-alpha'
  6. implementation 'io.opentelemetry.instrumentation:opentelemetry-runtime-metrics:1.26.0-alpha'
  7. }

Once the required packages are installed, write the code to create a metric export object that sends metrics to GreptimeDB. For the configration about the exporter, please refer to OTLP integration documentation in GreptimeDB or GreptimeCloud.

java

  1. String endpoint = String.format("https://%s/v1/otlp/v1/metrics", dbHost);
  2. String auth = username + ":" + password;
  3. String b64Auth = new String(Base64.getEncoder().encode(auth.getBytes()));
  4. OtlpHttpMetricExporter exporter = OtlpHttpMetricExporter.builder()
  5. .setEndpoint(endpoint)
  6. .addHeader("X-Greptime-DB-Name", db)
  7. .addHeader("Authorization", String.format("Basic %s", b64Auth))
  8. .setTimeout(Duration.ofSeconds(5))
  9. .build();

Then attach the exporter to the MeterProvider and start the JVM runtime metrics collection:

java

  1. PeriodicMetricReader metricReader = PeriodicMetricReader
  2. .builder(exporter)
  3. .setInterval(Duration.ofSeconds(5))
  4. .build();
  5. SdkMeterProvider meterProvider = SdkMeterProvider
  6. .builder()
  7. .setResource(resource)
  8. .registerMetricReader(metricReader)
  9. .build();
  10. OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder()
  11. .setMeterProvider(meterProvider)
  12. .buildAndRegisterGlobal();
  13. Runtime.getRuntime().addShutdownHook(new Thread(openTelemetrySdk::close));
  14. BufferPools.registerObservers(openTelemetry);
  15. Classes.registerObservers(openTelemetry);
  16. Cpu.registerObservers(openTelemetry);
  17. GarbageCollector.registerObservers(openTelemetry);
  18. MemoryPools.registerObservers(openTelemetry);
  19. Threads.registerObservers(openTelemetry);

For more details about the code, you can refer to the OpenTelemetry Documentation.

Congratulations on successfully completing the core section of the demo! You can now run the complete demo by following the instructions in the README.md file on the GitHub repository.

The connection information can be found on the service page of GreptimeCloud console.

Visualize Data

Visualizing data in panels and monitoring metrics is important in a developer’s daily work. From the GreptimeCloud console, click on Open Prometheus Workbench, then click on + New Ruleset and Add Group. Name the group host-monitor and add panels.

To add panels for all the tables you’re concerned with, select a table and click on Add Panel one by one. Once you’ve added all the necessary panels, click on the Save button to save them. You can then view the panels in your daily work to monitor the metrics. Additionally, you can set up alert rules for the panels to be notified when the metrics exceed the threshold.