Package io.roastedroot.proxywasm
Class SimpleMetricsHandler
- java.lang.Object
-
- io.roastedroot.proxywasm.SimpleMetricsHandler
-
- All Implemented Interfaces:
MetricsHandler
public class SimpleMetricsHandler extends Object implements MetricsHandler
A basic, in-memory implementation of theMetricsHandlerinterface.This handler manages metrics entirely within the host's memory using standard Java collections. It supports Counter, Gauge, and Histogram types in a simplified manner (e.g., Histograms are treated like Gauges for storage). It is suitable for single-process environments or testing scenarios where integration with a real metrics backend (like Prometheus, StatsD) is not required.
All operations on this handler are synchronized to ensure thread safety within a single JVM.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSimpleMetricsHandler.MetricRepresents an individual metric managed bySimpleMetricsHandler.
-
Field Summary
-
Fields inherited from interface io.roastedroot.proxywasm.MetricsHandler
DEFAULT
-
-
Constructor Summary
Constructors Constructor Description SimpleMetricsHandler()Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intdefineMetric(MetricType type, String name)Defines a new metric with the specified type and name.longgetMetric(int metricId)Retrieves the current value of the specified metric.io.roastedroot.proxywasm.internal.WasmResultincrementMetric(int metricId, long value)Increments the value of the specified metric by the given amount.io.roastedroot.proxywasm.internal.WasmResultrecordMetric(int metricId, long value)Sets the value of the specified metric to the given value.io.roastedroot.proxywasm.internal.WasmResultremoveMetric(int metricId)Removes the metric definition and its associated value.
-
-
-
Method Detail
-
defineMetric
public int defineMetric(MetricType type, String name) throws WasmException
Defines a new metric with the specified type and name. Assigns a new unique ID to the metric and stores it in memory. If a metric with the same name already exists, it is overwritten (behavior may vary in other handlers).- Specified by:
defineMetricin interfaceMetricsHandler- Parameters:
type- TheMetricTypeof the new metric.name- The name for the new metric.- Returns:
- The unique integer ID assigned to the newly defined metric.
- Throws:
WasmException- (Not currently thrown by this implementation, but part of the interface contract).
-
getMetric
public long getMetric(int metricId) throws WasmExceptionRetrieves the current value of the specified metric.- Specified by:
getMetricin interfaceMetricsHandler- Parameters:
metricId- The unique ID of the metric to query.- Returns:
- The current value of the metric.
- Throws:
WasmException- withWasmResult.NOT_FOUNDif no metric exists with the given ID.
-
incrementMetric
public io.roastedroot.proxywasm.internal.WasmResult incrementMetric(int metricId, long value)Increments the value of the specified metric by the given amount. Applicable primarily to Counters, but this implementation applies it additively to any metric type.- Specified by:
incrementMetricin interfaceMetricsHandler- Parameters:
metricId- The unique ID of the metric to increment.value- The amount to add to the metric's current value.- Returns:
WasmResult.OKif successful, orWasmResult.NOT_FOUNDif the metric ID is invalid.
-
recordMetric
public io.roastedroot.proxywasm.internal.WasmResult recordMetric(int metricId, long value)Sets the value of the specified metric to the given value. Applicable primarily to Gauges, but this implementation applies it directly to any metric type.- Specified by:
recordMetricin interfaceMetricsHandler- Parameters:
metricId- The unique ID of the metric to record.value- The value to set for the metric.- Returns:
WasmResult.OKif successful, orWasmResult.NOT_FOUNDif the metric ID is invalid.
-
removeMetric
public io.roastedroot.proxywasm.internal.WasmResult removeMetric(int metricId)
Removes the metric definition and its associated value.- Specified by:
removeMetricin interfaceMetricsHandler- Parameters:
metricId- The unique ID of the metric to remove.- Returns:
WasmResult.OKif the metric was successfully removed, orWasmResult.NOT_FOUNDif no metric exists with the given ID.
-
-