Skip to content

Commit

Permalink
PAYARA-3830 Further changes to ConcurrentGaugeImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
Cousjava committed Jul 19, 2019
1 parent 88ddcdb commit 91d7d20
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 37 deletions.
Expand Up @@ -81,26 +81,6 @@

@ApplicationScoped
public class MetricsHelper {

@ConfigProperty(name="mp.metrics.tags")
String globalTags;

public String getGlobalTagsString() {
return globalTags;
}

public List<Tag> getGlobalTags() {
return convertToTags(getGlobalTagsString());
}

private static List<Tag> convertToTags(String tagsString) {
List<Tag> tags = Collections.emptyList();
if (tagsString != null) {
String[] singleTags = tagsString.split(",");
tags = Arrays.asList(tagsFromString(singleTags));
}
return tags;
}

public String metricNameOf(InjectionPoint ip) {
Annotated annotated = ip.getAnnotated();
Expand Down
Expand Up @@ -40,7 +40,6 @@

package fish.payara.microprofile.metrics.cdi.interceptor;

import fish.payara.microprofile.metrics.impl.ConcurrentGaugeImpl;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Member;
import javax.annotation.Priority;
Expand All @@ -58,7 +57,7 @@ public class ConcurrentGuageInterceptor extends AbstractInterceptor {
protected <E extends Member & AnnotatedElement> Object applyInterceptor(InvocationContext context, E element)
throws Exception {
MetricID metricID = resolver.concurrentGauge(bean.getBeanClass(), element).metricID();
ConcurrentGaugeImpl counter = (ConcurrentGaugeImpl) registry.getMetrics().get(metricID);
org.eclipse.microprofile.metrics.ConcurrentGauge counter = registry.getConcurrentGauges().get(metricID);
if (counter == null) {
throw new IllegalStateException("No concurrent gauge with name [" + metricID.getName() + "] found in registry [" + registry + "]");
}
Expand Down
Expand Up @@ -37,20 +37,6 @@
* only if the new code is made subject to such option by the copyright
* holder.
*
* *****************************************************************************
* Copyright 2010-2013 Coda Hale and Yammer, Inc.
*
* 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 fish.payara.microprofile.metrics.impl;
Expand All @@ -73,19 +59,24 @@
public class ConcurrentGaugeImpl implements ConcurrentGauge {

private final LongAdder count = new LongAdder();
/**
* Last point for which clearOld() has been run
*/
private Instant lastInstant = Instant.now();

/**
* A map containing all the values that the count of the gauge has been at in the last minute.
* The key is the time in seconds that the value changed, the value is the value of the count at the moment
* before it changed
*/
private Map<Instant, Long> lastCounts = new ConcurrentHashMap<>();

/**
* Increment the counter by one.
*/
@Override
public void inc() {
clearOld();
if (count.longValue() > 0) {
lastCounts.put(Instant.now(), count.longValue());
}
Expand Down Expand Up @@ -136,6 +127,7 @@ public long getMin() {

@Override
public void dec() {
clearOld();
lastCounts.put(Instant.now(), count.longValue());
count.decrement();
}
Expand All @@ -145,12 +137,17 @@ public void dec() {
*/
private void clearOld() {
Instant previousMinute = Instant.now().truncatedTo(ChronoUnit.MINUTES).minus(1, ChronoUnit.MINUTES);
if (previousMinute.equals(lastInstant)) {
//already called in this minute
return;
}
Iterator<Instant> guages = lastCounts.keySet().iterator();
while (guages.hasNext()) {
Instant guageTime = guages.next();
if (guageTime.isBefore(previousMinute)) {
lastCounts.remove(guageTime);
}
}
lastInstant = previousMinute;
}
}

0 comments on commit 91d7d20

Please sign in to comment.