Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

import io.micrometer.common.lang.Nullable;
import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.config.filter.MultiTagInsertionFilter;
import io.micrometer.core.instrument.config.filter.NoOpFilter;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -53,12 +56,34 @@ public interface MeterFilter {
* @return A common tag filter.
*/
static MeterFilter commonTags(Iterable<Tag> tags) {
return new MeterFilter() {
@Override
public Meter.Id map(Meter.Id id) {
return id.replaceTags(Tags.concat(tags, id.getTagsAsIterable()));
}
};
List<Tag> collected = stream(tags.spliterator(), false).collect(toList());

if (collected.isEmpty()) {
return NoOpFilter.create();
}

return MultiTagInsertionFilter.of(collected);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There would be some sense in creating a OneTwoInsertionFilter IF we'd have the API from #6113. This is the case in my company, we insert only a single tag, and getting rid of collection abstractions could have provided extra boost. However, since any addition here triggers the sorting process, there's no sense in creating a special implementation for the most frequent case.

}

/**
* @see #commonTags(Iterable)
*/
static MeterFilter commonTags(Tag... tags) {
return commonTags(Arrays.asList(tags));
}

/**
* @see #commonTags(Iterable)
*/
static MeterFilter commonTags(String... tags) {
return commonTags(Tags.of(tags));
}

/**
* @see #commonTags(Iterable)
*/
static MeterFilter commonTags(String key, String value) {
return commonTags(Tag.of(key, value));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2025 VMware, 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
*
* https://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 io.micrometer.core.instrument.config.filter;

class FilterSupport {

/**
* At the moment of writing, it was impossible to estimate tags count from the outside
* of class, but quite often a temporary storage (ArrayList) had to be allocated
* during processing. To avoid excessive resizes, this constant is introduced to
* preallocate space for such a list.
*/
public static final int DEFAULT_TAG_COUNT_EXPECTATION = 32;

private FilterSupport() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2025 VMware, 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
*
* https://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 io.micrometer.core.instrument.config.filter;

import io.micrometer.common.lang.NonNull;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.config.MeterFilter;

/**
* A filter whose job is to inject provided tags in all mapped identifiers, skipping the
* injection of a particular tag if identifier already has a tag under such a key. In
* other words, it injects all the provided tags, and in case of conflict it prefers the
* tag from the identifier rather than provided tags.
*
* @see NoOpFilter Skip the processing completely if there are no tags to inject.
* @since 1.15
*/
public class MultiTagInsertionFilter implements MeterFilter {

/**
* Storing injected values directly as Tags saves potential sort processing on every
* call.
*/
private final Tags injection;

MultiTagInsertionFilter(@NonNull Tags injection) {
this.injection = injection;
}

@NonNull
@Override
public Meter.Id map(Meter.Id id) {
Iterable<Tag> original = id.getTagsAsIterable();
Tags replacement = original == Tags.empty() ? injection : Tags.concat(injection, original);
return id.replaceTags(replacement);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not doing replacement == original check as it's impossible - even if all the injected tags would get overridden, this would not result in the same instance being returned. Calling .equals() is redundant because it involves iterative string comparison, it will outweigh any potential savings on that single allocation.

}

public static MeterFilter of(@NonNull Tags injections) {
return new MultiTagInsertionFilter(injections);
}

public static MeterFilter of(@NonNull Tag... injections) {
return of(Tags.of(injections));
}

public static MeterFilter of(@NonNull Iterable<Tag> injections) {
return of(Tags.of(injections));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2025 VMware, 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
*
* https://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 io.micrometer.core.instrument.config.filter;

import io.micrometer.core.instrument.config.MeterFilter;

/**
* A fallback for all factory methods that have received an input functionally equivalent
* to "abstain from processing".
*
* @since 1.15
*/
public class NoOpFilter implements MeterFilter {

private static final MeterFilter INSTANCE = new NoOpFilter();

private NoOpFilter() {
}

public static MeterFilter create() {
return INSTANCE;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2025 VMware, 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
*
* https://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 io.micrometer.core.instrument.config.filter;
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.core.instrument;
package io.micrometer.core.instrument.config;

import io.micrometer.common.lang.Nullable;
import io.micrometer.core.Issue;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.config.MeterFilterReply;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.assertj.core.api.Condition;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2025 VMware, 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
*
* https://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 io.micrometer.core.instrument.config.filter;

import io.micrometer.core.instrument.Tag;
import org.junit.jupiter.params.provider.Arguments;

import java.util.stream.Stream;

public class InsertionFilterTestSupport {

public static Stream<Arguments> variations() {
return Stream.of(
// Identifier tags : injected tags : expectation

// Working against an empty instance
Arguments.of(new Tag[0], new Tag[] { Tag.of("k1", "v1") }, new Tag[] { Tag.of("k1", "v1") }),
Arguments.of(new Tag[0], new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2") }),
Arguments.of(new Tag[0], new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3") }),
// Strictly appending
Arguments.of(new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3") },
new Tag[] { Tag.of("k4", "v4") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3"), Tag.of("k4", "v4") }),
Arguments.of(new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3") },
new Tag[] { Tag.of("k4", "v4"), Tag.of("k5", "v5") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3"), Tag.of("k4", "v4"),
Tag.of("k5", "v5") }),
Arguments.of(new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3") },
new Tag[] { Tag.of("k4", "v4"), Tag.of("k5", "v5"), Tag.of("k6", "v6") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3"), Tag.of("k4", "v4"),
Tag.of("k5", "v5"), Tag.of("k6", "v6") }),
// Strictly prepending
Arguments.of(new Tag[] { Tag.of("k4", "v4"), Tag.of("k5", "v5"), Tag.of("k6", "v6") },
new Tag[] { Tag.of("k1", "v1") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k4", "v4"), Tag.of("k5", "v5"), Tag.of("k6", "v6") }),
Arguments.of(new Tag[] { Tag.of("k4", "v4"), Tag.of("k5", "v5"), Tag.of("k6", "v6") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k4", "v4"), Tag.of("k5", "v5"),
Tag.of("k6", "v6") }),
Arguments.of(new Tag[] { Tag.of("k4", "v4"), Tag.of("k5", "v5"), Tag.of("k6", "v6") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3"), Tag.of("k4", "v4"),
Tag.of("k5", "v5"), Tag.of("k6", "v6") }),
// Strictly injecting in the middle
Arguments.of(
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k6", "v6"), Tag.of("k7", "v7") },
new Tag[] { Tag.of("k3", "v3") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3"), Tag.of("k6", "v6"),
Tag.of("k7", "v7") }),
Arguments.of(
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k6", "v6"), Tag.of("k7", "v7") },
new Tag[] { Tag.of("k3", "v3"), Tag.of("k4", "v4") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3"), Tag.of("k4", "v4"),
Tag.of("k6", "v6"), Tag.of("k7", "v7") }),
Arguments.of(
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k6", "v6"), Tag.of("k7", "v7") },
new Tag[] { Tag.of("k3", "v3"), Tag.of("k4", "v4"), Tag.of("k5", "v5") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3"), Tag.of("k4", "v4"),
Tag.of("k5", "v5"), Tag.of("k6", "v6"), Tag.of("k7", "v7") }),
// Interleaving injection
Arguments.of(
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k4", "v4"), Tag.of("k6", "v6"),
Tag.of("k7", "v7") },
new Tag[] { Tag.of("k3", "v3"), Tag.of("k5", "v5") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "v3"), Tag.of("k4", "v4"),
Tag.of("k5", "v5"), Tag.of("k6", "v6"), Tag.of("k7", "v7") }),
// Finally, some delicious food
// Testing overlaps
Arguments.of(new Tag[] { Tag.of("k1", "v1") }, new Tag[] { Tag.of("k1", "o1") },
new Tag[] { Tag.of("k1", "v1") }),
Arguments.of(new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2") }, new Tag[] { Tag.of("k1", "o1") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2") }),
Arguments.of(new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2") },
new Tag[] { Tag.of("k3", "o3"), Tag.of("k1", "o1") },
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2"), Tag.of("k3", "o3") }),
// Sanity check
Arguments.of(new Tag[] {}, new Tag[] {}, new Tag[] {}),
Arguments.of(new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2") }, new Tag[] {},
new Tag[] { Tag.of("k1", "v1"), Tag.of("k2", "v2") }));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2025 VMware, 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
*
* https://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 io.micrometer.core.instrument.config.filter;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.config.MeterFilter;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

class MultiTagInsertionFilterTest {

static Stream<Arguments> variations() {
return InsertionFilterTestSupport.variations();
}

@ParameterizedTest
@MethodSource("variations")
void variation(Tag[] existing, Tag[] inserted, Tag[] expectation) {
Meter.Id identifier = new Meter.Id("any", Tags.of(existing), null, null, Meter.Type.COUNTER);
MeterFilter sut = MultiTagInsertionFilter.of(inserted);
Meter.Id result = sut.map(sut.map(identifier));
assertThat(result.getTags()).containsExactly(expectation);
}

}