本文整理汇总了Java中io.prometheus.client.exporter.common.TextFormat.write004方法的典型用法代码示例。如果您正苦于以下问题:Java TextFormat.write004方法的具体用法?Java TextFormat.write004怎么用?Java TextFormat.write004使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.prometheus.client.exporter.common.TextFormat
的用法示例。
在下文中一共展示了TextFormat.write004方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getServlet
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
@Override
public HttpHandler getServlet() {
return exchange -> {
final ByteArrayOutputStream response = new ByteArrayOutputStream(1 << 20);
final OutputStreamWriter osw = new OutputStreamWriter(response);
TextFormat.write004(osw, registry.metricFamilySamples());
osw.flush();
osw.close();
response.flush();
response.close();
exchange.getResponseHeaders().set("Content-Type", TextFormat.CONTENT_TYPE_004);
exchange.getResponseHeaders().set("Content-Length", String.valueOf(response.size()));
exchange.sendResponseHeaders(200, response.size());
response.writeTo(exchange.getResponseBody());
exchange.close();
};
}
示例2: doGet
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
@Override
protected void doGet(
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws IOException {
String paramToken = httpServletRequest.getParameter("token");
String storedToken = secureTokenManager.getToken();
if (StringUtils.isNotBlank(storedToken) && !storedToken.equals(paramToken)) {
httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
return;
}
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
httpServletResponse.setContentType(TextFormat.CONTENT_TYPE_004);
try (Writer writer = httpServletResponse.getWriter()) {
TextFormat.write004(writer, metricCollector.getRegistry().filteredMetricFamilySamples(parse(httpServletRequest)));
writer.flush();
}
}
示例3: doGet
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
@Override
protected void doGet(
final HttpServletRequest httpServletRequest,
final HttpServletResponse httpServletResponse) throws ServletException, IOException {
String paramToken = httpServletRequest.getParameter("token");
String storedToken = secureTokenManager.getToken();
if (StringUtils.isNotBlank(storedToken) && !storedToken.equals(paramToken)) {
httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
return;
}
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
httpServletResponse.setContentType(TextFormat.CONTENT_TYPE_004);
try (Writer writer = httpServletResponse.getWriter()) {
TextFormat.write004(writer, registry.filteredMetricFamilySamples(parse(httpServletRequest)));
writer.flush();
}
}
示例4: doGet
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
@Override
protected void doGet(
final HttpServletRequest httpServletRequest,
final HttpServletResponse httpServletResponse) throws ServletException, IOException {
String paramToken = httpServletRequest.getParameter("token");
String storedToken = secureTokenManager.getToken();
if (StringUtils.isNotBlank(storedToken) && !storedToken.equals(paramToken)) {
httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
return;
}
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
httpServletResponse.setContentType(TextFormat.CONTENT_TYPE_004);
try (Writer writer = httpServletResponse.getWriter()) {
TextFormat.write004(writer, metricCollector.getRegistry().filteredMetricFamilySamples(parse(httpServletRequest)));
writer.flush();
}
}
示例5: doGet
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType(TextFormat.CONTENT_TYPE_004);
List<ProbeResult> probeResults = metricsCache.get();
formatProbeResults(probeResults)
.forEach(formattedProbeResult -> {
try {
resp.getWriter().println(formattedProbeResult);
} catch (IOException e) {
logger.error("IO error occurred writing to /metrics page.", e);
}
});
try (Writer writer = resp.getWriter()) {
TextFormat.write004(writer, registry.filteredMetricFamilySamples(parse(req)));
writer.flush();
}
}
示例6: doGet
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的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();
}
示例7: getMetrics
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
@GET
@Path("/metrics")
private String getMetrics(Request request, Response response) throws IOException {
response.header(CACHE_CONTROL, "must-revalidate,no-cache,no-store");
response.status(HttpServletResponse.SC_OK);
Optional<String> format = Optional.ofNullable(request.queryParams(FORMAT));
String dFormat = format.orElse(JSON);
switch (dFormat) {
case PROMETHEUS:
// Prometheus format for the metrics
response.type(PROMETHEUS_CONTENT_TYPE);
final Writer writer1 = new StringWriter();
TextFormat.write004(writer1, this.prometheusRegistry.metricFamilySamples());
return writer1.toString();
case JSON:
// Json/Dropwizard format
response.type(APPLICATION_JSON);
final ObjectWriter writer = mapper.writer();
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
writer.writeValue(output, this.metricRegistry);
return new String(output.toByteArray(), "UTF-8");
}
default:
throw GraknServerException.requestInvalidParameter(FORMAT, dFormat);
}
}
示例8: invoke
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
@Override
@GetMapping(produces = TextFormat.CONTENT_TYPE_004)
@HypermediaDisabled
@ResponseBody
public ResponseEntity<String> invoke() {
PrometheusMetrics prometheusMetrics = (PrometheusMetrics) super.invoke();
Writer writer = new StringWriter();
try {
TextFormat.write004(writer, Collections.enumeration(prometheusMetrics.getMetricFamilySamples()));
} catch (IOException e) {
log.error("metric write error", e);
}
HttpStatus status = prometheusMetrics.isUp() ? HttpStatus.OK : HttpStatus.INTERNAL_SERVER_ERROR;
String body = writer.toString();
ResponseEntity<String> response = new ResponseEntity<>(body, status);
return response;
}
示例9: shouldWriteNiceMetricsOutput
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
@Test
public void shouldWriteNiceMetricsOutput() throws IOException {
// given
HystrixPrometheusMetricsPublisher.builder().shouldExportDeprecatedMetrics(false).buildAndRegister();
TestHystrixCommand command = new TestHystrixCommand("any");
// when
command.execute();
// then
Writer writer = new FileWriter("target/sample.txt");
try {
TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples());
writer.flush();
} finally {
writer.close();
}
}
示例10: shouldHaveExponentialBuckets
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
@Test
public void shouldHaveExponentialBuckets() throws IOException {
// given
HystrixPrometheusMetricsPublisher.builder().withExponentialBuckets().buildAndRegister();
TestHystrixCommand command = new TestHystrixCommand("any");
// when
command.execute();
// then
StringWriter writer = new StringWriter();
try {
TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples());
writer.flush();
} finally {
writer.close();
}
String result = writer.toString();
Assertions.assertThat(result).contains("le=\"0.001\"");
Assertions.assertThat(result).contains("le=\"2.5169093494697568\"");
}
示例11: shouldHaveLinearBuckets
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
@Test
public void shouldHaveLinearBuckets() throws IOException {
// given
HystrixPrometheusMetricsPublisher.builder().withLinearBuckets(0.1, 0.2, 3).buildAndRegister();
TestHystrixCommand command = new TestHystrixCommand("any");
// when
command.execute();
// then
StringWriter writer = new StringWriter();
try {
TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples());
writer.flush();
} finally {
writer.close();
}
String result = writer.toString();
Assertions.assertThat(result).contains("le=\"0.1\"");
Assertions.assertThat(result).contains("le=\"0.5\"");
}
示例12: shouldHaveDistinctBuckets
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
@Test
public void shouldHaveDistinctBuckets() throws IOException {
// given
HystrixPrometheusMetricsPublisher.builder().withBuckets(0.1, 1.0).buildAndRegister();
TestHystrixCommand command = new TestHystrixCommand("any");
// when
command.execute();
// then
StringWriter writer = new StringWriter();
try {
TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples());
writer.flush();
} finally {
writer.close();
}
String result = writer.toString();
Assertions.assertThat(result).contains("le=\"0.1\"");
Assertions.assertThat(result).contains("le=\"1.0\"");
}
示例13: getTextFormat
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
@Override
public String getTextFormat() {
try {
StringWriter result = new StringWriter();
TextFormat.write004(result, registry.metricFamilySamples());
return result.toString();
} catch (IOException e) {
throw new RuntimeException("Unexpected error when writing metrics to a String: " + e.getMessage(), e);
}
}
示例14: respondMetrics
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
private static void respondMetrics(CollectorRegistry registry, HttpExchange httpExchange) throws IOException {
StringWriter respBodyWriter = new StringWriter();
respBodyWriter.write("# Metrics will become visible when they are updated for the first time.\n");
TextFormat.write004(respBodyWriter, registry.metricFamilySamples());
byte[] respBody = respBodyWriter.toString().getBytes("UTF-8");
httpExchange.getResponseHeaders().put("Context-Type", Collections.singletonList("text/plain; charset=UTF-8"));
httpExchange.sendResponseHeaders(200, respBody.length);
httpExchange.getResponseBody().write(respBody);
httpExchange.getResponseBody().close();
}
示例15: invoke
import io.prometheus.client.exporter.common.TextFormat; //导入方法依赖的package包/类
@Override
public ResponseEntity<String> invoke() {
try {
Writer writer = new StringWriter();
TextFormat.write004(writer, collectorRegistry.metricFamilySamples());
return ResponseEntity.ok()
.header(CONTENT_TYPE, TextFormat.CONTENT_TYPE_004)
.body(writer.toString());
} catch (IOException e) {
// This actually never happens since StringWriter::write() doesn't throw any IOException
throw new RuntimeException("Writing metrics failed", e);
}
}