io.prometheus.client.Collector.Type Java Examples

The following examples show how to use io.prometheus.client.Collector.Type. 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: PrometheusExportUtils.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
static MetricFamilySamples createDescribableMetricFamilySamples(
    MetricDescriptor metricDescriptor, String namespace) {
  String name = getNamespacedName(metricDescriptor.getName(), namespace);
  Type type = getType(metricDescriptor.getType());
  List<String> labelNames = convertToLabelNames(metricDescriptor.getLabelKeys());

  if (containsDisallowedLeLabelForHistogram(labelNames, type)) {
    throw new IllegalStateException(
        "Prometheus Histogram cannot have a label named 'le', "
            + "because it is a reserved label for bucket boundaries. "
            + "Please remove this key from your view.");
  }

  if (containsDisallowedQuantileLabelForSummary(labelNames, type)) {
    throw new IllegalStateException(
        "Prometheus Summary cannot have a label named 'quantile', "
            + "because it is a reserved label. Please remove this key from your view.");
  }

  return new MetricFamilySamples(
      name, type, metricDescriptor.getDescription(), Collections.<Sample>emptyList());
}
 
Example #2
Source File: ParserTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleWithTimestampAndEmptyLine() {
	String textToParse = "# Minimalistic line:\n" + 
			"\n"+
			"metric_without_labels 12.47 123456789012345600\n";
	
	Parser subject = new Parser(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>(), 12.47);
	samples.add(sample);
	
	Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("metric_without_labels", Type.UNTYPED, "", samples);
	expectedList.add(expectedMFS);
	
	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
	
	// compare
	compareEMFS(expected, result);
}
 
Example #3
Source File: ParserTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimple() {
	String textToParse = "# Minimalistic line:\n" + 
			"metric_without_timestamp_and_labels 12.47\n";
	
	Parser subject = new Parser(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_timestamp_and_labels", new LinkedList<String>(), new LinkedList<String>(), 12.47);
	samples.add(sample);
	
	Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("metric_without_timestamp_and_labels", Type.UNTYPED, "", samples);
	expectedList.add(expectedMFS);
	
	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
	
	// compare
	compareEMFS(expected, result);
}
 
Example #4
Source File: ParserTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleWithEFormat() {
	String textToParse = "# Minimalistic line:\n" + 
			"\n"+
			"metric_without_labels 1.7560473e+07\n";
	
	Parser subject = new Parser(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>(), 1.7560473e+07);
	samples.add(sample);
	
	Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("metric_without_labels", Type.UNTYPED, "", samples);
	expectedList.add(expectedMFS);
	
	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
	
	// compare
	compareEMFS(expected, result);
}
 
Example #5
Source File: ParserTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimplePlusInf() {
	String textToParse = "# Minimalistic line:\n" + 
			"\n"+
			"metric_without_labels +Inf 123456789012345600\n";
	
	Parser subject = new Parser(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.POSITIVE_INFINITY);
	samples.add(sample);
	
	Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("metric_without_labels", Type.UNTYPED, "", samples);
	expectedList.add(expectedMFS);
	
	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
	
	// compare
	compareEMFS(expected, result);
}
 
Example #6
Source File: ParserTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleMinusInf() {
	String textToParse = "# Minimalistic line:\n" + 
			"\n"+
			"metric_without_labels -Inf 123456789012345600\n";
	
	Parser subject = new Parser(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, "", samples);
	expectedList.add(expectedMFS);
	
	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
	
	// compare
	compareEMFS(expected, result);
}
 
Example #7
Source File: GenericMetricFamilySamplesPrefixRewriterTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoesNotPrefixIfNotNeeded() {
	GenericMetricFamilySamplesPrefixRewriter subject = new GenericMetricFamilySamplesPrefixRewriter("prefix");
	
	List<Sample> samples = new LinkedList<>();
	Sample s = new Sample("prefix_dummyname", Arrays.asList(new String[] { "labelName" }), Arrays.asList(new String[] {"labelValue"}), 1.0);
	samples.add(s);
	
	MetricFamilySamples mfs = new MetricFamilySamples("prefix_dummyname", Type.GAUGE, "dummyHelp", samples);
	
	HashMap<String, MetricFamilySamples> map = new HashMap<>();
	map.put("prefix_metricName", mfs);

	HashMap<String,MetricFamilySamples> result = subject.determineEnumerationOfMetricFamilySamples(map);
	
	MetricFamilySamples mfsResult = result.get("prefix_metricName");
	Assert.assertNotNull(mfsResult);
	Assert.assertEquals("prefix_dummyname", mfsResult.name);
	
	Assert.assertEquals(1, mfsResult.samples.size());
	Sample sampleResult = mfsResult.samples.get(0);
	Assert.assertEquals("prefix_dummyname", sampleResult.name);
}
 
Example #8
Source File: GenericMetricFamilySamplesPrefixRewriterTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrefixesProperly() {
	GenericMetricFamilySamplesPrefixRewriter subject = new GenericMetricFamilySamplesPrefixRewriter("prefix");
	
	List<Sample> samples = new LinkedList<>();
	Sample s = new Sample("dummyname", Arrays.asList(new String[] { "labelName" }), Arrays.asList(new String[] {"labelValue"}), 1.0);
	samples.add(s);
	
	MetricFamilySamples mfs = new MetricFamilySamples("dummyname", Type.GAUGE, "dummyHelp", samples);
	
	HashMap<String, MetricFamilySamples> map = new HashMap<>();
	map.put("metricName", mfs);

	HashMap<String,MetricFamilySamples> result = subject.determineEnumerationOfMetricFamilySamples(map);
	
	MetricFamilySamples mfsResult = result.get("prefix_metricName");
	Assert.assertNotNull(mfsResult);
	Assert.assertEquals("prefix_dummyname", mfsResult.name);
	
	Assert.assertEquals(1, mfsResult.samples.size());
	Sample sampleResult = mfsResult.samples.get(0);
	Assert.assertEquals("prefix_dummyname", sampleResult.name);
}
 
Example #9
Source File: ParserTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testGaugeWithTimestampAndEmptyLine() {
	String textToParse = "# Simple metric without labels:\n" + 
			"# TYPE metric_without_labels gauge\n" + 
			"\n"+
			"metric_without_labels 12.47 123456789012345600\n";
	
	Parser subject = new Parser(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>(), 12.47);
	samples.add(sample);
	
	Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("metric_without_labels", Type.GAUGE, "", samples);
	expectedList.add(expectedMFS);
	
	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
	
	// compare
	compareEMFS(expected, result);
}
 
Example #10
Source File: Parser.java    From promregator with Apache License 2.0 6 votes vote down vote up
private Type determineType(String metricName) {
	Collector.Type type = null;
	// first check if the metric is typed natively.
	type = this.mapTypes.get(metricName);
	if (type != null) {
		return type;
	}
	
	// try to get the baseMetricName
	String baseMetricName = determineBaseMetricName(metricName);
	type = this.mapTypes.get(baseMetricName);
	// check that this also really makes sense and is a type, which requires baseMetricNames
	if (type == Type.HISTOGRAM || type == Type.SUMMARY) {
		return type;
	}
	
	// we have no clue what this metric is all about
	return Collector.Type.UNTYPED;
}
 
Example #11
Source File: Parser.java    From promregator with Apache License 2.0 6 votes vote down vote up
private void storeComplexType(Sample sample, final String metricName, Collector.Type type) {
	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.help must not be empty - see also  https://github.com/promregator/promregator/issues/73
		 */
		if (docString == null) {
			docString = "";
		}
		
		mfs = new Collector.MetricFamilySamples(baseMetricName, type, docString, new LinkedList<>());
		this.mapMFS.put(baseMetricName, mfs);
	}
	
	mfs.samples.add(sample);
}
 
Example #12
Source File: PrometheusExportUtils.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static Type getType(MetricDescriptor.Type type) {
  if (type == MetricDescriptor.Type.CUMULATIVE_INT64
      || type == MetricDescriptor.Type.CUMULATIVE_DOUBLE) {
    return Type.COUNTER;
  } else if (type == MetricDescriptor.Type.GAUGE_INT64
      || type == MetricDescriptor.Type.GAUGE_DOUBLE) {
    return Type.GAUGE;
  } else if (type == MetricDescriptor.Type.CUMULATIVE_DISTRIBUTION
      || type == MetricDescriptor.Type.GAUGE_DISTRIBUTION) {
    return Type.HISTOGRAM;
  } else if (type == MetricDescriptor.Type.SUMMARY) {
    return Type.SUMMARY;
  }
  return Type.UNTYPED;
}
 
Example #13
Source File: ParserTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testCounterWithTimestampAndEmptyLine() {
	String textToParse = "# Simple metric without labels:\n" + 
			"# TYPE metric_without_labels counter\n" + 
			"\n"+
			"metric_without_labels 12.47 123456789012345600\n";
	
	Parser subject = new Parser(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>(), 12.47);
	samples.add(sample);
	
	Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("metric_without_labels", Type.COUNTER, "", samples);
	expectedList.add(expectedMFS);
	
	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
	
	// compare
	compareEMFS(expected, result);
}
 
Example #14
Source File: ParserTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testHelp() {
	String textToParse = "# Simple metric without labels:\n" + 
			"# TYPE metric_without_labels counter\n" + 
			"# HELP metric_without_labels this is my help text\n" + 
			"metric_without_labels 12.47 123456789012345600\n";
	
	Parser subject = new Parser(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>(), 12.47);
	samples.add(sample);
	
	Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("metric_without_labels", Type.COUNTER, "this is my help text", samples);
	expectedList.add(expectedMFS);
	
	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
	
	// compare
	compareEMFS(expected, result);
}
 
Example #15
Source File: PrometheusExportUtilsTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void getType() {
  assertThat(PrometheusExportUtils.getType(MetricDescriptor.Type.CUMULATIVE_INT64))
      .isEqualTo(Type.COUNTER);
  assertThat(PrometheusExportUtils.getType(MetricDescriptor.Type.CUMULATIVE_DOUBLE))
      .isEqualTo(Type.COUNTER);
  assertThat(PrometheusExportUtils.getType(MetricDescriptor.Type.CUMULATIVE_DISTRIBUTION))
      .isEqualTo(Type.HISTOGRAM);
  assertThat(PrometheusExportUtils.getType(MetricDescriptor.Type.SUMMARY))
      .isEqualTo(Type.SUMMARY);
  assertThat(PrometheusExportUtils.getType(MetricDescriptor.Type.GAUGE_INT64))
      .isEqualTo(Type.GAUGE);
  assertThat(PrometheusExportUtils.getType(MetricDescriptor.Type.GAUGE_DOUBLE))
      .isEqualTo(Type.GAUGE);
  assertThat(PrometheusExportUtils.getType(MetricDescriptor.Type.GAUGE_DISTRIBUTION))
      .isEqualTo(Type.HISTOGRAM);
}
 
Example #16
Source File: PrometheusExportUtilsTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@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()));
}
 
Example #17
Source File: PrometheusExportUtilsTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void createDescribableMetricFamilySamples_WithNamespace() {
  String namespace1 = "myorg";
  assertThat(
          PrometheusExportUtils.createDescribableMetricFamilySamples(
              CUMULATIVE_METRIC_DESCRIPTOR, namespace1))
      .isEqualTo(
          new MetricFamilySamples(
              namespace1 + '_' + METRIC_NAME,
              Type.COUNTER,
              METRIC_DESCRIPTION,
              Collections.<Sample>emptyList()));

  String namespace2 = "opencensus/";
  assertThat(
          PrometheusExportUtils.createDescribableMetricFamilySamples(
              CUMULATIVE_METRIC_DESCRIPTOR, namespace2))
      .isEqualTo(
          new MetricFamilySamples(
              "opencensus_" + METRIC_NAME,
              Type.COUNTER,
              METRIC_DESCRIPTION,
              Collections.<Sample>emptyList()));
}
 
Example #18
Source File: PrometheusExportUtilsTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void createMetricFamilySamples_WithNamespace() {
  String namespace = "opencensus_";
  assertThat(PrometheusExportUtils.createMetricFamilySamples(LONG_METRIC, namespace))
      .isEqualTo(
          new MetricFamilySamples(
              namespace + METRIC_NAME,
              Type.COUNTER,
              METRIC_DESCRIPTION,
              Collections.singletonList(
                  new Sample(
                      namespace + METRIC_NAME,
                      Arrays.asList("k1", "k2"),
                      Arrays.asList("v1", "v2"),
                      123456789))));
}
 
Example #19
Source File: ParserTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testHelpEscaping() {
	String textToParse = "# Simple metric without labels:\n" + 
			"# TYPE metric_without_labels counter\n" + 
			"# HELP metric_without_labels this is my help text with \\\\ backslashes escaped \\\\ and escaped newline \\n\n" + 
			"metric_without_labels 12.47 123456789012345600\n";
	
	Parser subject = new Parser(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>(), 12.47);
	samples.add(sample);
	
	Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("metric_without_labels", Type.COUNTER, "this is my help text with \\ backslashes escaped \\ and escaped newline \n", samples);
	expectedList.add(expectedMFS);
	
	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
	
	// compare
	compareEMFS(expected, result);
}
 
Example #20
Source File: ParserTest.java    From promregator with Apache License 2.0 5 votes vote down vote up
@Test
public void testGaugeWithSingleLabelEscaping() {
	String textToParse = "# TYPE metric_with_label gauge\n" + 
			"metric_with_label{name=\"containing \\\" and \\\\ and \\n\"} 12.47\n";
	
	Parser subject = new Parser(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<>();
	
	List<String> labelNames = new LinkedList<>();
	labelNames.add("name");
	List<String> labelValues = new LinkedList<>();
	labelValues.add("containing \" and \\ and \n");
	
	Sample sample = new Sample("metric_with_label", labelNames, labelValues, 12.47);
	samples.add(sample);
	
	Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("metric_with_label", Type.GAUGE, "", samples);
	expectedList.add(expectedMFS);
	
	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
	
	// compare
	compareEMFS(expected, result);
}
 
Example #21
Source File: PrometheusStatsCollectorTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@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()));
}
 
Example #22
Source File: ParserTest.java    From promregator with Apache License 2.0 5 votes vote down vote up
@Test
public void testStandardExampleWithEscapingInLabels() {
	String textToParse = "# Escaping in label values:\n" + 
			"msdos_file_access_time_seconds{path=\"C:\\\\DIR\\\\FILE.TXT\",error=\"Cannot find file:\\n\\\"FILE.TXT\\\"\"} 1.458255915e9";
	
	Parser subject = new Parser(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<>();
	
	List<String> labelNames = new LinkedList<>();
	labelNames.add("path");
	labelNames.add("error");
	List<String> labelValues = new LinkedList<>();
	labelValues.add("C:\\DIR\\FILE.TXT");
	labelValues.add("Cannot find file:\n\"FILE.TXT\"");
	
	Sample sample = new Sample("msdos_file_access_time_seconds", labelNames, labelValues, 1.458255915e9);
	samples.add(sample);
	
	Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("msdos_file_access_time_seconds", Type.UNTYPED, "", samples);
	expectedList.add(expectedMFS);
	
	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
	
	// compare
	compareEMFS(expected, result);
}
 
Example #23
Source File: ParserTest.java    From promregator with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleWithLabelIncludingBraces() {
	String textToParse = "# Finally a summary, which has a complex representation, too:\n" +
			"rpc_duration_seconds{name=\"val/{ue}\",quantile=\"0.01\",} 3102\n";

	Parser subject = new Parser(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<>();

	List<String> labelNames = new LinkedList<>();
	labelNames.add("name");
	labelNames.add("quantile");
	List<String> labelValues = new LinkedList<>();
	labelValues.add("val/{ue}");
	labelValues.add("0.01");
	Sample sample = new Sample("rpc_duration_seconds", labelNames, labelValues, 3102);
	samples.add(sample);

	Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("rpc_duration_seconds", Type.UNTYPED, "", samples);
	expectedList.add(expectedMFS);

	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);

	// compare
	compareEMFS(expected, result);
}
 
Example #24
Source File: ParserTest.java    From promregator with Apache License 2.0 5 votes vote down vote up
@Test
public void testVariant1() throws IOException, URISyntaxException {
	String textToParse = new String(Files.readAllBytes(Paths.get(getClass().getResource("text004-variant1.txt").toURI())));
	
	Parser subject = new Parser(textToParse);
	HashMap<String, Collector.MetricFamilySamples> resultMap = subject.parse();
	
	// ensure that all metrics are understood
	for (MetricFamilySamples mfs : resultMap.values()) {
		Assert.assertNotEquals(Type.UNTYPED, mfs.type);
	}
	
}
 
Example #25
Source File: PrometheusExportUtils.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
static MetricFamilySamples createMetricFamilySamples(Metric metric, String namespace) {
  MetricDescriptor metricDescriptor = metric.getMetricDescriptor();
  String name = getNamespacedName(metricDescriptor.getName(), namespace);
  Type type = getType(metricDescriptor.getType());
  List<String> labelNames = convertToLabelNames(metricDescriptor.getLabelKeys());
  List<Sample> samples = Lists.newArrayList();

  for (io.opencensus.metrics.export.TimeSeries timeSeries : metric.getTimeSeriesList()) {
    for (io.opencensus.metrics.export.Point point : timeSeries.getPoints()) {
      samples.addAll(getSamples(name, labelNames, timeSeries.getLabelValues(), point.getValue()));
    }
  }
  return new MetricFamilySamples(name, type, metricDescriptor.getDescription(), samples);
}
 
Example #26
Source File: PrometheusExportUtils.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
static boolean containsDisallowedLeLabelForHistogram(List<String> labelNames, Type type) {
  if (!Type.HISTOGRAM.equals(type)) {
    return false;
  }
  for (String label : labelNames) {
    if (LABEL_NAME_BUCKET_BOUND.equals(label)) {
      return true;
    }
  }
  return false;
}
 
Example #27
Source File: PrometheusExportUtils.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
static boolean containsDisallowedQuantileLabelForSummary(List<String> labelNames, Type type) {
  if (!Type.SUMMARY.equals(type)) {
    return false;
  }
  for (String label : labelNames) {
    if (LABEL_NAME_QUANTILE.equals(label)) {
      return true;
    }
  }
  return false;
}
 
Example #28
Source File: PrometheusStatsCollectorTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDescribe() {
  PrometheusStatsCollector collector =
      new PrometheusStatsCollector(mockMetricProducerManager, "");
  assertThat(collector.describe())
      .containsExactly(
          new MetricFamilySamples(
              METRIC_NAME, Type.HISTOGRAM, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
}
 
Example #29
Source File: MetricFormatter.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Return the type of metric
 *
 * @param type Metric Prometheus type
 */
private static String typeString(Type type) {
    switch (type) {
        case GAUGE:
            return "gauge";
        case COUNTER:
            return "counter";
        case SUMMARY:
            return "summary";
        case HISTOGRAM:
            return "histogram";
        default:
            return "untyped";
    }
}
 
Example #30
Source File: MetricAdapterTest.java    From opentelemetry-java with Apache License 2.0 5 votes vote down vote up
@Test
public void toMetricFamilySamples() {
  Descriptor descriptor =
      Descriptor.create(
          "name", "description", "1", Descriptor.Type.MONOTONIC_DOUBLE, Labels.of("kc", "vc"));

  MetricData metricData =
      MetricData.create(
          descriptor,
          Resource.create(Attributes.of("kr", AttributeValue.stringAttributeValue("vr"))),
          InstrumentationLibraryInfo.create("full", "version"),
          Collections.singletonList(
              MetricData.DoublePoint.create(123, 456, Labels.of("kp", "vp"), 5)));

  assertThat(MetricAdapter.toMetricFamilySamples(metricData))
      .isEqualTo(
          new MetricFamilySamples(
              "full_name",
              Type.COUNTER,
              descriptor.getDescription(),
              ImmutableList.of(
                  new Sample(
                      "full_name",
                      ImmutableList.of("kc", "kp"),
                      ImmutableList.of("vc", "vp"),
                      5))));
}