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 theMetricsHandler
interface.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 class
SimpleMetricsHandler.Metric
Represents 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 int
defineMetric(MetricType type, String name)
Defines a new metric with the specified type and name.long
getMetric(int metricId)
Retrieves the current value of the specified metric.io.roastedroot.proxywasm.internal.WasmResult
incrementMetric(int metricId, long value)
Increments the value of the specified metric by the given amount.io.roastedroot.proxywasm.internal.WasmResult
recordMetric(int metricId, long value)
Sets the value of the specified metric to the given value.io.roastedroot.proxywasm.internal.WasmResult
removeMetric(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:
defineMetric
in interfaceMetricsHandler
- Parameters:
type
- TheMetricType
of 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 WasmException
Retrieves the current value of the specified metric.- Specified by:
getMetric
in interfaceMetricsHandler
- Parameters:
metricId
- The unique ID of the metric to query.- Returns:
- The current value of the metric.
- Throws:
WasmException
- withWasmResult.NOT_FOUND
if 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:
incrementMetric
in interfaceMetricsHandler
- Parameters:
metricId
- The unique ID of the metric to increment.value
- The amount to add to the metric's current value.- Returns:
WasmResult.OK
if successful, orWasmResult.NOT_FOUND
if 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:
recordMetric
in interfaceMetricsHandler
- Parameters:
metricId
- The unique ID of the metric to record.value
- The value to set for the metric.- Returns:
WasmResult.OK
if successful, orWasmResult.NOT_FOUND
if 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:
removeMetric
in interfaceMetricsHandler
- Parameters:
metricId
- The unique ID of the metric to remove.- Returns:
WasmResult.OK
if the metric was successfully removed, orWasmResult.NOT_FOUND
if no metric exists with the given ID.
-
-