Skip to content

Commit a9c4ed2

Browse files
New approach to naming conventions
with this change we're merging semantic name provider and key values provider into a single ObservationConvention construct users can register their conventions in the observationregistry configuration and then in the instrumentations ask for a registered convention (and provide a default if one is not registered)
1 parent f0a319f commit a9c4ed2

File tree

13 files changed

+252
-252
lines changed

13 files changed

+252
-252
lines changed

micrometer-commons/src/main/java/io/micrometer/common/docs/SemanticNameProvider.java

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
@NonNullApi
4040
@NonNullFields
41-
public class DefaultOkHttpKeyValuesProvider implements OkHttpKeyValuesProvider {
41+
public class DefaultOkHttpObservationConvention implements OkHttpObservationConvention {
4242

4343
static final boolean REQUEST_TAG_CLASS_EXISTS;
4444

@@ -67,6 +67,12 @@ private static Method getMethod(Class<?>... parameterTypes) {
6767
private static final KeyValues TAGS_TARGET_UNKNOWN = KeyValues.of(TAG_TARGET_SCHEME, TAG_VALUE_UNKNOWN,
6868
TAG_TARGET_HOST, TAG_VALUE_UNKNOWN, TAG_TARGET_PORT, TAG_VALUE_UNKNOWN);
6969

70+
private final String metricName;
71+
72+
public DefaultOkHttpObservationConvention(String metricName) {
73+
this.metricName = metricName;
74+
}
75+
7076
@Override
7177
public KeyValues getLowCardinalityKeyValues(OkHttpContext context) {
7278
OkHttpMetricsEventListener.CallState state = context.getState();
@@ -143,4 +149,9 @@ private KeyValues generateTagsForRoute(@Nullable Request request) {
143149
TAG_TARGET_PORT, Integer.toString(request.url().port()));
144150
}
145151

152+
@Override
153+
public String getName() {
154+
return this.metricName;
155+
}
156+
146157
}

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/okhttp3/OkHttpDocumentedObservation.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
public enum OkHttpDocumentedObservation implements DocumentedObservation {
3232

3333
/**
34-
* Observation when using
35-
* {@link ObservationRegistry.ObservationNamingConfiguration#DEFAULT} mode.
34+
* Default observation for OK HTTP.
3635
*/
3736
DEFAULT {
3837
@Override
@@ -51,29 +50,26 @@ public KeyName[] getLowCardinalityKeyNames() {
5150
* @param registry observation registry
5251
* @param okHttpContext the ok http context
5352
* @param requestsMetricName name of the observation
54-
* @param keyValuesProvider key values provider. If {@code null} then the default
55-
* provider will be used
53+
* @param customConvention custom convention. If {@code null}, the
54+
* {@link DefaultOkHttpObservationConvention} will be used.
5655
* @return a new {@link OkHttpDocumentedObservation}
5756
*/
57+
@SuppressWarnings("unchecked")
5858
static Observation of(@NonNull ObservationRegistry registry, @NonNull OkHttpContext okHttpContext,
5959
@NonNull String requestsMetricName,
60-
@Nullable Observation.KeyValuesProvider<OkHttpContext> keyValuesProvider) {
61-
Observation.KeyValuesProvider<?> provider = null;
60+
@Nullable Observation.ObservationConvention<OkHttpContext> customConvention) {
61+
Observation.ObservationConvention<OkHttpContext> convention = null;
6262
if (registry.isNoop()) {
63-
provider = Observation.KeyValuesProvider.EMPTY;
63+
convention = Observation.ObservationConvention.noOp();
6464
}
65-
else if (keyValuesProvider != null) {
66-
provider = keyValuesProvider;
67-
}
68-
else if (registry.observationConfig()
69-
.getObservationNamingConfiguration() == ObservationRegistry.ObservationNamingConfiguration.DEFAULT) {
70-
provider = new DefaultOkHttpKeyValuesProvider();
65+
else if (customConvention != null) {
66+
convention = customConvention;
7167
}
7268
else {
73-
throw new IllegalStateException(
74-
"You've provided a STANDARDIZED naming configuration but haven't provided a key values provider");
69+
convention = registry.observationConfig().getObservationConvention(okHttpContext,
70+
new DefaultOkHttpObservationConvention(requestsMetricName));
7571
}
76-
return Observation.createNotStarted(requestsMetricName, okHttpContext, registry).keyValuesProvider(provider);
72+
return Observation.createNotStarted(convention, okHttpContext, registry);
7773
}
7874

7975
enum OkHttpLegacyLowCardinalityTags implements KeyName {

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/okhttp3/OkHttpMetricsEventListener.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class OkHttpMetricsEventListener extends EventListener {
6767

6868
private final ObservationRegistry observationRegistry;
6969

70-
private final Observation.KeyValuesProvider<OkHttpContext> keyValuesProvider;
70+
private final Observation.ObservationConvention<OkHttpContext> keyValuesProvider;
7171

7272
private final String requestsMetricName;
7373

@@ -91,12 +91,12 @@ public class OkHttpMetricsEventListener extends EventListener {
9191
protected OkHttpMetricsEventListener(MeterRegistry registry, String requestsMetricName,
9292
Function<Request, String> urlMapper, Iterable<Tag> extraTags,
9393
Iterable<BiFunction<Request, Response, Tag>> contextSpecificTags) {
94-
this(registry, ObservationRegistry.NOOP, new DefaultOkHttpKeyValuesProvider(), requestsMetricName, urlMapper,
95-
extraTags, contextSpecificTags, emptyList(), true);
94+
this(registry, ObservationRegistry.NOOP, new DefaultOkHttpObservationConvention(requestsMetricName),
95+
requestsMetricName, urlMapper, extraTags, contextSpecificTags, emptyList(), true);
9696
}
9797

9898
OkHttpMetricsEventListener(MeterRegistry registry, ObservationRegistry observationRegistry,
99-
Observation.KeyValuesProvider<OkHttpContext> keyValuesProvider, String requestsMetricName,
99+
Observation.ObservationConvention<OkHttpContext> keyValuesProvider, String requestsMetricName,
100100
Function<Request, String> urlMapper, Iterable<Tag> extraTags,
101101
Iterable<BiFunction<Request, Response, Tag>> contextSpecificTags, Iterable<String> requestTagKeys,
102102
boolean includeHostTag) {
@@ -284,7 +284,7 @@ public static class Builder {
284284

285285
private Iterable<String> requestTagKeys = Collections.emptyList();
286286

287-
private OkHttpKeyValuesProvider keyValuesProvider;
287+
private OkHttpObservationConvention observationConvention;
288288

289289
Builder(MeterRegistry registry, String name) {
290290
this.registry = registry;
@@ -301,8 +301,8 @@ public Builder observationRegistry(ObservationRegistry observationRegistry) {
301301
return this;
302302
}
303303

304-
public Builder keyValuesProvider(OkHttpKeyValuesProvider keyValuesProvider) {
305-
this.keyValuesProvider = keyValuesProvider;
304+
public Builder observationConvention(OkHttpObservationConvention observationConvention) {
305+
this.observationConvention = observationConvention;
306306
return this;
307307
}
308308

@@ -379,7 +379,7 @@ public Builder requestTagKeys(Iterable<String> requestTagKeys) {
379379

380380
@SuppressWarnings("unchecked")
381381
public OkHttpMetricsEventListener build() {
382-
return new OkHttpMetricsEventListener(registry, observationRegistry, keyValuesProvider, name, uriMapper,
382+
return new OkHttpMetricsEventListener(registry, observationRegistry, observationConvention, name, uriMapper,
383383
tags, contextSpecificTags, requestTagKeys, includeHostTag);
384384
}
385385

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/okhttp3/OkHttpKeyValuesProvider.java renamed to micrometer-core/src/main/java/io/micrometer/core/instrument/binder/okhttp3/OkHttpObservationConvention.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @author Marcin Grzejszczak
2525
* @since 1.10.0
2626
*/
27-
public interface OkHttpKeyValuesProvider extends HttpKeyValueProvider<OkHttpContext> {
27+
public interface OkHttpObservationConvention extends Observation.ObservationConvention<OkHttpContext> {
2828

2929
@Override
3030
default boolean supportsContext(Observation.Context context) {

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/okhttp3/StandardizedOkHttpKeyValuesProvider.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

micrometer-core/src/test/java/io/micrometer/core/instrument/binder/okhttp3/OkHttpMetricsEventListenerTest.java

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -110,69 +110,22 @@ void timeSuccessfulWithDefaultObservation(@WiremockResolver.Wiremock WireMockSer
110110
}
111111

112112
@Test
113-
void timeSuccessfulWithKeyValuesProvider(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
114-
ObservationRegistry observationRegistry = ObservationRegistry.create();
115-
TestHandler testHandler = new TestHandler();
116-
observationRegistry.observationConfig().observationHandler(testHandler);
117-
observationRegistry.observationConfig().observationHandler(new TimerObservationHandler(registry));
118-
client = new OkHttpClient.Builder()
119-
.eventListener(defaultListenerBuilder().observationRegistry(observationRegistry)
120-
// Example of a custom key values provider
121-
.keyValuesProvider(new OkHttpKeyValuesProvider() {
122-
@Override
123-
public KeyValues getLowCardinalityKeyValues(OkHttpContext context) {
124-
return KeyValues.of("baz", "baz2");
125-
}
126-
}).build())
127-
.build();
128-
server.stubFor(any(anyUrl()));
129-
Request request = new Request.Builder().url(server.baseUrl()).build();
130-
131-
client.newCall(request).execute().close();
132-
133-
assertThat(registry.get("okhttp.requests").tags("baz", "baz2").timer().count()).isEqualTo(1L);
134-
}
135-
136-
@Test
137-
void timeSuccessfulWithKeyValueConvention(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
113+
void timeSuccessfulWithObservationConvention(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
138114
ObservationRegistry observationRegistry = ObservationRegistry.create();
139115
TestHandler testHandler = new TestHandler();
140116
MyConvention myConvention = new MyConvention();
141-
observationRegistry.observationConfig()
142-
.namingConfiguration(ObservationRegistry.ObservationNamingConfiguration.STANDARDIZED);
143117
observationRegistry.observationConfig().observationHandler(testHandler);
144118
observationRegistry.observationConfig().observationHandler(new TimerObservationHandler(registry));
145119
client = new OkHttpClient.Builder()
146120
.eventListener(defaultListenerBuilder().observationRegistry(observationRegistry)
147-
// Example of a custom key values provider that uses the
148-
// conventions mechanism
149-
.keyValuesProvider(new StandardizedOkHttpKeyValuesProvider(myConvention)).build())
121+
.observationConvention(new StandardizedOkHttpObservationConvention(myConvention)).build())
150122
.build();
151123
server.stubFor(any(anyUrl()));
152124
Request request = new Request.Builder().url(server.baseUrl()).build();
153125

154126
client.newCall(request).execute().close();
155127

156-
assertThat(registry.get("okhttp.requests").tags("peer", "name").timer().count()).isEqualTo(1L);
157-
}
158-
159-
@Test
160-
void timeIllegalStateExceptionWhenStandardOptionOnAndNoKeyValuesProviderSet(
161-
@WiremockResolver.Wiremock WireMockServer server) {
162-
ObservationRegistry observationRegistry = ObservationRegistry.create();
163-
// We're explicitly turning on the standard mode
164-
observationRegistry.observationConfig()
165-
.namingConfiguration(ObservationRegistry.ObservationNamingConfiguration.STANDARDIZED);
166-
observationRegistry.observationConfig().observationHandler(new TimerObservationHandler(registry));
167-
client = new OkHttpClient.Builder()
168-
// We're NOT setting the key values provider
169-
.eventListener(defaultListenerBuilder().observationRegistry(observationRegistry).build()).build();
170-
server.stubFor(any(anyUrl()));
171-
Request request = new Request.Builder().url(server.baseUrl()).build();
172-
173-
assertThatThrownBy(() -> client.newCall(request).execute().close()).isInstanceOf(IllegalStateException.class)
174-
.hasMessageContaining(
175-
"You've provided a STANDARDIZED naming configuration but haven't provided a key values provider");
128+
assertThat(registry.get("new.name").tags("peer", "name").timer().count()).isEqualTo(1L);
176129
}
177130

178131
@Test
@@ -375,11 +328,11 @@ public boolean supportsContext(Observation.Context context) {
375328

376329
}
377330

378-
static class StandardizedOkHttpKeyValuesProvider implements OkHttpKeyValuesProvider {
331+
static class StandardizedOkHttpObservationConvention implements OkHttpObservationConvention {
379332

380333
private final HttpClientKeyValuesConvention convention;
381334

382-
StandardizedOkHttpKeyValuesProvider(HttpClientKeyValuesConvention convention) {
335+
StandardizedOkHttpObservationConvention(HttpClientKeyValuesConvention convention) {
383336
this.convention = convention;
384337
}
385338

@@ -388,6 +341,11 @@ public KeyValues getLowCardinalityKeyValues(OkHttpContext context) {
388341
return KeyValues.of(convention.peerName(null));
389342
}
390343

344+
@Override
345+
public String getName() {
346+
return "new.name";
347+
}
348+
391349
}
392350

393351
static class MyConvention implements HttpClientKeyValuesConvention {

0 commit comments

Comments
 (0)