Java Code Examples for io.prometheus.client.Collector#Type

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: 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 2
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 3
Source File: RMQMetricsServiceImpl.java    From rocketmq-exporter with Apache License 2.0 5 votes vote down vote up
private static String typeString(Collector.Type t) {
    switch (t) {
        case GAUGE:
            return "gauge";
        case COUNTER:
            return "counter";
        case SUMMARY:
            return "summary";
        case HISTOGRAM:
            return "histogram";
        default:
            return "untyped";
    }
}
 
Example 4
Source File: TextFormat.java    From andesite-node with MIT License 5 votes vote down vote up
private static String typeString(Collector.Type t) {
    switch(t) {
        case GAUGE:
            return "gauge";
        case COUNTER:
            return "counter";
        case SUMMARY:
            return "summary";
        case HISTOGRAM:
            return "histogram";
        default:
            return "untyped";
    }
}
 
Example 5
Source File: Parser.java    From promregator with Apache License 2.0 5 votes vote down vote up
private void parseMetric(String line) {
	final MetricLine ml = new MetricLine(line);
	
	Sample sample = null;
	try {
		sample = ml.parse();
	} catch (MetricLine.ParseException e) {
		log.warn(String.format("Detected non-parsable metric line '%s'", line), e);
		return;
	}
	
	final String metricName = sample.name;
	
	Collector.Type type = determineType(metricName);
	if (type == Type.UNTYPED) {
		log.info(String.format("Definition of metric %s without type information (assuming untyped)", metricName));
	}
	
	if (type.equals(Collector.Type.COUNTER) || type.equals(Collector.Type.GAUGE) || type.equals(Collector.Type.UNTYPED)) {
		this.storeSimpleType(sample, metricName, type);
	} else if (type.equals(Collector.Type.HISTOGRAM) || type.equals(Collector.Type.SUMMARY)) {
		this.storeComplexType(sample, metricName, type);
	} else {
		log.warn(String.format("Unknown type %s; unclear how to handle this; skipping", type.toString()));
		// return; can be skipped here
	}
}
 
Example 6
Source File: Parser.java    From promregator with Apache License 2.0 5 votes vote down vote up
private void parseTypeLine(String line) {
	Matcher m = PATTERN_PARSE_TYPE.matcher(line);
	if (!m.matches()) {
		log.warn("TYPE line could not be properly matched: "+line);
		return;
	}
	
	String metricName = Utils.unescapeToken(m.group(1));
	String typeString = m.group(2);
	
	Collector.Type type = null;
	if (typeString.equalsIgnoreCase("gauge")) {
		type = Collector.Type.GAUGE;
	} else if (typeString.equalsIgnoreCase("counter")) {
		type = Collector.Type.COUNTER;
	} else if (typeString.equalsIgnoreCase("summary")) {
		type = Collector.Type.SUMMARY;
	} else if (typeString.equalsIgnoreCase("histogram")) {
		type = Collector.Type.HISTOGRAM;
	} else if (typeString.equalsIgnoreCase("untyped")) {
		type = Collector.Type.UNTYPED;
	} else {
		log.warn("Unable to parse type from TYPE line: "+line);
		return;
	}
	
	this.mapTypes.put(metricName, type);
}
 
Example 7
Source File: PrometheusMetricsGenerator.java    From pulsar with Apache License 2.0 5 votes vote down vote up
static String getTypeStr(Collector.Type type) {
    switch (type) {
    case COUNTER:
        return "counter";
    case GAUGE:
        return "gauge";
    case SUMMARY        :
        return "summary";
    case HISTOGRAM:
        return "histogram";
    case UNTYPED:
    default:
        return "untyped";
    }
}
 
Example 8
Source File: TextFormat.java    From client_java with Apache License 2.0 5 votes vote down vote up
private static String typeString(Collector.Type t) {
  switch (t) {
    case GAUGE:
      return "gauge";
    case COUNTER:
      return "counter";
    case SUMMARY:
      return "summary";
    case HISTOGRAM:
      return "histogram";
    default:
      return "untyped";
  }
}
 
Example 9
Source File: PrometheusMeterRegistry.java    From micrometer with Apache License 2.0 4 votes vote down vote up
@Override
protected Meter newMeter(Meter.Id id, Meter.Type type, Iterable<Measurement> measurements) {
    Collector.Type promType = Collector.Type.UNTYPED;
    switch (type) {
        case COUNTER:
            promType = Collector.Type.COUNTER;
            break;
        case GAUGE:
            promType = Collector.Type.GAUGE;
            break;
        case DISTRIBUTION_SUMMARY:
        case TIMER:
            promType = Collector.Type.SUMMARY;
            break;
    }

    final Collector.Type finalPromType = promType;

    applyToCollector(id, (collector) -> {
        List<String> tagValues = tagValues(id);
        collector.add(tagValues, (conventionName, tagKeys) -> {
            List<String> statKeys = new LinkedList<>(tagKeys);
            statKeys.add("statistic");

            return Stream.of(new MicrometerCollector.Family(finalPromType, conventionName,
                    stream(measurements.spliterator(), false)
                            .map(m -> {
                                List<String> statValues = new LinkedList<>(tagValues);
                                statValues.add(m.getStatistic().toString());

                                String name = conventionName;
                                switch (m.getStatistic()) {
                                    case TOTAL:
                                    case TOTAL_TIME:
                                        name += "_sum";
                                        break;
                                    case MAX:
                                        name += "_max";
                                        break;
                                    case ACTIVE_TASKS:
                                        name += "_active_count";
                                        break;
                                    case DURATION:
                                        name += "_duration_sum";
                                        break;
                                }

                                return new Collector.MetricFamilySamples.Sample(name, statKeys, statValues, m.getValue());
                            })));
        });
    });

    return new DefaultMeter(id, type, measurements);
}
 
Example 10
Source File: PrometheusMetricsGenerator.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private static void parseMetricsToPrometheusMetrics(Collection<Metrics> metrics, String cluster,
                                                    Collector.Type metricType, SimpleTextOutputStream stream) {
    Set<String> names = new HashSet<>();
    for (Metrics metrics1 : metrics) {
        for (Map.Entry<String, Object> entry : metrics1.getMetrics().entrySet()) {
            String value = null;
            if (entry.getKey().contains(".")) {
                try {
                    String key = entry.getKey();
                    int dotIndex = key.indexOf(".");
                    int nameIndex = key.substring(0, dotIndex).lastIndexOf("_");
                    if (nameIndex == -1) {
                        continue;
                    }

                    String name = key.substring(0, nameIndex);
                    value = key.substring(nameIndex + 1);
                    if (!names.contains(name)) {
                        stream.write("# TYPE ").write(name.replace("brk_", "pulsar_")).write(' ')
                                .write(getTypeStr(metricType)).write("\n");
                        names.add(name);
                    }
                    stream.write(name.replace("brk_", "pulsar_"))
                            .write("{cluster=\"").write(cluster).write('"');
                } catch (Exception e) {
                    continue;
                }
            } else {
                stream.write("# TYPE ").write(entry.getKey().replace("brk_", "pulsar_")).write(' ')
                        .write(getTypeStr(metricType)).write('\n');
                stream.write(entry.getKey().replace("brk_", "pulsar_"))
                        .write("{cluster=\"").write(cluster).write('"');
            }

            for (Map.Entry<String, String> metric : metrics1.getDimensions().entrySet()) {
                if (metric.getKey().isEmpty() || "cluster".equals(metric.getKey())) {
                    continue;
                }
                stream.write(", ").write(metric.getKey()).write("=\"").write(metric.getValue()).write('"');
                if (value != null && !value.isEmpty()) {
                    stream.write(", ").write("quantile=\"").write(value).write('"');
                }
            }
            stream.write("} ").write(String.valueOf(entry.getValue()))
                    .write(' ').write(System.currentTimeMillis()).write("\n");
        }
    }
}