Skip to content

Commit

Permalink
Add a method to check if a metric category is enabled to the plugin A…
Browse files Browse the repository at this point in the history
…PI (#7832)

Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 authored Oct 30, 2024
1 parent 22a570e commit ba86ce1
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Create and publish Besu BOM (Bill of Materials) [#7615](https://github.com/hyperledger/besu/pull/7615)
- Update Java dependencies [#7786](https://github.com/hyperledger/besu/pull/7786)
- Add a method to get all the transaction in the pool, to the `TransactionPoolService`, to easily access the transaction pool content from plugins [#7813](https://github.com/hyperledger/besu/pull/7813)
- Add a method to check if a metric category is enabled to the plugin API [#7832](https://github.com/hyperledger/besu/pull/7832)

### Bug fixes
- Fix registering new metric categories from plugins [#7825](https://github.com/hyperledger/besu/pull/7825)
Expand Down
4 changes: 3 additions & 1 deletion besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -1899,7 +1899,9 @@ public MetricsConfiguration metricsConfiguration() {
? p2PDiscoveryOptions.autoDiscoverDefaultIP().getHostAddress()
: metricsOptions.getMetricsPushHost())
.hostsAllowlist(hostsAllowlist);
return metricsConfigurationBuilder.build();
final var metricsConfiguration = metricsConfigurationBuilder.build();
metricCategoryRegistry.setMetricsConfiguration(metricsConfiguration);
return metricsConfiguration;
}

private PrivacyParameters privacyParameters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ protected MetricsOptions optionsFromDomainObject(
protected MetricsOptions getOptionsFromBesuCommand(final TestBesuCommand besuCommand) {
return besuCommand.getMetricsOptions();
}

@Override
protected String[] getNonOptionFields() {
return new String[] {"metricCategoryRegistry"};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/
package org.hyperledger.besu.metrics;

import static com.google.common.base.Preconditions.checkNotNull;

import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration;
import org.hyperledger.besu.plugin.services.metrics.MetricCategory;
import org.hyperledger.besu.plugin.services.metrics.MetricCategoryRegistry;

Expand All @@ -25,6 +28,7 @@
/** The Metric category registry implementation. */
public class MetricCategoryRegistryImpl implements MetricCategoryRegistry {
private final Map<String, MetricCategory> metricCategories = new HashMap<>();
private MetricsConfiguration metricsConfiguration;

/** Default constructor */
public MetricCategoryRegistryImpl() {}
Expand All @@ -49,6 +53,14 @@ public void addMetricCategory(final MetricCategory metricCategory) {
metricCategories.put(metricCategory.getName().toUpperCase(Locale.ROOT), metricCategory);
}

@Override
public boolean isMetricCategoryEnabled(final MetricCategory metricCategory) {
checkNotNull(
metricsConfiguration, "Metrics configuration must be set before calling this method");
return (metricsConfiguration.isEnabled() || metricsConfiguration.isPushEnabled())
&& metricsConfiguration.getMetricCategories().contains(metricCategory);
}

/**
* Return true if a category with that name is already registered
*
Expand All @@ -68,4 +80,14 @@ public boolean containsMetricCategory(final String name) {
public MetricCategory getMetricCategory(final String name) {
return metricCategories.get(name.toUpperCase(Locale.ROOT));
}

/**
* Set the metric configuration via a method since it is still not available when creating this
* object
*
* @param metricsConfiguration the metrics configuration
*/
public void setMetricsConfiguration(final MetricsConfiguration metricsConfiguration) {
this.metricsConfiguration = metricsConfiguration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright contributors to Besu.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.metrics;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration;

import java.util.Set;

import org.junit.jupiter.api.Test;

class MetricCategoryRegistryImplTest {

@Test
void metricCategoryIsEnabledAndMetricsAreEnabled() {
final var registry = new MetricCategoryRegistryImpl();
registry.addMetricCategory(BesuMetricCategory.BLOCKCHAIN);
final var metricsConfiguration =
MetricsConfiguration.builder()
.enabled(true)
.metricCategories(Set.of(BesuMetricCategory.BLOCKCHAIN))
.build();
registry.setMetricsConfiguration(metricsConfiguration);
assertTrue(registry.isMetricCategoryEnabled(BesuMetricCategory.BLOCKCHAIN));
}

@Test
void metricCategoryIsDisabledAndMetricsAreEnabled() {
final var registry = new MetricCategoryRegistryImpl();
registry.addMetricCategory(BesuMetricCategory.ETHEREUM);
final var metricsConfiguration =
MetricsConfiguration.builder()
.enabled(true)
.metricCategories(Set.of(BesuMetricCategory.ETHEREUM))
.build();
registry.setMetricsConfiguration(metricsConfiguration);
assertFalse(registry.isMetricCategoryEnabled(BesuMetricCategory.BLOCKCHAIN));
}

@Test
void metricCategoryNotEnabledWhenMetricsAreDisabled() {
final var registry = new MetricCategoryRegistryImpl();
registry.addMetricCategory(BesuMetricCategory.BLOCKCHAIN);
final var metricsConfiguration =
MetricsConfiguration.builder()
.enabled(false)
.metricCategories(Set.of(BesuMetricCategory.BLOCKCHAIN))
.build();
registry.setMetricsConfiguration(metricsConfiguration);
assertFalse(registry.isMetricCategoryEnabled(BesuMetricCategory.BLOCKCHAIN));
}
}
2 changes: 1 addition & 1 deletion plugin-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Calculated : ${currentHash}
tasks.register('checkAPIChanges', FileStateChecker) {
description = "Checks that the API for the Plugin-API project does not change without deliberate thought"
files = sourceSets.main.allJava.files
knownHash = 'uNQzVjMa7m1fw3d10NuVOjmzGxmCkgZd88yGFgP3qoY='
knownHash = '8rPIE3fYl48RPRQXxYhMk559e/r+wHSKU9bGSJmruKQ='
}
check.dependsOn('checkAPIChanges')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,13 @@ public interface MetricCategoryRegistry extends BesuService {
*
* @param newMetricCategory The {@link MetricCategory} that is being registered.
*/
public void addMetricCategory(final MetricCategory newMetricCategory);
void addMetricCategory(final MetricCategory newMetricCategory);

/**
* Return true if the metrics are enabled and the metric category is enabled
*
* @param metricCategory the metric category
* @return true if the metrics are enabled and the metric category is enabled
*/
boolean isMetricCategoryEnabled(final MetricCategory metricCategory);
}

0 comments on commit ba86ce1

Please sign in to comment.