本文整理汇总了Java中io.prometheus.client.Collector类的典型用法代码示例。如果您正苦于以下问题:Java Collector类的具体用法?Java Collector怎么用?Java Collector使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Collector类属于io.prometheus.client包,在下文中一共展示了Collector类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateSystemMetrics
import io.prometheus.client.Collector; //导入依赖的package包/类
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(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');
}
}
}
示例2: collectorByName
import io.prometheus.client.Collector; //导入依赖的package包/类
private MicrometerCollector collectorByName(Meter.Id id, Collector.Type type) {
return collectorMap.compute(getConventionName(id), (name, existingCollector) -> {
if(existingCollector == null) {
return new MicrometerCollector(id, type, config().namingConvention(), prometheusConfig).register(registry);
}
List<String> tagKeys = getConventionTags(id).stream().map(Tag::getKey).collect(toList());
if(existingCollector.getTagKeys().equals(tagKeys)) {
return existingCollector;
}
throw new IllegalArgumentException("Prometheus requires that all meters with the same name have the same" +
" set of tag keys. There is already an existing meter containing tag keys [" +
existingCollector.getTagKeys().stream().collect(joining(", ")) + "]. The meter you are attempting to register" +
" has keys [" + tagKeys.stream().collect(joining(", ")) + "].");
});
}
示例3: record
import io.prometheus.client.Collector; //导入依赖的package包/类
public void record(String method, String requestUri, int statusCode, long start) {
long duration = System.nanoTime() - start;
if (!config.shouldRecord(requestUri)) {
return;
}
String path = config.groupUrl(requestUri).orElse(normalize(requestUri, isClient));
requests.labels(
method,
String.valueOf(statusCode),
HttpStatusSeries.valueOf(statusCode).name(),
path
).observe(duration / Collector.NANOSECONDS_PER_SECOND);
}
示例4: doGet
import io.prometheus.client.Collector; //导入依赖的package包/类
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
throws ServletException, IOException {
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType(TextFormat.CONTENT_TYPE_004);
Writer writer = resp.getWriter();
Enumeration<Collector.MetricFamilySamples> metricsWithDuplicates = registry.metricFamilySamples();
List<String> names = new ArrayList<String>();
List<Collector.MetricFamilySamples> metricsWithoutDuplicates = new ArrayList<Collector.MetricFamilySamples>();
while (metricsWithDuplicates.hasMoreElements()) {
MetricFamilySamples metric = metricsWithDuplicates.nextElement();
if (!names.contains(metric.name)) {
metricsWithoutDuplicates.add(metric);
names.add(metric.name);
}
}
TextFormat.write004(writer, Collections.enumeration(metricsWithoutDuplicates));
writer.flush();
writer.close();
}
示例5: createPrometheusCollector
import io.prometheus.client.Collector; //导入依赖的package包/类
private Collector createPrometheusCollector() {
final io.prometheus.client.Counter counter = io.prometheus.client.Counter.build()
.name("Counter" + NAME).help(HELP).labelNames("label1", "label2")
.register();
final io.prometheus.client.Histogram histogram = io.prometheus.client.Histogram.build()
.name("Histogram" + NAME).help(HELP).labelNames("label1", "label2").linearBuckets(0, 1000, 100)
.register();
for (int i = 0; i < 100; i++) {
counter.labels("val", "val" + i).inc(i);
histogram.labels("val", "val" + i).observe(i);
}
return new Collector() {
@Override
public List<MetricFamilySamples> collect() {
final List<MetricFamilySamples> samples = new ArrayList<>();
samples.addAll(counter.collect());
samples.addAll(histogram.collect());
return samples;
}
};
}
示例6: countSamples
import io.prometheus.client.Collector; //导入依赖的package包/类
public static int countSamples(
String metricName, String sampleName, CollectorRegistry collectorRegistry) {
Enumeration<Collector.MetricFamilySamples> samples = collectorRegistry.metricFamilySamples();
while (samples.hasMoreElements()) {
Collector.MetricFamilySamples sample = samples.nextElement();
if (sample.name.equals(metricName)) {
int result = 0;
for (Collector.MetricFamilySamples.Sample s : sample.samples) {
if (s.name.equals(sampleName)) {
++result;
}
}
return result;
}
}
throw new IllegalArgumentException("Could not find sample family with name: " + metricName);
}
示例7: serverStreamRpcMetrics
import io.prometheus.client.Collector; //导入依赖的package包/类
@Test
public void serverStreamRpcMetrics() throws Throwable {
createClientStub(CHEAP_METRICS).sayHelloServerStream(REQUEST, responseRecorder);
responseRecorder.awaitCompletion();
assertThat(extractMetricValue("grpc_client_started_total")).isWithin(0).of(1);
assertThat(extractMetricValue("grpc_client_msg_received_total")).isWithin(0).of(1);
assertThat(findRecordedMetricOrThrow("grpc_client_msg_sent_total").samples).isEmpty();
Collector.MetricFamilySamples handled = findRecordedMetricOrThrow("grpc_client_completed");
assertThat(handled.samples).hasSize(1);
assertThat(handled.samples.get(0).labelValues).containsExactly(
"SERVER_STREAMING",
HelloServiceImpl.SERVICE_NAME,
HelloServiceImpl.SERVER_STREAM_METHOD_NAME,
"OK");
assertThat(handled.samples.get(0).value).isWithin(0).of(1);
}
开发者ID:grpc-ecosystem,项目名称:java-grpc-prometheus,代码行数:19,代码来源:MonitoringClientInterceptorIntegrationTest.java
示例8: bidiStreamRpcMetrics
import io.prometheus.client.Collector; //导入依赖的package包/类
@Test
public void bidiStreamRpcMetrics() throws Throwable {
StreamObserver<HelloProto.HelloRequest> requestStream =
createClientStub(CHEAP_METRICS).sayHelloBidiStream(responseRecorder);
requestStream.onNext(REQUEST);
requestStream.onNext(REQUEST);
requestStream.onCompleted();
responseRecorder.awaitCompletion();
assertThat(extractMetricValue("grpc_client_started_total")).isWithin(0).of(1);
assertThat(extractMetricValue("grpc_client_msg_received_total")).isWithin(0).of(2);
assertThat(extractMetricValue("grpc_client_msg_sent_total")).isWithin(0).of(2);
Collector.MetricFamilySamples handled = findRecordedMetricOrThrow("grpc_client_completed");
assertThat(handled.samples).hasSize(1);
assertThat(handled.samples.get(0).labelValues).containsExactly(
"BIDI_STREAMING",
HelloServiceImpl.SERVICE_NAME,
HelloServiceImpl.BIDI_STREAM_METHOD_NAME,
"OK");
assertThat(handled.samples.get(0).value).isWithin(0).of(1);
}
开发者ID:grpc-ecosystem,项目名称:java-grpc-prometheus,代码行数:24,代码来源:MonitoringClientInterceptorIntegrationTest.java
示例9: convertName
import io.prometheus.client.Collector; //导入依赖的package包/类
@Override
public String convertName(String springName) {
String prometheusName = prometheusMetricNames.get(springName);
if (null == prometheusName) {
prometheusName = Collector.sanitizeMetricName(springName);
Optional<String> suffixKey = typeSuffixs.keySet().stream()
.filter(startWith -> springName.startsWith(startWith)).findFirst();
if (suffixKey.isPresent()) {
String type = typeSuffixs.get(suffixKey.get());
prometheusName += "_" + type;
debug("Generated [" + prometheusName + "] by suffix [" + suffixKey.get() + "]");
} else {
debug("No preset prometeus name for [" + springName + "]. Generated [" + prometheusName + "]");
}
prometheusMetricNames.put(springName, prometheusName);
}
return prometheusName;
}
开发者ID:akaGelo,项目名称:spring-boot-starter-prometheus,代码行数:22,代码来源:DefaultPrometeusMetricNameConverter.java
示例10: createCollector
import io.prometheus.client.Collector; //导入依赖的package包/类
private static Collector createCollector(Metric metric, List<String> dimensionKeys, List<String> dimensionValues, String scopedMetricName, String helpString) {
Collector collector;
if (metric instanceof Gauge || metric instanceof Counter || metric instanceof Meter) {
collector = io.prometheus.client.Gauge
.build()
.name(scopedMetricName)
.help(helpString)
.labelNames(toArray(dimensionKeys))
.create();
} else if (metric instanceof Histogram) {
collector = new HistogramSummaryProxy((Histogram) metric, scopedMetricName, helpString, dimensionKeys, dimensionValues);
} else {
LOG.warn("Cannot create collector for unknown metric type: {}. This indicates that the metric type is not supported by this reporter.",
metric.getClass().getName());
collector = null;
}
return collector;
}
示例11: notifyOfRemovedMetric
import io.prometheus.client.Collector; //导入依赖的package包/类
@Override
public void notifyOfRemovedMetric(final Metric metric, final String metricName, final MetricGroup group) {
final String scopedMetricName = getScopedName(metricName, group);
synchronized (this) {
final AbstractMap.SimpleImmutableEntry<Collector, Integer> collectorWithCount = collectorsWithCountByMetricName.get(scopedMetricName);
final Integer count = collectorWithCount.getValue();
final Collector collector = collectorWithCount.getKey();
if (count == 1) {
try {
CollectorRegistry.defaultRegistry.unregister(collector);
} catch (Exception e) {
LOG.warn("There was a problem unregistering metric {}.", scopedMetricName, e);
}
collectorsWithCountByMetricName.remove(scopedMetricName);
} else {
collectorsWithCountByMetricName.put(scopedMetricName, new AbstractMap.SimpleImmutableEntry<>(collector, count - 1));
}
}
}
示例12: push
import io.prometheus.client.Collector; //导入依赖的package包/类
/**
* Push samples from the given registry to Graphite.
*/
public void push(CollectorRegistry registry) throws IOException {
Socket s = new Socket(host, port);
BufferedWriter writer = new BufferedWriter(new PrintWriter(s.getOutputStream()));
Matcher m = INVALID_GRAPHITE_CHARS.matcher("");
long now = System.currentTimeMillis() / 1000;
for (Collector.MetricFamilySamples metricFamilySamples: Collections.list(registry.metricFamilySamples())) {
for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) {
m.reset(sample.name);
writer.write(m.replaceAll("_"));
for (int i = 0; i < sample.labelNames.size(); ++i) {
m.reset(sample.labelValues.get(i));
writer.write("." + sample.labelNames.get(i) + "." + m.replaceAll("_"));
}
writer.write(" " + sample.value + " " + now + "\n");
}
}
writer.close();
s.close();
}
示例13: getValues
import io.prometheus.client.Collector; //导入依赖的package包/类
/**
* @see MetricMBean#getValues()
*/
@Override
public Map<Map<String, String>, Double> getValues() {
Map<Map<String, String>, Double> result = new HashMap<>();
for (Collector.MetricFamilySamples samples : metric.collect()) {
for (Collector.MetricFamilySamples.Sample sample : samples.samples) {
Map<String, String> labels = new HashMap<>();
for (int i = 0; i < sample.labelNames.size(); i++) {
labels.put(sample.labelNames.get(i), sample.labelValues.get(i));
}
result.put(labels, sample.value);
}
}
return result;
}
示例14: newCounter
import io.prometheus.client.Collector; //导入依赖的package包/类
@Override
public Counter newCounter(Meter.Id id) {
MicrometerCollector collector = collectorByName(id, Collector.Type.COUNTER);
PrometheusCounter counter = new PrometheusCounter(id);
List<String> tagValues = tagValues(id);
collector.add((conventionName, tagKeys) -> Stream.of(
new Collector.MetricFamilySamples.Sample(conventionName, tagKeys, tagValues, counter.count())
));
return counter;
}
示例15: newGauge
import io.prometheus.client.Collector; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
protected <T> io.micrometer.core.instrument.Gauge newGauge(Meter.Id id, T obj, ToDoubleFunction<T> f) {
MicrometerCollector collector = collectorByName(id, Collector.Type.GAUGE);
Gauge gauge = new DefaultGauge(id, obj, f);
List<String> tagValues = tagValues(id);
collector.add((conventionName, tagKeys) -> Stream.of(
new Collector.MetricFamilySamples.Sample(conventionName, tagKeys, tagValues, gauge.value())
));
return gauge;
}