Class MetricsSupport

  • All Implemented Interfaces:
    MetricsSupport, RestServiceSupport, Service

    public final class MetricsSupport
    extends HelidonRestServiceSupport
    implements MetricsSupport
    Support for metrics for Helidon Web Server.

    By defaults creates the /metrics endpoint with three sub-paths: application, vendor and base.

    To register with web server:

    
     Routing.builder()
            .register(MetricsSupport.create())
     

    This class supports finer grained configuration using Helidon Config: create(Config). The following configuration parameters can be used:

    Configuration parameters
    keydefault valuedescription
    helidon.metrics.context/metricsContext root under which the rest endpoints are available
    helidon.metrics.base.${metricName}.enabledtrueCan control which base metrics are exposed, set to false to disable a base metric

    The application metrics registry is then available as follows:

    
      req.context().get(MetricRegistry.class).ifPresent(reg -> reg.counter("myCounter").inc());
     
    • Method Detail

      • create

        public static MetricsSupport create()
        Create an instance to be registered with Web Server with all defaults.
        Returns:
        a new instance built with default values (for context, base metrics enabled)
      • create

        public static MetricsSupport create​(MetricsSettings metricsSettings,
                                            RestServiceSettings restServiceSettings)
        Create an instance to be registered with Web Server with the specific metrics settings.
        Parameters:
        metricsSettings - metrics settings to use for initializing metrics
        restServiceSettings - REST service settings for managing the endpoint
        Returns:
        a new instance built with the specified metrics settings
      • create

        public static MetricsSupport create​(Config config)
        Create an instance to be registered with Web Server maybe overriding default values with configured values.
        Parameters:
        config - Config instance to use to (maybe) override configuration of this component. See class javadoc for supported configuration keys.
        Returns:
        a new instance configured withe config provided
      • toPrometheusData

        public static String toPrometheusData​(MetricID metricID,
                                              Metric metric,
                                              boolean withHelpType)
        Formats a metric in Prometheus format.
        Parameters:
        metricID - the MetricID for the metric to be formatted
        metric - the Metric containing the data to be formatted
        withHelpType - flag controlling serialization of HELP and TYPE
        Returns:
        metric info in Prometheus format
      • toPrometheusData

        public static String toPrometheusData​(String name,
                                              Metric metric,
                                              boolean withHelpType)
        Formats a metric in Prometheus format.
        Parameters:
        name - the name of the metric
        metric - the Metric containing the data to be formatted
        withHelpType - flag controlling serialization of HELP and TYPE
        Returns:
        metric info in Prometheus format
      • postConfigureEndpoint

        protected void postConfigureEndpoint​(Routing.Rules defaultRules,
                                             Routing.Rules serviceEndpointRoutingRules)
        Finish configuring metrics endpoint on the provided routing rules. This method just adds the endpoint /metrics (or appropriate one as configured). For simple routings, just register MetricsSupport instance. This method is exclusive to update(io.helidon.webserver.Routing.Rules) (e.g. you should not use both, as otherwise you would register the endpoint twice)
        Specified by:
        postConfigureEndpoint in class HelidonRestServiceSupport
        Parameters:
        defaultRules - routing rules for default routing (also accepts Routing.Builder)
        serviceEndpointRoutingRules - possibly different rules for the metrics endpoint routing
      • prepareMetricsEndpoints

        public void prepareMetricsEndpoints​(String endpointContext,
                                            Routing.Rules serviceEndpointRoutingRules)
        Description copied from interface: MetricsSupport
        Prepares the family of /metrics endpoints.

        By default, requests to the metrics endpoints trigger a 404 response with an explanatory message that metrics are disabled. Implementations of this interface can provide more informative endpoints.

        Specified by:
        prepareMetricsEndpoints in interface MetricsSupport
        Parameters:
        endpointContext - context (typically /metrics)
        serviceEndpointRoutingRules - routing rules to update with the disabled metrics endpoints