Skip to content

Commit

Permalink
Add LegacyStatCounter and LegacyStatTimer
Browse files Browse the repository at this point in the history
- Also migrate GatewayReceiverStats events received to use new
LegacyStatCounter.

Co-authored-by: Kirk Lund <[email protected]>
  • Loading branch information
demery-pivotal and kirklund committed Jun 20, 2019
1 parent 51da276 commit a483562
Show file tree
Hide file tree
Showing 15 changed files with 1,590 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

import io.micrometer.core.instrument.FunctionCounter;
import io.micrometer.core.instrument.Counter;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -272,9 +272,9 @@ public static class GetEventsReceivedCountFunction implements Function<Void> {

@Override
public void execute(FunctionContext<Void> context) {
FunctionCounter eventsReceivedCounter = SimpleMetricsPublishingService.getRegistry()
Counter eventsReceivedCounter = SimpleMetricsPublishingService.getRegistry()
.find("cache.gatewayreceiver.events.received")
.functionCounter();
.counter();

Object result = eventsReceivedCounter == null
? "Meter not found."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
*/
package org.apache.geode.internal.cache.wan;

import io.micrometer.core.instrument.FunctionCounter;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;

import org.apache.geode.StatisticDescriptor;
import org.apache.geode.StatisticsFactory;
import org.apache.geode.distributed.internal.DistributionStats;
import org.apache.geode.internal.cache.tier.sockets.CacheServerStats;
import org.apache.geode.internal.statistics.meters.LegacyStatCounter;

public class GatewayReceiverStats extends CacheServerStats {

Expand Down Expand Up @@ -104,7 +105,7 @@ public class GatewayReceiverStats extends CacheServerStats {
* Id of the events distributed statistic
*/
private int eventsReceivedId;
private final FunctionCounter eventsReceivedCounter;
private final Counter eventsReceivedCounter;
private static final String EVENTS_RECEIVED_COUNTER_NAME =
"cache.gatewayreceiver.events.received";
private static final String EVENTS_RECEIVED_COUNTER_DESCRIPTION =
Expand Down Expand Up @@ -189,8 +190,8 @@ public GatewayReceiverStats(StatisticsFactory f, String ownerName, String typeNa
eventsRetriedId = statType.nameToId(EVENTS_RETRIED);

this.meterRegistry = meterRegistry;
eventsReceivedCounter = FunctionCounter.builder(EVENTS_RECEIVED_COUNTER_NAME, stats,
s -> s.getInt(eventsReceivedId))
eventsReceivedCounter = LegacyStatCounter.builder(EVENTS_RECEIVED_COUNTER_NAME)
.intStatistic(stats, eventsReceivedId)
.description(EVENTS_RECEIVED_COUNTER_DESCRIPTION)
.baseUnit(EVENTS_RECEIVED_COUNTER_UNITS)
.register(meterRegistry);
Expand Down Expand Up @@ -246,11 +247,11 @@ public int getEarlyAcks() {
* Increments the number of events received by 1.
*/
public void incEventsReceived(int delta) {
this.stats.incInt(eventsReceivedId, delta);
eventsReceivedCounter.increment(delta);
}

public int getEventsReceived() {
return this.stats.getInt(eventsReceivedId);
return (int) eventsReceivedCounter.count();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.geode.internal.statistics.meters;

import org.apache.geode.Statistics;

/**
* Increments and reads a {@code double} stat.
*/
public class DoubleStatisticBinding implements StatisticBinding {
private final Statistics statistics;
private final int statId;

public DoubleStatisticBinding(Statistics statistics, int statId) {
this.statistics = statistics;
this.statId = statId;
}

@Override
public void add(double amount) {
statistics.incDouble(statId, amount);
}

@Override
public double doubleValue() {
return statistics.getDouble(statId);
}

@Override
public long longValue() {
return (long) statistics.getDouble(statId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.geode.internal.statistics.meters;

import org.apache.geode.Statistics;

/**
* Increments and reads an {@code int} stat.
*/
public class IntStatisticBinding implements StatisticBinding {
private final Statistics statistics;
private final int statId;

public IntStatisticBinding(Statistics statistics, int statId) {
this.statistics = statistics;
this.statId = statId;
}

@Override
public void add(double amount) {
statistics.incInt(statId, (int) amount);
}

@Override
public double doubleValue() {
return (double) statistics.getInt(statId);
}

@Override
public long longValue() {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* 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.geode.internal.statistics.meters;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;

import org.apache.geode.Statistics;

/**
* Wraps a {@code Counter} to increment and read an associated stat as well as incrementing the
* counter.
*/
public class LegacyStatCounter implements Counter {
private final Counter underlyingCounter;
private final StatisticBinding statisticBinding;

/**
* Creates a {@code LegacyStatCounter} that wraps the given counter to add the ability
* to increment and read an associated stat.
*
* @param underlyingCounter the counter to wrap
* @param statisticBinding associates the {@code LegacyStatCounter} with a stat
*/
private LegacyStatCounter(Counter underlyingCounter,
StatisticBinding statisticBinding) {
this.underlyingCounter = underlyingCounter;
this.statisticBinding = statisticBinding;
}

/**
* Increments both the underlying counter and the associated stat by the given amount.
*/
@Override
public void increment(double amount) {
underlyingCounter.increment(amount);
statisticBinding.add(amount);
}

/**
* Returns the value of the associated stat.
*/
@Override
public double count() {
return statisticBinding.doubleValue();
}

/**
* Returns the ID of the underlying counter.
*/
@Override
public Id getId() {
return underlyingCounter.getId();
}

/**
* Returns a builder that can associate a stat with the eventual {@code LegacyStatCounter}. To
* associate the counter with a stat, call one of
* {@link Builder#doubleStatistic(Statistics, int)} or
* {@link Builder#longStatistic(Statistics, int)}.
*/
public static Builder builder(String name) {
return new Builder(name);
}

public static class Builder {
private final Counter.Builder builder;
private StatisticBinding statisticBinding = StatisticBinding.noOp();

private Builder(String name) {
builder = Counter.builder(name);
}

/**
* Prepares to associate the eventual {@code LegacyStatCounter} with the specified {@code
* double} stat. The given {@code Statistics} and {@code statId} must identify a {@code
* double} stat.
*/
public Builder doubleStatistic(Statistics statistics, int statId) {
statisticBinding = new DoubleStatisticBinding(statistics, statId);
return this;
}

/**
* Prepares to associate the eventual {@code LegacyStatCounter} with the specified {@code
* int} stat. The given {@code Statistics} and {@code statId} must identify an {@code int}
* stat.
*/
public Builder intStatistic(Statistics statistics, int statId) {
statisticBinding = new IntStatisticBinding(statistics, statId);
return this;
}

/**
* Prepares to associate the eventual {@code LegacyStatCounter} with the specified {@code
* long} stat. The given {@code Statistics} and {@code statId} must identify a {@code long}
* stat.
*/
public Builder longStatistic(Statistics statistics, int statId) {
statisticBinding = new LongStatisticBinding(statistics, statId);
return this;
}

public Builder baseUnit(String unit) {
builder.baseUnit(unit);
return this;
}

public Builder description(String description) {
builder.description(description);
return this;
}

public Builder tag(String name, String value) {
builder.tag(name, value);
return this;
}

public Builder tags(String... tags) {
builder.tags(tags);
return this;
}

public Builder tags(Iterable<Tag> tags) {
builder.tags(tags);
return this;
}

/**
* Registers a {@code Counter} with the given registry, and returns a {@code LegacyStatCounter}
* that wraps the counter to increment and read the associated stat. Note that the returned
* {@code LegacyStatCounter} is not registered with the registry, but it has the same ID, so
* it can be used to remove the registered counter from the registry.
*/
public LegacyStatCounter register(MeterRegistry registry) {
Counter underlyingCounter = builder.register(registry);
return new LegacyStatCounter(underlyingCounter, statisticBinding);
}
}
}
Loading

0 comments on commit a483562

Please sign in to comment.