|
| 1 | +/** |
| 2 | + * Copyright 2015 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.netflix.spectator.sandbox; |
| 17 | + |
| 18 | +import com.netflix.spectator.api.DistributionSummary; |
| 19 | +import com.netflix.spectator.api.Id; |
| 20 | +import com.netflix.spectator.api.Measurement; |
| 21 | +import com.netflix.spectator.api.Registry; |
| 22 | +import com.netflix.spectator.api.Spectator; |
| 23 | + |
| 24 | +import java.util.Collections; |
| 25 | + |
| 26 | +/** Distribution summaries that get updated based on the bucket for recorded values. */ |
| 27 | +public final class BucketDistributionSummary implements DistributionSummary { |
| 28 | + |
| 29 | + /** |
| 30 | + * Creates a distribution summary object that manages a set of distribution summaries based on |
| 31 | + * the bucket function supplied. Calling record will be mapped to the record on the appropriate |
| 32 | + * distribution summary. |
| 33 | + * |
| 34 | + * @param id |
| 35 | + * Identifier for the metric being registered. |
| 36 | + * @param f |
| 37 | + * Function to map values to buckets. |
| 38 | + * @return |
| 39 | + * Distribution summary that manages sub-counters based on the bucket function. |
| 40 | + */ |
| 41 | + public static BucketDistributionSummary get(Id id, BucketFunction f) { |
| 42 | + return get(Spectator.registry(), id, f); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates a distribution summary object that manages a set of distribution summaries based on |
| 47 | + * the bucket function supplied. Calling record will be mapped to the record on the appropriate |
| 48 | + * distribution summary. |
| 49 | + * |
| 50 | + * @param registry |
| 51 | + * Registry to use. |
| 52 | + * @param id |
| 53 | + * Identifier for the metric being registered. |
| 54 | + * @param f |
| 55 | + * Function to map values to buckets. |
| 56 | + * @return |
| 57 | + * Distribution summary that manages sub-counters based on the bucket function. |
| 58 | + */ |
| 59 | + public static BucketDistributionSummary get(Registry registry, Id id, BucketFunction f) { |
| 60 | + return new BucketDistributionSummary(registry, id, f); |
| 61 | + } |
| 62 | + |
| 63 | + private final Registry registry; |
| 64 | + private final Id id; |
| 65 | + private final BucketFunction f; |
| 66 | + |
| 67 | + /** Create a new instance. */ |
| 68 | + BucketDistributionSummary(Registry registry, Id id, BucketFunction f) { |
| 69 | + this.registry = registry; |
| 70 | + this.id = id; |
| 71 | + this.f = f; |
| 72 | + } |
| 73 | + |
| 74 | + @Override public Id id() { |
| 75 | + return id; |
| 76 | + } |
| 77 | + |
| 78 | + @Override public Iterable<Measurement> measure() { |
| 79 | + return Collections.emptyList(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override public boolean hasExpired() { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + @Override public void record(long amount) { |
| 87 | + distributionSummary(f.apply(amount)).record(amount); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Return the count for a given bucket. |
| 92 | + */ |
| 93 | + public DistributionSummary distributionSummary(String bucket) { |
| 94 | + return registry.distributionSummary(id.withTag("bucket", bucket)); |
| 95 | + } |
| 96 | + |
| 97 | + @Override public long count() { |
| 98 | + return 0L; |
| 99 | + } |
| 100 | + |
| 101 | + @Override public long totalAmount() { |
| 102 | + return 0L; |
| 103 | + } |
| 104 | +} |
0 commit comments