This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only increment the added transaction counter if we actually added the…
… transaction (#1543)
- Loading branch information
Showing
5 changed files
with
173 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
metrics/core/src/test-support/java/tech/pegasys/pantheon/metrics/StubMetricsSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* 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 tech.pegasys.pantheon.metrics; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.DoubleSupplier; | ||
import java.util.stream.Stream; | ||
|
||
public class StubMetricsSystem implements MetricsSystem { | ||
|
||
private final Map<String, StubLabelledCounter> counters = new HashMap<>(); | ||
private final Map<String, DoubleSupplier> gauges = new HashMap<>(); | ||
|
||
@Override | ||
public LabelledMetric<Counter> createLabelledCounter( | ||
final MetricCategory category, | ||
final String name, | ||
final String help, | ||
final String... labelNames) { | ||
return counters.computeIfAbsent(name, key -> new StubLabelledCounter()); | ||
} | ||
|
||
public long getCounterValue(final String name, final String... labels) { | ||
final StubLabelledCounter labelledCounter = counters.get(name); | ||
if (labelledCounter == null) { | ||
throw new IllegalArgumentException("Unknown counter: " + name); | ||
} | ||
final StubCounter metric = labelledCounter.getMetric(labels); | ||
if (metric == null) { | ||
return 0; | ||
} | ||
return metric.getValue(); | ||
} | ||
|
||
@Override | ||
public LabelledMetric<OperationTimer> createLabelledTimer( | ||
final MetricCategory category, | ||
final String name, | ||
final String help, | ||
final String... labelNames) { | ||
return labelValues -> NoOpMetricsSystem.NO_OP_OPERATION_TIMER; | ||
} | ||
|
||
@Override | ||
public void createGauge( | ||
final MetricCategory category, | ||
final String name, | ||
final String help, | ||
final DoubleSupplier valueSupplier) { | ||
gauges.put(name, valueSupplier); | ||
} | ||
|
||
public double getGaugeValue(final String name) { | ||
final DoubleSupplier gauge = gauges.get(name); | ||
if (gauge == null) { | ||
throw new IllegalArgumentException("Unknown gauge: " + name); | ||
} | ||
return gauge.getAsDouble(); | ||
} | ||
|
||
@Override | ||
public Stream<Observation> streamObservations(final MetricCategory category) { | ||
throw new UnsupportedOperationException("Observations aren't actually recorded"); | ||
} | ||
|
||
public static class StubLabelledCounter implements LabelledMetric<Counter> { | ||
private final Map<List<String>, StubCounter> metrics = new HashMap<>(); | ||
|
||
@Override | ||
public Counter labels(final String... labels) { | ||
return metrics.computeIfAbsent(asList(labels), key -> new StubCounter()); | ||
} | ||
|
||
private StubCounter getMetric(final String... labels) { | ||
return metrics.get(asList(labels)); | ||
} | ||
} | ||
|
||
public static class StubCounter implements Counter { | ||
private long value = 0; | ||
|
||
@Override | ||
public void inc() { | ||
value++; | ||
} | ||
|
||
@Override | ||
public void inc(final long amount) { | ||
value += amount; | ||
} | ||
|
||
public long getValue() { | ||
return value; | ||
} | ||
} | ||
} |