Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;

import org.apache.jackrabbit.api.stats.RepositoryStatistics;
import org.apache.jackrabbit.api.stats.RepositoryStatistics.Type;
Expand Down Expand Up @@ -60,6 +61,12 @@ public HistogramStats getHistogram(String name, StatsOptions options) {
return getStats(name, true, SimpleStats.Type.HISTOGRAM, options);
}

@Override
public <T> GaugeStats<T> getGauge(String name, Supplier<T> supplier) {
return statsMeters.computeIfAbsent(name,
k -> new SimpleStats<>(new AtomicLong(), SimpleStats.Type.GAUGE, supplier.get()));
}

private synchronized SimpleStats getStats(String type, boolean resetValueEachSecond, SimpleStats.Type statsType,
StatsOptions options){
SimpleStats stats = statsMeters.get(type);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.jackrabbit.oak.stats;

import org.osgi.annotation.versioning.ProviderType;

@ProviderType
public interface GaugeStats<T> extends Stats {

/**
* Returns the metric's current value.
*
* @return the metric's current value
*/
T getValue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public final class SimpleStats implements TimerStats, MeterStats, CounterStats, HistogramStats {
public enum Type {COUNTER, METER, TIMER, HISTOGRAM}
public final class SimpleStats<T> implements TimerStats, MeterStats, CounterStats, HistogramStats, GaugeStats<T> {

public enum Type {COUNTER, METER, TIMER, HISTOGRAM, GAUGE}
private final AtomicLong statsHolder;
private long counter;
private T value;

/*
Using 2 different variables for managing the sum in meter calls
Expand All @@ -44,8 +46,13 @@ This is done to ensure that more frequent mark() is fast (used for Session reads
private final Type type;

public SimpleStats(AtomicLong statsHolder, Type type) {
this(statsHolder, type, null);
}

public SimpleStats(AtomicLong statsHolder, Type type, T value) {
this.statsHolder = statsHolder;
this.type = type;
this.value = value;
}

@Override
Expand All @@ -62,6 +69,9 @@ public long getCount() {
//For Meter it can happen that backing statsHolder gets
//reset each second. So need to manage that sum separately
return meterSum + meterSumRef.get();
case GAUGE:
// For gauge type there is no count, it only returns the constant value set
return 0;
}
throw new IllegalStateException();
}
Expand Down Expand Up @@ -115,6 +125,11 @@ public void update(long value) {
statsHolder.getAndAdd(value);
}

@Override
public T getValue() {
return value;
}

private static final class SimpleContext implements Context {
private final TimerStats timer;
private final long startTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.osgi.annotation.versioning.ProviderType;
import org.apache.jackrabbit.api.stats.RepositoryStatistics;

import java.util.function.Supplier;

@ProviderType
public interface StatisticsProvider {
StatisticsProvider NOOP = new StatisticsProvider() {
Expand Down Expand Up @@ -49,6 +51,11 @@ public TimerStats getTimer(String name, StatsOptions options) {
public HistogramStats getHistogram(String name, StatsOptions options) {
return NoopStats.INSTANCE;
}

@Override
public <T> GaugeStats<T> getGauge(String name, Supplier<T> supplier) {
return null;
}
};


Expand All @@ -61,4 +68,14 @@ public HistogramStats getHistogram(String name, StatsOptions options) {
TimerStats getTimer(String name, StatsOptions options);

HistogramStats getHistogram(String name, StatsOptions options);

/**
* Creates a new {@link GaugeStats} and registers it under the given name.
* If a gauge with the same exists already then the same instance is returned.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/the same exists/the same name exists

* @param name the name of the gauge
* @param supplier provides the values which are returned by the gauge
* @param <T> the type of the metric
* @return the gauge
*/
<T> GaugeStats<T> getGauge(String name, Supplier<T> supplier);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class DefaultStatisticsProviderTest {
Expand All @@ -61,6 +60,14 @@ public void meter() throws Exception {
assertTrue(getRegisteredTimeSeries(statsProvider).contains("test"));
}

@Test
public void gauge() {
GaugeStats<String> gaugeStats = statsProvider.getGauge("test", () -> "value");

assertNotNull(gaugeStats);
assertEquals("value", gaugeStats.getValue());
}

@Test
public void counter() throws Exception {
CounterStats counterStats = statsProvider.getCounterStats("test", StatsOptions.DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;

import org.apache.jackrabbit.api.jmx.QueryStatManagerMBean;
import org.apache.jackrabbit.api.stats.RepositoryStatistics;
Expand Down Expand Up @@ -131,7 +132,7 @@ public void cleanup() {
executorService.shutdownNow();
}

private class DummyStatsProvider implements StatisticsProvider {
private static class DummyStatsProvider implements StatisticsProvider {

@Override
public RepositoryStatistics getStats() {
Expand All @@ -157,5 +158,10 @@ public TimerStats getTimer(String name, StatsOptions options) {
public HistogramStats getHistogram(String name, StatsOptions options) {
return NoopStats.INSTANCE;
}

@Override
public <T> GaugeStats<T> getGauge(String name, Supplier<T> supplier) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.jackrabbit.oak.plugins.metric;

import com.codahale.metrics.Gauge;
import org.apache.jackrabbit.oak.stats.GaugeStats;

public class GaugeImpl<T> implements GaugeStats<T> {

private final Gauge<T> gauge;

public GaugeImpl(Gauge<T> gauge) {
this.gauge = gauge;
}
@Override
public T getValue() {
return this.gauge.getValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;

import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
Expand All @@ -43,6 +45,7 @@
import org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardUtils;
import org.apache.jackrabbit.oak.stats.Clock;
import org.apache.jackrabbit.oak.stats.CounterStats;
import org.apache.jackrabbit.oak.stats.GaugeStats;
import org.apache.jackrabbit.oak.stats.HistogramStats;
import org.apache.jackrabbit.oak.stats.MeterStats;
import org.apache.jackrabbit.oak.stats.SimpleStats;
Expand Down Expand Up @@ -117,6 +120,11 @@ public HistogramStats getHistogram(String name, StatsOptions options) {
return getStats(name, StatsBuilder.HISTOGRAMS, options);
}

@Override
public <T> GaugeStats<T> getGauge(String name, Supplier<T> supplier) {
return getOrAddGauge(name, supplier);
}

public MetricRegistry getRegistry() {
return registry;
}
Expand All @@ -125,6 +133,36 @@ RepositoryStatisticsImpl getRepoStats() {
return repoStats;
}

@SuppressWarnings("unchecked")
private <T> GaugeStats<T> getOrAddGauge(final String name, final Supplier<T> supplier) {
final Stats stats = statsRegistry.get(name);
if (stats instanceof GaugeStats<?>) {
return (GaugeStats<T>) stats;
} else {
try {
return registerGauge(name, supplier);
} catch (IllegalArgumentException e) {
final Stats added = statsRegistry.get(name);
if (added instanceof GaugeStats<?>) {
return (GaugeStats<T>) added;
}
}
}
throw new IllegalArgumentException(name + " is already used for a different type of stats");
}

private <T> GaugeStats<T> registerGauge(final String name, final Supplier<T> supplier) {
final Gauge<T> codahaleGauge = supplier::get;
@SuppressWarnings("rawtypes")
MetricRegistry.MetricSupplier<Gauge> metricSupplier = () -> codahaleGauge;

@SuppressWarnings("unchecked")
Gauge<T> g = registry.gauge(name, metricSupplier);
GaugeImpl<T> gauge = new GaugeImpl<>(g);
statsRegistry.put(name, gauge);
return gauge;
}

private <T extends Stats> T getStats(String name, StatsBuilder<T> builder, StatsOptions options) {
Stats stats = statsRegistry.get(name);
//Use double locking pattern. The map should get populated with required set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.jackrabbit.api.stats.RepositoryStatistics.Type;
import org.apache.jackrabbit.oak.commons.collections.SetUtils;
import org.apache.jackrabbit.oak.stats.CounterStats;
import org.apache.jackrabbit.oak.stats.GaugeStats;
import org.apache.jackrabbit.oak.stats.HistogramStats;
import org.apache.jackrabbit.oak.stats.MeterStats;
import org.apache.jackrabbit.oak.stats.NoopStats;
Expand Down Expand Up @@ -111,6 +112,20 @@ public void histogram() throws Exception {
assertTrue(((CompositeStats) histoStats).isHistogram());
}

@Test
public void gauge() {
GaugeStats<String> gaugeStats = statsProvider.getGauge("test", () -> "value");

assertNotNull(gaugeStats);
assertEquals("value", statsProvider.getRegistry().getGauges().get("test").getValue());

// updating gauge is not possible
gaugeStats = statsProvider.getGauge("test", () -> "value2");
assertNotNull(gaugeStats);
assertEquals("value", statsProvider.getRegistry().getGauges().get("test").getValue());

}

@Test
public void timeSeriesIntegration() throws Exception {
MeterStats meterStats = statsProvider.getMeter(Type.SESSION_COUNT.name(), StatsOptions.DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
* Fixture encapsulating FullGC metrics exporter instance of T
* @param <T>
*/
public interface FullGCMetricsExporterFixture<T> extends FullGCMetricsExporter<T>, MetricsExporterFixture<T> {
public interface FullGCMetricsExporterFixture<T> extends FullGCMetricsExporter, MetricsExporterFixture<T> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
import org.apache.jackrabbit.api.stats.RepositoryStatistics;
import org.apache.jackrabbit.api.stats.TimeSeries;
import org.apache.jackrabbit.oak.stats.CounterStats;
import org.apache.jackrabbit.oak.stats.GaugeStats;
import org.apache.jackrabbit.oak.stats.HistogramStats;
import org.apache.jackrabbit.oak.stats.MeterStats;
import org.apache.jackrabbit.oak.stats.StatisticsProvider;
import org.apache.jackrabbit.oak.stats.StatsOptions;
import org.apache.jackrabbit.oak.stats.TimerStats;

import java.util.function.Supplier;

public class RoleStatisticsProvider implements StatisticsProvider{

private final StatisticsProvider delegate;
Expand Down Expand Up @@ -75,6 +78,11 @@ public HistogramStats getHistogram(String name, StatsOptions options) {
return delegate.getHistogram(addRoleToName(name, role), options);
}

@Override
public <T> GaugeStats<T> getGauge(String name, Supplier<T> supplier) {
return delegate.getGauge(addRoleToName(name, role), supplier);
}

private static String addRoleToName(String name, String role) {
return role + '.' + name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;

import org.apache.jackrabbit.api.stats.RepositoryStatistics;
import org.apache.jackrabbit.oak.commons.Buffer;
import org.apache.jackrabbit.oak.stats.CounterStats;
import org.apache.jackrabbit.oak.stats.GaugeStats;
import org.apache.jackrabbit.oak.stats.HistogramStats;
import org.apache.jackrabbit.oak.stats.MeterStats;
import org.apache.jackrabbit.oak.stats.SimpleStats;
Expand Down Expand Up @@ -71,6 +73,11 @@ public TimerStats getTimer(String name, StatsOptions options) {
public HistogramStats getHistogram(String name, StatsOptions options) {
throw new IllegalStateException();
}

@Override
public <T> GaugeStats<T> getGauge(String name, Supplier<T> supplier) {
throw new IllegalStateException();
}
});

@Test
Expand Down
Loading
Loading