|
| 1 | +--- |
| 2 | +date: 2020-05-09 |
| 3 | +title: "Azkarra Metrics" |
| 4 | +linkTitle: "Azkarra Metrics" |
| 5 | +weight: 40 |
| 6 | +description: > |
| 7 | + Integration between Azkarra and Micrometer |
| 8 | +--- |
| 9 | + |
| 10 | +This section describes how you can configure Azkarra Metrics. |
| 11 | + |
| 12 | +## 1 Introduction |
| 13 | + |
| 14 | +This project integrates Azkarra and [Micrometer](https://micrometer.io/) a metrics instrumentation library for JVM-based applications. |
| 15 | + |
| 16 | +Azkarra Metrics is available since Azkarra Streams `0.7.0`. |
| 17 | + |
| 18 | +## 2 Configuring |
| 19 | + |
| 20 | +For using Azkarra Metrics, you will first have to include the `azkarra-metrics` dependency to your project. |
| 21 | + |
| 22 | +* **Maven** : |
| 23 | +```xml |
| 24 | +<dependency> |
| 25 | + <groupId>io.streamthoughts</groupId> |
| 26 | + <artifactId>azkarra-metrics</artifactId> |
| 27 | + <version>0.7.0</version> |
| 28 | +</dependency> |
| 29 | +``` |
| 30 | + |
| 31 | +If `component-scan` is enable, then Azkarra will automatically discover and configure all necessary components. |
| 32 | + |
| 33 | +Then, Azkarra Metrics will only be enable if you configure the `azkarra.metrics.enable` property into you `application.conf` file: |
| 34 | + |
| 35 | +**Property**: |
| 36 | + |
| 37 | +```hocon |
| 38 | +azkarra { |
| 39 | + metrics { |
| 40 | + enable = true |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## 3 Micrometer Concepts |
| 46 | + |
| 47 | +Micrometer defines several [concepts](https://micrometer.io/docs/concepts) in order to collect and expose measurements. |
| 48 | + |
| 49 | +## 3.1 `MeterRegistry` and `Meter` |
| 50 | + |
| 51 | +The two main concepts defines by Micrometer are : |
| 52 | + |
| 53 | +* [`Meter`](http://micrometer.io/docs/concepts#_meters): The main interface for collecting individuals metrics. Micrometers packs with differents types of `Meter` (e.g `Timer`, `Counter`, `Gauge`, etc.). Each meter is uniquely identified by its name and dimensions. |
| 54 | + |
| 55 | +* [`MeterRegistry`](http://micrometer.io/docs/concepts#_registry) : A `MeterRegistry` is the core component used to create and hold `Meters`. Depending on the registry that you used, meters can be hold in memory or be published in an external monitoring system (e.g: `Graphite`, `Elastic`, `Datadog`, etc). |
| 56 | + |
| 57 | +Azkarra Streams builds a primary [`CompositeMeterRegistry`](http://micrometer.io/docs/concepts#_composite_registries) |
| 58 | +that will contain all `MeterRegistry` registered in AzkaraContext. |
| 59 | + |
| 60 | +**Example using [GraphiteRegistry](http://micrometer.io/docs/registry/graphite):** |
| 61 | + |
| 62 | +```java |
| 63 | +@Factory |
| 64 | +public class GraphiteMeterRegistryFactory implements Configureable { |
| 65 | + |
| 66 | + private Conf configs |
| 67 | + |
| 68 | + @Override |
| 69 | + public void configure(final Conf configs) { |
| 70 | + this.configs = configs; |
| 71 | + } |
| 72 | + |
| 73 | + @Component |
| 74 | + @Singleton |
| 75 | + @ConditionalOnMetricsEnable |
| 76 | + public FileDescriptorMetrics graphiteMeterRegistry() { |
| 77 | + GraphiteConfig graphiteConfig = new GraphiteConfig() { |
| 78 | + @Override |
| 79 | + public String host() { |
| 80 | + return configs.getString("graphite.host"); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public String get(String k) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + }; |
| 88 | + MeterRegistry registry = new GraphiteMeterRegistry(graphiteConfig, Clock.SYSTEM, HierarchicalNameMapper.DEFAULT) |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### 3.1.2 `MeterRegistryConfigurer` |
| 94 | + |
| 95 | +Azkarra allows you to automatically apply some customizations on registered `MeterRegistry`. |
| 96 | + |
| 97 | +Any component that implements the `MeterRegistryConfigurer` interface is applied on supported `MeterRegistry` during |
| 98 | +the `AzkarraContext` initialization. |
| 99 | + |
| 100 | +**The interface**: |
| 101 | + |
| 102 | +```java |
| 103 | +/* |
| 104 | + * Copyright 2019 StreamThoughts. |
| 105 | + * |
| 106 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 107 | + * contributor license agreements. See the NOTICE file distributed with |
| 108 | + * this work for additional information regarding copyright ownership. |
| 109 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 110 | + * (the "License"); you may not use this file except in compliance with |
| 111 | + * the License. You may obtain a copy of the License at |
| 112 | + * |
| 113 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 114 | + * |
| 115 | + * Unless required by applicable law or agreed to in writing, software |
| 116 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 117 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 118 | + * See the License for the specific language governing permissions and |
| 119 | + * limitations under the License. |
| 120 | + */ |
| 121 | +package io.streamthoughts.azkarra.metrics.micrometer; |
| 122 | + |
| 123 | +import io.micrometer.core.instrument.MeterRegistry; |
| 124 | + |
| 125 | +/** |
| 126 | + * Interface that is used to configure a specific type of {@link MeterRegistry}. |
| 127 | + * |
| 128 | + * Any component that implements this interface will be applied on the supported {@link MeterRegistry} during |
| 129 | + * the {@link io.streamthoughts.azkarra.api.components.ComponentFactory} initialization. |
| 130 | + * |
| 131 | + * @see MeterRegistryFactory |
| 132 | + * @see MicrometerMeterRegistryConfigurer |
| 133 | + * |
| 134 | + * @param <T> the meter registry type. |
| 135 | + */ |
| 136 | +@FunctionalInterface |
| 137 | +public interface MeterRegistryConfigurer<T extends MeterRegistry> { |
| 138 | + |
| 139 | + /** |
| 140 | + * Applies this configurer to the given meter registry. |
| 141 | + * |
| 142 | + * @param meterRegistry the {@link MeterRegistry} to configure. |
| 143 | + */ |
| 144 | + void apply(final T meterRegistry); |
| 145 | + |
| 146 | + /** |
| 147 | + * Checks whether this configurer supports the given meter registry type. |
| 148 | + * |
| 149 | + * @param meterRegistry a {@link MeterRegistry}. |
| 150 | + * @return boolean {@code true} if the meter registry is supported. |
| 151 | + */ |
| 152 | + default boolean supports(final T meterRegistry) { |
| 153 | + return true; |
| 154 | + } |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +**Example**: |
| 159 | + |
| 160 | +```java |
| 161 | +/** |
| 162 | + * Adds common tag `azkarra.version` on all meters. |
| 163 | + */ |
| 164 | +@Component |
| 165 | +public class VersionTagRegistryConfigurer implements MeterRegistryConfigurer { |
| 166 | + |
| 167 | + @Override |
| 168 | + public void apply(final MeterRegistry meterRegistry) { |
| 169 | + meterRegistry.config().commonTags("azkarra.version", AzkarraVersion.getVersion()); |
| 170 | + } |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +## 3.2 `MeterFilter` |
| 175 | + |
| 176 | +Micrometer allows you to configure meter filters on `MeterRegistry` in order to control over how and when meters are registered (see: [`MeterFilter`](https://micrometer.io/docs/concepts#_meter_filters)). |
| 177 | + |
| 178 | +Any component that implements the `MeterFilter` interface will be applied to all registries. |
| 179 | + |
| 180 | +```java |
| 181 | +@Factory |
| 182 | +public class MeterFilterFactory { |
| 183 | + |
| 184 | + /** |
| 185 | + * Deny all metrics starting with jvm. |
| 186 | + * @return {@link MeterFilter} |
| 187 | + */ |
| 188 | + @Component |
| 189 | + @Singleton |
| 190 | + MeterFilter jvmDenyFilter() { |
| 191 | + return MeterFilter.denyNameStartsWith("jvm"); |
| 192 | + } |
| 193 | +``` |
| 194 | + |
| 195 | +## 3.3 `MeterBinder` |
| 196 | + |
| 197 | +Micrometers provides several built-in [binders](https://github.com/micrometer-metrics/micrometer/tree/master/micrometer-core/src/main/java/io/micrometer/core/instrument/binder) to monitor the JVM, caches, ExecutorService, etc. |
| 198 | + |
| 199 | +Azkarra allows you to easily enable some of them through configuration. |
| 200 | + |
| 201 | +### 3.3.1 Configuring binders |
| 202 | + |
| 203 | +#### Kafka Streams |
| 204 | + |
| 205 | +Azkarra Metrics provides the `MeterKafkaStreamsInterceptor` class that is responsible to configure and register the [KafkaStreamsMetrics](https://github.com/micrometer-metrics/micrometer/blob/master/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/kafka/KafkaStreamsMetrics.java) binder for each running Kafka Streams instance. |
| 206 | + |
| 207 | +A tag `application_id` is added to each metrics. |
| 208 | + |
| 209 | +`MeterKafkaStreamsInterceptor`, will only be enable if you configure the `metrics.binders.kafkastreams.enable` property. |
| 210 | + |
| 211 | +**Property**: |
| 212 | + |
| 213 | +```hocon |
| 214 | +azkarra { |
| 215 | + metrics { |
| 216 | + binders.kafkastreams.enable = true |
| 217 | + } |
| 218 | +} |
| 219 | +``` |
| 220 | + |
| 221 | +#### JVM |
| 222 | + |
| 223 | +Azkarra Streams can also registered the [JVM metrics binders](https://micrometer.io/docs/ref/jvm) for you if you enable the `metrics.binders.jvm.enable` property. |
| 224 | + |
| 225 | +The JVM metrics binders provides several JVM metrics on classloaders, memory, garbage collection, threads, etc. |
| 226 | + |
| 227 | +**Property**: |
| 228 | +```hocon |
| 229 | +azkarra { |
| 230 | + metrics { |
| 231 | + binders.jvm.enable = true |
| 232 | + } |
| 233 | +} |
| 234 | +``` |
| 235 | + |
| 236 | +### 3.3.2 Adding custom binders |
| 237 | + |
| 238 | +You can easely add a new binder by registering it as a component. `Azkarra Metrics` will automatically retrieve all registered components of type `MeterBinder`. |
| 239 | + |
| 240 | +**Example:** |
| 241 | +```java |
| 242 | +/** |
| 243 | + * Get file descriptor metrics gathered by the JVM. |
| 244 | + **/ |
| 245 | +@Factory |
| 246 | +public class FileDescriptorMetricsBinderFactory { |
| 247 | + |
| 248 | + @Component |
| 249 | + @Singleton |
| 250 | + @ConditionalOnMetricsEnable |
| 251 | + public FileDescriptorMetrics fileDescriptorMetrics() { |
| 252 | + return new FileDescriptorMetrics(); |
| 253 | + } |
| 254 | +} |
| 255 | +``` |
| 256 | + |
| 257 | +## 4 Prometheus Endpoint |
| 258 | + |
| 259 | +Azkarra Metrics provides a REST extension to expose all registered metrics via the `/prometheus` endpoint. |
| 260 | + |
| 261 | + |
| 262 | +**Property**: |
| 263 | +```hocon |
| 264 | +azkarra { |
| 265 | + metrics { |
| 266 | + endpoints.prometheus.enable = true |
| 267 | + } |
| 268 | + |
| 269 | + server { |
| 270 | + rest.extensions.enable = true |
| 271 | + } |
| 272 | +} |
| 273 | +``` |
| 274 | + |
| 275 | +N.B : Azkarra server must configured with REST extensions enable for the prometheus endpoints being accessible. |
| 276 | + |
0 commit comments