Skip to content

Commit

Permalink
Issue helidon-io#6991 - Blocking DB Client: Common metrics module
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Kraus <tomas.kraus@oracle.com>
  • Loading branch information
Tomas-Kraus committed Jul 7, 2023
1 parent 760ce55 commit d54cc76
Show file tree
Hide file tree
Showing 17 changed files with 889 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ public T mapperManager(MapperManager manager) {
return identity();
}

@Override
public T dbMapperManager(DbMapperManager manager) {
this.dbMapperManager = manager;
return identity();
}

@Override
public T addMapperProvider(DbMapperProvider provider) {
this.dbMapperBuilder.addMapperProvider(provider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public abstract class CommonService implements DbClientService {
*
* @param builder builder to configure predicate to use
*/
protected CommonService(CommonServiceBuilder<?> builder) {
protected CommonService(CommonServiceBuilder<?, ?> builder) {
this.predicate = builder.predicate();
}

Expand All @@ -68,8 +68,9 @@ public final DbClientServiceContext statement(DbClientServiceContext context) {
* A base class for builders of {@link CommonService}.
*
* @param <B> type of the builder extending this class
* @param <T> Type of the built {@link CommonService} instance
*/
public abstract static class CommonServiceBuilder<B extends CommonServiceBuilder<B>> implements Builder<B, CommonService> {
public abstract static class CommonServiceBuilder<B extends CommonServiceBuilder<B, T>, T extends CommonService> implements Builder<B, T> {
private static final Predicate<DbClientServiceContext> YES = it -> true;
private static final Predicate<DbClientServiceContext> NO = it -> false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.helidon.dbclient.DbClient;
import io.helidon.dbclient.DbClientService;
import io.helidon.dbclient.DbMapper;
import io.helidon.dbclient.DbMapperManager;
import io.helidon.dbclient.DbStatements;

/**
Expand Down Expand Up @@ -108,6 +109,14 @@ public interface DbClientBuilder<T extends DbClientBuilder<T>> extends Builder<T
*/
T mapperManager(MapperManager manager);

/**
* Mapper manager of all configured {@link DbMapper mappers}.
*
* @param manager mapper manager
* @return updated builder instance
*/
T dbMapperManager(DbMapperManager manager);

/**
* Add an interceptor.
* This allows to add implementation of tracing, metrics, logging etc. without the need to hard-code these into
Expand Down
18 changes: 18 additions & 0 deletions dbclient/jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,22 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.helidon.common.features</groupId>
<artifactId>helidon-common-features-processor</artifactId>
<version>${helidon.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

</project>
72 changes: 72 additions & 0 deletions dbclient/metrics/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2023 Oracle and/or its affiliates.
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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.dbclient</groupId>
<artifactId>helidon-dbclient-project</artifactId>
<version>4.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>helidon-dbclient-metrics</artifactId>
<name>Helidon DB Client Metrics</name>
<description>Helidon Database Client Metrics</description>

<dependencies>
<dependency>
<groupId>io.helidon.dbclient</groupId>
<artifactId>helidon-dbclient-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.metrics</groupId>
<artifactId>helidon-metrics-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common.features</groupId>
<artifactId>helidon-common-features-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.metrics</groupId>
<artifactId>helidon-metrics</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.helidon.common.features</groupId>
<artifactId>helidon-common-features-processor</artifactId>
<version>${helidon.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* 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 io.helidon.dbclient.metrics;

import io.helidon.dbclient.common.CommonService;

/**
* DB Client metric builder.
*
* @param <B> type of the builder extending this class
* @param <T> Type of the built {@link CommonService} instance
*/
public abstract class DbClientMetricBuilder<B extends DbClientMetricBuilder<B, T>, T extends CommonService>
extends MetricBuilderBase<B, T> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* 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 io.helidon.dbclient.metrics;

import io.helidon.dbclient.common.CommonService;

/**
* Utility class to obtain various types of metrics to register
* with {@link io.helidon.dbclient.DbClient.Builder#addService(io.helidon.dbclient.DbClientService)}.
* Metrics can be limited to a set of statement types or statement names, and also configured to
* meter success, failure or both.
*
* @see DbClientMetricBuilder#statementTypes(io.helidon.dbclient.DbStatementType...)
* @see DbClientMetricBuilder#statementNames(String...)
* @see DbClientMetricBuilder#statementPredicate(java.util.function.Predicate)
* @see DbClientMetricBuilder#success(boolean)
* @see DbClientMetricBuilder#errors(boolean)
*/
public class DbClientMetrics {
private DbClientMetrics() {
}

/**
* Create a counter builder, to be registered
* with {@link io.helidon.dbclient.DbClient.Builder#addService(java.util.function.Supplier)}.
*
* @return a new counter builder
* @see org.eclipse.microprofile.metrics.Counter
*/
public static DbClientMetricBuilder<? extends DbClientMetricBuilder<?, ?>, ? extends CommonService> counter() {
return MetricCounter.builder();
}

/**
* Create a meter builder, to be registered
* with {@link io.helidon.dbclient.DbClient.Builder#addService(java.util.function.Supplier)}.
*
* @return a new meter builder
* @see org.eclipse.microprofile.metrics.Meter
*/
public static DbClientMetricBuilder<? extends DbClientMetricBuilder<?, ?>, ? extends CommonService> meter() {
return MetricMeter.builder();
}

/**
* Create a timer builder, to be registered
* with {@link io.helidon.dbclient.DbClient.Builder#addService(java.util.function.Supplier)}.
*
* @return a new timer builder
* @see org.eclipse.microprofile.metrics.Timer
*/
public static DbClientMetricBuilder<? extends DbClientMetricBuilder<?, ?>, ? extends CommonService> timer() {
return MetricTimer.builder();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* 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 io.helidon.dbclient.metrics;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import io.helidon.config.Config;
import io.helidon.dbclient.DbClientException;
import io.helidon.dbclient.DbClientService;
import io.helidon.dbclient.spi.DbClientServiceProvider;

/**
* Java service loader service for DB metrics.
*/
public class DbClientMetricsProvider implements DbClientServiceProvider {
private static final System.Logger LOGGER = System.getLogger(DbClientMetricsProvider.class.getName());

@Override
public String configKey() {
return "metrics";
}

@Override
public Collection<DbClientService> create(Config config) {
List<Config> metricConfigs = config.asNodeList().orElseGet(List::of);
List<DbClientService> result = new LinkedList<>();

for (Config metricConfig : metricConfigs) {
result.add(fromConfig(metricConfig));
}

if (result.isEmpty()) {
LOGGER.log(System.Logger.Level.INFO, "DB Client metrics are enabled, yet none are configured in config.");
}

return result;
}

private DbClientService fromConfig(Config config) {
String type = config.get("type").asString().orElse("COUNTER");
switch (type) {
case "COUNTER":
return DbClientMetrics.counter().config(config).build();
case "METER":
return DbClientMetrics.meter().config(config).build();
case "TIMER":
return DbClientMetrics.timer().config(config).build();
default:
throw new DbClientException("Metrics type " + type + " is not supported through service loader");
}
}
}
Loading

0 comments on commit d54cc76

Please sign in to comment.