From 11c2cb8ff5c0e4eb65e77fc9337155fc2a77b3c6 Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Tue, 19 May 2020 10:06:17 -0400 Subject: [PATCH 01/19] KIP-606 MetricsContext --- .../java/io/confluent/rest/Application.java | 24 +++++-- .../rest/metrics/RestMetricsContext.java | 67 +++++++++++++++++++ .../io/confluent/rest/ApplicationTest.java | 58 ++++++++++++++++ 3 files changed, 143 insertions(+), 6 deletions(-) create mode 100644 core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java diff --git a/core/src/main/java/io/confluent/rest/Application.java b/core/src/main/java/io/confluent/rest/Application.java index aace42baac..cbbf90bea7 100644 --- a/core/src/main/java/io/confluent/rest/Application.java +++ b/core/src/main/java/io/confluent/rest/Application.java @@ -19,7 +19,12 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.jaxrs.base.JsonParseExceptionMapper; +import io.confluent.rest.metrics.RestMetricsContext; import org.apache.kafka.common.config.ConfigException; +import org.apache.kafka.common.metrics.JmxReporter; +import org.apache.kafka.common.metrics.MetricConfig; +import org.apache.kafka.common.metrics.Metrics; +import org.apache.kafka.common.metrics.MetricsReporter; import org.eclipse.jetty.jaas.JAASLoginService; import org.eclipse.jetty.security.ConstraintMapping; import org.eclipse.jetty.security.ConstraintSecurityHandler; @@ -69,10 +74,6 @@ import javax.servlet.ServletException; import javax.ws.rs.core.Configurable; -import org.apache.kafka.common.metrics.JmxReporter; -import org.apache.kafka.common.metrics.MetricConfig; -import org.apache.kafka.common.metrics.Metrics; -import org.apache.kafka.common.metrics.MetricsReporter; import io.confluent.rest.auth.AuthUtil; import io.confluent.rest.exceptions.ConstraintViolationExceptionMapper; import io.confluent.rest.exceptions.GenericExceptionMapper; @@ -119,8 +120,13 @@ public Application(T config, String path) { List reporters = config.getConfiguredInstances(RestConfig.METRICS_REPORTER_CLASSES_CONFIG, MetricsReporter.class); - reporters.add(new JmxReporter(config.getString(RestConfig.METRICS_JMX_PREFIX_CONFIG))); - this.metrics = new Metrics(metricConfig, reporters, config.getTime()); + reporters.add(new JmxReporter()); + + RestMetricsContext metricsContext = new RestMetricsContext(config); + + this.setupMetricsContext(metricsContext, config); + + this.metrics = new Metrics(metricConfig, reporters, config.getTime(), metricsContext); this.getMetricsTags().putAll( parseListToMap(config.getList(RestConfig.METRICS_TAGS_CONFIG))); @@ -134,6 +140,12 @@ public final String getPath() { return path; } + + /** + * Register metadata with this Application's MetricsContext. + */ + public void setupMetricsContext(RestMetricsContext context, T appConfig) {} + /** * Returns {@link Metrics} object */ diff --git a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java new file mode 100644 index 0000000000..f5607c4d08 --- /dev/null +++ b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java @@ -0,0 +1,67 @@ +/** + * Copyright 2014 Confluent 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 + * + * http://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.confluent.rest.metrics; + +import io.confluent.rest.RestConfig; +import java.util.HashMap; +import java.util.Map; +import org.apache.kafka.common.metrics.MetricsContext; + +public class RestMetricsContext implements MetricsContext { + public static final String METRICS_CONTEXT_PREFIX = "metrics.context."; + public static final String METRICS_RESOURCE_NAME = "resource.type"; + + /** + * Client or Service's metadata map. + */ + private final Map metadata = new HashMap<>(); + + /** + * @param config RestConfig instance + */ + public RestMetricsContext(RestConfig config) { + metadata.put(MetricsContext.NAMESPACE, + config.getString(RestConfig.METRICS_JMX_PREFIX_CONFIG)); + + /* Copy all configuration properties prefixed into metadata instance. */ + config.originalsWithPrefix(METRICS_CONTEXT_PREFIX) + .forEach((key, value) -> metadata.put(key, (String) value)); + + /* + * RestApplication are most likely to be top-level compositions. + * Use JMX_PREFIX if METRICS_RESOURCE_NAME is not explicitly set. + */ + metadata.putIfAbsent(METRICS_RESOURCE_NAME, metadata.get(MetricsContext.NAMESPACE)); + } + + public String getResourceName() { + return metadata.get(METRICS_RESOURCE_NAME); + } + + public String getNameSpace() { + return metadata.get(MetricsContext.NAMESPACE); + } + + /** + * Returns client's metadata map. + * + * @return metadata fields + */ + public Map metadata() { + return metadata; + } +} diff --git a/core/src/test/java/io/confluent/rest/ApplicationTest.java b/core/src/test/java/io/confluent/rest/ApplicationTest.java index 87f019c774..77871cc988 100644 --- a/core/src/test/java/io/confluent/rest/ApplicationTest.java +++ b/core/src/test/java/io/confluent/rest/ApplicationTest.java @@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableMap; +import io.confluent.rest.metrics.RestMetricsContext; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; @@ -40,6 +41,7 @@ import org.junit.rules.ExpectedException; import java.io.IOException; +import java.lang.management.ManagementFactory; import java.net.URI; import java.net.URL; import java.util.ArrayList; @@ -52,6 +54,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; +import javax.management.MBeanServer; +import javax.management.ObjectName; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @@ -324,6 +328,54 @@ public void shouldShutdownProperlyEvenIfResourceExtensionThrowsOnShutdown() thro assertThat("shutdown called", TestApp.SHUTDOWN_CALLED.get(), is(true)); } + @Test + public void testDefaultMetricsContext() throws Exception { + TestApp testApp = new TestApp(); + + assertEquals(testApp.metricsContext.getResourceName(), RestConfig.METRICS_JMX_PREFIX_DEFAULT); + assertEquals(testApp.metricsContext.getNameSpace(), RestConfig.METRICS_JMX_PREFIX_DEFAULT); + } + + @Test + public void testMetricsContextResourceOverride() throws Exception { + Map props = new HashMap<>(); + props.put("metrics.context.resource.type", "FooApp"); + + TestApp testApp = new TestApp(props); + + assertEquals(testApp.metricsContext.getResourceName(), "FooApp"); + assertEquals(testApp.metricsContext.getNameSpace(), RestConfig.METRICS_JMX_PREFIX_DEFAULT); + + /* Only NameSpace should be propagated to JMX */ + String jmx_domain = RestConfig.METRICS_JMX_PREFIX_DEFAULT; + String mbean_name = String.format("%s:type=kafka-metrics-count", jmx_domain); + + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + assertNotNull(server.getObjectInstance(new ObjectName(mbean_name))); + } + + @Test + public void testMetricsContextJMXPrefixPropagation() throws Exception { + Map props = new HashMap<>(); + props.put(RestConfig.METRICS_JMX_PREFIX_CONFIG, "FooApp"); + + TestApp testApp = new TestApp(props); + + assertEquals(testApp.metricsContext.getResourceName(), "FooApp"); + assertEquals(testApp.metricsContext.getNameSpace(), "FooApp"); + } + + @Test + public void testMetricsContextJMXBeanRegistration() throws Exception { + Map props = new HashMap<>(); + props.put(RestConfig.METRICS_JMX_PREFIX_CONFIG, "FooApp"); + + new TestApp(props); + + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + assertNotNull(server.getObjectInstance(new ObjectName("FooApp:type=kafka-metrics-count"))); + } + private void assertExpectedUri(URI uri, String scheme, String host, int port) { assertEquals("Scheme should be " + scheme, scheme, uri.getScheme()); assertEquals("Host should be " + host, host, uri.getHost()); @@ -342,6 +394,7 @@ private HttpStatus.Code makeGetRequest(final TestApp app, final String path) thr private static class TestApp extends Application implements AutoCloseable { private static final AtomicBoolean SHUTDOWN_CALLED = new AtomicBoolean(true); + public RestMetricsContext metricsContext; @SafeVarargs private TestApp(final Class... extensions) throws Exception { @@ -354,6 +407,11 @@ public TestApp(final Map config) { super(createConfig(config)); } + @Override + public void setupMetricsContext(RestMetricsContext metricsContext, TestRestConfig appConfig) { + this.metricsContext = metricsContext; + } + @Override public void setupResources(final Configurable config, final TestRestConfig appConfig) { } From fa5c43a80f51ae8b6a119fbb55d28dc4c5223b3e Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Tue, 19 May 2020 11:41:19 -0400 Subject: [PATCH 02/19] Add ability to attach namespaces to a resource --- .../rest/metrics/RestMetricsContext.java | 21 +++-- .../rest/metrics/TestRestMetricsContext.java | 79 +++++++++++++++++++ 2 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java diff --git a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java index f5607c4d08..338ee37070 100644 --- a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java +++ b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java @@ -34,12 +34,12 @@ public class RestMetricsContext implements MetricsContext { * @param config RestConfig instance */ public RestMetricsContext(RestConfig config) { - metadata.put(MetricsContext.NAMESPACE, - config.getString(RestConfig.METRICS_JMX_PREFIX_CONFIG)); - /* Copy all configuration properties prefixed into metadata instance. */ - config.originalsWithPrefix(METRICS_CONTEXT_PREFIX) - .forEach((key, value) -> metadata.put(key, (String) value)); + this(config.originalsWithPrefix(METRICS_CONTEXT_PREFIX)); + + /* JMX_PREFIX is synonymous with MetricsContext.NAMESPACE */ + metadata.putIfAbsent(MetricsContext.NAMESPACE, + config.getString(RestConfig.METRICS_JMX_PREFIX_CONFIG)); /* * RestApplication are most likely to be top-level compositions. @@ -48,6 +48,10 @@ public RestMetricsContext(RestConfig config) { metadata.putIfAbsent(METRICS_RESOURCE_NAME, metadata.get(MetricsContext.NAMESPACE)); } + public RestMetricsContext(Map config) { + config.forEach((key, value) -> metadata.put(key, (String) value)); + } + public String getResourceName() { return metadata.get(METRICS_RESOURCE_NAME); } @@ -56,6 +60,13 @@ public String getNameSpace() { return metadata.get(MetricsContext.NAMESPACE); } + public MetricsContext newNamespace(String namespace) { + Map child = new HashMap<>(this.metadata); + child.put(MetricsContext.NAMESPACE, namespace); + + return new RestMetricsContext(child); + } + /** * Returns client's metadata map. * diff --git a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java new file mode 100644 index 0000000000..37c5f9f29e --- /dev/null +++ b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java @@ -0,0 +1,79 @@ +/** + * Copyright 2014 Confluent 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 + * + * http://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.confluent.rest.metrics; + +import static org.junit.Assert.assertEquals; + +import io.confluent.rest.RestConfig; +import io.confluent.rest.TestRestConfig; +import java.util.HashMap; +import java.util.Map; +import org.apache.kafka.common.metrics.MetricsContext; +import org.junit.Test; + +public class TestRestMetricsContext { + + @Test + public void testMetricsContextNewNameSpace() { + Map props = new HashMap<>(); + props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX + + RestMetricsContext.METRICS_RESOURCE_NAME, "root"); + + TestRestConfig config = new TestRestConfig(props); + RestMetricsContext context = new RestMetricsContext(config); + + System.out.println(context.metadata()); + + MetricsContext context2 = context.newNamespace("kafka-consumer"); + + System.out.println(context2.metadata()); + } + + @Test + public void testDefaultMetricsContext() throws Exception { + TestRestConfig config = new TestRestConfig(); + RestMetricsContext context = new RestMetricsContext(config); + + assertEquals(context.getResourceName(), "rest-utils"); + assertEquals(context.getNameSpace(), "rest-utils"); + } + + @Test + public void testMetricsContextResourceOverride() throws Exception { + Map props = new HashMap<>(); + props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX + + RestMetricsContext.METRICS_RESOURCE_NAME, "root"); + + TestRestConfig config = new TestRestConfig(props); + RestMetricsContext context = new RestMetricsContext(config); + + assertEquals(context.getResourceName(), "root"); + assertEquals(context.getNameSpace(), "rest-utils"); + } + + @Test + public void testMetricsContextJMXPrefixPropagation() throws Exception { + Map props = new HashMap<>(); + props.put(RestConfig.METRICS_JMX_PREFIX_CONFIG, "FooApp"); + + TestRestConfig config = new TestRestConfig(props); + RestMetricsContext context = new RestMetricsContext(config); + + assertEquals(context.getResourceName(), "FooApp"); + assertEquals(context.getNameSpace(), "FooApp"); + } +} From 14b5af6899d190959871a6be711aac4be123a0a1 Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Tue, 19 May 2020 11:57:32 -0400 Subject: [PATCH 03/19] Application should hold RestMetcisContext --- core/src/main/java/io/confluent/rest/Application.java | 9 ++++----- .../src/test/java/io/confluent/rest/ApplicationTest.java | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/io/confluent/rest/Application.java b/core/src/main/java/io/confluent/rest/Application.java index cbbf90bea7..132a09da15 100644 --- a/core/src/main/java/io/confluent/rest/Application.java +++ b/core/src/main/java/io/confluent/rest/Application.java @@ -94,6 +94,7 @@ public abstract class Application { // CHECKSTYLE_RULES.ON: ClassDataAbstractionCoupling protected T config; + protected RestMetricsContext metricsContext; private final String path; protected ApplicationServer server; @@ -113,6 +114,9 @@ public Application(T config, String path) { this.config = config; this.path = Objects.requireNonNull(path);; + this.metricsContext = new RestMetricsContext(config); + this.setupMetricsContext(metricsContext, config); + MetricConfig metricConfig = new MetricConfig() .samples(config.getInt(RestConfig.METRICS_NUM_SAMPLES_CONFIG)) .timeWindow(config.getLong(RestConfig.METRICS_SAMPLE_WINDOW_MS_CONFIG), @@ -122,12 +126,7 @@ public Application(T config, String path) { MetricsReporter.class); reporters.add(new JmxReporter()); - RestMetricsContext metricsContext = new RestMetricsContext(config); - - this.setupMetricsContext(metricsContext, config); - this.metrics = new Metrics(metricConfig, reporters, config.getTime(), metricsContext); - this.getMetricsTags().putAll( parseListToMap(config.getList(RestConfig.METRICS_TAGS_CONFIG))); diff --git a/core/src/test/java/io/confluent/rest/ApplicationTest.java b/core/src/test/java/io/confluent/rest/ApplicationTest.java index 77871cc988..af206ccb4a 100644 --- a/core/src/test/java/io/confluent/rest/ApplicationTest.java +++ b/core/src/test/java/io/confluent/rest/ApplicationTest.java @@ -394,7 +394,6 @@ private HttpStatus.Code makeGetRequest(final TestApp app, final String path) thr private static class TestApp extends Application implements AutoCloseable { private static final AtomicBoolean SHUTDOWN_CALLED = new AtomicBoolean(true); - public RestMetricsContext metricsContext; @SafeVarargs private TestApp(final Class... extensions) throws Exception { From e3685cd8b0244890f7cdab28f7ea16565729382b Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Fri, 22 May 2020 10:03:44 -0400 Subject: [PATCH 04/19] [DO NOT MERGE][WIP] KIP-606 implementation --- core/pom.xml | 3 + .../java/io/confluent/rest/Application.java | 2 +- .../rest/metrics/RestMetricsContext.java | 67 +++++++++++++------ .../io/confluent/rest/ApplicationTest.java | 12 ++-- .../rest/metrics/TestRestMetricsContext.java | 30 ++------- 5 files changed, 62 insertions(+), 52 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 91f8d75e1f..5305a8a7ca 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -80,6 +80,7 @@ org.apache.kafka kafka-clients + 2.6.0-SNAPSHOT @@ -100,12 +101,14 @@ org.apache.kafka kafka_${kafka.scala.version} + 2.6.0-SNAPSHOT test test org.apache.kafka kafka-clients + 2.6.0-SNAPSHOT test test diff --git a/core/src/main/java/io/confluent/rest/Application.java b/core/src/main/java/io/confluent/rest/Application.java index 132a09da15..b0f2b5a7e6 100644 --- a/core/src/main/java/io/confluent/rest/Application.java +++ b/core/src/main/java/io/confluent/rest/Application.java @@ -141,7 +141,7 @@ public final String getPath() { /** - * Register metadata with this Application's MetricsContext. + * Update MetricsContext labels. */ public void setupMetricsContext(RestMetricsContext context, T appConfig) {} diff --git a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java index 338ee37070..8145ed5863 100644 --- a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java +++ b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java @@ -17,62 +17,85 @@ package io.confluent.rest.metrics; import io.confluent.rest.RestConfig; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import org.apache.kafka.common.metrics.MetricsContext; +import org.apache.kafka.common.utils.AppInfoParser; public class RestMetricsContext implements MetricsContext { + /** + * MetricsContext Label's for use by Confluent's TelemetryReporter + */ public static final String METRICS_CONTEXT_PREFIX = "metrics.context."; - public static final String METRICS_RESOURCE_NAME = "resource.type"; + public static final String RESOURCE_LABEL_PREFIX = "resource."; + public static final String RESOURCE_LABEL_TYPE = RESOURCE_LABEL_PREFIX + "type"; + public static final String RESOURCE_LABEL_VERSION = RESOURCE_LABEL_PREFIX + "version"; /** * Client or Service's metadata map. */ - private final Map metadata = new HashMap<>(); + private final Map metadata; /** - * @param config RestConfig instance + * {@link io.confluent.rest.Application} {@link MetricsContext} configuration. */ public RestMetricsContext(RestConfig config) { /* Copy all configuration properties prefixed into metadata instance. */ this(config.originalsWithPrefix(METRICS_CONTEXT_PREFIX)); /* JMX_PREFIX is synonymous with MetricsContext.NAMESPACE */ - metadata.putIfAbsent(MetricsContext.NAMESPACE, + this.putNamespaceLabel(MetricsContext.NAMESPACE, config.getString(RestConfig.METRICS_JMX_PREFIX_CONFIG)); - /* - * RestApplication are most likely to be top-level compositions. - * Use JMX_PREFIX if METRICS_RESOURCE_NAME is not explicitly set. - */ - metadata.putIfAbsent(METRICS_RESOURCE_NAME, metadata.get(MetricsContext.NAMESPACE)); + /* Never overwrite preexisting resource labels */ + this.putResourceLabel(RESOURCE_LABEL_TYPE, + config.getString(RestConfig.METRICS_JMX_PREFIX_CONFIG)); + this.putResourceLabel(RESOURCE_LABEL_VERSION, + AppInfoParser.getVersion()); } public RestMetricsContext(Map config) { + this.metadata = new HashMap<>(); config.forEach((key, value) -> metadata.put(key, (String) value)); } - - public String getResourceName() { - return metadata.get(METRICS_RESOURCE_NAME); + + /** + * Sets {@link MetricsContext} namespace label. + */ + protected void putNamespaceLabel(String labelKey, String labelValue) { + /* Remove resource label if present*/ + this.metadata.put(labelKey.replace(RESOURCE_LABEL_PREFIX, ""), + labelValue); } - public String getNameSpace() { - return metadata.get(MetricsContext.NAMESPACE); + /** + * Sets {@link MetricsContext} resource label if not previously set. + * Returns null if the resource value was not previously set else + * returns the current value. + */ + protected String putResourceLabel(String resource, String value) { + return this.metadata.putIfAbsent(resource, value); } - public MetricsContext newNamespace(String namespace) { - Map child = new HashMap<>(this.metadata); - child.put(MetricsContext.NAMESPACE, namespace); + /** + * Returns {@link MetricsContext} Resource type. + */ + public String getResourceType() { + return metadata.get(RESOURCE_LABEL_TYPE); + } - return new RestMetricsContext(child); + /** + * Returns {@link MetricsContext} namespace. + */ + public String getNamespace() { + return metadata.get(MetricsContext.NAMESPACE); } /** - * Returns client's metadata map. - * - * @return metadata fields + * Returns {@link MetricsContext} as an immutable Map. */ public Map metadata() { - return metadata; + return Collections.unmodifiableMap(metadata); } } diff --git a/core/src/test/java/io/confluent/rest/ApplicationTest.java b/core/src/test/java/io/confluent/rest/ApplicationTest.java index af206ccb4a..e883681680 100644 --- a/core/src/test/java/io/confluent/rest/ApplicationTest.java +++ b/core/src/test/java/io/confluent/rest/ApplicationTest.java @@ -332,8 +332,8 @@ public void shouldShutdownProperlyEvenIfResourceExtensionThrowsOnShutdown() thro public void testDefaultMetricsContext() throws Exception { TestApp testApp = new TestApp(); - assertEquals(testApp.metricsContext.getResourceName(), RestConfig.METRICS_JMX_PREFIX_DEFAULT); - assertEquals(testApp.metricsContext.getNameSpace(), RestConfig.METRICS_JMX_PREFIX_DEFAULT); + assertEquals(testApp.metricsContext.getResourceType(), RestConfig.METRICS_JMX_PREFIX_DEFAULT); + assertEquals(testApp.metricsContext.getNamespace(), RestConfig.METRICS_JMX_PREFIX_DEFAULT); } @Test @@ -343,8 +343,8 @@ public void testMetricsContextResourceOverride() throws Exception { TestApp testApp = new TestApp(props); - assertEquals(testApp.metricsContext.getResourceName(), "FooApp"); - assertEquals(testApp.metricsContext.getNameSpace(), RestConfig.METRICS_JMX_PREFIX_DEFAULT); + assertEquals(testApp.metricsContext.getResourceType(), "FooApp"); + assertEquals(testApp.metricsContext.getNamespace(), RestConfig.METRICS_JMX_PREFIX_DEFAULT); /* Only NameSpace should be propagated to JMX */ String jmx_domain = RestConfig.METRICS_JMX_PREFIX_DEFAULT; @@ -361,8 +361,8 @@ public void testMetricsContextJMXPrefixPropagation() throws Exception { TestApp testApp = new TestApp(props); - assertEquals(testApp.metricsContext.getResourceName(), "FooApp"); - assertEquals(testApp.metricsContext.getNameSpace(), "FooApp"); + assertEquals(testApp.metricsContext.getResourceType(), "FooApp"); + assertEquals(testApp.metricsContext.getNamespace(), "FooApp"); } @Test diff --git a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java index 37c5f9f29e..1ff275c0ad 100644 --- a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java +++ b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java @@ -27,42 +27,26 @@ public class TestRestMetricsContext { - @Test - public void testMetricsContextNewNameSpace() { - Map props = new HashMap<>(); - props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX - + RestMetricsContext.METRICS_RESOURCE_NAME, "root"); - - TestRestConfig config = new TestRestConfig(props); - RestMetricsContext context = new RestMetricsContext(config); - - System.out.println(context.metadata()); - - MetricsContext context2 = context.newNamespace("kafka-consumer"); - - System.out.println(context2.metadata()); - } - @Test public void testDefaultMetricsContext() throws Exception { TestRestConfig config = new TestRestConfig(); RestMetricsContext context = new RestMetricsContext(config); - assertEquals(context.getResourceName(), "rest-utils"); - assertEquals(context.getNameSpace(), "rest-utils"); + assertEquals(context.getResourceType(), "rest-utils"); + assertEquals(context.getNamespace(), "rest-utils"); } @Test public void testMetricsContextResourceOverride() throws Exception { Map props = new HashMap<>(); props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX - + RestMetricsContext.METRICS_RESOURCE_NAME, "root"); + + RestMetricsContext.RESOURCE_LABEL_TYPE, "root"); TestRestConfig config = new TestRestConfig(props); RestMetricsContext context = new RestMetricsContext(config); - assertEquals(context.getResourceName(), "root"); - assertEquals(context.getNameSpace(), "rest-utils"); + assertEquals(context.getResourceType(), "root"); + assertEquals(context.getNamespace(), "rest-utils"); } @Test @@ -73,7 +57,7 @@ public void testMetricsContextJMXPrefixPropagation() throws Exception { TestRestConfig config = new TestRestConfig(props); RestMetricsContext context = new RestMetricsContext(config); - assertEquals(context.getResourceName(), "FooApp"); - assertEquals(context.getNameSpace(), "FooApp"); + assertEquals(context.getResourceType(), "FooApp"); + assertEquals(context.getNamespace(), "FooApp"); } } From a35dbc5302dd99c94a5791a4b69c57f646733772 Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Fri, 22 May 2020 10:20:33 -0400 Subject: [PATCH 05/19] Add a putNamespace/Resource Label tests --- .../rest/metrics/RestMetricsContext.java | 2 +- .../rest/metrics/TestRestMetricsContext.java | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java index 8145ed5863..130da57384 100644 --- a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java +++ b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java @@ -64,7 +64,7 @@ public RestMetricsContext(Map config) { * Sets {@link MetricsContext} namespace label. */ protected void putNamespaceLabel(String labelKey, String labelValue) { - /* Remove resource label if present*/ + /* Remove resource label if present */ this.metadata.put(labelKey.replace(RESOURCE_LABEL_PREFIX, ""), labelValue); } diff --git a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java index 1ff275c0ad..623afb0c92 100644 --- a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java +++ b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java @@ -60,4 +60,57 @@ public void testMetricsContextJMXPrefixPropagation() throws Exception { assertEquals(context.getResourceType(), "FooApp"); assertEquals(context.getNamespace(), "FooApp"); } + + @Test + public void testMetricsContextPutNamespaceLabelStripResourcePrefix() throws Exception { + Map props = new HashMap<>(); + props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX + + RestMetricsContext.RESOURCE_LABEL_TYPE, "root"); + + TestRestConfig config = new TestRestConfig(props); + RestMetricsContext context = new RestMetricsContext(config); + + context.putNamespaceLabel(RestMetricsContext.RESOURCE_LABEL_TYPE, + "rest-utils-resource"); + + assertEquals(context.getResourceType(), "root"); + assertEquals(context.getNamespace(), "rest-utils"); + } + + @Test + public void testMetricsContextResourceLabelSetIfAbsent() throws Exception { + Map props = new HashMap<>(); + props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX + + RestMetricsContext.RESOURCE_LABEL_TYPE, "root"); + + TestRestConfig config = new TestRestConfig(props); + RestMetricsContext context = new RestMetricsContext(config); + + context.putResourceLabel(RestMetricsContext.RESOURCE_LABEL_TYPE, + "rest-utils-resource"); + + assertEquals(context.getResourceType(), "root"); + assertEquals(context.getNamespace(), "rest-utils"); + } + + @Test + public void testMetricsContextResourceLabelNew() throws Exception { + Map props = new HashMap<>(); + props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX + + RestMetricsContext.RESOURCE_LABEL_TYPE, "root"); + + TestRestConfig config = new TestRestConfig(props); + RestMetricsContext context = new RestMetricsContext(config); + + String RESOURCE_CLUSTER_ID = + RestMetricsContext.RESOURCE_LABEL_PREFIX + "cluster.id"; + + context.putResourceLabel( + RESOURCE_CLUSTER_ID, + "rest-utils-bootstrap"); + + assertEquals(context.getResourceType(), "root"); + assertEquals(context.getNamespace(), "rest-utils"); + assertEquals(context.metadata().get(RESOURCE_CLUSTER_ID), "rest-utils-bootstrap"); + } } From 8aed8eff7f325cec18dce84c391f17db0f4c9abc Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Fri, 22 May 2020 11:31:32 -0400 Subject: [PATCH 06/19] Genericize RestMetricsContext --- .../main/java/io/confluent/rest/Application.java | 16 +++++++--------- .../rest/metrics/RestMetricsContext.java | 6 +----- .../java/io/confluent/rest/ApplicationTest.java | 4 ++-- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/io/confluent/rest/Application.java b/core/src/main/java/io/confluent/rest/Application.java index b0f2b5a7e6..f9882fd89d 100644 --- a/core/src/main/java/io/confluent/rest/Application.java +++ b/core/src/main/java/io/confluent/rest/Application.java @@ -21,10 +21,7 @@ import io.confluent.rest.metrics.RestMetricsContext; import org.apache.kafka.common.config.ConfigException; -import org.apache.kafka.common.metrics.JmxReporter; -import org.apache.kafka.common.metrics.MetricConfig; -import org.apache.kafka.common.metrics.Metrics; -import org.apache.kafka.common.metrics.MetricsReporter; +import org.apache.kafka.common.metrics.*; import org.eclipse.jetty.jaas.JAASLoginService; import org.eclipse.jetty.security.ConstraintMapping; import org.eclipse.jetty.security.ConstraintSecurityHandler; @@ -94,7 +91,7 @@ public abstract class Application { // CHECKSTYLE_RULES.ON: ClassDataAbstractionCoupling protected T config; - protected RestMetricsContext metricsContext; + protected RestMetricsContext metricsContext; private final String path; protected ApplicationServer server; @@ -114,8 +111,7 @@ public Application(T config, String path) { this.config = config; this.path = Objects.requireNonNull(path);; - this.metricsContext = new RestMetricsContext(config); - this.setupMetricsContext(metricsContext, config); + setupMetricsContext(config); MetricConfig metricConfig = new MetricConfig() .samples(config.getInt(RestConfig.METRICS_NUM_SAMPLES_CONFIG)) @@ -141,9 +137,11 @@ public final String getPath() { /** - * Update MetricsContext labels. + * Set MetricsContext labels. */ - public void setupMetricsContext(RestMetricsContext context, T appConfig) {} + protected void setupMetricsContext(T appConfig) { + this.metricsContext = new RestMetricsContext<>(appConfig); + } /** * Returns {@link Metrics} object diff --git a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java index 130da57384..68e65db113 100644 --- a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java +++ b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java @@ -21,16 +21,14 @@ import java.util.HashMap; import java.util.Map; import org.apache.kafka.common.metrics.MetricsContext; -import org.apache.kafka.common.utils.AppInfoParser; -public class RestMetricsContext implements MetricsContext { +public class RestMetricsContext implements MetricsContext { /** * MetricsContext Label's for use by Confluent's TelemetryReporter */ public static final String METRICS_CONTEXT_PREFIX = "metrics.context."; public static final String RESOURCE_LABEL_PREFIX = "resource."; public static final String RESOURCE_LABEL_TYPE = RESOURCE_LABEL_PREFIX + "type"; - public static final String RESOURCE_LABEL_VERSION = RESOURCE_LABEL_PREFIX + "version"; /** * Client or Service's metadata map. @@ -51,8 +49,6 @@ public RestMetricsContext(RestConfig config) { /* Never overwrite preexisting resource labels */ this.putResourceLabel(RESOURCE_LABEL_TYPE, config.getString(RestConfig.METRICS_JMX_PREFIX_CONFIG)); - this.putResourceLabel(RESOURCE_LABEL_VERSION, - AppInfoParser.getVersion()); } public RestMetricsContext(Map config) { diff --git a/core/src/test/java/io/confluent/rest/ApplicationTest.java b/core/src/test/java/io/confluent/rest/ApplicationTest.java index e883681680..705f860638 100644 --- a/core/src/test/java/io/confluent/rest/ApplicationTest.java +++ b/core/src/test/java/io/confluent/rest/ApplicationTest.java @@ -407,8 +407,8 @@ public TestApp(final Map config) { } @Override - public void setupMetricsContext(RestMetricsContext metricsContext, TestRestConfig appConfig) { - this.metricsContext = metricsContext; + protected void setupMetricsContext(TestRestConfig appConfig) { + this.metricsContext = new RestMetricsContext<>(appConfig); } @Override From cbb443d695a1d4904035bfa1b3ef35c42819d5ea Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Fri, 22 May 2020 11:33:26 -0400 Subject: [PATCH 07/19] Fix checkstyle --- core/src/main/java/io/confluent/rest/Application.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/io/confluent/rest/Application.java b/core/src/main/java/io/confluent/rest/Application.java index f9882fd89d..5828c43a50 100644 --- a/core/src/main/java/io/confluent/rest/Application.java +++ b/core/src/main/java/io/confluent/rest/Application.java @@ -21,7 +21,10 @@ import io.confluent.rest.metrics.RestMetricsContext; import org.apache.kafka.common.config.ConfigException; -import org.apache.kafka.common.metrics.*; +import org.apache.kafka.common.metrics.JmxReporter; +import org.apache.kafka.common.metrics.MetricConfig; +import org.apache.kafka.common.metrics.Metrics; +import org.apache.kafka.common.metrics.MetricsReporter; import org.eclipse.jetty.jaas.JAASLoginService; import org.eclipse.jetty.security.ConstraintMapping; import org.eclipse.jetty.security.ConstraintSecurityHandler; From cd76bc39e55f14ab94670664f5c03a03764f069d Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Fri, 22 May 2020 12:37:33 -0400 Subject: [PATCH 08/19] Use Object.toString to avoid ClassCastException when creating MetricsContext --- .../rest/metrics/RestMetricsContext.java | 4 ++-- .../rest/metrics/TestRestMetricsContext.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java index 68e65db113..dd9c81bbf2 100644 --- a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java +++ b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java @@ -51,9 +51,9 @@ public RestMetricsContext(RestConfig config) { config.getString(RestConfig.METRICS_JMX_PREFIX_CONFIG)); } - public RestMetricsContext(Map config) { + private RestMetricsContext(Map config) { this.metadata = new HashMap<>(); - config.forEach((key, value) -> metadata.put(key, (String) value)); + config.forEach((key, value) -> metadata.put(key, value.toString())); } /** diff --git a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java index 623afb0c92..1796557547 100644 --- a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java +++ b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java @@ -113,4 +113,21 @@ public void testMetricsContextResourceLabelNew() throws Exception { assertEquals(context.getNamespace(), "rest-utils"); assertEquals(context.metadata().get(RESOURCE_CLUSTER_ID), "rest-utils-bootstrap"); } + + @Test + public void testMetricsContextResourceNonStringValue() throws Exception { + Map props = new HashMap<>(); + props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX + + RestMetricsContext.RESOURCE_LABEL_TYPE, "root"); + props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX + "notString", + this.getClass()); + + TestRestConfig config = new TestRestConfig(props); + RestMetricsContext context = new RestMetricsContext(config); + + + assertEquals(context.getResourceType(), "root"); + assertEquals(context.getNamespace(), "rest-utils"); + assertEquals(context.metadata().get("notString"), this.getClass().toString()); + } } From a72ac8607beb1c86b08fd8e526214cef49d1f3c4 Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Fri, 22 May 2020 12:43:59 -0400 Subject: [PATCH 09/19] Disambiguate non resource label setter --- .../java/io/confluent/rest/metrics/RestMetricsContext.java | 6 +++--- .../io/confluent/rest/metrics/TestRestMetricsContext.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java index dd9c81bbf2..55522cb141 100644 --- a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java +++ b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java @@ -43,7 +43,7 @@ public RestMetricsContext(RestConfig config) { this(config.originalsWithPrefix(METRICS_CONTEXT_PREFIX)); /* JMX_PREFIX is synonymous with MetricsContext.NAMESPACE */ - this.putNamespaceLabel(MetricsContext.NAMESPACE, + this.putLabel(MetricsContext.NAMESPACE, config.getString(RestConfig.METRICS_JMX_PREFIX_CONFIG)); /* Never overwrite preexisting resource labels */ @@ -57,9 +57,9 @@ private RestMetricsContext(Map config) { } /** - * Sets {@link MetricsContext} namespace label. + * Sets a {@link MetricsContext} key, value pair. */ - protected void putNamespaceLabel(String labelKey, String labelValue) { + protected void putLabel(String labelKey, String labelValue) { /* Remove resource label if present */ this.metadata.put(labelKey.replace(RESOURCE_LABEL_PREFIX, ""), labelValue); diff --git a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java index 1796557547..87d8ffc6ee 100644 --- a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java +++ b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java @@ -22,7 +22,7 @@ import io.confluent.rest.TestRestConfig; import java.util.HashMap; import java.util.Map; -import org.apache.kafka.common.metrics.MetricsContext; + import org.junit.Test; public class TestRestMetricsContext { @@ -70,7 +70,7 @@ public void testMetricsContextPutNamespaceLabelStripResourcePrefix() throws Exce TestRestConfig config = new TestRestConfig(props); RestMetricsContext context = new RestMetricsContext(config); - context.putNamespaceLabel(RestMetricsContext.RESOURCE_LABEL_TYPE, + context.putLabel(RestMetricsContext.RESOURCE_LABEL_TYPE, "rest-utils-resource"); assertEquals(context.getResourceType(), "root"); From 6db358e40768f882fa275530d3a14b6f645aed71 Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Fri, 22 May 2020 14:32:16 -0400 Subject: [PATCH 10/19] No need for Generic RestMetricsContext --- core/src/main/java/io/confluent/rest/Application.java | 4 ++-- .../java/io/confluent/rest/metrics/RestMetricsContext.java | 4 ++-- core/src/test/java/io/confluent/rest/ApplicationTest.java | 5 ----- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/io/confluent/rest/Application.java b/core/src/main/java/io/confluent/rest/Application.java index 5828c43a50..0ab15e772a 100644 --- a/core/src/main/java/io/confluent/rest/Application.java +++ b/core/src/main/java/io/confluent/rest/Application.java @@ -94,7 +94,7 @@ public abstract class Application { // CHECKSTYLE_RULES.ON: ClassDataAbstractionCoupling protected T config; - protected RestMetricsContext metricsContext; + protected RestMetricsContext metricsContext; private final String path; protected ApplicationServer server; @@ -143,7 +143,7 @@ public final String getPath() { * Set MetricsContext labels. */ protected void setupMetricsContext(T appConfig) { - this.metricsContext = new RestMetricsContext<>(appConfig); + this.metricsContext = new RestMetricsContext(appConfig); } /** diff --git a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java index 55522cb141..22a4955b45 100644 --- a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java +++ b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java @@ -22,7 +22,7 @@ import java.util.Map; import org.apache.kafka.common.metrics.MetricsContext; -public class RestMetricsContext implements MetricsContext { +public class RestMetricsContext implements MetricsContext { /** * MetricsContext Label's for use by Confluent's TelemetryReporter */ @@ -52,7 +52,7 @@ public RestMetricsContext(RestConfig config) { } private RestMetricsContext(Map config) { - this.metadata = new HashMap<>(); + metadata = new HashMap<>(); config.forEach((key, value) -> metadata.put(key, value.toString())); } diff --git a/core/src/test/java/io/confluent/rest/ApplicationTest.java b/core/src/test/java/io/confluent/rest/ApplicationTest.java index 705f860638..5e5c18a8d3 100644 --- a/core/src/test/java/io/confluent/rest/ApplicationTest.java +++ b/core/src/test/java/io/confluent/rest/ApplicationTest.java @@ -406,11 +406,6 @@ public TestApp(final Map config) { super(createConfig(config)); } - @Override - protected void setupMetricsContext(TestRestConfig appConfig) { - this.metricsContext = new RestMetricsContext<>(appConfig); - } - @Override public void setupResources(final Configurable config, final TestRestConfig appConfig) { } From d90f155ef35b63bbcc0748350b2aea402d8e8ca4 Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Tue, 26 May 2020 05:59:53 -0400 Subject: [PATCH 11/19] Move metrics config instantiation into RestConfig --- core/src/main/java/io/confluent/rest/Application.java | 5 ++--- core/src/main/java/io/confluent/rest/RestConfig.java | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/io/confluent/rest/Application.java b/core/src/main/java/io/confluent/rest/Application.java index 0ab15e772a..2903b3834b 100644 --- a/core/src/main/java/io/confluent/rest/Application.java +++ b/core/src/main/java/io/confluent/rest/Application.java @@ -94,7 +94,7 @@ public abstract class Application { // CHECKSTYLE_RULES.ON: ClassDataAbstractionCoupling protected T config; - protected RestMetricsContext metricsContext; + protected final RestMetricsContext metricsContext; private final String path; protected ApplicationServer server; @@ -114,8 +114,6 @@ public Application(T config, String path) { this.config = config; this.path = Objects.requireNonNull(path);; - setupMetricsContext(config); - MetricConfig metricConfig = new MetricConfig() .samples(config.getInt(RestConfig.METRICS_NUM_SAMPLES_CONFIG)) .timeWindow(config.getLong(RestConfig.METRICS_SAMPLE_WINDOW_MS_CONFIG), @@ -125,6 +123,7 @@ public Application(T config, String path) { MetricsReporter.class); reporters.add(new JmxReporter()); + metricsContext = config.getMetricsContext(); this.metrics = new Metrics(metricConfig, reporters, config.getTime(), metricsContext); this.getMetricsTags().putAll( parseListToMap(config.getList(RestConfig.METRICS_TAGS_CONFIG))); diff --git a/core/src/main/java/io/confluent/rest/RestConfig.java b/core/src/main/java/io/confluent/rest/RestConfig.java index 761fcbe4c0..7c1e055897 100644 --- a/core/src/main/java/io/confluent/rest/RestConfig.java +++ b/core/src/main/java/io/confluent/rest/RestConfig.java @@ -16,6 +16,7 @@ package io.confluent.rest; +import io.confluent.rest.metrics.RestMetricsContext; import org.apache.kafka.common.config.AbstractConfig; import org.apache.kafka.common.config.ConfigException; import org.apache.kafka.common.config.ConfigDef; @@ -677,6 +678,9 @@ public Time getTime() { return defaultTime; } + public RestMetricsContext getMetricsContext(){ + return new RestMetricsContext(this); + } public static void validateHttpResponseHeaderConfig(String config) { try { // validate format From 4985c8d64259e6ec2f5cd08589304b1a4ab5ee08 Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Thu, 28 May 2020 07:01:46 -0400 Subject: [PATCH 12/19] Remove telemetry reporter labels from base context, update base context to reflect merged changes to AK --- .../java/io/confluent/rest/RestConfig.java | 19 ++- .../rest/metrics/RestMetricsContext.java | 59 +++---- .../io/confluent/rest/ApplicationTest.java | 81 +++++----- .../io/confluent/rest/TestRestConfig.java | 7 + .../rest/metrics/RestMetricsContextTest.java | 130 ++++++++++++++++ .../rest/metrics/TestRestMetricsContext.java | 144 +++++------------- 6 files changed, 239 insertions(+), 201 deletions(-) create mode 100644 core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java diff --git a/core/src/main/java/io/confluent/rest/RestConfig.java b/core/src/main/java/io/confluent/rest/RestConfig.java index 7c1e055897..b13e358c8d 100644 --- a/core/src/main/java/io/confluent/rest/RestConfig.java +++ b/core/src/main/java/io/confluent/rest/RestConfig.java @@ -16,7 +16,13 @@ package io.confluent.rest; +import io.confluent.rest.extension.ResourceExtension; import io.confluent.rest.metrics.RestMetricsContext; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; import org.apache.kafka.common.config.AbstractConfig; import org.apache.kafka.common.config.ConfigException; import org.apache.kafka.common.config.ConfigDef; @@ -24,14 +30,6 @@ import org.apache.kafka.common.config.ConfigDef.Importance; import org.apache.kafka.common.utils.Time; -import io.confluent.rest.extension.ResourceExtension; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - public class RestConfig extends AbstractConfig { public static final String DEBUG_CONFIG = "debug"; protected static final String DEBUG_CONFIG_DOC = @@ -671,16 +669,17 @@ public RestConfig(ConfigDef definition, Map originals) { } public RestConfig(ConfigDef definition) { - super(definition, new TreeMap<>()); + this(definition, new TreeMap<>()); } public Time getTime() { return defaultTime; } - public RestMetricsContext getMetricsContext(){ + public RestMetricsContext getMetricsContext() { return new RestMetricsContext(this); } + public static void validateHttpResponseHeaderConfig(String config) { try { // validate format diff --git a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java index 22a4955b45..4d7590b5d0 100644 --- a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java +++ b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2014 Confluent Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,6 +16,8 @@ package io.confluent.rest.metrics; +import static org.apache.kafka.clients.CommonClientConfigs.METRICS_CONTEXT_PREFIX; + import io.confluent.rest.RestConfig; import java.util.Collections; import java.util.HashMap; @@ -23,17 +25,10 @@ import org.apache.kafka.common.metrics.MetricsContext; public class RestMetricsContext implements MetricsContext { - /** - * MetricsContext Label's for use by Confluent's TelemetryReporter - */ - public static final String METRICS_CONTEXT_PREFIX = "metrics.context."; - public static final String RESOURCE_LABEL_PREFIX = "resource."; - public static final String RESOURCE_LABEL_TYPE = RESOURCE_LABEL_PREFIX + "type"; - /** * Client or Service's metadata map. */ - private final Map metadata; + protected final Map contextLabels; /** * {@link io.confluent.rest.Application} {@link MetricsContext} configuration. @@ -43,55 +38,35 @@ public RestMetricsContext(RestConfig config) { this(config.originalsWithPrefix(METRICS_CONTEXT_PREFIX)); /* JMX_PREFIX is synonymous with MetricsContext.NAMESPACE */ - this.putLabel(MetricsContext.NAMESPACE, - config.getString(RestConfig.METRICS_JMX_PREFIX_CONFIG)); - - /* Never overwrite preexisting resource labels */ - this.putResourceLabel(RESOURCE_LABEL_TYPE, + this.setLabel(MetricsContext.NAMESPACE, config.getString(RestConfig.METRICS_JMX_PREFIX_CONFIG)); } private RestMetricsContext(Map config) { - metadata = new HashMap<>(); - config.forEach((key, value) -> metadata.put(key, value.toString())); + contextLabels = new HashMap<>(); + config.forEach((key, value) -> contextLabels.put(key, value.toString())); } /** * Sets a {@link MetricsContext} key, value pair. */ - protected void putLabel(String labelKey, String labelValue) { - /* Remove resource label if present */ - this.metadata.put(labelKey.replace(RESOURCE_LABEL_PREFIX, ""), - labelValue); - } - - /** - * Sets {@link MetricsContext} resource label if not previously set. - * Returns null if the resource value was not previously set else - * returns the current value. - */ - protected String putResourceLabel(String resource, String value) { - return this.metadata.putIfAbsent(resource, value); - } - - /** - * Returns {@link MetricsContext} Resource type. - */ - public String getResourceType() { - return metadata.get(RESOURCE_LABEL_TYPE); + protected void setLabel(String labelKey, String labelValue) { + this.contextLabels.put(labelKey, labelValue); } /** - * Returns {@link MetricsContext} namespace. + * Returns the value associated with the specified label else + * {@code null}. */ - public String getNamespace() { - return metadata.get(MetricsContext.NAMESPACE); + public String getLabel(String label) { + return contextLabels.get(label); } /** - * Returns {@link MetricsContext} as an immutable Map. + * Returns {@link MetricsContext} as an unmodifiable Map. */ - public Map metadata() { - return Collections.unmodifiableMap(metadata); + @Override + public Map contextLabels() { + return Collections.unmodifiableMap(contextLabels); } } diff --git a/core/src/test/java/io/confluent/rest/ApplicationTest.java b/core/src/test/java/io/confluent/rest/ApplicationTest.java index 5e5c18a8d3..46a782c846 100644 --- a/core/src/test/java/io/confluent/rest/ApplicationTest.java +++ b/core/src/test/java/io/confluent/rest/ApplicationTest.java @@ -16,30 +16,19 @@ package io.confluent.rest; -import com.google.common.collect.ImmutableMap; - -import io.confluent.rest.metrics.RestMetricsContext; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.kafka.common.config.ConfigException; -import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.http.HttpStatus.Code; -import org.eclipse.jetty.jaas.JAASLoginService; -import org.eclipse.jetty.security.authentication.LoginAuthenticator; -import org.eclipse.jetty.security.ConstraintMapping; -import org.eclipse.jetty.security.ConstraintSecurityHandler; -import org.eclipse.jetty.security.LoginService; -import org.eclipse.jetty.security.authentication.BasicAuthenticator; -import org.eclipse.jetty.server.ServerConnector; -import org.eclipse.jetty.util.security.Constraint; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import static io.confluent.rest.metrics.TestRestMetricsContext.RESOURCE_LABEL_TYPE; +import static org.apache.kafka.common.metrics.MetricsContext.NAMESPACE; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import com.google.common.collect.ImmutableMap; +import io.confluent.rest.extension.ResourceExtension; import java.io.IOException; import java.lang.management.ManagementFactory; import java.net.URI; @@ -53,7 +42,6 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; - import javax.management.MBeanServer; import javax.management.ObjectName; import javax.ws.rs.GET; @@ -61,17 +49,26 @@ import javax.ws.rs.Produces; import javax.ws.rs.core.Configurable; import javax.ws.rs.core.MediaType; - -import io.confluent.rest.extension.ResourceExtension; - -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.kafka.common.config.ConfigException; +import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.http.HttpStatus.Code; +import org.eclipse.jetty.jaas.JAASLoginService; +import org.eclipse.jetty.security.authentication.LoginAuthenticator; +import org.eclipse.jetty.security.ConstraintMapping; +import org.eclipse.jetty.security.ConstraintSecurityHandler; +import org.eclipse.jetty.security.LoginService; +import org.eclipse.jetty.security.authentication.BasicAuthenticator; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.util.security.Constraint; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; public class ApplicationTest { @@ -332,8 +329,10 @@ public void shouldShutdownProperlyEvenIfResourceExtensionThrowsOnShutdown() thro public void testDefaultMetricsContext() throws Exception { TestApp testApp = new TestApp(); - assertEquals(testApp.metricsContext.getResourceType(), RestConfig.METRICS_JMX_PREFIX_DEFAULT); - assertEquals(testApp.metricsContext.getNamespace(), RestConfig.METRICS_JMX_PREFIX_DEFAULT); + assertEquals(testApp.metricsContext.getLabel(RESOURCE_LABEL_TYPE), + RestConfig.METRICS_JMX_PREFIX_DEFAULT); + assertEquals(testApp.metricsContext.getLabel(NAMESPACE), + RestConfig.METRICS_JMX_PREFIX_DEFAULT); } @Test @@ -343,8 +342,8 @@ public void testMetricsContextResourceOverride() throws Exception { TestApp testApp = new TestApp(props); - assertEquals(testApp.metricsContext.getResourceType(), "FooApp"); - assertEquals(testApp.metricsContext.getNamespace(), RestConfig.METRICS_JMX_PREFIX_DEFAULT); + assertEquals(testApp.metricsContext.getLabel(RESOURCE_LABEL_TYPE), "FooApp"); + assertEquals(testApp.metricsContext.getLabel(NAMESPACE), RestConfig.METRICS_JMX_PREFIX_DEFAULT); /* Only NameSpace should be propagated to JMX */ String jmx_domain = RestConfig.METRICS_JMX_PREFIX_DEFAULT; @@ -361,8 +360,8 @@ public void testMetricsContextJMXPrefixPropagation() throws Exception { TestApp testApp = new TestApp(props); - assertEquals(testApp.metricsContext.getResourceType(), "FooApp"); - assertEquals(testApp.metricsContext.getNamespace(), "FooApp"); + assertEquals(testApp.metricsContext.getLabel(RESOURCE_LABEL_TYPE), "FooApp"); + assertEquals(testApp.metricsContext.getLabel(NAMESPACE), "FooApp"); } @Test diff --git a/core/src/test/java/io/confluent/rest/TestRestConfig.java b/core/src/test/java/io/confluent/rest/TestRestConfig.java index 07a3ec87ee..44962ccc65 100644 --- a/core/src/test/java/io/confluent/rest/TestRestConfig.java +++ b/core/src/test/java/io/confluent/rest/TestRestConfig.java @@ -16,6 +16,8 @@ package io.confluent.rest; +import io.confluent.rest.metrics.RestMetricsContext; +import io.confluent.rest.metrics.TestRestMetricsContext; import org.apache.kafka.common.config.ConfigDef; import java.util.Map; @@ -36,4 +38,9 @@ public TestRestConfig() { public TestRestConfig(Map originals) { super(config, originals); } + + @Override + public RestMetricsContext getMetricsContext() { + return new TestRestMetricsContext(this); + } } diff --git a/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java b/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java new file mode 100644 index 0000000000..fef0643fe5 --- /dev/null +++ b/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java @@ -0,0 +1,130 @@ +/** + * Copyright 2014 Confluent 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 + * + * http://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.confluent.rest.metrics; + +import static io.confluent.rest.metrics.TestRestMetricsContext.RESOURCE_LABEL_TYPE; +import static org.apache.kafka.clients.CommonClientConfigs.METRICS_CONTEXT_PREFIX; +import static org.apache.kafka.common.metrics.MetricsContext.NAMESPACE; +import static org.junit.Assert.assertEquals; + +import io.confluent.rest.RestConfig; +import io.confluent.rest.TestRestConfig; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +public class RestMetricsContextTest { + + @Test + public void testDefaultMetricsContext() throws Exception { + TestRestConfig config = new TestRestConfig(); + TestRestMetricsContext context = new TestRestMetricsContext(config); + + assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "rest-utils"); + assertEquals(context.getLabel(NAMESPACE), "rest-utils"); + } + + @Test + public void testMetricsContextResourceOverride() throws Exception { + Map props = new HashMap<>(); + props.put(METRICS_CONTEXT_PREFIX + + RESOURCE_LABEL_TYPE, "root"); + + TestRestConfig config = new TestRestConfig(props); + TestRestMetricsContext context = new TestRestMetricsContext(config); + + assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "root"); + assertEquals(context.getLabel(NAMESPACE), "rest-utils"); + } + + @Test + public void testMetricsContextJMXPrefixPropagation() throws Exception { + Map props = new HashMap<>(); + props.put(RestConfig.METRICS_JMX_PREFIX_CONFIG, "FooApp"); + + TestRestConfig config = new TestRestConfig(props); + TestRestMetricsContext context = new TestRestMetricsContext(config); + + assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "FooApp"); + assertEquals(context.getLabel(NAMESPACE), "FooApp"); + } + + @Test + public void testMetricsContextPutNamespaceLabelStripResourcePrefix() throws Exception { + Map props = new HashMap<>(); + + TestRestConfig config = new TestRestConfig(props); + TestRestMetricsContext context = new TestRestMetricsContext(config); + + assertEquals("rest-utils", context.getLabel(NAMESPACE)); + } + + @Test + public void testMetricsContextResourceLabelSetIfAbsent() throws Exception { + Map props = new HashMap<>(); + props.put(METRICS_CONTEXT_PREFIX + + RESOURCE_LABEL_TYPE, "root"); + + TestRestConfig config = new TestRestConfig(props); + TestRestMetricsContext context = new TestRestMetricsContext(config); + + context.setResourceLabel(RESOURCE_LABEL_TYPE, + "rest-utils-resource"); + + assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "root"); + assertEquals(context.getLabel(NAMESPACE), "rest-utils"); + } + + @Test + public void testMetricsContextResourceLabelNew() throws Exception { + Map props = new HashMap<>(); + props.put(METRICS_CONTEXT_PREFIX + + RESOURCE_LABEL_TYPE, "root"); + + TestRestConfig config = new TestRestConfig(props); + TestRestMetricsContext context = new TestRestMetricsContext(config); + + String RESOURCE_CLUSTER_ID = + TestRestMetricsContext.RESOURCE_LABEL_PREFIX + "cluster.id"; + + context.setResourceLabel( + RESOURCE_CLUSTER_ID, + "rest-utils-bootstrap"); + + assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "root"); + assertEquals(context.getLabel(NAMESPACE), "rest-utils"); + assertEquals(context.getLabel(RESOURCE_CLUSTER_ID), "rest-utils-bootstrap"); + } + + @Test + public void testMetricsContextResourceNonStringValue() throws Exception { + Map props = new HashMap<>(); + props.put(METRICS_CONTEXT_PREFIX + + RESOURCE_LABEL_TYPE, "root"); + props.put(METRICS_CONTEXT_PREFIX + "notString", + this.getClass()); + + TestRestConfig config = new TestRestConfig(props); + TestRestMetricsContext context = new TestRestMetricsContext(config); + + + assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "root"); + assertEquals(context.getLabel(NAMESPACE), "rest-utils"); + assertEquals(context.getLabel("notString"), this.getClass().toString()); + } +} diff --git a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java index 87d8ffc6ee..39f921fadd 100644 --- a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java +++ b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java @@ -1,5 +1,5 @@ -/** - * Copyright 2014 Confluent Inc. +/* + * Copyright 2020 Confluent Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,118 +16,46 @@ package io.confluent.rest.metrics; -import static org.junit.Assert.assertEquals; - -import io.confluent.rest.RestConfig; import io.confluent.rest.TestRestConfig; -import java.util.HashMap; -import java.util.Map; - -import org.junit.Test; - -public class TestRestMetricsContext { - - @Test - public void testDefaultMetricsContext() throws Exception { - TestRestConfig config = new TestRestConfig(); - RestMetricsContext context = new RestMetricsContext(config); - - assertEquals(context.getResourceType(), "rest-utils"); - assertEquals(context.getNamespace(), "rest-utils"); - } - - @Test - public void testMetricsContextResourceOverride() throws Exception { - Map props = new HashMap<>(); - props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX - + RestMetricsContext.RESOURCE_LABEL_TYPE, "root"); - - TestRestConfig config = new TestRestConfig(props); - RestMetricsContext context = new RestMetricsContext(config); - - assertEquals(context.getResourceType(), "root"); - assertEquals(context.getNamespace(), "rest-utils"); - } - - @Test - public void testMetricsContextJMXPrefixPropagation() throws Exception { - Map props = new HashMap<>(); - props.put(RestConfig.METRICS_JMX_PREFIX_CONFIG, "FooApp"); - - TestRestConfig config = new TestRestConfig(props); - RestMetricsContext context = new RestMetricsContext(config); - - assertEquals(context.getResourceType(), "FooApp"); - assertEquals(context.getNamespace(), "FooApp"); - } - - @Test - public void testMetricsContextPutNamespaceLabelStripResourcePrefix() throws Exception { - Map props = new HashMap<>(); - props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX - + RestMetricsContext.RESOURCE_LABEL_TYPE, "root"); - - TestRestConfig config = new TestRestConfig(props); - RestMetricsContext context = new RestMetricsContext(config); - - context.putLabel(RestMetricsContext.RESOURCE_LABEL_TYPE, - "rest-utils-resource"); - - assertEquals(context.getResourceType(), "root"); - assertEquals(context.getNamespace(), "rest-utils"); +import org.apache.kafka.common.metrics.MetricsContext; + +public class TestRestMetricsContext extends RestMetricsContext { + /** + * MetricsContext Label's for use by Confluent's TelemetryReporter + */ + public static final String RESOURCE_LABEL_PREFIX = "resource."; + public static final String RESOURCE_LABEL_TYPE = RESOURCE_LABEL_PREFIX + "type"; + public static final String RESOURCE_LABEL_COMMIT_ID = RESOURCE_LABEL_PREFIX + "commit.id"; + + /** + * {@link io.confluent.rest.Application} {@link MetricsContext} configuration. + */ + public TestRestMetricsContext(TestRestConfig config) { + /* Copy all configuration properties prefixed into metadata instance. */ + super(config); + + /* Never overwrite preexisting resource labels */ + this.setResourceLabel(RESOURCE_LABEL_TYPE, + config.getString(TestRestConfig.METRICS_JMX_PREFIX_CONFIG)); } - @Test - public void testMetricsContextResourceLabelSetIfAbsent() throws Exception { - Map props = new HashMap<>(); - props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX - + RestMetricsContext.RESOURCE_LABEL_TYPE, "root"); + /** + * Sets a {@link MetricsContext} key, value pair. + */ + @Override + protected void setLabel(String labelKey, String labelValue) { + /* Remove resource label if present */ + if (labelKey.startsWith(RESOURCE_LABEL_PREFIX)) + setResourceLabel(labelKey, labelValue); - TestRestConfig config = new TestRestConfig(props); - RestMetricsContext context = new RestMetricsContext(config); - - context.putResourceLabel(RestMetricsContext.RESOURCE_LABEL_TYPE, - "rest-utils-resource"); - - assertEquals(context.getResourceType(), "root"); - assertEquals(context.getNamespace(), "rest-utils"); + super.setLabel(labelKey, labelValue); } - @Test - public void testMetricsContextResourceLabelNew() throws Exception { - Map props = new HashMap<>(); - props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX - + RestMetricsContext.RESOURCE_LABEL_TYPE, "root"); - - TestRestConfig config = new TestRestConfig(props); - RestMetricsContext context = new RestMetricsContext(config); - - String RESOURCE_CLUSTER_ID = - RestMetricsContext.RESOURCE_LABEL_PREFIX + "cluster.id"; - - context.putResourceLabel( - RESOURCE_CLUSTER_ID, - "rest-utils-bootstrap"); - - assertEquals(context.getResourceType(), "root"); - assertEquals(context.getNamespace(), "rest-utils"); - assertEquals(context.metadata().get(RESOURCE_CLUSTER_ID), "rest-utils-bootstrap"); + /** + * Sets {@link MetricsContext} resource label if not previously set. + */ + protected void setResourceLabel(String resource, String value) { + contextLabels.putIfAbsent(resource, value); } - @Test - public void testMetricsContextResourceNonStringValue() throws Exception { - Map props = new HashMap<>(); - props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX - + RestMetricsContext.RESOURCE_LABEL_TYPE, "root"); - props.put(RestMetricsContext.METRICS_CONTEXT_PREFIX + "notString", - this.getClass()); - - TestRestConfig config = new TestRestConfig(props); - RestMetricsContext context = new RestMetricsContext(config); - - - assertEquals(context.getResourceType(), "root"); - assertEquals(context.getNamespace(), "rest-utils"); - assertEquals(context.metadata().get("notString"), this.getClass().toString()); - } } From 5a7a13321447a9148a986e2ac0812fdab6a9493f Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Thu, 28 May 2020 07:03:26 -0400 Subject: [PATCH 13/19] Remove pom override --- core/pom.xml | 133 --------------------------------------------------- 1 file changed, 133 deletions(-) delete mode 100644 core/pom.xml diff --git a/core/pom.xml b/core/pom.xml deleted file mode 100644 index 5305a8a7ca..0000000000 --- a/core/pom.xml +++ /dev/null @@ -1,133 +0,0 @@ - - - 4.0.0 - - - io.confluent - rest-utils-parent - 6.0.0-SNAPSHOT - - - rest-utils - jar - rest-utils - - - - org.eclipse.jetty.websocket - javax-websocket-server-impl - - - org.glassfish.jersey.containers - jersey-container-servlet - - - org.glassfish.jersey.inject - jersey-hk2 - - - - javax.xml.bind - jaxb-api - - - - javax.activation - activation - - - org.glassfish.jersey.ext - jersey-bean-validation - - - org.eclipse.jetty - jetty-jmx - - - org.eclipse.jetty - jetty-server - - - org.eclipse.jetty - jetty-servlet - - - org.eclipse.jetty - jetty-servlets - - - org.eclipse.jetty - jetty-jaas - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.jaxrs - jackson-jaxrs-json-provider - - - com.fasterxml.jackson.jaxrs - jackson-jaxrs-base - - - com.fasterxml.jackson.core - jackson-annotations - - - org.apache.kafka - kafka-clients - 2.6.0-SNAPSHOT - - - - junit - junit - test - - - org.bouncycastle - bcpkix-jdk15on - test - - - org.apache.httpcomponents - httpclient - test - - - org.apache.kafka - kafka_${kafka.scala.version} - 2.6.0-SNAPSHOT - test - test - - - org.apache.kafka - kafka-clients - 2.6.0-SNAPSHOT - test - test - - - org.asynchttpclient - async-http-client - ${asynchttpclient.version} - test - - - org.slf4j - slf4j-log4j12 - test - - - com.google.guava - guava - ${guava.version} - test - - - From 6b5b5600769fcfe0d5492480dd50a1093b157c1b Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Thu, 28 May 2020 07:34:09 -0400 Subject: [PATCH 14/19] Add unmodified POM back --- core/pom.xml | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 core/pom.xml diff --git a/core/pom.xml b/core/pom.xml new file mode 100644 index 0000000000..91f8d75e1f --- /dev/null +++ b/core/pom.xml @@ -0,0 +1,130 @@ + + + 4.0.0 + + + io.confluent + rest-utils-parent + 6.0.0-SNAPSHOT + + + rest-utils + jar + rest-utils + + + + org.eclipse.jetty.websocket + javax-websocket-server-impl + + + org.glassfish.jersey.containers + jersey-container-servlet + + + org.glassfish.jersey.inject + jersey-hk2 + + + + javax.xml.bind + jaxb-api + + + + javax.activation + activation + + + org.glassfish.jersey.ext + jersey-bean-validation + + + org.eclipse.jetty + jetty-jmx + + + org.eclipse.jetty + jetty-server + + + org.eclipse.jetty + jetty-servlet + + + org.eclipse.jetty + jetty-servlets + + + org.eclipse.jetty + jetty-jaas + + + com.fasterxml.jackson.core + jackson-databind + + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-json-provider + + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-base + + + com.fasterxml.jackson.core + jackson-annotations + + + org.apache.kafka + kafka-clients + + + + junit + junit + test + + + org.bouncycastle + bcpkix-jdk15on + test + + + org.apache.httpcomponents + httpclient + test + + + org.apache.kafka + kafka_${kafka.scala.version} + test + test + + + org.apache.kafka + kafka-clients + test + test + + + org.asynchttpclient + async-http-client + ${asynchttpclient.version} + test + + + org.slf4j + slf4j-log4j12 + test + + + com.google.guava + guava + ${guava.version} + test + + + From 688ac6d23015698fd566b61be9127d480d6e9232 Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Thu, 28 May 2020 09:10:59 -0400 Subject: [PATCH 15/19] Update RestMetricsContext to adhere more closely to AK metrics context --- .../java/io/confluent/rest/RestConfig.java | 10 +++++++- .../rest/metrics/RestMetricsContext.java | 23 ++++++------------- .../io/confluent/rest/TestRestConfig.java | 9 +++++--- .../rest/metrics/RestMetricsContextTest.java | 16 ++++++------- .../rest/metrics/TestRestMetricsContext.java | 15 ++++-------- 5 files changed, 34 insertions(+), 39 deletions(-) diff --git a/core/src/main/java/io/confluent/rest/RestConfig.java b/core/src/main/java/io/confluent/rest/RestConfig.java index b13e358c8d..6d5ceefe02 100644 --- a/core/src/main/java/io/confluent/rest/RestConfig.java +++ b/core/src/main/java/io/confluent/rest/RestConfig.java @@ -30,7 +30,12 @@ import org.apache.kafka.common.config.ConfigDef.Importance; import org.apache.kafka.common.utils.Time; +import static org.apache.kafka.clients.CommonClientConfigs.METRICS_CONTEXT_PREFIX; + public class RestConfig extends AbstractConfig { + + private final RestMetricsContext metricsContext; + public static final String DEBUG_CONFIG = "debug"; protected static final String DEBUG_CONFIG_DOC = "Boolean indicating whether extra debugging information is generated in some " @@ -666,6 +671,9 @@ private static ConfigDef incompleteBaseConfigDef() { public RestConfig(ConfigDef definition, Map originals) { super(definition, originals); + metricsContext = new RestMetricsContext( + this.getString(METRICS_JMX_PREFIX_CONFIG), + originalsWithPrefix(METRICS_CONTEXT_PREFIX)); } public RestConfig(ConfigDef definition) { @@ -677,7 +685,7 @@ public Time getTime() { } public RestMetricsContext getMetricsContext() { - return new RestMetricsContext(this); + return metricsContext; } public static void validateHttpResponseHeaderConfig(String config) { diff --git a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java index 4d7590b5d0..82cbb397ef 100644 --- a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java +++ b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java @@ -16,9 +16,6 @@ package io.confluent.rest.metrics; -import static org.apache.kafka.clients.CommonClientConfigs.METRICS_CONTEXT_PREFIX; - -import io.confluent.rest.RestConfig; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -30,23 +27,17 @@ public class RestMetricsContext implements MetricsContext { */ protected final Map contextLabels; - /** - * {@link io.confluent.rest.Application} {@link MetricsContext} configuration. - */ - public RestMetricsContext(RestConfig config) { - /* Copy all configuration properties prefixed into metadata instance. */ - this(config.originalsWithPrefix(METRICS_CONTEXT_PREFIX)); - - /* JMX_PREFIX is synonymous with MetricsContext.NAMESPACE */ - this.setLabel(MetricsContext.NAMESPACE, - config.getString(RestConfig.METRICS_JMX_PREFIX_CONFIG)); - } - - private RestMetricsContext(Map config) { + public RestMetricsContext(String namespace, + Map config) { contextLabels = new HashMap<>(); + contextLabels.put(MetricsContext.NAMESPACE, namespace); config.forEach((key, value) -> contextLabels.put(key, value.toString())); } + protected void setContextLabels(Map labels) { + labels.forEach(this::setLabel); + } + /** * Sets a {@link MetricsContext} key, value pair. */ diff --git a/core/src/test/java/io/confluent/rest/TestRestConfig.java b/core/src/test/java/io/confluent/rest/TestRestConfig.java index 44962ccc65..f7f610208b 100644 --- a/core/src/test/java/io/confluent/rest/TestRestConfig.java +++ b/core/src/test/java/io/confluent/rest/TestRestConfig.java @@ -16,7 +16,8 @@ package io.confluent.rest; -import io.confluent.rest.metrics.RestMetricsContext; +import static org.apache.kafka.clients.CommonClientConfigs.METRICS_CONTEXT_PREFIX; + import io.confluent.rest.metrics.TestRestMetricsContext; import org.apache.kafka.common.config.ConfigDef; @@ -40,7 +41,9 @@ public TestRestConfig(Map originals) { } @Override - public RestMetricsContext getMetricsContext() { - return new TestRestMetricsContext(this); + public TestRestMetricsContext getMetricsContext() { + return new TestRestMetricsContext( + getString(METRICS_JMX_PREFIX_CONFIG), + originalsWithPrefix(METRICS_CONTEXT_PREFIX)); } } diff --git a/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java b/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java index fef0643fe5..726c9e3373 100644 --- a/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java +++ b/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java @@ -33,7 +33,7 @@ public class RestMetricsContextTest { @Test public void testDefaultMetricsContext() throws Exception { TestRestConfig config = new TestRestConfig(); - TestRestMetricsContext context = new TestRestMetricsContext(config); + TestRestMetricsContext context = config.getMetricsContext(); assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "rest-utils"); assertEquals(context.getLabel(NAMESPACE), "rest-utils"); @@ -46,7 +46,7 @@ public void testMetricsContextResourceOverride() throws Exception { + RESOURCE_LABEL_TYPE, "root"); TestRestConfig config = new TestRestConfig(props); - TestRestMetricsContext context = new TestRestMetricsContext(config); + TestRestMetricsContext context = config.getMetricsContext(); assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "root"); assertEquals(context.getLabel(NAMESPACE), "rest-utils"); @@ -58,9 +58,8 @@ public void testMetricsContextJMXPrefixPropagation() throws Exception { props.put(RestConfig.METRICS_JMX_PREFIX_CONFIG, "FooApp"); TestRestConfig config = new TestRestConfig(props); - TestRestMetricsContext context = new TestRestMetricsContext(config); + TestRestMetricsContext context = config.getMetricsContext(); - assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "FooApp"); assertEquals(context.getLabel(NAMESPACE), "FooApp"); } @@ -69,7 +68,7 @@ public void testMetricsContextPutNamespaceLabelStripResourcePrefix() throws Exce Map props = new HashMap<>(); TestRestConfig config = new TestRestConfig(props); - TestRestMetricsContext context = new TestRestMetricsContext(config); + TestRestMetricsContext context = config.getMetricsContext(); assertEquals("rest-utils", context.getLabel(NAMESPACE)); } @@ -81,7 +80,7 @@ public void testMetricsContextResourceLabelSetIfAbsent() throws Exception { + RESOURCE_LABEL_TYPE, "root"); TestRestConfig config = new TestRestConfig(props); - TestRestMetricsContext context = new TestRestMetricsContext(config); + TestRestMetricsContext context = config.getMetricsContext(); context.setResourceLabel(RESOURCE_LABEL_TYPE, "rest-utils-resource"); @@ -97,7 +96,7 @@ public void testMetricsContextResourceLabelNew() throws Exception { + RESOURCE_LABEL_TYPE, "root"); TestRestConfig config = new TestRestConfig(props); - TestRestMetricsContext context = new TestRestMetricsContext(config); + TestRestMetricsContext context = config.getMetricsContext(); String RESOURCE_CLUSTER_ID = TestRestMetricsContext.RESOURCE_LABEL_PREFIX + "cluster.id"; @@ -120,8 +119,7 @@ public void testMetricsContextResourceNonStringValue() throws Exception { this.getClass()); TestRestConfig config = new TestRestConfig(props); - TestRestMetricsContext context = new TestRestMetricsContext(config); - + TestRestMetricsContext context = config.getMetricsContext(); assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "root"); assertEquals(context.getLabel(NAMESPACE), "rest-utils"); diff --git a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java index 39f921fadd..e5442b6752 100644 --- a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java +++ b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java @@ -16,27 +16,22 @@ package io.confluent.rest.metrics; -import io.confluent.rest.TestRestConfig; import org.apache.kafka.common.metrics.MetricsContext; +import java.util.Map; + public class TestRestMetricsContext extends RestMetricsContext { /** * MetricsContext Label's for use by Confluent's TelemetryReporter */ public static final String RESOURCE_LABEL_PREFIX = "resource."; public static final String RESOURCE_LABEL_TYPE = RESOURCE_LABEL_PREFIX + "type"; - public static final String RESOURCE_LABEL_COMMIT_ID = RESOURCE_LABEL_PREFIX + "commit.id"; - /** - * {@link io.confluent.rest.Application} {@link MetricsContext} configuration. - */ - public TestRestMetricsContext(TestRestConfig config) { - /* Copy all configuration properties prefixed into metadata instance. */ - super(config); + public TestRestMetricsContext(String namespace, Map config) { + super(namespace, config); - /* Never overwrite preexisting resource labels */ this.setResourceLabel(RESOURCE_LABEL_TYPE, - config.getString(TestRestConfig.METRICS_JMX_PREFIX_CONFIG)); + namespace); } /** From f0c53be7d53919a494fe9cb4ea6fd75e7c05857b Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Wed, 3 Jun 2020 12:30:20 -0400 Subject: [PATCH 16/19] Make RestMetricsContext final --- .../rest/metrics/RestMetricsContext.java | 2 +- .../io/confluent/rest/TestRestConfig.java | 5 +++-- .../rest/metrics/RestMetricsContextTest.java | 18 ++++++++--------- .../rest/metrics/TestRestMetricsContext.java | 20 ++++++++++++------- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java index 82cbb397ef..4cd69e1f86 100644 --- a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java +++ b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java @@ -21,7 +21,7 @@ import java.util.Map; import org.apache.kafka.common.metrics.MetricsContext; -public class RestMetricsContext implements MetricsContext { +public final class RestMetricsContext implements MetricsContext { /** * Client or Service's metadata map. */ diff --git a/core/src/test/java/io/confluent/rest/TestRestConfig.java b/core/src/test/java/io/confluent/rest/TestRestConfig.java index f7f610208b..5d7da79078 100644 --- a/core/src/test/java/io/confluent/rest/TestRestConfig.java +++ b/core/src/test/java/io/confluent/rest/TestRestConfig.java @@ -18,6 +18,7 @@ import static org.apache.kafka.clients.CommonClientConfigs.METRICS_CONTEXT_PREFIX; +import io.confluent.rest.metrics.RestMetricsContext; import io.confluent.rest.metrics.TestRestMetricsContext; import org.apache.kafka.common.config.ConfigDef; @@ -41,9 +42,9 @@ public TestRestConfig(Map originals) { } @Override - public TestRestMetricsContext getMetricsContext() { + public RestMetricsContext getMetricsContext() { return new TestRestMetricsContext( getString(METRICS_JMX_PREFIX_CONFIG), - originalsWithPrefix(METRICS_CONTEXT_PREFIX)); + originalsWithPrefix(METRICS_CONTEXT_PREFIX)).metricsContext(); } } diff --git a/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java b/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java index 726c9e3373..d2fd36eda7 100644 --- a/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java +++ b/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java @@ -33,7 +33,7 @@ public class RestMetricsContextTest { @Test public void testDefaultMetricsContext() throws Exception { TestRestConfig config = new TestRestConfig(); - TestRestMetricsContext context = config.getMetricsContext(); + RestMetricsContext context = config.getMetricsContext(); assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "rest-utils"); assertEquals(context.getLabel(NAMESPACE), "rest-utils"); @@ -46,7 +46,7 @@ public void testMetricsContextResourceOverride() throws Exception { + RESOURCE_LABEL_TYPE, "root"); TestRestConfig config = new TestRestConfig(props); - TestRestMetricsContext context = config.getMetricsContext(); + RestMetricsContext context = config.getMetricsContext(); assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "root"); assertEquals(context.getLabel(NAMESPACE), "rest-utils"); @@ -58,7 +58,7 @@ public void testMetricsContextJMXPrefixPropagation() throws Exception { props.put(RestConfig.METRICS_JMX_PREFIX_CONFIG, "FooApp"); TestRestConfig config = new TestRestConfig(props); - TestRestMetricsContext context = config.getMetricsContext(); + RestMetricsContext context = config.getMetricsContext(); assertEquals(context.getLabel(NAMESPACE), "FooApp"); } @@ -68,7 +68,7 @@ public void testMetricsContextPutNamespaceLabelStripResourcePrefix() throws Exce Map props = new HashMap<>(); TestRestConfig config = new TestRestConfig(props); - TestRestMetricsContext context = config.getMetricsContext(); + RestMetricsContext context = config.getMetricsContext(); assertEquals("rest-utils", context.getLabel(NAMESPACE)); } @@ -80,9 +80,9 @@ public void testMetricsContextResourceLabelSetIfAbsent() throws Exception { + RESOURCE_LABEL_TYPE, "root"); TestRestConfig config = new TestRestConfig(props); - TestRestMetricsContext context = config.getMetricsContext(); + RestMetricsContext context = config.getMetricsContext(); - context.setResourceLabel(RESOURCE_LABEL_TYPE, + context.setLabel(RESOURCE_LABEL_TYPE, "rest-utils-resource"); assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "root"); @@ -96,12 +96,12 @@ public void testMetricsContextResourceLabelNew() throws Exception { + RESOURCE_LABEL_TYPE, "root"); TestRestConfig config = new TestRestConfig(props); - TestRestMetricsContext context = config.getMetricsContext(); + RestMetricsContext context = config.getMetricsContext(); String RESOURCE_CLUSTER_ID = TestRestMetricsContext.RESOURCE_LABEL_PREFIX + "cluster.id"; - context.setResourceLabel( + context.setLabel( RESOURCE_CLUSTER_ID, "rest-utils-bootstrap"); @@ -119,7 +119,7 @@ public void testMetricsContextResourceNonStringValue() throws Exception { this.getClass()); TestRestConfig config = new TestRestConfig(props); - TestRestMetricsContext context = config.getMetricsContext(); + RestMetricsContext context = config.getMetricsContext(); assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "root"); assertEquals(context.getLabel(NAMESPACE), "rest-utils"); diff --git a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java index e5442b6752..54a79e7b04 100644 --- a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java +++ b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java @@ -16,19 +16,20 @@ package io.confluent.rest.metrics; +import java.util.Map; import org.apache.kafka.common.metrics.MetricsContext; -import java.util.Map; -public class TestRestMetricsContext extends RestMetricsContext { +public class TestRestMetricsContext { /** * MetricsContext Label's for use by Confluent's TelemetryReporter */ + private final RestMetricsContext metricsContext; public static final String RESOURCE_LABEL_PREFIX = "resource."; public static final String RESOURCE_LABEL_TYPE = RESOURCE_LABEL_PREFIX + "type"; public TestRestMetricsContext(String namespace, Map config) { - super(namespace, config); + metricsContext = new RestMetricsContext(namespace, config); this.setResourceLabel(RESOURCE_LABEL_TYPE, namespace); @@ -37,20 +38,25 @@ public TestRestMetricsContext(String namespace, Map config) { /** * Sets a {@link MetricsContext} key, value pair. */ - @Override protected void setLabel(String labelKey, String labelValue) { /* Remove resource label if present */ if (labelKey.startsWith(RESOURCE_LABEL_PREFIX)) setResourceLabel(labelKey, labelValue); - super.setLabel(labelKey, labelValue); + metricsContext.setLabel(labelKey, labelValue); } /** * Sets {@link MetricsContext} resource label if not previously set. */ - protected void setResourceLabel(String resource, String value) { - contextLabels.putIfAbsent(resource, value); + private void setResourceLabel(String resource, String value) { + if (metricsContext.getLabel(resource) == null) { + metricsContext.setLabel(resource, value); + } + } + + public RestMetricsContext metricsContext() { + return metricsContext; } } From 6d72fef37524413bc52e353cbbb2aab85a18acbb Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Wed, 3 Jun 2020 12:41:03 -0400 Subject: [PATCH 17/19] PutifAbsent test no longer applicable. Removing --- .../rest/metrics/RestMetricsContextTest.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java b/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java index d2fd36eda7..28bc8aeb2e 100644 --- a/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java +++ b/core/src/test/java/io/confluent/rest/metrics/RestMetricsContextTest.java @@ -73,22 +73,6 @@ public void testMetricsContextPutNamespaceLabelStripResourcePrefix() throws Exce assertEquals("rest-utils", context.getLabel(NAMESPACE)); } - @Test - public void testMetricsContextResourceLabelSetIfAbsent() throws Exception { - Map props = new HashMap<>(); - props.put(METRICS_CONTEXT_PREFIX - + RESOURCE_LABEL_TYPE, "root"); - - TestRestConfig config = new TestRestConfig(props); - RestMetricsContext context = config.getMetricsContext(); - - context.setLabel(RESOURCE_LABEL_TYPE, - "rest-utils-resource"); - - assertEquals(context.getLabel(RESOURCE_LABEL_TYPE), "root"); - assertEquals(context.getLabel(NAMESPACE), "rest-utils"); - } - @Test public void testMetricsContextResourceLabelNew() throws Exception { Map props = new HashMap<>(); From 8e35bf5da5501395d3e10e253c2649d236ab2613 Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Wed, 3 Jun 2020 14:00:28 -0400 Subject: [PATCH 18/19] make contextLabels private --- .../confluent/rest/metrics/RestMetricsContext.java | 12 ++++++------ .../rest/metrics/TestRestMetricsContext.java | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java index 4cd69e1f86..d2c4596d1b 100644 --- a/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java +++ b/core/src/main/java/io/confluent/rest/metrics/RestMetricsContext.java @@ -25,7 +25,7 @@ public final class RestMetricsContext implements MetricsContext { /** * Client or Service's metadata map. */ - protected final Map contextLabels; + private final Map contextLabels; public RestMetricsContext(String namespace, Map config) { @@ -34,17 +34,17 @@ public RestMetricsContext(String namespace, config.forEach((key, value) -> contextLabels.put(key, value.toString())); } - protected void setContextLabels(Map labels) { - labels.forEach(this::setLabel); - } - /** * Sets a {@link MetricsContext} key, value pair. */ - protected void setLabel(String labelKey, String labelValue) { + public void setLabel(String labelKey, String labelValue) { this.contextLabels.put(labelKey, labelValue); } + public void setLabels(Map labels) { + labels.forEach(this::setLabel); + } + /** * Returns the value associated with the specified label else * {@code null}. diff --git a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java index 54a79e7b04..387318fca9 100644 --- a/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java +++ b/core/src/test/java/io/confluent/rest/metrics/TestRestMetricsContext.java @@ -20,7 +20,7 @@ import org.apache.kafka.common.metrics.MetricsContext; -public class TestRestMetricsContext { +public final class TestRestMetricsContext { /** * MetricsContext Label's for use by Confluent's TelemetryReporter */ @@ -38,7 +38,7 @@ public TestRestMetricsContext(String namespace, Map config) { /** * Sets a {@link MetricsContext} key, value pair. */ - protected void setLabel(String labelKey, String labelValue) { + public void setLabel(String labelKey, String labelValue) { /* Remove resource label if present */ if (labelKey.startsWith(RESOURCE_LABEL_PREFIX)) setResourceLabel(labelKey, labelValue); From b932ac7d51ab5621a2021f1d64e56f262b59e8b6 Mon Sep 17 00:00:00 2001 From: rnpridgeon Date: Wed, 3 Jun 2020 16:13:32 -0400 Subject: [PATCH 19/19] Fastforward remove lingering MetricsContext method --- core/src/main/java/io/confluent/rest/Application.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/core/src/main/java/io/confluent/rest/Application.java b/core/src/main/java/io/confluent/rest/Application.java index 2903b3834b..adfc9939ef 100644 --- a/core/src/main/java/io/confluent/rest/Application.java +++ b/core/src/main/java/io/confluent/rest/Application.java @@ -138,13 +138,6 @@ public final String getPath() { } - /** - * Set MetricsContext labels. - */ - protected void setupMetricsContext(T appConfig) { - this.metricsContext = new RestMetricsContext(appConfig); - } - /** * Returns {@link Metrics} object */