Class SimpleMetricsHandler

  • All Implemented Interfaces:
    MetricsHandler

    public class SimpleMetricsHandler
    extends Object
    implements MetricsHandler
    A basic, in-memory implementation of the MetricsHandler 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.

    • Constructor Detail

      • SimpleMetricsHandler

        public SimpleMetricsHandler()
        Default constructor.
    • 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 interface MetricsHandler
        Parameters:
        type - The MetricType 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 interface MetricsHandler
        Parameters:
        metricId - The unique ID of the metric to query.
        Returns:
        The current value of the metric.
        Throws:
        WasmException - with WasmResult.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 interface MetricsHandler
        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, or WasmResult.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 interface MetricsHandler
        Parameters:
        metricId - The unique ID of the metric to record.
        value - The value to set for the metric.
        Returns:
        WasmResult.OK if successful, or WasmResult.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 interface MetricsHandler
        Parameters:
        metricId - The unique ID of the metric to remove.
        Returns:
        WasmResult.OK if the metric was successfully removed, or WasmResult.NOT_FOUND if no metric exists with the given ID.