use of io.strimzi.api.kafka.model.JmxPrometheusExporterMetricsBuilder in project strimzi-kafka-operator by strimzi.
the class RollingUpdateST method testMetricsChange.
@IsolatedTest
@Tag(ROLLING_UPDATE)
@SuppressWarnings("checkstyle:MethodLength")
void testMetricsChange(ExtensionContext extensionContext) throws JsonProcessingException {
final String clusterName = mapWithClusterNames.get(extensionContext.getDisplayName());
final String kafkaClientsName = mapWithKafkaClientNames.get(extensionContext.getDisplayName());
final LabelSelector kafkaSelector = KafkaResource.getLabelSelector(clusterName, KafkaResources.kafkaStatefulSetName(clusterName));
final LabelSelector zkSelector = KafkaResource.getLabelSelector(clusterName, KafkaResources.zookeeperStatefulSetName(clusterName));
// Kafka
Map<String, Object> kafkaRule = new HashMap<>();
kafkaRule.put("pattern", "kafka.(\\w+)<type=(.+), name=(.+)><>Count");
kafkaRule.put("name", "kafka_$1_$2_$3_count");
kafkaRule.put("type", "COUNTER");
Map<String, Object> kafkaMetrics = new HashMap<>();
kafkaMetrics.put("lowercaseOutputName", true);
kafkaMetrics.put("rules", Collections.singletonList(kafkaRule));
String metricsCMNameK = "k-metrics-cm";
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
String yaml = mapper.writeValueAsString(kafkaMetrics);
ConfigMap metricsCMK = new ConfigMapBuilder().withNewMetadata().withName(metricsCMNameK).withNamespace(namespace).endMetadata().withData(singletonMap("metrics-config.yml", yaml)).build();
JmxPrometheusExporterMetrics kafkaMetricsConfig = new JmxPrometheusExporterMetricsBuilder().withNewValueFrom().withConfigMapKeyRef(new ConfigMapKeySelectorBuilder().withName(metricsCMNameK).withKey("metrics-config.yml").withOptional(true).build()).endValueFrom().build();
// Zookeeper
Map<String, Object> zookeeperLabels = new HashMap<>();
zookeeperLabels.put("replicaId", "$2");
Map<String, Object> zookeeperRule = new HashMap<>();
zookeeperRule.put("labels", zookeeperLabels);
zookeeperRule.put("name", "zookeeper_$3");
zookeeperRule.put("pattern", "org.apache.ZooKeeperService<name0=ReplicatedServer_id(\\d+), name1=replica.(\\d+)><>(\\w+)");
Map<String, Object> zookeeperMetrics = new HashMap<>();
zookeeperMetrics.put("lowercaseOutputName", true);
zookeeperMetrics.put("rules", Collections.singletonList(zookeeperRule));
String metricsCMNameZk = "zk-metrics-cm";
ConfigMap metricsCMZk = new ConfigMapBuilder().withNewMetadata().withName(metricsCMNameZk).withNamespace(namespace).endMetadata().withData(singletonMap("metrics-config.yml", mapper.writeValueAsString(zookeeperMetrics))).build();
JmxPrometheusExporterMetrics zkMetricsConfig = new JmxPrometheusExporterMetricsBuilder().withNewValueFrom().withConfigMapKeyRef(new ConfigMapKeySelectorBuilder().withName(metricsCMNameZk).withKey("metrics-config.yml").withOptional(true).build()).endValueFrom().build();
kubeClient(namespace).getClient().configMaps().inNamespace(namespace).createOrReplace(metricsCMK);
kubeClient(namespace).getClient().configMaps().inNamespace(namespace).createOrReplace(metricsCMZk);
resourceManager.createResource(extensionContext, KafkaTemplates.kafkaEphemeral(clusterName, 3, 3).editMetadata().withNamespace(namespace).endMetadata().editSpec().editKafka().withMetricsConfig(kafkaMetricsConfig).endKafka().editOrNewZookeeper().withMetricsConfig(zkMetricsConfig).endZookeeper().withNewKafkaExporter().endKafkaExporter().endSpec().build());
Map<String, String> kafkaPods = PodUtils.podSnapshot(namespace, kafkaSelector);
Map<String, String> zkPods = PodUtils.podSnapshot(namespace, zkSelector);
resourceManager.createResource(extensionContext, false, KafkaClientsTemplates.kafkaClients(false, kafkaClientsName).editMetadata().withNamespace(namespace).endMetadata().build());
String metricsScraperPodName = PodUtils.getPodsByPrefixInNameWithDynamicWait(namespace, kafkaClientsName).get(0).getMetadata().getName();
MetricsCollector metricsCollector = new MetricsCollector.Builder().withNamespaceName(namespace).withScraperPodName(metricsScraperPodName).withComponentName(clusterName).withComponentType(ComponentType.Kafka).build();
LOGGER.info("Check if metrics are present in pod of Kafka and Zookeeper");
Map<String, String> kafkaMetricsOutput = metricsCollector.collectMetricsFromPods();
Map<String, String> zkMetricsOutput = metricsCollector.toBuilder().withComponentType(ComponentType.Zookeeper).build().collectMetricsFromPods();
assertThat(kafkaMetricsOutput.values().toString().contains("kafka_"), is(true));
assertThat(zkMetricsOutput.values().toString().contains("replicaId"), is(true));
LOGGER.info("Changing metrics to something else");
kafkaRule.replace("pattern", "kafka.(\\w+)<type=(.+), name=(.+)><>Count", "kafka.(\\w+)<type=(.+), name=(.+)Percent\\w*><>MeanRate");
kafkaRule.replace("name", "kafka_$1_$2_$3_count", "kafka_$1_$2_$3_percent");
kafkaRule.replace("type", "COUNTER", "GAUGE");
zookeeperRule.replace("pattern", "org.apache.ZooKeeperService<name0=ReplicatedServer_id(\\d+), name1=replica.(\\d+)><>(\\w+)", "org.apache.ZooKeeperService<name0=StandaloneServer_port(\\d+)><>(\\w+)");
zookeeperRule.replace("name", "zookeeper_$3", "zookeeper_$2");
zookeeperRule.replace("labels", zookeeperLabels, null);
metricsCMZk = new ConfigMapBuilder().withNewMetadata().withName(metricsCMNameZk).withNamespace(namespace).endMetadata().withData(singletonMap("metrics-config.yml", mapper.writeValueAsString(zookeeperMetrics))).build();
metricsCMK = new ConfigMapBuilder().withNewMetadata().withName(metricsCMNameK).withNamespace(namespace).endMetadata().withData(singletonMap("metrics-config.yml", mapper.writeValueAsString(kafkaMetrics))).build();
kubeClient(namespace).getClient().configMaps().inNamespace(namespace).createOrReplace(metricsCMK);
kubeClient(namespace).getClient().configMaps().inNamespace(namespace).createOrReplace(metricsCMZk);
PodUtils.verifyThatRunningPodsAreStable(namespace, KafkaResources.zookeeperStatefulSetName(clusterName));
PodUtils.verifyThatRunningPodsAreStable(namespace, KafkaResources.kafkaStatefulSetName(clusterName));
LOGGER.info("Check if Kafka and Zookeeper pods didn't roll");
assertThat(PodUtils.podSnapshot(namespace, zkSelector), is(zkPods));
assertThat(PodUtils.podSnapshot(namespace, kafkaSelector), is(kafkaPods));
LOGGER.info("Check if Kafka and Zookeeper metrics are changed");
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
String kafkaMetricsConf = kubeClient(namespace).getClient().configMaps().inNamespace(namespace).withName(metricsCMNameK).get().getData().get("metrics-config.yml");
String zkMetricsConf = kubeClient(namespace).getClient().configMaps().inNamespace(namespace).withName(metricsCMNameZk).get().getData().get("metrics-config.yml");
Object kafkaMetricsJsonToYaml = yamlReader.readValue(kafkaMetricsConf, Object.class);
Object zkMetricsJsonToYaml = yamlReader.readValue(zkMetricsConf, Object.class);
ObjectMapper jsonWriter = new ObjectMapper();
assertThat(kubeClient(namespace).getClient().configMaps().inNamespace(namespace).withName(KafkaResources.kafkaMetricsAndLogConfigMapName(clusterName)).get().getData().get(Constants.METRICS_CONFIG_JSON_NAME), is(jsonWriter.writeValueAsString(kafkaMetricsJsonToYaml)));
assertThat(kubeClient(namespace).getClient().configMaps().inNamespace(namespace).withName(KafkaResources.zookeeperMetricsAndLogConfigMapName(clusterName)).get().getData().get(Constants.METRICS_CONFIG_JSON_NAME), is(jsonWriter.writeValueAsString(zkMetricsJsonToYaml)));
LOGGER.info("Check if metrics are present in pod of Kafka and Zookeeper");
kafkaMetricsOutput = metricsCollector.collectMetricsFromPods();
zkMetricsOutput = metricsCollector.toBuilder().withComponentType(ComponentType.Zookeeper).build().collectMetricsFromPods();
assertThat(kafkaMetricsOutput.values().toString().contains("kafka_"), is(true));
assertThat(zkMetricsOutput.values().toString().contains("replicaId"), is(true));
LOGGER.info("Removing metrics from Kafka and Zookeeper and setting them to null");
KafkaResource.replaceKafkaResourceInSpecificNamespace(clusterName, kafka -> {
kafka.getSpec().getKafka().setMetricsConfig(null);
kafka.getSpec().getZookeeper().setMetricsConfig(null);
}, namespace);
LOGGER.info("Wait if Kafka and Zookeeper pods will roll");
RollingUpdateUtils.waitTillComponentHasRolledAndPodsReady(namespace, zkSelector, 3, zkPods);
RollingUpdateUtils.waitTillComponentHasRolled(namespace, kafkaSelector, 3, kafkaPods);
LOGGER.info("Check if metrics are not existing in pods");
kafkaMetricsOutput = metricsCollector.collectMetricsFromPodsWithoutWait();
zkMetricsOutput = metricsCollector.toBuilder().withComponentType(ComponentType.Zookeeper).build().collectMetricsFromPodsWithoutWait();
kafkaMetricsOutput.values().forEach(value -> assertThat(value, is("")));
zkMetricsOutput.values().forEach(value -> assertThat(value, is("")));
}
use of io.strimzi.api.kafka.model.JmxPrometheusExporterMetricsBuilder in project strimzi-kafka-operator by strimzi.
the class KafkaConnectClusterTest method testMetricsParsingFromConfigMap.
@ParallelTest
public void testMetricsParsingFromConfigMap() {
MetricsConfig metrics = new JmxPrometheusExporterMetricsBuilder().withNewValueFrom().withConfigMapKeyRef(new ConfigMapKeySelectorBuilder().withName("my-metrics-configuration").withKey("config.yaml").build()).endValueFrom().build();
KafkaConnect kafkaConnect = new KafkaConnectBuilder(this.resource).editSpec().withMetricsConfig(metrics).endSpec().build();
KafkaConnectCluster kc = KafkaConnectCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, kafkaConnect, VERSIONS);
assertThat(kc.isMetricsEnabled(), is(true));
assertThat(kc.getMetricsConfigInCm(), is(metrics));
}
use of io.strimzi.api.kafka.model.JmxPrometheusExporterMetricsBuilder in project strimzi-kafka-operator by strimzi.
the class ZookeeperClusterTest method testMetricsParsingFromConfigMap.
@ParallelTest
public void testMetricsParsingFromConfigMap() {
MetricsConfig metrics = new JmxPrometheusExporterMetricsBuilder().withNewValueFrom().withConfigMapKeyRef(new ConfigMapKeySelectorBuilder().withName("my-metrics-configuration").withKey("config.yaml").build()).endValueFrom().build();
Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafka(namespace, cluster, replicas, image, healthDelay, healthTimeout)).editSpec().editZookeeper().withMetricsConfig(metrics).endZookeeper().endSpec().build();
ZookeeperCluster zc = ZookeeperCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, kafkaAssembly, VERSIONS);
assertThat(zc.isMetricsEnabled(), is(true));
assertThat(zc.getMetricsConfigInCm(), is(metrics));
}
use of io.strimzi.api.kafka.model.JmxPrometheusExporterMetricsBuilder in project strimzi by strimzi.
the class CruiseControlTest method testMetricsParsingFromConfigMap.
@ParallelTest
public void testMetricsParsingFromConfigMap() {
MetricsConfig metrics = new JmxPrometheusExporterMetricsBuilder().withNewValueFrom().withConfigMapKeyRef(new ConfigMapKeySelectorBuilder().withName("my-metrics-configuration").withKey("config.yaml").build()).endValueFrom().build();
CruiseControlSpec cruiseControlSpec = new CruiseControlSpecBuilder().withMetricsConfig(metrics).build();
Kafka kafkaAssembly = createKafka(cruiseControlSpec);
CruiseControl cc = createCruiseControl(kafkaAssembly);
assertThat(cc.isMetricsEnabled(), is(true));
assertThat(cc.getMetricsConfigInCm(), is(metrics));
}
use of io.strimzi.api.kafka.model.JmxPrometheusExporterMetricsBuilder in project strimzi by strimzi.
the class RollingUpdateST method testMetricsChange.
@IsolatedTest
@Tag(ROLLING_UPDATE)
@SuppressWarnings("checkstyle:MethodLength")
void testMetricsChange(ExtensionContext extensionContext) throws JsonProcessingException {
final String clusterName = mapWithClusterNames.get(extensionContext.getDisplayName());
final String kafkaClientsName = mapWithKafkaClientNames.get(extensionContext.getDisplayName());
final LabelSelector kafkaSelector = KafkaResource.getLabelSelector(clusterName, KafkaResources.kafkaStatefulSetName(clusterName));
final LabelSelector zkSelector = KafkaResource.getLabelSelector(clusterName, KafkaResources.zookeeperStatefulSetName(clusterName));
// Kafka
Map<String, Object> kafkaRule = new HashMap<>();
kafkaRule.put("pattern", "kafka.(\\w+)<type=(.+), name=(.+)><>Count");
kafkaRule.put("name", "kafka_$1_$2_$3_count");
kafkaRule.put("type", "COUNTER");
Map<String, Object> kafkaMetrics = new HashMap<>();
kafkaMetrics.put("lowercaseOutputName", true);
kafkaMetrics.put("rules", Collections.singletonList(kafkaRule));
String metricsCMNameK = "k-metrics-cm";
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
String yaml = mapper.writeValueAsString(kafkaMetrics);
ConfigMap metricsCMK = new ConfigMapBuilder().withNewMetadata().withName(metricsCMNameK).withNamespace(namespace).endMetadata().withData(singletonMap("metrics-config.yml", yaml)).build();
JmxPrometheusExporterMetrics kafkaMetricsConfig = new JmxPrometheusExporterMetricsBuilder().withNewValueFrom().withConfigMapKeyRef(new ConfigMapKeySelectorBuilder().withName(metricsCMNameK).withKey("metrics-config.yml").withOptional(true).build()).endValueFrom().build();
// Zookeeper
Map<String, Object> zookeeperLabels = new HashMap<>();
zookeeperLabels.put("replicaId", "$2");
Map<String, Object> zookeeperRule = new HashMap<>();
zookeeperRule.put("labels", zookeeperLabels);
zookeeperRule.put("name", "zookeeper_$3");
zookeeperRule.put("pattern", "org.apache.ZooKeeperService<name0=ReplicatedServer_id(\\d+), name1=replica.(\\d+)><>(\\w+)");
Map<String, Object> zookeeperMetrics = new HashMap<>();
zookeeperMetrics.put("lowercaseOutputName", true);
zookeeperMetrics.put("rules", Collections.singletonList(zookeeperRule));
String metricsCMNameZk = "zk-metrics-cm";
ConfigMap metricsCMZk = new ConfigMapBuilder().withNewMetadata().withName(metricsCMNameZk).withNamespace(namespace).endMetadata().withData(singletonMap("metrics-config.yml", mapper.writeValueAsString(zookeeperMetrics))).build();
JmxPrometheusExporterMetrics zkMetricsConfig = new JmxPrometheusExporterMetricsBuilder().withNewValueFrom().withConfigMapKeyRef(new ConfigMapKeySelectorBuilder().withName(metricsCMNameZk).withKey("metrics-config.yml").withOptional(true).build()).endValueFrom().build();
kubeClient(namespace).getClient().configMaps().inNamespace(namespace).createOrReplace(metricsCMK);
kubeClient(namespace).getClient().configMaps().inNamespace(namespace).createOrReplace(metricsCMZk);
resourceManager.createResource(extensionContext, KafkaTemplates.kafkaEphemeral(clusterName, 3, 3).editMetadata().withNamespace(namespace).endMetadata().editSpec().editKafka().withMetricsConfig(kafkaMetricsConfig).endKafka().editOrNewZookeeper().withMetricsConfig(zkMetricsConfig).endZookeeper().withNewKafkaExporter().endKafkaExporter().endSpec().build());
Map<String, String> kafkaPods = PodUtils.podSnapshot(namespace, kafkaSelector);
Map<String, String> zkPods = PodUtils.podSnapshot(namespace, zkSelector);
resourceManager.createResource(extensionContext, false, KafkaClientsTemplates.kafkaClients(false, kafkaClientsName).editMetadata().withNamespace(namespace).endMetadata().build());
String metricsScraperPodName = PodUtils.getPodsByPrefixInNameWithDynamicWait(namespace, kafkaClientsName).get(0).getMetadata().getName();
MetricsCollector metricsCollector = new MetricsCollector.Builder().withNamespaceName(namespace).withScraperPodName(metricsScraperPodName).withComponentName(clusterName).withComponentType(ComponentType.Kafka).build();
LOGGER.info("Check if metrics are present in pod of Kafka and Zookeeper");
Map<String, String> kafkaMetricsOutput = metricsCollector.collectMetricsFromPods();
Map<String, String> zkMetricsOutput = metricsCollector.toBuilder().withComponentType(ComponentType.Zookeeper).build().collectMetricsFromPods();
assertThat(kafkaMetricsOutput.values().toString().contains("kafka_"), is(true));
assertThat(zkMetricsOutput.values().toString().contains("replicaId"), is(true));
LOGGER.info("Changing metrics to something else");
kafkaRule.replace("pattern", "kafka.(\\w+)<type=(.+), name=(.+)><>Count", "kafka.(\\w+)<type=(.+), name=(.+)Percent\\w*><>MeanRate");
kafkaRule.replace("name", "kafka_$1_$2_$3_count", "kafka_$1_$2_$3_percent");
kafkaRule.replace("type", "COUNTER", "GAUGE");
zookeeperRule.replace("pattern", "org.apache.ZooKeeperService<name0=ReplicatedServer_id(\\d+), name1=replica.(\\d+)><>(\\w+)", "org.apache.ZooKeeperService<name0=StandaloneServer_port(\\d+)><>(\\w+)");
zookeeperRule.replace("name", "zookeeper_$3", "zookeeper_$2");
zookeeperRule.replace("labels", zookeeperLabels, null);
metricsCMZk = new ConfigMapBuilder().withNewMetadata().withName(metricsCMNameZk).withNamespace(namespace).endMetadata().withData(singletonMap("metrics-config.yml", mapper.writeValueAsString(zookeeperMetrics))).build();
metricsCMK = new ConfigMapBuilder().withNewMetadata().withName(metricsCMNameK).withNamespace(namespace).endMetadata().withData(singletonMap("metrics-config.yml", mapper.writeValueAsString(kafkaMetrics))).build();
kubeClient(namespace).getClient().configMaps().inNamespace(namespace).createOrReplace(metricsCMK);
kubeClient(namespace).getClient().configMaps().inNamespace(namespace).createOrReplace(metricsCMZk);
PodUtils.verifyThatRunningPodsAreStable(namespace, KafkaResources.zookeeperStatefulSetName(clusterName));
PodUtils.verifyThatRunningPodsAreStable(namespace, KafkaResources.kafkaStatefulSetName(clusterName));
LOGGER.info("Check if Kafka and Zookeeper pods didn't roll");
assertThat(PodUtils.podSnapshot(namespace, zkSelector), is(zkPods));
assertThat(PodUtils.podSnapshot(namespace, kafkaSelector), is(kafkaPods));
LOGGER.info("Check if Kafka and Zookeeper metrics are changed");
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
String kafkaMetricsConf = kubeClient(namespace).getClient().configMaps().inNamespace(namespace).withName(metricsCMNameK).get().getData().get("metrics-config.yml");
String zkMetricsConf = kubeClient(namespace).getClient().configMaps().inNamespace(namespace).withName(metricsCMNameZk).get().getData().get("metrics-config.yml");
Object kafkaMetricsJsonToYaml = yamlReader.readValue(kafkaMetricsConf, Object.class);
Object zkMetricsJsonToYaml = yamlReader.readValue(zkMetricsConf, Object.class);
ObjectMapper jsonWriter = new ObjectMapper();
assertThat(kubeClient(namespace).getClient().configMaps().inNamespace(namespace).withName(KafkaResources.kafkaMetricsAndLogConfigMapName(clusterName)).get().getData().get(Constants.METRICS_CONFIG_JSON_NAME), is(jsonWriter.writeValueAsString(kafkaMetricsJsonToYaml)));
assertThat(kubeClient(namespace).getClient().configMaps().inNamespace(namespace).withName(KafkaResources.zookeeperMetricsAndLogConfigMapName(clusterName)).get().getData().get(Constants.METRICS_CONFIG_JSON_NAME), is(jsonWriter.writeValueAsString(zkMetricsJsonToYaml)));
LOGGER.info("Check if metrics are present in pod of Kafka and Zookeeper");
kafkaMetricsOutput = metricsCollector.collectMetricsFromPods();
zkMetricsOutput = metricsCollector.toBuilder().withComponentType(ComponentType.Zookeeper).build().collectMetricsFromPods();
assertThat(kafkaMetricsOutput.values().toString().contains("kafka_"), is(true));
assertThat(zkMetricsOutput.values().toString().contains("replicaId"), is(true));
LOGGER.info("Removing metrics from Kafka and Zookeeper and setting them to null");
KafkaResource.replaceKafkaResourceInSpecificNamespace(clusterName, kafka -> {
kafka.getSpec().getKafka().setMetricsConfig(null);
kafka.getSpec().getZookeeper().setMetricsConfig(null);
}, namespace);
LOGGER.info("Wait if Kafka and Zookeeper pods will roll");
RollingUpdateUtils.waitTillComponentHasRolledAndPodsReady(namespace, zkSelector, 3, zkPods);
RollingUpdateUtils.waitTillComponentHasRolled(namespace, kafkaSelector, 3, kafkaPods);
LOGGER.info("Check if metrics are not existing in pods");
kafkaMetricsOutput = metricsCollector.collectMetricsFromPodsWithoutWait();
zkMetricsOutput = metricsCollector.toBuilder().withComponentType(ComponentType.Zookeeper).build().collectMetricsFromPodsWithoutWait();
kafkaMetricsOutput.values().forEach(value -> assertThat(value, is("")));
zkMetricsOutput.values().forEach(value -> assertThat(value, is("")));
}
Aggregations