Add property for common key/values on observations
- Deprecates 'management.metrics.tags.*' Closes gh-33241pull/35890/head
parent
214f06083b
commit
5b06224af5
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.boot.actuate.autoconfigure.observation;
|
||||
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import io.micrometer.common.KeyValues;
|
||||
import io.micrometer.observation.Observation.Context;
|
||||
import io.micrometer.observation.ObservationFilter;
|
||||
|
||||
/**
|
||||
* {@link ObservationFilter} to apply settings from {@link ObservationProperties}.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
*/
|
||||
class PropertiesObservationFilter implements ObservationFilter {
|
||||
|
||||
private final ObservationFilter delegate;
|
||||
|
||||
PropertiesObservationFilter(ObservationProperties properties) {
|
||||
this.delegate = createDelegate(properties);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Context map(Context context) {
|
||||
return this.delegate.map(context);
|
||||
}
|
||||
|
||||
private static ObservationFilter createDelegate(ObservationProperties properties) {
|
||||
if (properties.getKeyValues().isEmpty()) {
|
||||
return (context) -> context;
|
||||
}
|
||||
KeyValues keyValues = KeyValues.of(properties.getKeyValues().entrySet(), Entry::getKey, Entry::getValue);
|
||||
return (context) -> context.addLowCardinalityKeyValues(keyValues);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.boot.actuate.autoconfigure.observation;
|
||||
|
||||
import io.micrometer.common.KeyValue;
|
||||
import io.micrometer.common.KeyValues;
|
||||
import io.micrometer.observation.Observation.Context;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link PropertiesObservationFilter}.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
*/
|
||||
class PropertiesObservationFilterTests {
|
||||
|
||||
@Test
|
||||
void shouldDoNothingIfKeyValuesAreEmpty() {
|
||||
PropertiesObservationFilter filter = createFilter();
|
||||
Context mapped = mapContext(filter, "a", "alpha");
|
||||
assertThat(mapped.getLowCardinalityKeyValues()).containsExactly(KeyValue.of("a", "alpha"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAddKeyValues() {
|
||||
PropertiesObservationFilter filter = createFilter("b", "beta");
|
||||
Context mapped = mapContext(filter, "a", "alpha");
|
||||
assertThat(mapped.getLowCardinalityKeyValues()).containsExactly(KeyValue.of("a", "alpha"),
|
||||
KeyValue.of("b", "beta"));
|
||||
}
|
||||
|
||||
private static Context mapContext(PropertiesObservationFilter filter, String... initialKeyValues) {
|
||||
Context context = new Context();
|
||||
context.addLowCardinalityKeyValues(KeyValues.of(initialKeyValues));
|
||||
return filter.map(context);
|
||||
}
|
||||
|
||||
private static PropertiesObservationFilter createFilter(String... keyValues) {
|
||||
ObservationProperties properties = new ObservationProperties();
|
||||
for (int i = 0; i < keyValues.length; i += 2) {
|
||||
properties.getKeyValues().put(keyValues[i], keyValues[i + 1]);
|
||||
}
|
||||
return new PropertiesObservationFilter(properties);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue