diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/Meter.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/Meter.java index 49fbc9294f..2b96206e69 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/Meter.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/Meter.java @@ -244,8 +244,7 @@ public Id withTags(Iterable tags) { /** * Generate a new id replacing all tags with new ones. * @param tags The tags to add. - * @return A new id with the only the provided tags. The source id remains - * unchanged. + * @return A new id with only the provided tags. The source id remains unchanged. * @since 1.1.0 */ public Id replaceTags(Iterable tags) { @@ -282,9 +281,15 @@ public String getName() { * @return A set of dimensions that allows you to break down the name. */ public List getTags() { - List tags = new ArrayList<>(); - this.tags.forEach(tags::add); - return Collections.unmodifiableList(tags); + if (this.tags == Tags.empty()) { + return Collections.emptyList(); + } + + List list = new ArrayList<>(this.tags.size()); + for (Tag tag : this.tags) { + list.add(tag); + } + return Collections.unmodifiableList(list); } public Iterable getTagsAsIterable() { diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/Tags.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/Tags.java index 1c696066e8..ea1c224f89 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/Tags.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/Tags.java @@ -225,6 +225,15 @@ public Tags and(@Nullable Iterable tags) { return merge(Tags.of(tags)); } + /** + * Non-public (for now) method to get the size of this, which can be useful in sizing + * a collection where these elements will be copied. + * @return number of unique {@link Tag} instances in this + */ + int size() { + return length; + } + @Override public Iterator iterator() { return new ArrayIterator();