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"));
}
}
}
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;
}
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);
}
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;
}
};
}
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());
}
}
Aggregations