|
1 | 1 | package io.prometheus.client;
|
2 | 2 |
|
| 3 | +import java.util.AbstractList; |
3 | 4 | import java.util.ArrayList;
|
4 |
| -import java.util.concurrent.ConcurrentHashMap; |
5 |
| -import java.util.concurrent.ConcurrentMap; |
6 | 5 | import java.util.Arrays;
|
| 6 | +import java.util.Collections; |
7 | 7 | import java.util.List;
|
| 8 | +import java.util.concurrent.ConcurrentHashMap; |
| 9 | +import java.util.concurrent.ConcurrentMap; |
8 | 10 |
|
9 | 11 | /**
|
10 | 12 | * Common functionality for {@link Gauge}, {@link Counter}, {@link Summary} and {@link Histogram}.
|
@@ -53,30 +55,159 @@ public abstract class SimpleCollector<Child> extends Collector {
|
53 | 55 |
|
54 | 56 | protected final ConcurrentMap<List<String>, Child> children = new ConcurrentHashMap<List<String>, Child>();
|
55 | 57 | protected Child noLabelsChild;
|
| 58 | + private final ThreadLocal<ArrayList<String>> labelNamesPool = new ThreadLocal<ArrayList<String>>(); |
56 | 59 |
|
57 |
| - /** |
58 |
| - * Return the Child with the given labels, creating it if needed. |
59 |
| - * <p> |
60 |
| - * Must be passed the same number of labels are were passed to {@link #labelNames}. |
61 |
| - */ |
62 |
| - public Child labels(String... labelValues) { |
63 |
| - if (labelValues.length != labelNames.size()) { |
64 |
| - throw new IllegalArgumentException("Incorrect number of labels."); |
| 60 | + /** |
| 61 | + * It is just reimplementing in a more JIT-friendly way both equals/hashCode to avoid |
| 62 | + * using Iterators like the original {@link AbstractList}. |
| 63 | + */ |
| 64 | + private static final class LabelNames extends ArrayList<String> { |
| 65 | + |
| 66 | + public LabelNames(int capacity) { |
| 67 | + super(capacity); |
| 68 | + } |
| 69 | + |
| 70 | + public boolean equals(Object o) { |
| 71 | + if (o == this) |
| 72 | + return true; |
| 73 | + if (!(o instanceof ArrayList)) { |
| 74 | + //what if o is a singleton list or empty? |
| 75 | + //We can just use the common fast path |
| 76 | + if (o instanceof List) { |
| 77 | + if (((List) o).size() > 1) { |
| 78 | + return super.equals(o); |
| 79 | + } |
| 80 | + } else { |
| 81 | + return super.equals(o); |
| 82 | + } |
| 83 | + } |
| 84 | + final int size = size(); |
| 85 | + final List<?> other = (List<?>) o; |
| 86 | + if (size != other.size()) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + for (int i = 0; i < size; i++) { |
| 90 | + final Object a = get(i); |
| 91 | + final Object b = other.get(i); |
| 92 | + final boolean eq = (a == b) || (a != null && a.equals(b)); |
| 93 | + if (!eq) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + } |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns the hash code value for this list. |
| 102 | + * |
| 103 | + * <p>This implementation uses exactly the code that is used to define the |
| 104 | + * list hash function in the documentation for the {@link List#hashCode} |
| 105 | + * method. |
| 106 | + * |
| 107 | + * @return the hash code value for this list |
| 108 | + */ |
| 109 | + public int hashCode() { |
| 110 | + int hashCode = 1; |
| 111 | + for (int i = 0, size = size(); i < size; i++) { |
| 112 | + final Object e = get(i); |
| 113 | + final int objHash = (e == null ? 0 : e.hashCode()); |
| 114 | + hashCode = 31 * hashCode + objHash; |
| 115 | + } |
| 116 | + return hashCode; |
| 117 | + } |
65 | 118 | }
|
66 |
| - for (String label: labelValues) { |
67 |
| - if (label == null) { |
68 |
| - throw new IllegalArgumentException("Label cannot be null."); |
69 |
| - } |
| 119 | + |
| 120 | + /** |
| 121 | + * Return the Child with the given labels, creating it if needed. |
| 122 | + * <p> |
| 123 | + * Must be passed the same number of labels are were passed to {@link #labelNames}. |
| 124 | + */ |
| 125 | + public Child labels(String... labelValues) { |
| 126 | + validateLabels(labelValues); |
| 127 | + final List<String> labels; |
| 128 | + if (labelValues.length > 0) { |
| 129 | + labels = pooledLabelNamesOf(labelValues); |
| 130 | + } else { |
| 131 | + labels = Collections.emptyList(); |
| 132 | + } |
| 133 | + return getOrCreateChild(labels); |
70 | 134 | }
|
71 |
| - List<String> key = Arrays.asList(labelValues); |
72 |
| - Child c = children.get(key); |
73 |
| - if (c != null) { |
74 |
| - return c; |
| 135 | + |
| 136 | + private List<String> pooledLabelNamesOf(String... labelValues) { |
| 137 | + final ThreadLocal<ArrayList<String>> labelNamesPool = this.labelNamesPool; |
| 138 | + ArrayList<String> pooledLabels = labelNamesPool.get(); |
| 139 | + final int labelValuesCount = labelValues.length; |
| 140 | + if (pooledLabels == null) { |
| 141 | + pooledLabels = new LabelNames(labelValuesCount); |
| 142 | + labelNamesPool.set(pooledLabels); |
| 143 | + } |
| 144 | + for (String label : labelValues) { |
| 145 | + pooledLabels.add(label); |
| 146 | + } |
| 147 | + return pooledLabels; |
| 148 | + } |
| 149 | + |
| 150 | + private Child getOrCreateChild(List<String> labels) { |
| 151 | + Child c = children.get(labels); |
| 152 | + if (c != null) { |
| 153 | + labels.clear(); |
| 154 | + return c; |
| 155 | + } |
| 156 | + return tryCreateChild(labels); |
| 157 | + } |
| 158 | + |
| 159 | + private Child tryCreateChild(List<String> labels) { |
| 160 | + Child c2 = newChild(); |
| 161 | + Child tmp = children.putIfAbsent(labels, c2); |
| 162 | + if (tmp == null) { |
| 163 | + //given that putIfAbsent return null only when a new |
| 164 | + //labels has been added, we need to clear up |
| 165 | + //the pool to avoid labels to be both in the pool |
| 166 | + //and as children key |
| 167 | + labelNamesPool.set(null); |
| 168 | + return c2; |
| 169 | + } else { |
| 170 | + labels.clear(); |
| 171 | + return tmp; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private void validateLabels(String... labelValues) { |
| 176 | + if (labelValues.length != labelNames.size()) { |
| 177 | + throw new IllegalArgumentException("Incorrect number of labels."); |
| 178 | + } |
| 179 | + for (String label : labelValues) { |
| 180 | + if (label == null) { |
| 181 | + throw new IllegalArgumentException("Label cannot be null."); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + private void validateLabel(String labelValue) { |
| 187 | + if (labelNames.size() != 1) { |
| 188 | + throw new IllegalArgumentException("Incorrect number of labels."); |
| 189 | + } |
| 190 | + if (labelValue == null) { |
| 191 | + throw new IllegalArgumentException("Label cannot be null."); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Return the Child with the given labels, creating it if needed. |
| 197 | + * <p> |
| 198 | + * Must be passed the same number of labels are were passed to {@link #labelNames}. |
| 199 | + */ |
| 200 | + public Child labels(String labelValue) { |
| 201 | + validateLabel(labelValue); |
| 202 | + final ThreadLocal<ArrayList<String>> labelNamesPool = this.labelNamesPool; |
| 203 | + ArrayList<String> labels = labelNamesPool.get(); |
| 204 | + if (labels == null) { |
| 205 | + labels = new LabelNames(1); |
| 206 | + labelNamesPool.set(labels); |
| 207 | + } |
| 208 | + labels.add(labelValue); |
| 209 | + return getOrCreateChild(labels); |
75 | 210 | }
|
76 |
| - Child c2 = newChild(); |
77 |
| - Child tmp = children.putIfAbsent(key, c2); |
78 |
| - return tmp == null ? c2 : tmp; |
79 |
| - } |
80 | 211 |
|
81 | 212 | /**
|
82 | 213 | * Remove the Child with the given labels.
|
@@ -164,9 +295,11 @@ protected SimpleCollector(Builder b) {
|
164 | 295 | checkMetricName(fullname);
|
165 | 296 | if (b.help.isEmpty()) throw new IllegalStateException("Help hasn't been set.");
|
166 | 297 | help = b.help;
|
167 |
| - labelNames = Arrays.asList(b.labelNames); |
168 |
| - |
169 |
| - for (String n: labelNames) { |
| 298 | + labelNames = new LabelNames(b.labelNames.length); |
| 299 | + for (String label : b.labelNames) { |
| 300 | + labelNames.add(label); |
| 301 | + } |
| 302 | + for (String n: b.labelNames) { |
170 | 303 | checkMetricLabelName(n);
|
171 | 304 | }
|
172 | 305 |
|
|
0 commit comments