|
| 1 | +/* |
| 2 | + * Copyright 2019, OpenCensus Authors |
| 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 | + |
| 17 | +package io.opencensus.metrics; |
| 18 | + |
| 19 | +import com.google.auto.value.AutoValue; |
| 20 | +import io.opencensus.internal.Utils; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import javax.annotation.concurrent.Immutable; |
| 28 | + |
| 29 | +/** |
| 30 | + * Options for every metric added to the {@link MetricRegistry}. |
| 31 | + * |
| 32 | + * @since 0.20 |
| 33 | + */ |
| 34 | +@Immutable |
| 35 | +@AutoValue |
| 36 | +public abstract class MetricOptions { |
| 37 | + |
| 38 | + /** |
| 39 | + * Returns the description of the Metric. |
| 40 | + * |
| 41 | + * <p>Default value is {@code ""}. |
| 42 | + * |
| 43 | + * @return the description of the Metric. |
| 44 | + */ |
| 45 | + public abstract String getDescription(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns the unit of the Metric. |
| 49 | + * |
| 50 | + * <p>Default value is {@code "1"}. |
| 51 | + * |
| 52 | + * @return the unit of the Metric. |
| 53 | + */ |
| 54 | + public abstract String getUnit(); |
| 55 | + |
| 56 | + /** |
| 57 | + * Returns the list of label keys for the Metric. |
| 58 | + * |
| 59 | + * <p>Default value is {@link Collections#emptyList()}. |
| 60 | + * |
| 61 | + * @return the list of label keys for the Metric. |
| 62 | + */ |
| 63 | + public abstract List<LabelKey> getLabelKeys(); |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns the map of constant labels (they will be added to all the TimeSeries) for the Metric. |
| 67 | + * |
| 68 | + * <p>Default value is {@link Collections#emptyMap()}. |
| 69 | + * |
| 70 | + * @return the map of constant labels for the Metric. |
| 71 | + */ |
| 72 | + // TODO: add support for this and make it public. |
| 73 | + abstract Map<LabelKey, LabelValue> getConstantLabels(); |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns a new {@link Builder} with default options. |
| 77 | + * |
| 78 | + * @return a new {@code Builder} with default options. |
| 79 | + * @since 0.20.0 |
| 80 | + */ |
| 81 | + public static Builder builder() { |
| 82 | + return new AutoValue_MetricOptions.Builder() |
| 83 | + .setDescription("") |
| 84 | + .setUnit("1") |
| 85 | + .setLabelKeys(Collections.<LabelKey>emptyList()) |
| 86 | + .setConstantLabels(Collections.<LabelKey, LabelValue>emptyMap()); |
| 87 | + } |
| 88 | + |
| 89 | + @AutoValue.Builder |
| 90 | + public abstract static class Builder { |
| 91 | + |
| 92 | + /** |
| 93 | + * Sets the description of the Metric. |
| 94 | + * |
| 95 | + * @param description the description of the Metric. |
| 96 | + * @return this. |
| 97 | + */ |
| 98 | + public abstract Builder setDescription(String description); |
| 99 | + |
| 100 | + /** |
| 101 | + * Sets the unit of the Metric. |
| 102 | + * |
| 103 | + * @param unit the unit of the Metric. |
| 104 | + * @return this. |
| 105 | + */ |
| 106 | + public abstract Builder setUnit(String unit); |
| 107 | + |
| 108 | + /** |
| 109 | + * Sets the list of label keys for the Metric. |
| 110 | + * |
| 111 | + * @param labelKeys the list of label keys for the Metric. |
| 112 | + * @return this. |
| 113 | + */ |
| 114 | + public abstract Builder setLabelKeys(List<LabelKey> labelKeys); |
| 115 | + |
| 116 | + /** |
| 117 | + * Sets the map of constant labels (they will be added to all the TimeSeries) for the Metric. |
| 118 | + * |
| 119 | + * @param constantLabels the map of constant labels for the Metric. |
| 120 | + * @return this. |
| 121 | + */ |
| 122 | + // TODO: add support for this and make it public. |
| 123 | + abstract Builder setConstantLabels(Map<LabelKey, LabelValue> constantLabels); |
| 124 | + |
| 125 | + abstract Map<LabelKey, LabelValue> getConstantLabels(); |
| 126 | + |
| 127 | + abstract List<LabelKey> getLabelKeys(); |
| 128 | + |
| 129 | + abstract MetricOptions autoBuild(); |
| 130 | + |
| 131 | + /** |
| 132 | + * Builds and returns a {@code MetricOptions} with the desired options. |
| 133 | + * |
| 134 | + * @return a {@code MetricOptions} with the desired options. |
| 135 | + * @since 0.20 |
| 136 | + * @throws NullPointerException if {@code description}, OR {@code unit} is null, OR {@code |
| 137 | + * labelKeys} is null OR any element of {@code labelKeys} is null, OR OR {@code |
| 138 | + * constantLabels} is null OR any element of {@code constantLabels} is null. |
| 139 | + * @throws IllegalArgumentException if any {@code LabelKey} from the {@code labelKeys} is in the |
| 140 | + * {@code constantLabels}. |
| 141 | + */ |
| 142 | + public MetricOptions build() { |
| 143 | + setLabelKeys(Collections.unmodifiableList(new ArrayList<LabelKey>(getLabelKeys()))); |
| 144 | + setConstantLabels( |
| 145 | + Collections.unmodifiableMap( |
| 146 | + new LinkedHashMap<LabelKey, LabelValue>(getConstantLabels()))); |
| 147 | + MetricOptions options = autoBuild(); |
| 148 | + Utils.checkListElementNotNull(options.getLabelKeys(), "labelKeys elements"); |
| 149 | + Utils.checkMapElementNotNull(options.getConstantLabels(), "constantLabels elements"); |
| 150 | + |
| 151 | + HashSet<String> labelKeyNamesMap = new HashSet<String>(); |
| 152 | + for (LabelKey labelKey : options.getLabelKeys()) { |
| 153 | + if (labelKeyNamesMap.contains(labelKey.getKey())) { |
| 154 | + throw new IllegalArgumentException("Invalid LabelKey in labelKeys"); |
| 155 | + } |
| 156 | + labelKeyNamesMap.add(labelKey.getKey()); |
| 157 | + } |
| 158 | + for (Map.Entry<LabelKey, LabelValue> constantLabel : options.getConstantLabels().entrySet()) { |
| 159 | + if (labelKeyNamesMap.contains(constantLabel.getKey().getKey())) { |
| 160 | + throw new IllegalArgumentException("Invalid LabelKey in constantLabels"); |
| 161 | + } |
| 162 | + labelKeyNamesMap.add(constantLabel.getKey().getKey()); |
| 163 | + } |
| 164 | + return options; |
| 165 | + } |
| 166 | + |
| 167 | + Builder() {} |
| 168 | + } |
| 169 | + |
| 170 | + MetricOptions() {} |
| 171 | +} |
0 commit comments