use of io.prometheus.client.Collector.MetricFamilySamples.Sample in project promregator by promregator.
the class TextFormat004Parser method parseMetric.
private void parseMetric(String line) {
Matcher mMetricName = PATTERN_TOKEN_WITH_SPACE_SEPARATOR.matcher(line);
if (!mMetricName.find()) {
log.warn("Detected metric line without proper metric name: " + line);
return;
}
String metricName = mMetricName.group(1);
String rest = line.substring(mMetricName.end());
// skip spaces if there...
Matcher mSkipSpaces = PATTERN_SKIP_SPACES.matcher(rest);
if (mSkipSpaces.find()) {
rest = rest.substring(mSkipSpaces.end());
}
// check if the metric has an optional block of labels
Matcher mLabelBlock = PATTERN_PARSE_LABELBLOCK.matcher(rest);
Labels labels = null;
if (mLabelBlock.find()) {
labels = this.parseLabelBlock(mLabelBlock.group(1));
rest = rest.substring(mLabelBlock.end());
mSkipSpaces = PATTERN_SKIP_SPACES.matcher(rest);
if (mSkipSpaces.find()) {
rest = rest.substring(mSkipSpaces.end());
}
}
double value = 0.0f;
String valueString = null;
Matcher mValueText = PATTERN_PARSE_VALUETEXT.matcher(rest);
int end = 0;
if (mValueText.find()) {
valueString = mValueText.group(1);
// NB: an exception cannot be thrown here (and the exception in fact is an Error)
value = this.parseGoDouble(valueString);
end = mValueText.end();
} else {
Matcher mValue = PATTERN_PARSE_VALUE.matcher(rest);
if (!mValue.find()) {
log.warn("Unable to parse value in metric line: " + line);
return;
}
valueString = mValue.group(1);
try {
value = this.parseGoDouble(valueString);
} catch (NumberFormatException nfe) {
log.warn("Unable to parse value in metrics line properly: " + line, nfe);
return;
}
end = mValue.end();
}
rest = rest.substring(end);
/*
* currently not supported in java simpleclient!
*/
// optional timestamp
/*
double timestamp = 0.0;
if (!"".equals(rest)) {
try {
timestamp = this.parseGoDouble(rest);
} catch (NumberFormatException nfe) {
log.warn("Unable to parse timestamp in metrics line properly: "+line, nfe);
return;
}
}*/
Collector.Type type = determineType(metricName);
List<String> labelNames = labels == null ? new LinkedList<String>() : labels.getNames();
List<String> labelValues = labels == null ? new LinkedList<String>() : labels.getValues();
Sample sample = new Sample(metricName, labelNames, labelValues, value);
if (type.equals(Collector.Type.COUNTER) || type.equals(Collector.Type.GAUGE) || type.equals(Collector.Type.UNTYPED)) {
List<Sample> samples = new LinkedList<Sample>();
samples.add(sample);
String docString = this.mapHelps.get(metricName);
Collector.MetricFamilySamples mfs = new Collector.MetricFamilySamples(metricName, type, docString, samples);
this.mapMFS.put(metricName, mfs);
} else if (type.equals(Collector.Type.HISTOGRAM)) {
String baseMetricName = determineBaseMetricName(metricName);
// is this already in our Map?
Collector.MetricFamilySamples mfs = this.mapMFS.get(baseMetricName);
if (mfs == null) {
// no, we have to create a new one
String docString = this.mapHelps.get(baseMetricName);
mfs = new Collector.MetricFamilySamples(baseMetricName, type, docString, new LinkedList<>());
this.mapMFS.put(baseMetricName, mfs);
}
mfs.samples.add(sample);
} else if (type.equals(Collector.Type.SUMMARY)) {
String baseMetricName = determineBaseMetricName(metricName);
// is this already in our Map?
Collector.MetricFamilySamples mfs = this.mapMFS.get(baseMetricName);
if (mfs == null) {
// no, we have to create a new one
String docString = this.mapHelps.get(baseMetricName);
mfs = new Collector.MetricFamilySamples(baseMetricName, type, docString, new LinkedList<>());
this.mapMFS.put(baseMetricName, mfs);
}
mfs.samples.add(sample);
} else {
log.warn(String.format("Unknown type %s; unclear how to handle this; skipping", type.toString()));
return;
}
}
use of io.prometheus.client.Collector.MetricFamilySamples.Sample in project promregator by promregator.
the class MergableMetricFamilySamplesTest method testStraightFowardEnumeration.
@Test
public void testStraightFowardEnumeration() {
MergableMetricFamilySamples subject = new MergableMetricFamilySamples();
List<Sample> samples = new LinkedList<>();
MetricFamilySamples mfs = new MetricFamilySamples("dummy", Type.COUNTER, "somehelp", samples);
List<MetricFamilySamples> list = new LinkedList<>();
list.add(mfs);
Enumeration<MetricFamilySamples> emfs = new Vector<MetricFamilySamples>(list).elements();
subject.merge(emfs);
Enumeration<MetricFamilySamples> returnedEMFS = subject.getEnumerationMetricFamilySamples();
Assert.assertTrue(returnedEMFS.hasMoreElements());
MetricFamilySamples element = returnedEMFS.nextElement();
Assert.assertFalse(returnedEMFS.hasMoreElements());
Assert.assertEquals(mfs, element);
HashMap<String, MetricFamilySamples> returnedHMMFS = subject.getEnumerationMetricFamilySamplesInHashMap();
Assert.assertEquals(1, returnedHMMFS.size());
Assert.assertEquals(mfs, returnedHMMFS.get("dummy"));
}
use of io.prometheus.client.Collector.MetricFamilySamples.Sample in project instrumentation-java by census-instrumentation.
the class PrometheusStatsCollectorTest method testDescribe_WithNamespace.
@Test
public void testDescribe_WithNamespace() {
String namespace = "myorg";
PrometheusStatsCollector collector = new PrometheusStatsCollector(mockMetricProducerManager, namespace);
assertThat(collector.describe()).containsExactly(new MetricFamilySamples(namespace + '_' + METRIC_NAME, Type.HISTOGRAM, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
}
use of io.prometheus.client.Collector.MetricFamilySamples.Sample in project instrumentation-java by census-instrumentation.
the class PrometheusStatsCollectorTest method testDescribe.
@Test
public void testDescribe() {
PrometheusStatsCollector collector = new PrometheusStatsCollector(mockMetricProducerManager, "");
assertThat(collector.describe()).containsExactly(new MetricFamilySamples(METRIC_NAME, Type.HISTOGRAM, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
}
use of io.prometheus.client.Collector.MetricFamilySamples.Sample in project instrumentation-java by census-instrumentation.
the class PrometheusExportUtilsTest method createDescribableMetricFamilySamples.
@Test
public void createDescribableMetricFamilySamples() {
assertThat(PrometheusExportUtils.createDescribableMetricFamilySamples(CUMULATIVE_METRIC_DESCRIPTOR, "")).isEqualTo(new MetricFamilySamples(METRIC_NAME, Type.COUNTER, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
assertThat(PrometheusExportUtils.createDescribableMetricFamilySamples(SUMMARY_METRIC_DESCRIPTOR, "")).isEqualTo(new MetricFamilySamples(METRIC_NAME2, Type.SUMMARY, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
assertThat(PrometheusExportUtils.createDescribableMetricFamilySamples(HISTOGRAM_METRIC_DESCRIPTOR, "")).isEqualTo(new MetricFamilySamples(METRIC_NAME3, Type.HISTOGRAM, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
}
Aggregations