Skip to content

Commit

Permalink
Feature buckets (#37)
Browse files Browse the repository at this point in the history
* add buckets to DistributionSummary & Timer api
* change bucket tag to low-high
  • Loading branch information
cp3fantasy authored and luyiisme committed Sep 18, 2018
1 parent 191a005 commit 44c68b1
Show file tree
Hide file tree
Showing 27 changed files with 489 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 com.alipay.lookout.api;

/**
*
* @author zhangzhuo
* @version $Id: BucketCounter.java, v 0.1 2018年09月11日 下午1:37 zhangzhuo Exp $
*/
public interface BucketCounter extends Metric {

/**
* enable recording bucket counts
* @param buckets
*/
void buckets(long[] buckets);

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
* hitting and http server.
* Created by kevin.luy@alipay.com on 2017/2/14.
*/
public interface DistributionSummary extends Metric {
public interface DistributionSummary extends BucketCounter {

String BUCKET_TAG_NAME = "_bucket";

/**
* Updates the statistics with the specified amount.
Expand All @@ -45,4 +47,5 @@ public interface DistributionSummary extends Metric {
* @return total
*/
long totalAmount();

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@ public long getTimestamp() {
public Collection<Measurement<T>> measurements() {
return measurements;
}

@Override
public String toString() {
return "Indicator{" + "timestamp=" + timestamp + ", id=" + id + ", measurements="
+ measurements + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ public long count() {
public long totalAmount() {
return 0L;
}

@Override
public void buckets(long[] buckets) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ public long count() {
public long totalTime() {
return 0L;
}

@Override
public void buckets(long[] buckets) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
package com.alipay.lookout.api;

/**
* Measurement type
* Created by kevin.luy@alipay.com on 2017/2/14.
* Measurement type Created by kevin.luy@alipay.com on 2017/2/14.
*/
public enum Statistic {
rate,
Expand All @@ -37,6 +36,11 @@ public enum Statistic {
*/
totalAmount,

/**
* buckets of the amounts recorded
*/
buckets,

// /**
// * The sum of the squares of the amounts recorded.
// */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* A timer metric
* Created by kevin.luy@alipay.com on 2017/2/14.
*/
public interface Timer extends Metric {
public interface Timer extends BucketCounter {
/**
* @param amount Duration of a single event
* @param unit time unit
Expand Down Expand Up @@ -52,4 +52,5 @@ public interface Timer extends Metric {
* @return The total time of recorded events
*/
long totalTime();

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public long totalAmount() {
return it.hasNext() ? getMetric(it.next()).totalAmount() : 0L;
}

@Override
public void buckets(long[] buckets) {
for (Registry r : registries) {
getMetric(r).buckets(buckets);
}
}

@Override
protected DistributionSummary getMetric(Registry registry) {
return registry.distributionSummary(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,11 @@ public long totalTime() {
Iterator<Registry> it = registries.iterator();
return it.hasNext() ? getMetric(it.next()).totalTime() : 0L;
}

@Override
public void buckets(long[] buckets) {
for (Registry r : registries) {
getMetric(r).buckets(buckets);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public abstract class AbstractRegistry extends MetricRegistry {
.getLogger(getClass());

private final ConcurrentHashMap<Id, Metric> metrics;

private MetricConfig config;

private final List<MetricRegistryListener> listeners = new CopyOnWriteArrayList<MetricRegistryListener>();
Expand Down Expand Up @@ -180,8 +181,7 @@ protected <I, Y extends Info<I>> InfoWrapper<I, Y> wrapperInfo(Id id, Y info) {
}

/**
* only support gauge or info now!
* for not singleton and temp life;
* only support gauge or info now! for not singleton and temp life;
*
* @param id
*/
Expand Down Expand Up @@ -353,7 +353,7 @@ public final <X extends Metric> X get(Id id) {

@Override
public final Iterator<Metric> iterator() {
return metrics.values().iterator();
return new MetricIterator(metrics.values().iterator());
}

@Override
Expand Down Expand Up @@ -386,8 +386,8 @@ public Metric noopMetric() {
protected abstract Metric newMixinMetric(Id id);

/**
* Adds a {@link MetricRegistryListener} to a collection of listeners that will be notified on
* metric creation. Listeners will be notified in the order in which they are added.
* Adds a {@link MetricRegistryListener} to a collection of listeners that will be notified on metric creation. Listeners will be
* notified in the order in which they are added.
*
* @param listener the listener that will be notified
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ public void record(Runnable runnable) {
record(clock.monotonicTime() - st, TimeUnit.NANOSECONDS);
}
}

@Override
public void buckets(long[] buckets) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@ public long count() {
public long totalAmount() {
return totalAmount.get();
}

@Override
public void buckets(long[] buckets) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ protected Counter newCounter(Id id) {

@Override
protected DistributionSummary newDistributionSummary(Id id) {
return new DefaultDistributionSummary(clock(), id);
DefaultDistributionSummary distributionSummary = new DefaultDistributionSummary(clock(), id);
return distributionSummary;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 com.alipay.lookout.core;

import com.alipay.lookout.api.Metric;

/**
* @author zhangzhuo
* @version $Id: MetricIterable.java, v 0.1 2018年09月17日 下午2:33 zhangzhuo Exp $
*/
public interface MetricIterable extends Iterable<Metric> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 com.alipay.lookout.core;

import com.alipay.lookout.api.Metric;

import java.util.Iterator;

/**
* @author zhangzhuo
* @version $Id: MetricIterator.java, v 0.1 2018年09月17日 下午2:29 zhangzhuo Exp $
*/
public class MetricIterator implements Iterator<Metric> {

private final Iterator<Metric> baseIterator;

private Iterator<Metric> extendIterator;

public MetricIterator(Iterator<Metric> baseIterator) {
this.baseIterator = baseIterator;
}

@Override
public boolean hasNext() {
if (extendIterator != null && extendIterator.hasNext()) {
return true;
}
return baseIterator.hasNext();
}

@Override
public Metric next() {
if (extendIterator != null && extendIterator.hasNext()) {
return extendIterator.next();
}
Metric metric = baseIterator.next();
if (metric instanceof MetricIterable) {
MetricIterable bucketCounter = (MetricIterable) metric;
extendIterator = bucketCounter.iterator();
}
return metric;
}

@Override
public void remove() {
baseIterator.remove();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ public long count() {
public long totalAmount() {
return 0L;
}

@Override
public void buckets(long[] buckets) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ public long count() {
public long totalTime() {
return 0L;
}

@Override
public void buckets(long[] buckets) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
*/
package com.alipay.lookout.core;

import com.alipay.lookout.api.DistributionSummary;
import com.alipay.lookout.api.ManualClock;
import com.alipay.lookout.api.*;

import org.junit.Assert;
import org.junit.Test;

import java.util.Iterator;

public class DefaultDistributionSummaryTest {

private final ManualClock clock = new ManualClock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
*/
package com.alipay.lookout.dropwizard.metrics;

import com.alipay.lookout.api.Clock;
import com.alipay.lookout.api.DistributionSummary;
import com.alipay.lookout.api.Id;
import com.alipay.lookout.api.Indicator;
import com.alipay.lookout.api.*;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Snapshot;

Expand Down Expand Up @@ -69,6 +66,11 @@ public long totalAmount() {
return totalAmount.get();
}

@Override
public void buckets(long[] buckets) {

}

@Override
public Histogram getOriginDwMetric() {
return impl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import com.alipay.lookout.api.PRIORITY;
import com.alipay.lookout.common.utils.PriorityTagUtil;
import com.alipay.lookout.core.InfoWrapper;
import com.alipay.lookout.core.MetricIterator;
import com.alipay.lookout.event.MetricRegistryListener;
import com.google.common.collect.Sets;

import java.util.Iterator;
import java.util.Set;

/**
Expand All @@ -47,6 +49,10 @@ public Set<Metric> getMetricByPriority(PRIORITY priority) {
}
}

public Iterator<Metric> getMetricIteratorByPriority(PRIORITY priority) {
return new MetricIterator(getMetricByPriority(priority).iterator());
}

public Set<Metric> getHighMetircs() {
return highMetircs;
}
Expand Down
Loading

0 comments on commit 44c68b1

Please sign in to comment.