Implementation:Ray project Ray Metric Count
| Knowledge Sources | |
|---|---|
| Domains | Observability, Metrics |
| Last Updated | 2026-02-13 16:00 GMT |
Overview
The Count class is a concrete metric implementation that tracks cumulative event counts within the Ray Java runtime.
Description
⚠️ Partial Deprecation: The direct constructor Count(String, String, String, Map<TagKey, String>) is annotated @Deprecated. Use the Metrics.count() builder API instead.
Count extends the abstract Metric base class and uses a thread-safe DoubleAdder for incremental counting. It registers itself with the native stats system via NativeMetric.registerCountNative and maintains both a transient count (reset on each flush) and a total cumulative value in the inherited value field. The getAndReset() method returns the transient count since the last flush and resets it, while the cumulative total remains accessible through getCount().
Usage
Use Count when you need to track the number of discrete events occurring in a Ray Java application, such as request counts, error counts, or task completions. Create instances through the Metrics.count() builder API rather than instantiating directly.
Code Reference
Source Location
- Repository: Ray
- File:
java/runtime/src/main/java/io/ray/runtime/metric/Count.java
Signature
public class Count extends Metric {
public Count(String name, String description, String unit, Map<TagKey, String> tags)
public Count(String name, String description, Map<String, String> tags)
public void update(double value)
protected double getAndReset()
public double getCount()
public void inc(double delta)
}
Import
import io.ray.runtime.metric.Count;
I/O Contract
| Constructor Parameters | |||
|---|---|---|---|
| Name | Type | Required | Description |
name |
String |
Yes | The metric name used for identification in the stats system |
description |
String |
Yes | Human-readable description of what the metric tracks |
unit |
String |
No (deprecated constructor) | Unit of measurement (empty string in simplified constructor) |
tags |
Map<String, String> |
Yes | Key-value pairs for metric labeling and filtering |
| Methods | |||
|---|---|---|---|
| Method | Parameter | Return Type | Description |
update(double) |
value - the amount to add |
void |
Adds the given value to both the transient and cumulative counters |
inc(double) |
delta - the increment amount |
void |
Convenience alias for update()
|
getCount() |
none | double |
Returns the cumulative count since creation |
getAndReset() |
none | double |
Returns the transient count since last flush and resets it |
Usage Examples
// Create and register a Count metric via the Metrics builder API
Map<String, String> tags = new HashMap<>();
tags.put("method", "GET");
tags.put("endpoint", "/api/tasks");
Count requestCount = Metrics.count()
.name("ray.requests.total")
.description("Total number of requests received")
.unit("")
.tags(tags)
.register();
// Increment the counter
requestCount.update(1.0);
// Or use the convenience method
requestCount.inc(1.0);
// Read the cumulative count
double total = requestCount.getCount();
// Flush to native stats
requestCount.record();
Related Pages
- Environment:Ray_project_Ray_Java_Build_Environment
- Ray_project_Ray_Metric_Base - Abstract base class for all metric types
- Ray_project_Ray_Metrics_API - Entry point builder API for creating metrics
- Ray_project_Ray_Metric_Sum - Similar accumulating metric for sums