Search in sources :

Example 1 with ResolvedTarget

use of org.cloudfoundry.promregator.scanner.ResolvedTarget in project promregator by promregator.

the class InstanceLifecycleHandlerTest method testReceiverCleansUpProperly.

@Test
void testReceiverCleansUpProperly() {
    InstanceLifecycleHandler subject = new InstanceLifecycleHandler();
    ResolvedTarget rt = new ResolvedTarget();
    rt.setOrgName("testOrgName");
    rt.setSpaceName("testSpaceName");
    rt.setApplicationName("testapp");
    rt.setPath("/path/test");
    rt.setProtocol("https");
    Instance i = new Instance(rt, "55820b2c-2fa5-11e8-b467-0ed5f89f718b:3", "access.url.bogus", false);
    /* generate some data first */
    String orgName = i.getTarget().getOrgName();
    String spaceName = i.getTarget().getSpaceName();
    String appName = i.getTarget().getApplicationName();
    AbstractMetricFamilySamplesEnricher mfse = new CFAllLabelsMetricFamilySamplesEnricher(orgName, spaceName, appName, i.getInstanceId());
    List<String> labelValues = mfse.getEnrichedLabelValues(new LinkedList<>());
    String[] ownTelemetryLabelValues = labelValues.toArray(new String[0]);
    MetricsFetcherMetrics mfm = new MetricsFetcherMetrics(ownTelemetryLabelValues, true);
    mfm.getFailedRequests().inc();
    mfm.getLatencyRequest().observe(42.0);
    mfm.getRequestSize().observe(2000);
    // trigger cleanup now
    subject.receiver(i);
    Enumeration<MetricFamilySamples> mfs = CollectorRegistry.defaultRegistry.metricFamilySamples();
    while (mfs.hasMoreElements()) {
        MetricFamilySamples metric = mfs.nextElement();
        for (MetricFamilySamples.Sample sample : metric.samples) {
            Assertions.assertFalse(sample.labelValues.contains("testapp"));
        }
    }
}
Also used : MetricsFetcherMetrics(org.cloudfoundry.promregator.fetcher.MetricsFetcherMetrics) Instance(org.cloudfoundry.promregator.scanner.Instance) ResolvedTarget(org.cloudfoundry.promregator.scanner.ResolvedTarget) AbstractMetricFamilySamplesEnricher(org.cloudfoundry.promregator.rewrite.AbstractMetricFamilySamplesEnricher) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) CFAllLabelsMetricFamilySamplesEnricher(org.cloudfoundry.promregator.rewrite.CFAllLabelsMetricFamilySamplesEnricher) Test(org.junit.jupiter.api.Test)

Example 2 with ResolvedTarget

use of org.cloudfoundry.promregator.scanner.ResolvedTarget in project promregator by promregator.

the class AbstractMetricsEndpoint method createMetricsFetchers.

protected List<MetricsFetcher> createMetricsFetchers(List<Instance> instanceList) {
    List<MetricsFetcher> callablesList = new LinkedList<>();
    for (Instance instance : instanceList) {
        log.debug(String.format("Creating Metrics Fetcher for instance %s", instance.getInstanceId()));
        ResolvedTarget target = instance.getTarget();
        String orgName = target.getOrgName();
        String spaceName = target.getSpaceName();
        String appName = target.getApplicationName();
        String accessURL = instance.getAccessUrl();
        if (accessURL == null) {
            log.warn(String.format("Unable to retrieve hostname for %s/%s/%s; skipping", orgName, spaceName, appName));
            continue;
        }
        String[] ownTelemetryLabelValues = this.determineOwnTelemetryLabelValues(orgName, spaceName, appName, instance.getInstanceId());
        MetricsFetcherMetrics mfm = new MetricsFetcherMetrics(ownTelemetryLabelValues, this.recordRequestLatency);
        final boolean labelEnrichmentEnabled = this.isLabelEnrichmentEnabled();
        /*
			 * Warning! the gauge "up" is a very special beast!
			 * As it is always transferred along the other metrics (it's not a promregator-own metric!), it must always
			 * follow the same labels as the other metrics which are scraped
			 */
        Gauge.Child upChild = null;
        AbstractMetricFamilySamplesEnricher mfse = null;
        if (labelEnrichmentEnabled) {
            mfse = new CFAllLabelsMetricFamilySamplesEnricher(orgName, spaceName, appName, instance.getInstanceId());
        } else {
            mfse = new NullMetricFamilySamplesEnricher();
        }
        upChild = this.up.labels(mfse.getEnrichedLabelValues(new LinkedList<>()).toArray(new String[0]));
        AuthenticationEnricher ae = this.authenticatorController.getAuthenticationEnricherByTarget(instance.getTarget().getOriginalTarget());
        MetricsFetcher mf = null;
        if (this.simulationMode) {
            mf = new MetricsFetcherSimulator(accessURL, ae, mfse, mfm, upChild);
        } else {
            CFMetricsFetcherConfig cfmfConfig = new CFMetricsFetcherConfig();
            cfmfConfig.setAuthenticationEnricher(ae);
            cfmfConfig.setMetricFamilySamplesEnricher(mfse);
            cfmfConfig.setMetricsFetcherMetrics(mfm);
            cfmfConfig.setUpChild(upChild);
            cfmfConfig.setPromregatorInstanceIdentifier(this.promregatorInstanceIdentifier);
            cfmfConfig.setConnectionTimeoutInMillis(this.fetcherConnectionTimeout);
            cfmfConfig.setSocketReadTimeoutInMillis(this.fetcherSocketReadTimeout);
            this.provideProxyConfiguration(cfmfConfig);
            mf = new CFMetricsFetcher(accessURL, instance.getInstanceId(), cfmfConfig, instance.isInternal());
        }
        callablesList.add(mf);
    }
    return callablesList;
}
Also used : MetricsFetcherMetrics(org.cloudfoundry.promregator.fetcher.MetricsFetcherMetrics) CFMetricsFetcherConfig(org.cloudfoundry.promregator.fetcher.CFMetricsFetcherConfig) CFMetricsFetcher(org.cloudfoundry.promregator.fetcher.CFMetricsFetcher) Instance(org.cloudfoundry.promregator.scanner.Instance) ResolvedTarget(org.cloudfoundry.promregator.scanner.ResolvedTarget) AbstractMetricFamilySamplesEnricher(org.cloudfoundry.promregator.rewrite.AbstractMetricFamilySamplesEnricher) LinkedList(java.util.LinkedList) Gauge(io.prometheus.client.Gauge) AuthenticationEnricher(org.cloudfoundry.promregator.auth.AuthenticationEnricher) MetricsFetcherSimulator(org.cloudfoundry.promregator.fetcher.MetricsFetcherSimulator) CFAllLabelsMetricFamilySamplesEnricher(org.cloudfoundry.promregator.rewrite.CFAllLabelsMetricFamilySamplesEnricher) MetricsFetcher(org.cloudfoundry.promregator.fetcher.MetricsFetcher) CFMetricsFetcher(org.cloudfoundry.promregator.fetcher.CFMetricsFetcher) NullMetricFamilySamplesEnricher(org.cloudfoundry.promregator.rewrite.NullMetricFamilySamplesEnricher)

Example 3 with ResolvedTarget

use of org.cloudfoundry.promregator.scanner.ResolvedTarget in project promregator by promregator.

the class SingleTargetMetricsEndpoint method handleScrapeDuration.

@Override
protected void handleScrapeDuration(CollectorRegistry requestRegistry, Duration duration) {
    /*
		 * Note: The scrape_duration_seconds metric is being passed on to Prometheus with
		 * the normal scraping request.
		 * If the configuration option promregator.scraping.labelEnrichment is disabled, then 
		 * the metric must also comply to this approach. Otherwise there might arise issues
		 * with rewriting in Prometheus.
		 */
    AbstractMetricFamilySamplesEnricher enricher = null;
    String[] ownTelemetryLabels = null;
    if (this.isLabelEnrichmentEnabled()) {
        if (this.instance == null) {
            log.warn("Internal inconsistency: Single Target Metrics Endpoint triggered, even though instance could not be detected; skipping scrape_duration");
            return;
        }
        ResolvedTarget t = this.instance.getTarget();
        ownTelemetryLabels = CFAllLabelsMetricFamilySamplesEnricher.getEnrichingLabelNames();
        enricher = new CFAllLabelsMetricFamilySamplesEnricher(t.getOrgName(), t.getSpaceName(), t.getApplicationName(), this.instance.getInstanceId());
    } else {
        ownTelemetryLabels = NullMetricFamilySamplesEnricher.getEnrichingLabelNames();
        enricher = new NullMetricFamilySamplesEnricher();
    }
    Gauge scrapeDuration = Gauge.build("promregator_scrape_duration_seconds", "Duration in seconds indicating how long scraping of all metrics took").labelNames(ownTelemetryLabels).register(requestRegistry);
    List<String> labelValues = enricher.getEnrichedLabelValues(new ArrayList<>(0));
    scrapeDuration.labels(labelValues.toArray(new String[0])).set(duration.toMillis() / 1000.0);
}
Also used : AbstractMetricFamilySamplesEnricher(org.cloudfoundry.promregator.rewrite.AbstractMetricFamilySamplesEnricher) ResolvedTarget(org.cloudfoundry.promregator.scanner.ResolvedTarget) CFAllLabelsMetricFamilySamplesEnricher(org.cloudfoundry.promregator.rewrite.CFAllLabelsMetricFamilySamplesEnricher) NullMetricFamilySamplesEnricher(org.cloudfoundry.promregator.rewrite.NullMetricFamilySamplesEnricher) Gauge(io.prometheus.client.Gauge)

Example 4 with ResolvedTarget

use of org.cloudfoundry.promregator.scanner.ResolvedTarget in project promregator by promregator.

the class LabelEnrichmentMockedMetricsEndpointSpringApplication method appInstanceScanner.

@Bean
public AppInstanceScanner appInstanceScanner() {
    return new AppInstanceScanner() {

        @Override
        public List<Instance> determineInstancesFromTargets(List<ResolvedTarget> targets, @Nullable Predicate<? super String> applicationIdFilter, @Nullable Predicate<? super Instance> instanceFilter) {
            LinkedList<Instance> result = new LinkedList<>();
            ResolvedTarget t = new ResolvedTarget();
            t.setOrgName("unittestorg");
            t.setSpaceName("unittestspace");
            t.setApplicationName("unittestapp");
            t.setPath("/metrics");
            t.setProtocol("http");
            // Must be the same port as in MetricsEndpointMockServer
            result.add(new Instance(t, "faedbb0a-2273-4cb4-a659-bd31331f7daf:0", "http://localhost:9002/metrics", false));
            if (applicationIdFilter != null) {
                for (Iterator<Instance> it = result.iterator(); it.hasNext(); ) {
                    Instance instance = it.next();
                    if (!applicationIdFilter.test(instance.getApplicationId()))
                        it.remove();
                }
            }
            if (instanceFilter != null) {
                for (Iterator<Instance> it = result.iterator(); it.hasNext(); ) {
                    Instance instance = it.next();
                    if (!instanceFilter.test(instance))
                        it.remove();
                }
            }
            return result;
        }
    };
}
Also used : AppInstanceScanner(org.cloudfoundry.promregator.scanner.AppInstanceScanner) Instance(org.cloudfoundry.promregator.scanner.Instance) LinkedList(java.util.LinkedList) List(java.util.List) ResolvedTarget(org.cloudfoundry.promregator.scanner.ResolvedTarget) Nullable(javax.annotation.Nullable) LinkedList(java.util.LinkedList) Predicate(java.util.function.Predicate) Bean(org.springframework.context.annotation.Bean)

Example 5 with ResolvedTarget

use of org.cloudfoundry.promregator.scanner.ResolvedTarget in project promregator by promregator.

the class CFMultiDiscovererTest method testDiscoverWithCleanup.

@Test
void testDiscoverWithCleanup() throws InterruptedException {
    List<ResolvedTarget> resolvedTargets = new ArrayList<>();
    ResolvedTarget aTarget = new ResolvedTarget();
    aTarget.setOrgName("unittestorg");
    aTarget.setSpaceName("unittestspace");
    aTarget.setApplicationName("testapp");
    aTarget.setApplicationId(CFAccessorMock.UNITTEST_APP1_UUID);
    aTarget.setProtocol("https");
    aTarget.setPath("/metrics");
    aTarget.setOriginalTarget(new Target());
    resolvedTargets.add(aTarget);
    when(targetResolver.resolveTargets(any())).thenReturn(resolvedTargets);
    List<Instance> result = this.cfDiscoverer.discover(null, null);
    Assertions.assertEquals(2, result.size());
    boolean testapp1_instance1 = false;
    boolean testapp1_instance2 = false;
    Instance i1 = null;
    Instance i2 = null;
    for (Instance instance : result) {
        String instanceId = instance.getInstanceId();
        if (instanceId.equals(CFAccessorMock.UNITTEST_APP1_UUID + ":0")) {
            testapp1_instance1 = true;
            Assertions.assertEquals("https://hostapp1.shared.domain.example.org/metrics", instance.getAccessUrl());
            i1 = instance;
        } else if (instanceId.equals(CFAccessorMock.UNITTEST_APP1_UUID + ":1")) {
            testapp1_instance2 = true;
            Assertions.assertEquals("https://hostapp1.shared.domain.example.org/metrics", instance.getAccessUrl());
            i2 = instance;
        } else if (instanceId.equals(CFAccessorMock.UNITTEST_APP2_UUID + ":0")) {
            Assertions.fail("Should not have been returned");
        }
    }
    Assertions.assertTrue(testapp1_instance1);
    Assertions.assertTrue(testapp1_instance2);
    Assertions.assertNotNull(i1);
    Assertions.assertNotNull(i2);
    Assertions.assertTrue(this.cfDiscoverer.isInstanceRegistered(i1));
    Assertions.assertTrue(this.cfDiscoverer.isInstanceRegistered(i2));
    Assertions.assertTrue(this.removerTriggerForInstances.isEmpty());
    // early cleaning does not change anything
    this.cfDiscoverer.cleanup();
    Assertions.assertTrue(this.cfDiscoverer.isInstanceRegistered(i1));
    Assertions.assertTrue(this.cfDiscoverer.isInstanceRegistered(i2));
    // Wait a little to allow JMX do its job... (if it really did something)
    for (int i = 0; i < 10; i++) {
        if (!this.removerTriggerForInstances.isEmpty()) {
            Thread.sleep(100);
            continue;
        }
        Assertions.assertTrue(this.removerTriggerForInstances.isEmpty());
    }
    // later cleaning does...
    this.cfDiscoverer.setClock(Clock.offset(this.clock, Duration.ofMinutes(10)));
    this.cfDiscoverer.cleanup();
    Assertions.assertFalse(this.cfDiscoverer.isInstanceRegistered(i1));
    Assertions.assertFalse(this.cfDiscoverer.isInstanceRegistered(i2));
    for (int i = 0; i < 10; i++) {
        if (this.removerTriggerForInstances.isEmpty()) {
            Thread.sleep(400);
            continue;
        }
        Assertions.assertEquals(2, this.removerTriggerForInstances.size());
    }
}
Also used : Target(org.cloudfoundry.promregator.config.Target) ResolvedTarget(org.cloudfoundry.promregator.scanner.ResolvedTarget) Instance(org.cloudfoundry.promregator.scanner.Instance) ArrayList(java.util.ArrayList) ResolvedTarget(org.cloudfoundry.promregator.scanner.ResolvedTarget) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ResolvedTarget (org.cloudfoundry.promregator.scanner.ResolvedTarget)7 Instance (org.cloudfoundry.promregator.scanner.Instance)6 LinkedList (java.util.LinkedList)3 Nullable (javax.annotation.Nullable)3 AbstractMetricFamilySamplesEnricher (org.cloudfoundry.promregator.rewrite.AbstractMetricFamilySamplesEnricher)3 CFAllLabelsMetricFamilySamplesEnricher (org.cloudfoundry.promregator.rewrite.CFAllLabelsMetricFamilySamplesEnricher)3 Gauge (io.prometheus.client.Gauge)2 List (java.util.List)2 Predicate (java.util.function.Predicate)2 MetricsFetcherMetrics (org.cloudfoundry.promregator.fetcher.MetricsFetcherMetrics)2 NullMetricFamilySamplesEnricher (org.cloudfoundry.promregator.rewrite.NullMetricFamilySamplesEnricher)2 AppInstanceScanner (org.cloudfoundry.promregator.scanner.AppInstanceScanner)2 Test (org.junit.jupiter.api.Test)2 Bean (org.springframework.context.annotation.Bean)2 MetricFamilySamples (io.prometheus.client.Collector.MetricFamilySamples)1 ArrayList (java.util.ArrayList)1 AuthenticationEnricher (org.cloudfoundry.promregator.auth.AuthenticationEnricher)1 Target (org.cloudfoundry.promregator.config.Target)1 CFMetricsFetcher (org.cloudfoundry.promregator.fetcher.CFMetricsFetcher)1 CFMetricsFetcherConfig (org.cloudfoundry.promregator.fetcher.CFMetricsFetcherConfig)1