下面列出了怎么用io.prometheus.client.Counter的API类实例代码及写法,或者点击链接到github查看源代码。
public SuccessRatioCollector(BaseCollectorConfig config) {
this.success = new Counter.Builder()
.help(config.getHelp())
.name(extendedName(config.getMetricName(), "success"))
.labelNames(config.getLabels())
.create();
this.failure = new Counter.Builder()
.help(config.getHelp())
.name(extendedName(config.getMetricName(), "failure"))
.labelNames(config.getLabels())
.create();
this.total = new Counter.Builder()
.help(config.getHelp())
.name(extendedName(config.getMetricName(), "total"))
.labelNames(config.getLabels())
.create();
}
@Override
public void initErrorMetrics(String serviceType, String type, String metricName, String metricHelp,
String[] properties) {
String[] labels = properties;
if (serviceType.equals(SERVICE.PROXY.name())) {
ERROR_REQUESTS_RECEIVED_PROXY_SERVICE = Counter.build(MetricConstants.PROXY_REQUEST_COUNT_ERROR_TOTAL,
metricHelp).
labelNames(labels).register();
metricMap.put(metricName, ERROR_REQUESTS_RECEIVED_PROXY_SERVICE);
} else if (serviceType.equals(SERVICE.API.name())) {
ERROR_REQUESTS_RECEIVED_API = Counter.build(MetricConstants.API_REQUEST_COUNT_ERROR_TOTAL, metricHelp).
labelNames(labels).register();
metricMap.put(metricName, ERROR_REQUESTS_RECEIVED_API);
} else if (serviceType.equals(SERVICE.INBOUND_ENDPOINT.name())) {
ERROR_REQUESTS_RECEIVED_INBOUND_ENDPOINT = Counter.
build(MetricConstants.INBOUND_ENDPOINT_REQUEST_COUNT_ERROR_TOTAL, metricHelp).labelNames(labels).
register();
metricMap.put(metricName, ERROR_REQUESTS_RECEIVED_INBOUND_ENDPOINT);
}
}
@Override
public LabelledMetric<org.hyperledger.besu.plugin.services.metrics.Counter> createLabelledCounter(
final MetricCategory category,
final String name,
final String help,
final String... labelNames) {
final String metricName = convertToPrometheusName(category, name);
return cachedCounters.computeIfAbsent(
metricName,
(k) -> {
if (isCategoryEnabled(category)) {
final Counter counter = Counter.build(metricName, help).labelNames(labelNames).create();
addCollectorUnchecked(category, counter);
return new PrometheusCounter(counter);
} else {
return NoOpMetricsSystem.getCounterLabelledMetric(labelNames.length);
}
});
}
private GarmadonReader.GarmadonMessageHandler buildGarmadonMessageHandler(PartitionedWriter<Message> writer,
String eventName) {
return msg -> {
final CommittableOffset offset = msg.getCommittableOffset();
final Counter.Child messagesWritingFailures = PrometheusMetrics.messageWritingFailuresCounter(eventName, offset.getPartition());
final Counter.Child messagesWritten = PrometheusMetrics.messageWrittenCounter(eventName, offset.getPartition());
Gauge.Child gauge = PrometheusMetrics.currentRunningOffsetsGauge(eventName, offset.getPartition());
gauge.set(offset.getOffset());
try {
writer.write(Instant.ofEpochMilli(msg.getTimestamp()), offset, msg.toProto());
messagesWritten.inc();
} catch (IOException e) {
// We accept losing messages every now and then, but still log failures
messagesWritingFailures.inc();
LOGGER.warn("Couldn't write a message", e);
}
};
}
static void executeBatchJob() throws Exception {
CollectorRegistry registry = CollectorRegistry.defaultRegistry;
Counter requests = Counter.build()
.name("my_library_requests_total").help("Total requests.")
.labelNames("method").register();
requests.labels("get").inc();
PushGateway pushgateway = new PushGateway("127.0.0.1:7200/prom");
// pushgateway.setConnectionFactory(new BasicAuthHttpConnectionFactory("my_user", "my_password"));
Map<String, String> groupingkeys = new HashMap<>();
groupingkeys.put("app", "xx");
pushgateway.pushAdd(registry, "my_batch_job", groupingkeys);
// pushgateway.pushAdd(registry, "my_batch_job");
}
public JdbcHook(MetricsStore metricsStore) {
sqlQueriesTotal = metricsStore.createOrGet(new MetricDef<>(
"sql_queries_total",
(name, registry) -> Counter.build()
.name(name)
.labelNames("method", "path", "query")
.help("Total number of sql queries.")
.register(registry)
));
sqlQueriesDuration = metricsStore.createOrGet(new MetricDef<>(
"sql_query_duration",
(name, registry) -> Summary.build()
.quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
.quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
.quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error
.name(name)
.labelNames("method", "path", "query")
.help("Duration for serving the sql queries in seconds.")
.register(registry)
));
}
public ServletHook(MetricsStore metricsStore) {
httpRequestsTotal = metricsStore.createOrGet(new MetricDef<>(
"http_requests_total",
(name, registry) -> Counter.build()
.name(name)
.labelNames("method", "path", "status")
.help("Total number of http requests.")
.register(registry)
));
httpRequestsDuration = metricsStore.createOrGet(new MetricDef<>(
"http_request_duration",
(name, registry) -> Summary.build()
.quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
.quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
.quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error
.name(name)
.labelNames("method", "path", "status")
.help("Duration for serving the http requests in seconds.")
.register(registry)
));
}
@Test
public void testCreateOrGetCollector() throws NoSuchMethodException, SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
CollectorRegistry registry = new CollectorRegistry();
ConcurrentHashMap<String, Collector> namesToCollectors = new ConcurrentHashMap<>();
PrometheusMetric metric = new PrometheusMetric(registry, namesToCollectors, null, "");
Method createOrGetCollector = metric.getClass().getDeclaredMethod("createOrGetCollector", String.class, SimpleCollector.Builder.class);
createOrGetCollector.setAccessible(true);
// test create
String metricName = "metric_test";
Counter.Builder builder = Counter.build();
double countValue = 110.110d;
Counter counter = (Counter) createOrGetCollector.invoke(metric, metricName, builder);
counter.labels("", "").inc(countValue);
// assertions
Assert.assertSame(counter, namesToCollectors.get(metricName));
Assert.assertEquals(registry.getSampleValue(metricName, this.labelNames, new String[]{"", ""}), countValue);
// test get
Counter counter_2 = (Counter) createOrGetCollector.invoke(metric, metricName, builder);
// assertions
Assert.assertSame(counter_2, namesToCollectors.get(metricName));
Assert.assertSame(counter_2, counter);
}
public Counter.Child addCounter(String subsystem, String metric, String helpDoc, SortedMap<String, String> labels) {
lock.writeLock().lock();
try {
String name = name(subsystem, metric);
Counter counter = counters.get(name);
if (counter == null) {
counter = Counter.build().name(name(subsystem, metric)).help(helpDoc).
labelNames(labels.keySet().toArray(new String[]{})).create();
counter.register(registry);
counters.put(name, counter);
}
return counter.labels(labels.values().toArray(new String[]{}));
} finally {
lock.writeLock().unlock();
}
}
private Counter initializeCounter(String dmnType, CollectorRegistry registry) {
Counter.Builder builder = Counter.build().name(dmnType + DecisionConstants.DECISIONS_NAME_SUFFIX)
.help(DecisionConstants.DECISIONS_HELP)
.labelNames(DecisionConstants.DECISION_ENDPOINT_IDENTIFIER_LABELS);
return registry == null ? builder.register(CollectorRegistry.defaultRegistry) : builder.register(registry);
}
private Counter initializeCounter(String dmnType, CollectorRegistry registry) {
Counter.Builder builder = Counter.build().name(dmnType + DecisionConstants.DECISIONS_NAME_SUFFIX)
.help(DecisionConstants.DECISIONS_HELP)
.labelNames(DecisionConstants.DECISION_ENDPOINT_IDENTIFIER_LABELS);
return registry == null ? builder.register(CollectorRegistry.defaultRegistry) : builder.register(registry);
}
private PrometheusClientInstanceProfiler() {
this.outboundCounter = Counter.build()
.labelNames(DEST_LABELS)
.name(OUTBOUND_BYTES)
.help("Total bytes sent to client.")
.create();
this.packetsCounter = Counter.build()
.labelNames(new String[]{DEST, "packetType"})
.name(PACKET_TYPE)
.help("Total packets sent to client.")
.create();
this.emptyBatchesCounter = Counter.build()
.labelNames(DEST_LABELS)
.name(EMPTY_BATCHES)
.help("Total empty batches sent to client.")
.create();
this.errorsCounter = Counter.build()
.labelNames(new String[]{DEST, "errorCode"})
.name(ERRORS)
.help("Total client request errors.")
.create();
this.responseLatency = Histogram.build()
.labelNames(DEST_LABELS)
.name(LATENCY)
.help("Client request latency.")
// buckets in milliseconds
.buckets(2.5, 10.0, 25.0, 100.0)
.create();
}
private static Counter buildCounter(String name, String help, boolean withPartition) {
Counter.Builder builder = Counter.build()
.name(name).help(help);
if (withPartition) {
builder.labelNames("name", "partition", "hostname", "release");
} else {
builder.labelNames("name", "hostname", "release");
}
return builder.register();
}
private void possiblyCloseConsumers(Predicate<ExpiringConsumer> shouldClose) {
synchronized (perPartitionDayWriters) {
perPartitionDayWriters.forEach((partitionId, dailyWriters) ->
dailyWriters.entrySet().removeIf(entry -> {
final ExpiringConsumer<MESSAGE_KIND> consumer = entry.getValue();
final LocalDateTime day = entry.getKey();
if (shouldClose.test(consumer)) {
if (tryExpireConsumer(consumer)) {
final Counter.Child filesCommitted = PrometheusMetrics.filesCommittedCounter(eventName);
final Counter.Child checkpointsFailures = PrometheusMetrics.checkpointFailuresCounter(eventName, partitionId);
final Counter.Child checkpointsSuccesses = PrometheusMetrics.checkpointSuccessesCounter(eventName, partitionId);
filesCommitted.inc();
try {
checkpointer.tryCheckpoint(partitionId, latestMessageTimeForPartitionAndDay.get(
new AbstractMap.SimpleEntry<>(partitionId, day)));
} catch (RuntimeException e) {
String msg = String.format("Failed to checkpoint partition %d, date %s, event %s",
partitionId, day.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
eventName);
LOGGER.warn(msg, e);
checkpointsFailures.inc();
}
checkpointsSuccesses.inc();
return true;
}
}
return false;
}));
}
}
/**
* Creates a counter based on a event name
*/
private static Counter createCounter(final String name, boolean isAdmin) {
final Counter.Builder counter = Counter.build().name(name);
if (isAdmin) {
counter.labelNames("realm", "resource").help("Generic KeyCloak Admin event");
} else {
counter.labelNames("realm").help("Generic KeyCloak User event");
}
return counter.register();
}
@PostConstruct
@SuppressWarnings("PMD.UnusedPrivateMethod")
// method is required and called by the Spring Framework
private void registerMetrics() {
if (!this.enabled)
return;
this.latencyCFFetch = Histogram.build("promregator_cffetch_latency", "Latency on retrieving CF values")
.labelNames("request_type").linearBuckets(0.1, 0.1, 50).register();
this.autoRefreshingCacheMapSize = Gauge.build("promregator_autorefreshingcachemap_size", "The size of objects stored in an AutoRefreshingCacheMap")
.labelNames(CACHE_MAP_NAME).register();
this.autoRefreshingCacheMapExpiry = Counter.build("promregator_autorefreshingcachemap_expiry", "The number of objects having expired so far in an AutoRefreshingCacheMap")
.labelNames(CACHE_MAP_NAME).register();
this.autoRefreshingCacheMapRefreshSuccess = Counter.build("promregator_autorefreshingcachemap_refresh_success", "The number of successful refreshes of object so far in an AutoRefreshingCacheMap")
.labelNames(CACHE_MAP_NAME).register();
this.autoRefreshingCacheMapRefreshFailure = Counter.build("promregator_autorefreshingcachemap_refresh_failure", "The number of failed refreshes of object so far in an AutoRefreshingCacheMap")
.labelNames(CACHE_MAP_NAME).register();
this.autoRefreshingCacheMapErroneousEntryDisplaced = Counter.build("promregator_autorefreshingcachemap_erroneous_entry_displaced", "The number of cache items displaced in an AutoRefreshingCacheMap, because they were detected to be erroneous")
.labelNames(CACHE_MAP_NAME).register();
this.autoRefreshingCacheMapLastScan = Gauge.build("promregator_autorefreshingcachemap_scantimestamp", "The timestamp of the last execution of the RefreshThread execution of an AutoRefreshingCacheMap")
.labelNames(CACHE_MAP_NAME).register();
this.connectionWatchdogReconnects = Counter.build("promregator_connection_watchdog_reconnect", "The number of reconnection attempts made by the Connection Watchdog")
.register();
this.caffeineCacheMetricsCollector = new CacheMetricsCollector().register();
this.rateLimitWaitTime = Histogram.build("promregator_cffetch_ratelimit_waittime", "Wait time due to CFCC rate limiting")
.labelNames("request_type").linearBuckets(0.0, 0.05, 50).register();
CollectorRegistry.defaultRegistry.register(new InternalCollector());
}
@Test
public void testSuccessAssertions() {
ListenerCollectorConfig cfg = TestUtilities.listenerCounterCfg(
"count_assertion_success_test",
Measurable.SuccessTotal,
ListenerCollectorConfig.ASSERTIONS);
Counter c = (Counter) reg.getOrCreateAndRegister(cfg);
CountTypeUpdater u = new CountTypeUpdater(cfg);
SampleResult result = newSampleResultWithAssertion(true);
u.update(new SampleEvent(result,"tg1", vars())); // #1
double shouldBeOne = c.labels(TestUtilities.EXPECTED_ASSERTION_LABELS).get();
Assert.assertEquals(1.0, shouldBeOne, 0.1);
result = newSampleResultWithAssertion(false);
u.update(new SampleEvent(result,"tg1", vars())); // #could be 2, but should be 1
shouldBeOne = c.labels(TestUtilities.EXPECTED_ASSERTION_LABELS).get();
Assert.assertEquals(1.0, shouldBeOne, 0.1);
// now update 2 assertions
result = newSampleResultWithAssertion(true);
result.addAssertionResult(altAssertion(true));
u.update(new SampleEvent(result,"tg1", vars())); // #now should be 2
double shouldBeTwo = c.labels(TestUtilities.EXPECTED_ASSERTION_LABELS).get();
Assert.assertEquals(2.0, shouldBeTwo, 0.1);
shouldBeOne = c.labels(TestUtilities.EXPECTED_ASSERTION_LABELS_ALT).get(); //but alt is just 1
Assert.assertEquals(1.0, shouldBeOne, 0.1);
}
@Test
public void testFailureAssertions() {
ListenerCollectorConfig cfg = TestUtilities.listenerCounterCfg(
"count_assertion_failure_test",
Measurable.FailureTotal,
ListenerCollectorConfig.ASSERTIONS);
Counter c = (Counter) reg.getOrCreateAndRegister(cfg);
CountTypeUpdater u = new CountTypeUpdater(cfg);
SampleResult result = newSampleResultWithAssertion(false);
u.update(new SampleEvent(result,"tg1", vars())); // #1
double shouldBeOne = c.labels(TestUtilities.EXPECTED_ASSERTION_LABELS).get();
Assert.assertEquals(1.0, shouldBeOne, 0.1);
result = newSampleResultWithAssertion(true);
u.update(new SampleEvent(result,"tg1", vars())); // #could be 2, but should be 1
shouldBeOne = c.labels(TestUtilities.EXPECTED_ASSERTION_LABELS).get();
Assert.assertEquals(1.0, shouldBeOne, 0.1);
// now update 2 assertions
result = newSampleResultWithAssertion(false);
result.addAssertionResult(altAssertion(false));
u.update(new SampleEvent(result,"tg1", vars())); // #now should be 2
double shouldBeTwo = c.labels(TestUtilities.EXPECTED_ASSERTION_LABELS).get();
Assert.assertEquals(2.0, shouldBeTwo, 0.1);
shouldBeOne = c.labels(TestUtilities.EXPECTED_ASSERTION_LABELS_ALT).get(); //but alt is just 1
Assert.assertEquals(1.0, shouldBeOne, 0.1);
}
@Test
public void testTotalAssertions() {
ListenerCollectorConfig cfg = TestUtilities.listenerCounterCfg(
"count_assertion_total_test",
Measurable.CountTotal,
ListenerCollectorConfig.ASSERTIONS);
Counter c = (Counter) reg.getOrCreateAndRegister(cfg);
CountTypeUpdater u = new CountTypeUpdater(cfg);
SampleResult result = newSampleResultWithAssertion(false);
u.update(new SampleEvent(result,"tg1", vars())); // #1
double shouldBeOne = c.labels(TestUtilities.EXPECTED_ASSERTION_LABELS).get();
Assert.assertEquals(1.0, shouldBeOne, 0.1);
result = newSampleResultWithAssertion(true);
u.update(new SampleEvent(result,"tg1", vars())); // #2
double shouldBeTwo = c.labels(TestUtilities.EXPECTED_ASSERTION_LABELS).get();
Assert.assertEquals(2.0, shouldBeTwo, 0.1);
// now update 2 assertions
result = newSampleResultWithAssertion(false);
result.addAssertionResult(altAssertion(false));
u.update(new SampleEvent(result,"tg1", vars())); // #3
double shouldBeThree = c.labels(TestUtilities.EXPECTED_ASSERTION_LABELS).get();
Assert.assertEquals(3.0, shouldBeThree, 0.1);
shouldBeOne = c.labels(TestUtilities.EXPECTED_ASSERTION_LABELS_ALT).get(); //but alt is just 1
Assert.assertEquals(1.0, shouldBeOne, 0.1);
}
@PostConstruct
private void initMetrics() {
createdCars = Counter.build("cars_manufactured_total",
"Total number of manufactured cars")
.labelNames("color", "engine")
.register();
}
/**
* @param configFile where the config is located
*/
public static synchronized void open(File configFile) {
if (open) {
LOGGER.error("Already initialized...");
return;
}
SessionExtractionContainer.configFile = configFile;
initalizeExtraction();
if (Config.sharedConfig().getMonitoring().enablePrometheus) {
LOGGER.debug("Initalizing Prometheus monitoring for submitted paths");
submittedPaths = Counter.build().name("cineast_submitted_paths")
.help("Submitted Paths for this instance").register();
}
open = true;
}
public SessionContainerProvider() {
if (Config.sharedConfig().getMonitoring().enablePrometheus) {
LOGGER.debug("Enabling prometheus monitoring for paths in queue {}", queueNumber.get());
instance = queueNumber.getAndIncrement();
pathsInQueue = Gauge.build().name("cineast_item_in_queue_" + instance)
.help("Paths currently in Queue " + instance).register();
pathsCompleted = Counter.build().name("cineast_item_completed_queue_" + instance)
.help("Paths completed in Queue " + instance).register();
} else {
instance = 0;
}
}
private ClientMetrics(
GrpcMethod method,
Counter rpcStarted,
Counter rpcCompleted,
Counter streamMessagesReceived,
Counter streamMessagesSent,
Optional<Histogram> completedLatencySeconds) {
this.method = method;
this.rpcStarted = rpcStarted;
this.rpcCompleted = rpcCompleted;
this.streamMessagesReceived = streamMessagesReceived;
this.streamMessagesSent = streamMessagesSent;
this.completedLatencySeconds = completedLatencySeconds;
}
private ServerMetrics(
GrpcMethod method,
Counter serverStarted,
Counter serverHandled,
Counter serverStreamMessagesReceived,
Counter serverStreamMessagesSent,
Optional<Histogram> serverHandledLatencySeconds) {
this.method = method;
this.serverStarted = serverStarted;
this.serverHandled = serverHandled;
this.serverStreamMessagesReceived = serverStreamMessagesReceived;
this.serverStreamMessagesSent = serverStreamMessagesSent;
this.serverHandledLatencySeconds = serverHandledLatencySeconds;
}
@Override
public void increment(String metricName, String requestDomainName, String principalDomainName, int count) {
// prometheus does not allow null labels
requestDomainName = (this.isLabelRequestDomainNameEnable) ? Objects.toString(requestDomainName, "") : "";
principalDomainName = (this.isLabelPrincipalDomainNameEnable) ? Objects.toString(principalDomainName, "") : "";
metricName = this.normalizeCounterMetricName(metricName);
Counter counter = (Counter) createOrGetCollector(metricName, Counter.build());
counter.labels(requestDomainName, principalDomainName).inc(count);
}
@Test
public void testConstructor() throws IOException {
int port = 8181;
String counterName = "constructor_test_total";
String counterHelp = "constructor_test_help";
double counterValue = 1234.6789;
CollectorRegistry registry = new CollectorRegistry();
Counter counter = Counter.build().name(counterName).help(counterHelp).register(registry);
counter.inc(counterValue);
// new
String expectedResponseText = String.join(
"\n",
String.format("# HELP %s %s", counterName, counterHelp),
String.format("# TYPE %s %s", counterName, counter.getClass().getSimpleName().toLowerCase()),
String.format("%s %.4f", counterName, counterValue)
);
PrometheusPullServer exporter = null;
try {
exporter = new PrometheusPullServer(port, registry);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(String.format("http://localhost:%d/metrics", port));
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String responseText = rd.lines().collect(Collectors.joining("\n"));
// assertions
Assert.assertEquals(responseText, expectedResponseText);
} finally {
// cleanup
if (exporter != null) {
exporter.quit();
}
}
}
protected AbstractTimeLimiterMetrics(MetricNames names) {
this.names = requireNonNull(names);
callsCounter = Counter.build(names.getCallsMetricName(),
"Total number of calls by kind")
.labelNames("name", "kind")
.create().register(collectorRegistry);
}
private CallCollectors createMetrics() {
final Counter totalCounter = Counter
.build()
.namespace(namespace)
.subsystem(subsystem)
.name(name + "_total")
.help(help + " total")
.labelNames(labelNames)
.create();
final Counter errorCounter = Counter
.build()
.namespace(namespace)
.subsystem(subsystem)
.name(name + "_failures_total")
.help(help + " failures total")
.labelNames(labelNames)
.create();
final Histogram histogram = Histogram
.build()
.namespace(namespace)
.subsystem(subsystem)
.name(name + "_latency")
.help(help + " latency")
.labelNames(labelNames)
.create();
return new CallCollectors(histogram, totalCounter, errorCounter);
}
@Override
public void inc() {
Counter.Child metrics = this.getMetric();
if (metrics != null) {
metrics.inc();
}
}
@Override
public void inc(double value) {
Counter.Child metrics = this.getMetric();
if (metrics != null) {
metrics.inc(value);
}
}