io.helidon.metrics.api.NoOpMeterRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of helidon-metrics-api Show documentation
Show all versions of helidon-metrics-api Show documentation
Loader for metrics implementation and a no-op implementation
/*
* 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.metrics.api;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.function.Predicate;
/**
* No-op implementation of {@link io.helidon.metrics.api.MeterRegistry}.
*
* Note that the no-op meter registry implementation does not actually
* store meters or their IDs, in line with the documented behavior of disabled metrics.
*
*/
class NoOpMeterRegistry implements MeterRegistry, NoOpWrapper {
private final List> onAddListeners = new CopyOnWriteArrayList<>();
private final List> onRemoveListeners = new CopyOnWriteArrayList<>();
static Builder builder() {
return new Builder();
}
@Override
public void close() {
}
@Override
public List meters() {
return List.of();
}
@Override
public Collection meters(Predicate filter) {
return Set.of();
}
@Override
public Iterable meters(Iterable scopeSelection) {
return Set.of();
}
@Override
public Clock clock() {
return Clock.system();
}
@Override
public Optional remove(Meter.Id id) {
return Optional.empty();
}
@Override
public Optional remove(Meter meter) {
onRemoveListeners.forEach(listener -> listener.accept(meter));
return Optional.empty();
}
@Override
public Optional remove(String name, Iterable tags) {
return Optional.empty();
}
@Override
public Optional remove(Meter.Id id, String scope) {
return Optional.empty();
}
@Override
public Optional remove(String name, Iterable tags, String scope) {
return Optional.empty();
}
@Override
public Optional meter(Class mClass, String name, Iterable tags) {
return Optional.empty();
}
@Override
public boolean isDeleted(Meter meter) {
return false;
}
@Override
public boolean isMeterEnabled(String name, Map tags, Optional scope) {
return true;
}
@Override
public Iterable scopes() {
return Set.of();
}
@Override
public , M extends Meter> M getOrCreate(B builder) {
NoOpMeter.Builder, ?> b = (NoOpMeter.Builder, ?>) builder;
M result = findOrRegister(NoOpMeter.Id.create(b.name(),
b.tags()),
builder);
onAddListeners.forEach(listener -> listener.accept(result));
return result;
}
@Override
public MeterRegistry onMeterAdded(Consumer listener) {
onAddListeners.add(listener);
return this;
}
@Override
public MeterRegistry onMeterRemoved(Consumer listener) {
onRemoveListeners.add(listener);
return this;
}
private Optional find(Meter.Id id, Class mClass) {
return Optional.empty();
}
private > M findOrRegister(Meter.Id id, B builder) {
NoOpMeter.Builder, ?> noOpBuilder = (NoOpMeter.Builder, ?>) builder;
// The following cast will always succeed if we create the meter by invoking the builder,
// it will succeed if we retrieved a previously-registered meter of a compatible type,
// and it will (correctly) fail if we found a previously-registered meter of an incompatible
// type compared to what the caller requested.
return (M) noOpBuilder.build();
}
static class Builder implements MeterRegistry.Builder {
@Override
public NoOpMeterRegistry build() {
return new NoOpMeterRegistry();
}
@Override
public Builder clock(Clock clock) {
return identity();
}
@Override
public Builder metricsConfig(MetricsConfig metricsConfig) {
return identity();
}
@Override
public Builder onMeterAdded(Consumer addListener) {
return identity();
}
@Override
public Builder onMeterRemoved(Consumer removeListener) {
return identity();
}
}
}