PrometheusEnricher extends BaseEnricher { @Override public void create(PlatformMode platformMode, KubernetesListBuilder builder) { builder.accept(new TypedVisitor<ServiceBuilder>() { @Override public void visit(ServiceBuilder serviceBuilder) { String prometheusPort = findPrometheusPort(); if (StringUtils.isNotBlank(prometheusPort)) { Map<String, String> annotations = new HashMap<>(); MapUtil.putIfAbsent(annotations, ANNOTATION_PROMETHEUS_PORT, prometheusPort); MapUtil.putIfAbsent(annotations, ANNOTATION_PROMETHEUS_SCRAPE, "true"); String prometheusPath = getConfig(Config.prometheusPath); if (StringUtils.isNotBlank(prometheusPath)) { MapUtil.putIfAbsent(annotations, ANNOTATION_PROMETHEUS_PATH, prometheusPath); } log.verbose(Logger.LogVerboseCategory.BUILD, "Adding prometheus.io annotations: %s", annotations.entrySet() .stream() .map(Object::toString) .collect(Collectors.joining(", "))); serviceBuilder.editMetadata().addToAnnotations(annotations).endMetadata(); } } }); } PrometheusEnricher(MavenEnricherContext buildContext); @Override void create(PlatformMode platformMode, KubernetesListBuilder builder);  }
@Test public void testCustomPrometheusPort() throws Exception { final ProcessorConfig config = new ProcessorConfig( null, null, Collections.singletonMap( PrometheusEnricher.ENRICHER_NAME, new TreeMap(Collections.singletonMap( Config.prometheusPort.name(), "1234") ) ) ); new Expectations() {{ context.getConfiguration(); result = new Configuration.Builder().processorConfig(config).build(); }}; KubernetesListBuilder builder = new KubernetesListBuilder().withItems(new ServiceBuilder().withNewMetadata().withName("foo").endMetadata().build()); PrometheusEnricher enricher = new PrometheusEnricher(context); enricher.create(PlatformMode.kubernetes, builder); Map<String, String> annotations = builder.buildFirstItem().getMetadata().getAnnotations(); assertEquals(2, annotations.size()); assertEquals("1234", annotations.get(PrometheusEnricher.ANNOTATION_PROMETHEUS_PORT)); assertEquals("true", annotations.get(PrometheusEnricher.ANNOTATION_PROMETHEUS_SCRAPE)); }
@Test public void testDetectPrometheusPort() throws Exception { final ProcessorConfig config = new ProcessorConfig( null, null, Collections.singletonMap( PrometheusEnricher.ENRICHER_NAME, new TreeMap() ) ); final BuildImageConfiguration imageConfig = new BuildImageConfiguration.Builder() .ports(Arrays.asList(PrometheusEnricher.PROMETHEUS_PORT)) .build(); new Expectations() {{ context.getConfiguration(); result = new Configuration.Builder() .processorConfig(config) .images(Arrays.asList(imageConfiguration)) .build(); imageConfiguration.getBuildConfiguration(); result = imageConfig; }}; KubernetesListBuilder builder = new KubernetesListBuilder().withItems(new ServiceBuilder().withNewMetadata().withName("foo").endMetadata().build()); PrometheusEnricher enricher = new PrometheusEnricher(context); enricher.create(PlatformMode.kubernetes, builder); Map<String, String> annotations = builder.buildFirstItem().getMetadata().getAnnotations(); assertEquals(2, annotations.size()); assertEquals("9779", annotations.get(PrometheusEnricher.ANNOTATION_PROMETHEUS_PORT)); assertEquals("true", annotations.get(PrometheusEnricher.ANNOTATION_PROMETHEUS_SCRAPE)); }
@Test public void testNoDefinedPrometheusPort() throws Exception { final ProcessorConfig config = new ProcessorConfig( null, null, Collections.singletonMap( PrometheusEnricher.ENRICHER_NAME, new TreeMap() ) ); final BuildImageConfiguration imageConfig = new BuildImageConfiguration.Builder() .build(); new Expectations() {{ context.getConfiguration(); result = new Configuration.Builder() .processorConfig(config) .images(Arrays.asList(imageConfiguration)) .build(); imageConfiguration.getBuildConfiguration(); result = imageConfig; }}; KubernetesListBuilder builder = new KubernetesListBuilder().withItems(new ServiceBuilder().withNewMetadata().withName("foo").endMetadata().build()); PrometheusEnricher enricher = new PrometheusEnricher(context); enricher.create(PlatformMode.kubernetes, builder); Map<String, String> annotations = builder.buildFirstItem().getMetadata().getAnnotations(); assertNull(annotations); }
@Test public void testCustomPrometheusPath() throws Exception { final ProcessorConfig config = new ProcessorConfig( null, null, Collections.singletonMap( PrometheusEnricher.ENRICHER_NAME, new TreeMap(Collections.singletonMap( Config.prometheusPath.name(), "/prometheus") ) ) ); final BuildImageConfiguration imageConfig = new BuildImageConfiguration.Builder() .ports(Arrays.asList(PrometheusEnricher.PROMETHEUS_PORT)) .build(); new Expectations() {{ context.getConfiguration(); result = new Configuration.Builder() .processorConfig(config) .images(Arrays.asList(imageConfiguration)) .build(); imageConfiguration.getBuildConfiguration(); result = imageConfig; }}; KubernetesListBuilder builder = new KubernetesListBuilder().withItems(new ServiceBuilder().withNewMetadata().withName("foo").endMetadata().build()); PrometheusEnricher enricher = new PrometheusEnricher(context); enricher.create(PlatformMode.kubernetes, builder); Map<String, String> annotations = builder.buildFirstItem().getMetadata().getAnnotations(); assertEquals(3, annotations.size()); assertEquals("9779", annotations.get(PrometheusEnricher.ANNOTATION_PROMETHEUS_PORT)); assertEquals("true", annotations.get(PrometheusEnricher.ANNOTATION_PROMETHEUS_SCRAPE)); assertEquals("/prometheus", annotations.get(PrometheusEnricher.ANNOTATION_PROMETHEUS_PATH)); }