Skip to content

Commit 959c341

Browse files
authored
Allow metrics-capable components to work in absence of full-featured metrics (helidon-io#3441)
* Allow metrics-capable components to be written and executed without caring whether full-featured metrics are on the runtime path and enabled Signed-off-by: tim.quinn@oracle.com <tim.quinn@oracle.com>
1 parent b522492 commit 959c341

89 files changed

Lines changed: 5702 additions & 1139 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bom/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,16 @@
532532
<artifactId>helidon-metrics</artifactId>
533533
<version>${helidon.version}</version>
534534
</dependency>
535+
<dependency>
536+
<groupId>io.helidon.metrics</groupId>
537+
<artifactId>helidon-metrics-api</artifactId>
538+
<version>${helidon.version}</version>
539+
</dependency>
540+
<dependency>
541+
<groupId>io.helidon.metrics</groupId>
542+
<artifactId>helidon-metrics-service-api</artifactId>
543+
<version>${helidon.version}</version>
544+
</dependency>
535545
<dependency>
536546
<groupId>io.helidon.metrics</groupId>
537547
<artifactId>helidon-metrics-trace-exemplar</artifactId>

docs/common/guides/metrics.adoc

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ The following example will demonstrate how to use the built-in metrics. All exa
6868
from the root directory of your project (helidon-quickstart-{lower-case-flavor}).
6969
// end::using-built-in-metrics-intro[]
7070
71+
// tag::metrics-dependency[]
72+
----
73+
<dependency>
74+
<groupId>io.helidon.metrics</groupId>
75+
<artifactId>helidon-metrics</artifactId>
76+
</dependency>
77+
----
78+
// end::metrics-dependency[]
79+
7180
// tag::build-and-run-intro[]
7281
7382
[source,bash,subs="attributes+"]
@@ -164,6 +173,51 @@ curl -H "Accept: application/json" http://localhost:8080/metrics/vendor/request
164173
NOTE: You cannot get the individual fields of a metric. For example, you cannot target http://localhost:8080/metrics/vendor/requests.meter.count.
165174
// end::get-single-metric[]
166175
176+
// tag::controlling[]
177+
// tag::controlling-intro[]
178+
179+
=== Controlling Metrics Behavior
180+
You can control how metrics behaves in several ways by adding a `metrics` section to your application configuration. You can:
181+
182+
* disable metrics entirely,
183+
* disable the use of metrics from individual components, and
184+
* control the key performance indicator metrics which Helidon collects.
185+
186+
// end::controlling-intro[]
187+
188+
// tag::enabling-disabling-whole-intro[]
189+
==== Enabling and Disabling Metrics as a Whole
190+
By default, if your application depends on the `helidon-metrics` Maven module then full-featured metrics are enabled.
191+
You can disable metrics entirely using configuration:
192+
[source,properties]
193+
.Configuration properties file disabling metrics
194+
----
195+
metrics.enabled=false
196+
----
197+
// end::enabling-disabling-whole-intro[]
198+
199+
// tag::enabling-disabling-whole-summary[]
200+
With metrics disabled, Helidon never updates any metrics and the `/metrics` endpoints respond with `404` plus a message that metrics are disabled.
201+
// end::enabling-disabling-whole-summary[]
202+
203+
// tag::controlling-by-component-intro[]
204+
==== Enabling and Disabling Metrics Usage by a Component
205+
Helidon contains several components and integrations which register and update metrics.
206+
Depending on how the component is written, you might be able to disable just that component's use of metrics:
207+
[source,properties]
208+
.Configuration properties file disabling a component's use of metrics
209+
----
210+
some-component.metrics.enabled=false
211+
----
212+
Check the documentation for a specific component to find out whether that component uses metrics and whether it allows you to disable that use.
213+
// end::controlling-by-component-intro[]
214+
215+
// tag::controlling-by-component-summary[]
216+
If you disable a component's use of metrics, Helidon does not register the component's metrics in the visible metrics registries nor do those metrics ever update their values. The response from the `/metrics` endpoint excludes that component's metrics.
217+
218+
Note that if you disable overall metrics, no component updates its metrics regardless of any component-level metrics settings.
219+
// end::controlling-by-component-summary[]
220+
167221
// tag::KPI[]
168222
==== Key Performance Indicator (KPI) Metrics
169223
Any time you include the Helidon metrics module in your application, Helidon tracks two basic performance indicator metrics:
@@ -185,23 +239,8 @@ You can enable and control these metrics using configuration:
185239
metrics.key-performance-indicators.extended = true
186240
metrics.key-performance-indicators.long-running.threshold-ms = 2000
187241
----
188-
189-
ifeval::["{h1Prefix}" == "SE"]
190-
Your Helidon {h1Prefix} application can also control the behavior of the KPI metrics programmatically.
191-
192-
. Prepare a `KeyPerformanceIndicatorSettings` object, using its builder, and then pass the builder when invoking the `MetricsSupport.Builder#keyPerformanceIndicatorMetricsSettings()` method.
193-
. Prepare a `Config` object and pass it to the `MetricsSupport.Builder#keyPerformanceIndicatorMetricsConfig()` method.
194-
+
195-
[source,properties]
196-
+
197-
.Example KPI metrics config fragment
198-
----
199-
extended = true
200-
long-running.threshold-ms = 2000
201-
----
202-
endif::[]
203-
204242
// end::KPI[]
243+
// end::controlling[]
205244
206245
// tag::metrics-metadata[]
207246
=== Metrics Metadata

docs/mp/guides/05_metrics.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ You can see the results in the sample output shown in earlier example runs.
8585
8686
include::{metrics-common}[tag=KPI]
8787
88+
include::{metrics-common}[tag=controlling]
89+
8890
include::{metrics-common}[tag=metrics-metadata]
8991
9092
=== Application-Specific Metrics Data

docs/se/guides/05_metrics.adoc

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
:common-page-prefix-inc: ../../shared/common_prereqs/common_prereqs.adoc
2424
:common-guides: ../../common/guides
2525
:metrics-common: {common-guides}/metrics.adoc
26-
:javadoc-base-url-api: {javadoc-base-url}io.helidon.metrics/io/helidon/metrics
26+
:javadoc-base-url-api: {javadoc-base-url}io.helidon.metrics.api/io/helidon/metrics/api
2727
:metrics-support-builder-javadoc: {javadoc-base-url-api}/MetricsSupport.Builder.html
2828
:lower-case-flavor: se
2929
:intro-project-name: {h1Prefix}
@@ -37,14 +37,7 @@ The generated source code is already configured for both metrics and health chec
3737
3838
[source,xml]
3939
.Notice that the metrics dependency is already in the project's pom.xml file:
40-
----
41-
<dependency>
42-
<groupId>io.helidon.metrics</groupId>
43-
<artifactId>helidon-metrics</artifactId>
44-
</dependency>
45-
----
46-
47-
40+
include::{metrics-common}[tag=metrics-dependency]
4841
[source,java]
4942
.Replace the `Main.createRouting` method with the following code:
5043
----
@@ -89,9 +82,96 @@ include::{metrics-common}[tag=vendor-metrics-json-output]
8982
9083
include::{metrics-common}[tag=get-single-metric]
9184
85+
////
86+
Controlling Metrics section
87+
88+
For the SE guide, we stitch in the shared content about controlling metrics around SE-specific content that is not shared. It makes for a bit choppy-looking source here but it renders just fine and lets us easily share the content that's common between SE and MP.
89+
90+
////
91+
include::{metrics-common}[tag=controlling-intro]
92+
93+
Your Helidon SE application can also control metrics programmatically as described in the following sections.
94+
95+
include::{metrics-common}[tag=enabling-disabling-whole-intro]
96+
97+
A Helidon SE application can disable metrics programmatically.
98+
[source,java]
99+
.Disable all metrics behavior
100+
----
101+
import io.helidon.metrics.api.MetricsSettings;
102+
import io.helidon.metrics.serviceapi.MetricsSupport;
103+
import io.helidon.metrics.api.RegistryFactory;
104+
...
105+
106+
MetricsSettings metricsSettings = MetricsSettings.builder()
107+
.enabled(false)
108+
.build(); // <1>
109+
110+
MetricsSupport metricsSupport = MetricsSupport.create(metricsSettings); // <2>
111+
112+
RegistryFactory registryFactory = RegistryFactory.getInstance(metricsSettings); // <3>
113+
----
114+
<1> Create a link:{javadoc-base-url-api}/MetricsSettings.html[`MetricsSettings]` instance (via its link:{javadoc-base-url-api}/MetricsSettings.Builder.html[`Builder`]) with metrics disabled.
115+
<2> Get a link:{javadoc-base-url-api}/MetricsSupport.html[`MetricsSupport]` service (usable in setting routing rules) that responds
116+
to the `/metrics` endpoint with `404` and an explanatory message.
117+
<3> Get a link:{javadoc-base-url-api}/RegistryFactory.html[`RegistryFactory]` instance that provides `MetricRegistry` instances which register
118+
no-op metric objects (counters, timers, etc.).
119+
120+
These builders and interfaces also have methods which accept `Config` objects representing the `metrics` node from the application configuration.
121+
122+
include::{metrics-common}[tag=enabling-disabling-whole-summary]
123+
92124
include::{metrics-common}[tag=KPI]
93125
94-
Your Helidon SE application can also enable or disable the extended KPI metrics and control the long-running request threshold using methods on the link:{metrics-support-builder-javadoc}[MetricsSupport.Builder] class.
126+
Your Helidon SE application can also control the KPI settings programmatically.
127+
[source,java]
128+
.Assign KPI metrics behavior from code
129+
----
130+
import io.helidon.metrics.api.KeyPerformanceIndicatorMetricsSettings;
131+
import io.helidon.metrics.api.MetricsSettings;
132+
import io.helidon.metrics.serviceapi.MetricsSupport;
133+
import io.helidon.metrics.api.RegistryFactory;
134+
...
135+
136+
KeyPerformanceIndicatorMetricsSettings.Builder kpiSettingsBuilder =
137+
KeyPerformanceIndicatorMetricsSettings.builder()
138+
.extended(true)
139+
.longRunningThresholdMs(2000); <1>
140+
141+
MetricsSettings metricsSettings = MetricsSettings.builder()
142+
.keyPerformanceIndicatorSettings(kpiSettingsBuilder)
143+
.build(); // <2>
144+
----
145+
<1> Create a link:{javadoc-base-url-api}/KeyPerformanceIndicatorMetricsSettings.html[`KeyPerformanceIndicatorMetricsSettings]` instance (via its link:{javadoc-base-url-api}/KeyPerformanceIndicatorMetricsSettings.Builder.html[`Builder`]) with non-default values.
146+
<2> Create a link:{javadoc-base-url-api}/MetricsSettings.html[`MetricsSettings]` instance reflecting the KPI settings.
147+
148+
include::{metrics-common}[tag=controlling-by-component-intro]
149+
150+
Your Helidon SE application can disable a metrics-capable component's use of metrics programmatically.
151+
152+
[source,java]
153+
.Disable metrics use in a metrics-capable component
154+
----
155+
import io.helidon.metrics.api.ComponentMetricsSettings;
156+
...
157+
158+
ComponentMetricsSettings.Builder componentMetricsSettingsBuilder = ComponentMetricsSettings.builder()
159+
.enabled(false); // <1>
160+
161+
SomeService someService = SomeService.builder()
162+
...
163+
.componentMetricsSettings(componentMetricsSettingsBuilder)
164+
...
165+
.build(); // <2>
166+
167+
----
168+
<1> Create a link:{javadoc-base-url-api}/ComponentMetricsSettings.html[`ComponentMetricsSettings]` instance (via its link:{javadoc-base-url-api}/ComponentMetricsSettings.Builder.html[`Builder`]) indicating that metrics usage should be disabled.
169+
<2> Create an instance of the service with its metrics usage disabled.
170+
171+
include::{metrics-common}[tag=controlling-by-component-summary]
172+
173+
174+
// end of Controlling Metrics section
95175
96176
include::{metrics-common}[tag=metrics-metadata]
97177
@@ -654,6 +734,9 @@ curl -H "Accept: application/json" http://localhost:8080/metrics/application
654734
655735
include::{metrics-common}[tag=k8s-and-prometheus-integration]
656736
737+
=== Advanced Topic: Metrics-capable Applications and Components
738+
Metrics-capable components are written to register and update metrics but can work even if the Helidon metrics implementation _is not_ on the runtime path. Please see this additional section on <<se/guides/_metrics-capable-components.adoc,Metrics-capable components>> which describes this feature.
739+
657740
=== Summary
658741
659742
This guide demonstrated how to use metrics in a Helidon SE application using various combinations of

0 commit comments

Comments
 (0)