Java Code Examples for io.prometheus.client.Gauge#Child

The following examples show how to use io.prometheus.client.Gauge#Child . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrometheusHttpConsumerMetrics.java    From garmadon with Apache License 2.0 6 votes vote down vote up
protected static void exposeKafkaMetrics() {
    try {
        for (Map.Entry<ObjectName, BiFunction<ObjectName, MBeanAttributeInfo, Gauge.Child>> entry : kafkaJmxNameToPrometheusLabeler.entrySet()) {
            // jmx object to expose
            ObjectName baseKafkaJmxName = entry.getKey();
            // callback to build a prometheus metric with labels
            BiFunction<ObjectName, MBeanAttributeInfo, Gauge.Child> prometheusMetricLabelsFromObjectNameAndAttr = entry.getValue();

            for (ObjectName name : MBS.queryNames(baseKafkaJmxName, null)) {
                MBeanInfo info = MBS.getMBeanInfo(name);
                MBeanAttributeInfo[] attrInfo = info.getAttributes();
                for (MBeanAttributeInfo attr : attrInfo) {
                    if (attr.isReadable() && attr.getType().equals("double")) {
                        // create metrics target labels and set the corresponding value
                        prometheusMetricLabelsFromObjectNameAndAttr.apply(name, attr)
                                .set((Double) MBS.getAttribute(name, attr.getName()));
                    }
                }
            }
        }
    } catch (InstanceNotFoundException | IntrospectionException | ReflectionException | MBeanException | AttributeNotFoundException e) {
        LOGGER.error(e.getMessage(), e);
    }
}
 
Example 2
Source File: ReaderFactory.java    From garmadon with Apache License 2.0 6 votes vote down vote up
private GarmadonReader.GarmadonMessageHandler buildGarmadonMessageHandler(PartitionedWriter<Message> writer,
                                                                          String eventName) {
    return msg -> {
        final CommittableOffset offset = msg.getCommittableOffset();
        final Counter.Child messagesWritingFailures = PrometheusMetrics.messageWritingFailuresCounter(eventName, offset.getPartition());
        final Counter.Child messagesWritten = PrometheusMetrics.messageWrittenCounter(eventName, offset.getPartition());

        Gauge.Child gauge = PrometheusMetrics.currentRunningOffsetsGauge(eventName, offset.getPartition());
        gauge.set(offset.getOffset());

        try {
            writer.write(Instant.ofEpochMilli(msg.getTimestamp()), offset, msg.toProto());

            messagesWritten.inc();
        } catch (IOException e) {
            // We accept losing messages every now and then, but still log failures
            messagesWritingFailures.inc();
            LOGGER.warn("Couldn't write a message", e);
        }
    };
}
 
Example 3
Source File: PrometheusMetrics.java    From garmadon with Apache License 2.0 5 votes vote down vote up
public static Gauge.Child latestCommittedTimestampGauge(String eventName, int partition) {
    Gauge.Child gauge = buildChild(LATEST_COMMITTED_TIMESTAMPS, partition, eventName, String.valueOf(partition));
    return new Gauge.Child() {
        @Override
        public void set(double val) {
            gauge.set(Double.max(gauge.get(), val));
        }

        @Override
        public double get() {
            return gauge.get();
        }
    };
}
 
Example 4
Source File: MetricsFetcherSimulator.java    From promregator with Apache License 2.0 5 votes vote down vote up
public MetricsFetcherSimulator(String accessURL, AuthenticationEnricher ae,
		AbstractMetricFamilySamplesEnricher mfse, MetricsFetcherMetrics mfm, Gauge.Child up) {
			this.accessURL = accessURL;
			this.ae = ae;
			this.mfse = mfse;
			this.mfm = mfm;
			this.up = up;
	
}
 
Example 5
Source File: PrometheusGaugeMetrics.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void inc() {
    Gauge.Child metrics = this.getMetric();
    if (metrics != null) {
        metrics.inc();
    }
}
 
Example 6
Source File: PrometheusGaugeMetrics.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void inc(double value) {
    Gauge.Child metrics = this.getMetric();
    if (metrics != null) {
        metrics.inc(value);
    }
}
 
Example 7
Source File: PrometheusGaugeMetrics.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void dec() {
    Gauge.Child metrics = this.getMetric();
    if (metrics != null) {
        metrics.dec();
    }
}
 
Example 8
Source File: PrometheusGaugeMetrics.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void dec(double value) {
    Gauge.Child metrics = this.getMetric();
    if (metrics != null) {
        metrics.dec(value);
    }
}
 
Example 9
Source File: PrometheusGaugeMetrics.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void setValue(double value) {
    Gauge.Child metrics = this.getMetric();
    if (metrics != null) {
        metrics.set(value);
    }
}
 
Example 10
Source File: PrometheusHttpConsumerMetrics.java    From garmadon with Apache License 2.0 4 votes vote down vote up
private static BiFunction<ObjectName, MBeanAttributeInfo, Gauge.Child> getKafkaMetricConsumerFromGauge(Gauge gauge) {
    return (name, attr) -> {
        String clientId = name.getKeyProperty("client-id");
        return gauge.labels(attr.getName(), GarmadonReader.getHostname(), RELEASE, clientId);
    };
}
 
Example 11
Source File: PrometheusMetrics.java    From garmadon with Apache License 2.0 4 votes vote down vote up
public static Gauge.Child currentRunningOffsetsGauge(String eventName, int partition) {
    return buildChild(CURRENT_RUNNING_OFFSETS, partition, eventName, String.valueOf(partition));
}
 
Example 12
Source File: PrometheusMetrics.java    From garmadon with Apache License 2.0 4 votes vote down vote up
public static Gauge.Child latestCommittedOffsetGauge(String eventName, int partition) {
    return buildChild(LATEST_COMMITTED_OFFSETS, partition, eventName, String.valueOf(partition));
}
 
Example 13
Source File: CFMetricsFetcherConfig.java    From promregator with Apache License 2.0 4 votes vote down vote up
/**
 * @return the upChild
 */
public Gauge.Child getUpChild() {
	return upChild;
}
 
Example 14
Source File: CFMetricsFetcherConfig.java    From promregator with Apache License 2.0 4 votes vote down vote up
/**
 * @param upChild the upChild to set
 */
public void setUpChild(Gauge.Child upChild) {
	this.upChild = upChild;
}
 
Example 15
Source File: AbstractMetricsEndpoint.java    From promregator with Apache License 2.0 4 votes vote down vote up
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);
		}
		callablesList.add(mf);
	}
	
	return callablesList;
}