Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1439 from beckje01/ratpack-micrometer
Micrometer
- Loading branch information
Showing
14 changed files
with
630 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
description = "Provides integration with Micrometer - https://micrometer.io/" | ||
|
||
apply from: "$rootDir/gradle/javaModule.gradle" | ||
|
||
ext { | ||
micrometerVersion = "1.0.7" | ||
} | ||
dependencies { | ||
compile project(":ratpack-core") | ||
compile project(":ratpack-guice") | ||
|
||
compile "io.micrometer:micrometer-core:$micrometerVersion" | ||
compile "io.micrometer:micrometer-registry-prometheus:$micrometerVersion" | ||
} |
28 changes: 28 additions & 0 deletions
28
ratpack-micrometer/src/main/java/ratpack/micrometer/MicrometerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ratpack.micrometer; | ||
|
||
import java.util.Map; | ||
|
||
public class MicrometerConfig { | ||
public final String application; | ||
public final Map<String, String> groups; | ||
|
||
public MicrometerConfig(String application, Map<String, String> groups) { | ||
this.application = application; | ||
this.groups = groups; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
ratpack-micrometer/src/main/java/ratpack/micrometer/PrometheusHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ratpack.micrometer; | ||
|
||
import io.micrometer.prometheus.PrometheusMeterRegistry; | ||
import io.prometheus.client.exporter.common.TextFormat; | ||
import ratpack.handling.Context; | ||
import ratpack.handling.Handler; | ||
import ratpack.http.Status; | ||
|
||
import javax.inject.Inject; | ||
|
||
class PrometheusHandler implements Handler { | ||
private final PrometheusMeterRegistry registry; | ||
|
||
@Inject | ||
public PrometheusHandler(PrometheusMeterRegistry registry) { | ||
this.registry = registry; | ||
} | ||
|
||
@Override | ||
public void handle(Context ctx) { | ||
ctx.getResponse().contentType(TextFormat.CONTENT_TYPE_004).status(Status.OK).send(registry.scrape()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
ratpack-micrometer/src/main/java/ratpack/micrometer/PrometheusHandlerModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ratpack.micrometer; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.google.inject.multibindings.Multibinder; | ||
import ratpack.handling.HandlerDecorator; | ||
import ratpack.micrometer.internal.DefaultRequestTimingHandler; | ||
|
||
import static ratpack.handling.Handlers.chain; | ||
|
||
public class PrometheusHandlerModule extends AbstractModule { | ||
|
||
@Override | ||
public void configure() { | ||
bind(PrometheusHandler.class); | ||
bind(RequestTimingHandler.class).to(DefaultRequestTimingHandler.class); | ||
|
||
Multibinder.newSetBinder(binder(), HandlerDecorator.class) | ||
.addBinding() | ||
.toInstance((registry, rest) -> chain(chain(registry, chain -> | ||
chain.get("metrics", PrometheusHandler.class)))); | ||
} | ||
} | ||
|
92 changes: 92 additions & 0 deletions
92
ratpack-micrometer/src/main/java/ratpack/micrometer/PrometheusMicrometerModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ratpack.micrometer; | ||
|
||
import com.google.inject.Provides; | ||
import io.micrometer.core.instrument.Meter; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics; | ||
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics; | ||
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics; | ||
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; | ||
import io.micrometer.core.instrument.binder.logging.LogbackMetrics; | ||
import io.micrometer.core.instrument.binder.system.ProcessorMetrics; | ||
import io.micrometer.core.instrument.binder.system.UptimeMetrics; | ||
import io.micrometer.core.instrument.composite.CompositeMeterRegistry; | ||
import io.micrometer.core.instrument.config.MeterFilter; | ||
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig; | ||
import io.micrometer.prometheus.PrometheusConfig; | ||
import io.micrometer.prometheus.PrometheusMeterRegistry; | ||
import ratpack.guice.ConfigurableModule; | ||
import ratpack.micrometer.internal.DefaultRequestTimingHandler; | ||
|
||
import javax.inject.Singleton; | ||
import java.time.Duration; | ||
|
||
public class PrometheusMicrometerModule extends ConfigurableModule<MicrometerConfig> { | ||
|
||
@Override | ||
public void configure() { | ||
bind(RequestTimingHandler.class).to(DefaultRequestTimingHandler.class); | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
PrometheusMeterRegistry providesPrometheus() { | ||
return new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
CompositeMeterRegistry providesCompositeMeterRegistry() { | ||
return new CompositeMeterRegistry(); | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
MeterRegistry providesMeterRegistry(PrometheusMeterRegistry prometheus, | ||
CompositeMeterRegistry composite, | ||
MicrometerConfig config) { | ||
prometheus.config().meterFilter(new MeterFilter() { | ||
@Override | ||
public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) { | ||
return DistributionStatisticConfig.builder() | ||
.percentilesHistogram(true) | ||
.minimumExpectedValue(Duration.ofMillis(10).toNanos()) | ||
.maximumExpectedValue(Duration.ofSeconds(5).toNanos()) | ||
.build() | ||
.merge(config); | ||
} | ||
}); | ||
|
||
prometheus.config().commonTags("application", config.application); | ||
|
||
composite.add(prometheus); | ||
|
||
new ClassLoaderMetrics().bindTo(composite); | ||
new JvmMemoryMetrics().bindTo(composite); | ||
new JvmGcMetrics().bindTo(composite); | ||
new ProcessorMetrics().bindTo(composite); | ||
new JvmThreadMetrics().bindTo(composite); | ||
new LogbackMetrics().bindTo(composite); | ||
new UptimeMetrics().bindTo(composite); | ||
|
||
return composite; | ||
} | ||
} | ||
|
||
|
||
|
22 changes: 22 additions & 0 deletions
22
ratpack-micrometer/src/main/java/ratpack/micrometer/RequestTimingHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ratpack.micrometer; | ||
|
||
import ratpack.handling.Handler; | ||
|
||
@FunctionalInterface | ||
public interface RequestTimingHandler extends Handler { | ||
} |
78 changes: 78 additions & 0 deletions
78
...ack-micrometer/src/main/java/ratpack/micrometer/internal/DefaultRequestTimingHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ratpack.micrometer.internal; | ||
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
import ratpack.handling.Context; | ||
import ratpack.micrometer.MicrometerConfig; | ||
import ratpack.micrometer.RequestTimingHandler; | ||
|
||
import javax.inject.Inject; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class DefaultRequestTimingHandler implements RequestTimingHandler { | ||
private final MeterRegistry meterRegistry; | ||
private final MicrometerConfig metricsConfig; | ||
|
||
@Inject | ||
public DefaultRequestTimingHandler(MeterRegistry meterRegistry, | ||
MicrometerConfig metricsConfig) { | ||
this.meterRegistry = meterRegistry; | ||
this.metricsConfig = metricsConfig; | ||
} | ||
|
||
@Override | ||
public void handle(Context ctx) { | ||
ctx.onClose((outcome) -> { | ||
String statusCode = String.valueOf(outcome.getResponse().getStatus().getCode()); | ||
meterRegistry.timer("http.requests", | ||
"status", statusCode, | ||
"path", findPathGroup(outcome.getRequest().getPath()), | ||
"method", outcome.getRequest().getMethod().getName().toLowerCase()) | ||
.record(outcome.getDuration().toNanos(), TimeUnit.NANOSECONDS); | ||
|
||
meterRegistry.timer("http.server.requests", "status", statusCode).record(outcome.getDuration().toNanos(), TimeUnit.NANOSECONDS); | ||
} | ||
); | ||
ctx.next(); | ||
} | ||
|
||
private String findPathGroup(String requestPath) { | ||
String tagName = "".equals(requestPath) ? "root" : requestPath; | ||
|
||
for (Map.Entry<String, String> metricGrouping : metricsConfig.groups.entrySet()) { | ||
Pattern pattern = Pattern.compile(metricGrouping.getValue()); | ||
Matcher match = pattern.matcher(requestPath); | ||
|
||
if (requestPath.matches(metricGrouping.getValue())) { | ||
tagName = metricGrouping.getKey(); | ||
} | ||
|
||
if (match.groupCount() > 1) { | ||
while (match.find()) { | ||
for (int index = 1; index <= match.groupCount(); index++) { | ||
tagName = tagName.replace("$" + index, match.group(index)); | ||
} | ||
} | ||
} | ||
} | ||
return tagName; | ||
} | ||
} | ||
|
22 changes: 22 additions & 0 deletions
22
ratpack-micrometer/src/main/java/ratpack/micrometer/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Integration with <a href="https://micrometer.io/docs/concepts">Micrometer</a>. | ||
* <p> | ||
* See {@link ratpack.micrometer.PrometheusMicrometerModule} to get started. | ||
*/ | ||
package ratpack.micrometer; |
Oops, something went wrong.