use of io.prometheus.client.Collector.MetricFamilySamples.Sample in project incubator-servicecomb-java-chassis by apache.
the class MetricsCollector method load.
private List<MetricFamilySamples> load() {
Map<String, Double> metrics = MonitorManager.getInstance().measure();
List<MetricFamilySamples> familySamples = new ArrayList<>();
List<Sample> samples = new ArrayList<>();
for (Entry<String, Double> metric : metrics.entrySet()) {
List<String> tagNames = new ArrayList<>();
List<String> tagValues = new ArrayList<>();
String name = metric.getKey();
if (metric.getKey().contains("(")) {
String[] nameAndTag = metric.getKey().split("\\(");
name = nameAndTag[0];
String[] tagAnValues = nameAndTag[1].split("[=,)]");
for (int i = 0; i < tagAnValues.length; i += 2) {
// we need put operation name in metrics name,not a label
if (MetricsConst.TAG_OPERATION.equals(tagAnValues[i])) {
name = name + "." + tagAnValues[i + 1];
} else if (!"type".equals(tagAnValues[i])) {
tagNames.add(tagAnValues[i]);
tagValues.add(tagAnValues[i + 1]);
}
}
}
samples.add(new Sample(formatMetricName(name), tagNames, tagValues, metric.getValue()));
}
familySamples.add(new MetricFamilySamples("ServiceComb Metrics", Type.UNTYPED, "ServiceComb Metrics", samples));
return familySamples;
}
use of io.prometheus.client.Collector.MetricFamilySamples.Sample in project bookkeeper by apache.
the class PrometheusTextFormatUtil method writeMetricsCollectedByPrometheusClient.
static void writeMetricsCollectedByPrometheusClient(Writer w, CollectorRegistry registry) throws IOException {
Enumeration<MetricFamilySamples> metricFamilySamples = registry.metricFamilySamples();
while (metricFamilySamples.hasMoreElements()) {
MetricFamilySamples metricFamily = metricFamilySamples.nextElement();
for (int i = 0; i < metricFamily.samples.size(); i++) {
Sample sample = metricFamily.samples.get(i);
w.write(sample.name);
w.write('{');
for (int j = 0; j < sample.labelNames.size(); j++) {
if (j != 0) {
w.write(", ");
}
w.write(sample.labelNames.get(j));
w.write("=\"");
w.write(sample.labelValues.get(j));
w.write('"');
}
w.write("} ");
w.write(Collector.doubleToGoString(sample.value));
w.write('\n');
}
}
}
use of io.prometheus.client.Collector.MetricFamilySamples.Sample in project incubator-pulsar by apache.
the class PrometheusMetricsGenerator method generateSystemMetrics.
private static void generateSystemMetrics(SimpleTextOutputStream stream, String cluster) {
Enumeration<MetricFamilySamples> metricFamilySamples = CollectorRegistry.defaultRegistry.metricFamilySamples();
while (metricFamilySamples.hasMoreElements()) {
MetricFamilySamples metricFamily = metricFamilySamples.nextElement();
for (int i = 0; i < metricFamily.samples.size(); i++) {
Sample sample = metricFamily.samples.get(i);
stream.write(sample.name);
stream.write("{cluster=\"").write(cluster).write('"');
for (int j = 0; j < sample.labelNames.size(); j++) {
stream.write(", ");
stream.write(sample.labelNames.get(j));
stream.write("=\"");
stream.write(sample.labelValues.get(j));
stream.write('"');
}
stream.write("} ");
stream.write(Collector.doubleToGoString(sample.value));
stream.write('\n');
}
}
}
use of io.prometheus.client.Collector.MetricFamilySamples.Sample in project promregator by promregator.
the class TextFormat004ParserTest method testSimpleMinusInf.
@Test
public void testSimpleMinusInf() {
String textToParse = "# Minimalistic line:\n" + "\n" + "metric_without_labels -Inf 123456789012345600\n";
TextFormat004Parser subject = new TextFormat004Parser(textToParse);
HashMap<String, Collector.MetricFamilySamples> resultMap = subject.parse();
Enumeration<Collector.MetricFamilySamples> result = Collections.enumeration(resultMap.values());
// creating expected result
LinkedList<Collector.MetricFamilySamples> expectedList = new LinkedList<>();
List<Sample> samples = new LinkedList<>();
Sample sample = new Sample("metric_without_labels", new LinkedList<String>(), new LinkedList<String>(), Double.NEGATIVE_INFINITY);
samples.add(sample);
Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("metric_without_labels", Type.UNTYPED, null, samples);
expectedList.add(expectedMFS);
Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
// compare
compareEMFS(expected, result);
}
use of io.prometheus.client.Collector.MetricFamilySamples.Sample in project promregator by promregator.
the class TextFormat004ParserTest method testHistogramWithLabels.
@Test
public void testHistogramWithLabels() {
String textToParse = "# A histogram, which has a pretty complex representation in the text format:\n" + "# HELP http_request_duration_seconds A histogram of the request duration.\n" + "# TYPE http_request_duration_seconds histogram\n" + "http_request_duration_seconds_bucket{name=\"value\",le=\"0.05\",} 24054\n" + "http_request_duration_seconds_bucket{name=\"value\",le=\"0.1\"} 33444\n" + "http_request_duration_seconds_bucket{le=\"0.2\",name=\"value\",} 100392\n" + "http_request_duration_seconds_bucket{le=\"0.5\",name=\"value\"} 129389\n" + "http_request_duration_seconds_bucket{name=\"value\",le=\"1\"} 133988\n" + "http_request_duration_seconds_bucket{name=\"value\",le=\"+Inf\"} 144320\n" + "http_request_duration_seconds_sum 53423\n" + "http_request_duration_seconds_count 144320";
TextFormat004Parser subject = new TextFormat004Parser(textToParse);
HashMap<String, Collector.MetricFamilySamples> resultMap = subject.parse();
Enumeration<Collector.MetricFamilySamples> result = Collections.enumeration(resultMap.values());
// creating expected result
LinkedList<Collector.MetricFamilySamples> expectedList = new LinkedList<>();
List<Sample> samples = new LinkedList<>();
Sample sample = null;
sample = createSampleForHistogramWithDummyLabel("http_request_duration_seconds_bucket", "0.05", 24054, true);
samples.add(sample);
sample = createSampleForHistogramWithDummyLabel("http_request_duration_seconds_bucket", "0.1", 33444, true);
samples.add(sample);
sample = createSampleForHistogramWithDummyLabel("http_request_duration_seconds_bucket", "0.2", 100392, false);
samples.add(sample);
sample = createSampleForHistogramWithDummyLabel("http_request_duration_seconds_bucket", "0.5", 129389, false);
samples.add(sample);
sample = createSampleForHistogramWithDummyLabel("http_request_duration_seconds_bucket", "1", 133988, true);
samples.add(sample);
sample = createSampleForHistogramWithDummyLabel("http_request_duration_seconds_bucket", "+Inf", 144320, true);
samples.add(sample);
sample = new Sample("http_request_duration_seconds_sum", new LinkedList<>(), new LinkedList<>(), 53423);
samples.add(sample);
sample = new Sample("http_request_duration_seconds_count", new LinkedList<>(), new LinkedList<>(), 144320);
samples.add(sample);
Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("http_request_duration_seconds", Type.HISTOGRAM, "A histogram of the request duration.", samples);
expectedList.add(expectedMFS);
Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
// compare
compareEMFS(expected, result);
}
Aggregations