Convert Figma logo to code with AI

prometheus logoclient_java

Prometheus instrumentation library for JVM applications

2,169
792
2,169
111

Top Related Projects

An application observability facade for the most popular observability tools. Think SLF4J, but for observability.

7,825

:chart_with_upwards_trend: Capturing JVM- and application-level metrics. So you know what's going on.

1,407

Distributed Tracing, Metrics and Context Propagation for applications running on the JVM

Quick Overview

Prometheus Client Java is the official Java client library for Prometheus, a popular open-source monitoring and alerting toolkit. It provides Java developers with the tools to instrument their applications for Prometheus monitoring, allowing them to collect and expose metrics in the Prometheus format.

Pros

  • Easy integration with Java applications
  • Supports various metric types (Counter, Gauge, Histogram, Summary)
  • Provides both simple and advanced usage patterns
  • Active development and community support

Cons

  • Learning curve for developers new to Prometheus
  • Can add overhead to applications if not used judiciously
  • Limited built-in exporters compared to some other Prometheus client libraries
  • Requires additional setup for full Prometheus ecosystem integration

Code Examples

  1. Creating a simple counter:
Counter requests = Counter.build()
    .name("requests_total")
    .help("Total number of requests.")
    .register();

// Increment the counter
requests.inc();
  1. Using a gauge to track current temperature:
Gauge temperature = Gauge.build()
    .name("current_temperature_celsius")
    .help("Current temperature in Celsius.")
    .register();

// Set the current temperature
temperature.set(23.5);
  1. Creating a histogram for request duration:
Histogram requestDuration = Histogram.build()
    .name("request_duration_seconds")
    .help("Request duration in seconds.")
    .buckets(0.1, 0.5, 1, 5)
    .register();

// Record the duration of a request
requestDuration.observe(0.3);

Getting Started

To use Prometheus Client Java in your project, add the following dependency to your pom.xml file:

<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient</artifactId>
    <version>0.16.0</version>
</dependency>

For a basic HTTP server that exposes metrics, also add:

<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_httpserver</artifactId>
    <version>0.16.0</version>
</dependency>

Then, in your Java code:

import io.prometheus.client.Counter;
import io.prometheus.client.exporter.HTTPServer;

public class Example {
    public static void main(String[] args) throws IOException {
        Counter requests = Counter.build()
            .name("requests_total")
            .help("Total requests.")
            .register();

        HTTPServer server = new HTTPServer(8080);

        // Your application logic here
        requests.inc();
    }
}

This sets up a basic counter and starts an HTTP server on port 8080 that exposes the metrics for Prometheus to scrape.

Competitor Comparisons

An application observability facade for the most popular observability tools. Think SLF4J, but for observability.

Pros of Micrometer

  • Supports multiple monitoring systems, not just Prometheus
  • Provides a more abstracted and flexible API
  • Offers richer set of metric types and dimensions

Cons of Micrometer

  • Slightly more complex setup due to its abstraction layer
  • Potential performance overhead from supporting multiple backends

Code Comparison

Micrometer:

MeterRegistry registry = new SimpleMeterRegistry();
Counter counter = registry.counter("my.counter", "tag", "value");
counter.increment();

Client Java:

Counter counter = Counter.build()
    .name("my_counter")
    .labelNames("tag")
    .help("A sample counter")
    .register();
counter.labels("value").inc();

Key Differences

  1. API Design: Micrometer offers a more fluent and flexible API, while Client Java follows a builder pattern.
  2. Metric Types: Micrometer provides additional metric types like Timer and FunctionCounter.
  3. Backend Support: Micrometer is designed for multiple monitoring systems, whereas Client Java is Prometheus-specific.
  4. Configuration: Micrometer uses a MeterRegistry for configuration, while Client Java uses static methods and global configuration.
  5. Community and Ecosystem: Micrometer has broader adoption and integration with various frameworks, while Client Java is more focused on the Prometheus ecosystem.

Both libraries are actively maintained and suitable for Prometheus integration in Java applications, with the choice depending on specific project requirements and preferences.

7,825

:chart_with_upwards_trend: Capturing JVM- and application-level metrics. So you know what's going on.

Pros of Metrics

  • More mature and widely adopted in the Java ecosystem
  • Supports a broader range of metric types (e.g., histograms, timers)
  • Offers built-in reporting mechanisms for various backends (e.g., Graphite, Ganglia)

Cons of Metrics

  • Lacks native support for labels/tags (important for dimensional metrics)
  • Not designed specifically for Prometheus, requiring additional adapters
  • Can be more complex to set up and configure compared to client_java

Code Comparison

Metrics:

final MetricRegistry metrics = new MetricRegistry();
final Counter requests = metrics.counter("requests");
requests.inc();

client_java:

static final Counter requests = Counter.build()
    .name("requests_total").help("Total requests.").register();
requests.inc();

Summary

Metrics is a versatile and mature metrics library for Java applications, offering a wide range of metric types and reporting options. However, it lacks native support for labels and Prometheus-specific features. client_java, on the other hand, is purpose-built for Prometheus integration, providing simpler setup and native support for labels, but with a more limited set of metric types. The choice between the two depends on specific project requirements and the monitoring ecosystem in use.

1,407

Distributed Tracing, Metrics and Context Propagation for applications running on the JVM

Pros of Kamon

  • More comprehensive monitoring solution, covering metrics, tracing, and context propagation
  • Supports multiple backends beyond Prometheus (e.g., InfluxDB, Datadog)
  • Offers automatic instrumentation for popular frameworks and libraries

Cons of Kamon

  • Steeper learning curve due to its broader feature set
  • May introduce more overhead compared to the lightweight Prometheus client
  • Less focused on Prometheus-specific features and optimizations

Code Comparison

Kamon example:

val counter = Kamon.counter("my_counter").withTag("label", "value")
counter.increment()

Prometheus client_java example:

Counter counter = Counter.build()
    .name("my_counter").help("My counter")
    .labelNames("label").register();
counter.labels("value").inc();

Summary

Kamon is a more comprehensive monitoring solution with support for multiple backends, while Prometheus client_java is a lightweight, Prometheus-focused library. Kamon offers broader features but may have a steeper learning curve, whereas Prometheus client_java is simpler but more limited in scope. The choice between them depends on your specific monitoring needs and preferred level of complexity.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Prometheus Java Metrics Library

Build Status

Documentation

https://prometheus.github.io/client_java

Contributing and community

See CONTRIBUTING.md and the community section of the Prometheus homepage.

The Prometheus Java community is present on the CNCF Slack on #prometheus-java, and we have a fortnightly community call in the Prometheus public calendar.

Previous Releases

The source code for 0.16.0 and older is on the simpleclient branch.

License

Apache License 2.0, see LICENSE.