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,12 +17,14 @@

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

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -93,21 +95,25 @@ public Meter.Id map(Meter.Id id) {
* @param tagKeys Keys of tags that should be suppressed.
* @return A tag-suppressing filter.
*/
static MeterFilter ignoreTags(String... tagKeys) {
return new MeterFilter() {
@Override
public Meter.Id map(Meter.Id id) {
List<Tag> tags = stream(id.getTagsAsIterable().spliterator(), false).filter(t -> {
for (String tagKey : tagKeys) {
if (t.getKey().equals(tagKey))
return false;
}
return true;
}).collect(toList());
static MeterFilter ignoreTags(Collection<String> tagKeys) {
switch (tagKeys.size()) {
case 0:
return NoOpFilter.create();
case 1:
case 2:
return OneTwoTagsDroppingFilter.of(tagKeys);
default:
return SetBackedTagDroppingFilter.of(tagKeys);
}
}

return id.replaceTags(tags);
}
};
/**
* Suppress tags with given tag keys.
* @param tagKeys Keys of tags that should be suppressed.
* @return A tag-suppressing filter.
*/
static MeterFilter ignoreTags(String... tagKeys) {
return ignoreTags(Arrays.asList(tagKeys));
}

/**
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,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,120 @@
/*
* 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.common.lang.Nullable;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.config.MeterFilter;

import java.util.*;

/**
* Processes identifiers by dropping the tags with the matching keys. Separated from
* {@link SetBackedTagDroppingFilter} for performance reasons: in a frequent case end user
* needs to drop only one or two tags, it's much cheaper to abstain from more expensive
* lookups.
*
* @see SetBackedTagDroppingFilter for input with larger cardinality.
* @see NoOpFilter for input with the lowest cardinality.
* @since 1.15
*/
public class OneTwoTagsDroppingFilter implements MeterFilter {

@NonNull
private final String first;

@Nullable
private final String second;

private final int expectedSize;

OneTwoTagsDroppingFilter(@NonNull String first, @Nullable String second, int expectedSize) {
this.first = first;
this.second = second;
this.expectedSize = expectedSize;
}

@NonNull
@Override
public Meter.Id map(Meter.Id id) {
Iterator<Tag> iterator = id.getTagsAsIterable().iterator();

if (!iterator.hasNext()) {
// fast path avoiding list allocation completely
return id;
}

List<Tag> replacement = new ArrayList<>(expectedSize);
int removals = 0;

while (iterator.hasNext()) {
Tag tag = iterator.next();
String key = tag.getKey();

if (removals != 2 && (key.equals(first) || key.equals(second))) {
removals++;
continue;
}

replacement.add(tag);
}

return removals == 0 ? id : id.replaceTags(replacement);
}

public static MeterFilter of(@NonNull String first, @Nullable String second, int expectedSize) {
return new OneTwoTagsDroppingFilter(first, second, expectedSize);
}

public static MeterFilter of(@NonNull String first, @Nullable String second) {
return of(first, second, FilterSupport.DEFAULT_TAG_COUNT_EXPECTATION);
}

public static MeterFilter of(@NonNull String first, int expectedSize) {
return of(first, null, expectedSize);
}

public static MeterFilter of(@NonNull String first) {
return of(first, null);
}

public static MeterFilter of(@NonNull Collection<String> keys, int expectedSize) {
if (keys.size() != 1 && keys.size() != 2) {
throw new IllegalArgumentException("Expected collection with exactly one or two elements, got " + keys);
}

Iterator<String> iterator = keys.iterator();
String first = iterator.next();
String second = iterator.hasNext() ? iterator.next() : null;

return of(first, second, expectedSize);
}

public static MeterFilter of(@NonNull Collection<String> keys) {
return of(keys, FilterSupport.DEFAULT_TAG_COUNT_EXPECTATION);
}

public static MeterFilter of(@NonNull String[] keys, int expectedSize) {
return of(Arrays.asList(keys), expectedSize);
}

public static MeterFilter of(@NonNull String... keys) {
return of(keys, FilterSupport.DEFAULT_TAG_COUNT_EXPECTATION);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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;

import java.util.*;

/**
* Processes identifiers by dropping the tags with the matching keys.
*
* @see OneTwoTagsDroppingFilter for input with lower cardinality.
* @see NoOpFilter for input with the lowest cardinality.
* @since 1.15
*/
public class SetBackedTagDroppingFilter implements MeterFilter {

private final Set<String> keys;

private final int expectedSize;

SetBackedTagDroppingFilter(@NonNull Set<String> keys, int expectedSize) {
this.keys = keys;
this.expectedSize = expectedSize;
}

@Override
public Meter.Id map(Meter.Id id) {
Iterator<Tag> iterator = id.getTagsAsIterable().iterator();

if (!iterator.hasNext()) {
// fast path avoiding list allocation completely
return id;
}

List<Tag> replacement = new ArrayList<>(expectedSize);
boolean intercepted = false;

while (iterator.hasNext()) {
Tag tag = iterator.next();

if (keys.contains(tag.getKey())) {
intercepted = true;
continue;
}

replacement.add(tag);
}

if (!intercepted) {
// Nothing has changed? Return as is and let GC do the easy
// job of marking zero references array list as trash.
return id;
}

if (replacement.isEmpty()) {
// At the moment of writing replaceTags(List) would invoke
// a bit heavier path, so it's better to provide empty tags
// directly
return id.replaceTags(Tags.empty());
}

return id.replaceTags(replacement);
}

public static MeterFilter of(@NonNull Set<String> keys, int expectedSize) {
return new SetBackedTagDroppingFilter(keys, expectedSize);
}

public static MeterFilter of(@NonNull Set<String> keys) {
return of(keys, FilterSupport.DEFAULT_TAG_COUNT_EXPECTATION);
}

public static MeterFilter of(@NonNull Collection<String> keys, int expectedSize) {
Set<String> converted = keys instanceof Set ? (Set<String>) keys : new HashSet<>(keys);
return of(converted, expectedSize);
}

public static MeterFilter of(@NonNull Collection<String> keys) {
return of(keys, FilterSupport.DEFAULT_TAG_COUNT_EXPECTATION);
}

public static MeterFilter of(@NonNull String[] keys, int expectedSize) {
return of(Arrays.asList(keys), expectedSize);
}

public static MeterFilter of(@NonNull String... keys) {
return of(keys, FilterSupport.DEFAULT_TAG_COUNT_EXPECTATION);
}

}
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
Loading