Skip to content

Commit

Permalink
move bucket helpers from sandbox to api
Browse files Browse the repository at this point in the history
These have been used for quite a while and are considered
stable now. The old classes in sandbox have been kept and
marked as deprecated to avoid breaking existing users. To
avoid drift in the implementations they now just wrap the
new versions in the api lib.

A few small changes as part of the move:

* Remove BucketFunction in favor of jdk LongFunction
* Accessor to get type for a given bucket changed to package
  private
  • Loading branch information
brharrington committed Jan 16, 2016
1 parent 1663dd1 commit c24d983
Show file tree
Hide file tree
Showing 11 changed files with 765 additions and 292 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright 2015 Netflix, 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 com.netflix.spectator.api.histogram;

import com.netflix.spectator.api.Counter;
import com.netflix.spectator.api.DistributionSummary;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Measurement;
import com.netflix.spectator.api.Registry;

import java.util.Collections;
import java.util.function.LongFunction;

/** Counters that get incremented based on the bucket for recorded values. */
public final class BucketCounter implements DistributionSummary {

/**
* Creates a distribution summary object that manages a set of counters based on the bucket
* function supplied. Calling record will increment the appropriate counter.
*
* @param registry
* Registry to use.
* @param id
* Identifier for the metric being registered.
* @param f
* Function to map values to buckets. See {@link BucketFunctions} for more information.
* @return
* Distribution summary that manages sub-counters based on the bucket function.
*/
public static BucketCounter get(Registry registry, Id id, LongFunction<String> f) {
return new BucketCounter(registry, id, f);
}

private final Registry registry;
private final Id id;
private final LongFunction<String> f;

/** Create a new instance. */
BucketCounter(Registry registry, Id id, LongFunction<String> f) {
this.registry = registry;
this.id = id;
this.f = f;
}

@Override public Id id() {
return id;
}

@Override public Iterable<Measurement> measure() {
return Collections.emptyList();
}

@Override public boolean hasExpired() {
return false;
}

@Override public void record(long amount) {
counter(f.apply(amount)).increment();
}

/**
* Return the count for a given bucket.
*/
Counter counter(String bucket) {
return registry.counter(id.withTag("bucket", bucket));
}

@Override public long count() {
return 0L;
}

@Override public long totalAmount() {
return 0L;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright 2015 Netflix, 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 com.netflix.spectator.api.histogram;

import com.netflix.spectator.api.DistributionSummary;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Measurement;
import com.netflix.spectator.api.Registry;

import java.util.Collections;
import java.util.function.LongFunction;

/** Distribution summaries that get updated based on the bucket for recorded values. */
public final class BucketDistributionSummary implements DistributionSummary {

/**
* Creates a distribution summary object that manages a set of distribution summaries based on
* the bucket function supplied. Calling record will be mapped to the record on the appropriate
* distribution summary.
*
* @param registry
* Registry to use.
* @param id
* Identifier for the metric being registered.
* @param f
* Function to map values to buckets. See {@link BucketFunctions} for more information.
* @return
* Distribution summary that manages sub-counters based on the bucket function.
*/
public static BucketDistributionSummary get(Registry registry, Id id, LongFunction<String> f) {
return new BucketDistributionSummary(registry, id, f);
}

private final Registry registry;
private final Id id;
private final LongFunction<String> f;

/** Create a new instance. */
BucketDistributionSummary(Registry registry, Id id, LongFunction<String> f) {
this.registry = registry;
this.id = id;
this.f = f;
}

@Override public Id id() {
return id;
}

@Override public Iterable<Measurement> measure() {
return Collections.emptyList();
}

@Override public boolean hasExpired() {
return false;
}

@Override public void record(long amount) {
distributionSummary(f.apply(amount)).record(amount);
}

/**
* Return the distribution summary for a given bucket.
*/
DistributionSummary distributionSummary(String bucket) {
return registry.distributionSummary(id.withTag("bucket", bucket));
}

@Override public long count() {
return 0L;
}

@Override public long totalAmount() {
return 0L;
}
}
Loading

0 comments on commit c24d983

Please sign in to comment.