Search in sources :

Example 1 with Nullable

use of io.micrometer.core.lang.Nullable in project micrometer by micrometer-metrics.

the class ExecutorServiceMetrics method unwrapThreadPoolExecutor.

/**
 * Every ScheduledThreadPoolExecutor created by {@link Executors} is wrapped. Also,
 * {@link Executors#newSingleThreadExecutor()} wrap a regular {@link ThreadPoolExecutor}.
 */
@Nullable
private ThreadPoolExecutor unwrapThreadPoolExecutor(ExecutorService executor, Class<?> wrapper) {
    try {
        Field e = wrapper.getDeclaredField("e");
        e.setAccessible(true);
        return (ThreadPoolExecutor) e.get(executorService);
    } catch (NoSuchFieldException | IllegalAccessException e) {
    // Do nothing. We simply can't get to the underlying ThreadPoolExecutor.
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) Nullable(io.micrometer.core.lang.Nullable)

Example 2 with Nullable

use of io.micrometer.core.lang.Nullable in project micrometer by micrometer-metrics.

the class WavefrontMeterRegistry method writeMetricDirect.

private String writeMetricDirect(Meter.Id id, @Nullable String suffix, double value) {
    Meter.Id fullId = id;
    if (suffix != null)
        fullId = idWithSuffix(id, suffix);
    List<Tag> conventionTags = getConventionTags(fullId);
    String tags = conventionTags.stream().map(t -> "\"" + t.getKey() + "\": \"" + t.getValue() + "\"").collect(joining(","));
    UUID uuid = UUID.randomUUID();
    String uniqueNameSuffix = ((Long) uuid.getMostSignificantBits()).toString() + uuid.getLeastSignificantBits();
    // it as part of the name. Wavefront strips a $<NUMERIC> suffix from the name at parsing time.
    return "\"" + getConventionName(fullId) + "$" + uniqueNameSuffix + "\"" + ": {" + "\"value\": " + DoubleFormat.decimalOrNan(value) + "," + "\"tags\": {" + tags + "}" + "}";
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Nullable(io.micrometer.core.lang.Nullable) Socket(java.net.Socket) Logger(org.slf4j.Logger) URL(java.net.URL) StepMeterRegistry(io.micrometer.core.instrument.step.StepMeterRegistry) LoggerFactory(org.slf4j.LoggerFactory) UUID(java.util.UUID) MissingRequiredConfigurationException(io.micrometer.core.instrument.config.MissingRequiredConfigurationException) DoubleFormat(io.micrometer.core.instrument.util.DoubleFormat) Executors(java.util.concurrent.Executors) Collectors.joining(java.util.stream.Collectors.joining) TimeUnit(java.util.concurrent.TimeUnit) MeterPartition(io.micrometer.core.instrument.util.MeterPartition) List(java.util.List) Stream(java.util.stream.Stream) java.io(java.io) StreamSupport.stream(java.util.stream.StreamSupport.stream) io.micrometer.core.instrument(io.micrometer.core.instrument) URI(java.net.URI) ThreadFactory(java.util.concurrent.ThreadFactory) UUID(java.util.UUID)

Example 3 with Nullable

use of io.micrometer.core.lang.Nullable in project micrometer by micrometer-metrics.

the class GraphiteMeterRegistryTest method metricPrefixes.

@Test
void metricPrefixes() {
    final CountDownLatch bindLatch = new CountDownLatch(1);
    final CountDownLatch receiveLatch = new CountDownLatch(1);
    final CountDownLatch terminateLatch = new CountDownLatch(1);
    final GraphiteMeterRegistry registry = new GraphiteMeterRegistry(new GraphiteConfig() {

        @Override
        @Nullable
        public String get(String key) {
            return null;
        }

        @Override
        public Duration step() {
            return Duration.ofSeconds(1);
        }

        @Override
        public GraphiteProtocol protocol() {
            return GraphiteProtocol.UDP;
        }

        @Override
        public int port() {
            return 8127;
        }

        @Override
        public String[] tagsAsPrefix() {
            return new String[] { "application" };
        }
    }, mockClock);
    final Disposable.Swap server = Disposables.swap();
    Consumer<ClientOptions.Builder<?>> opts = builder -> builder.option(ChannelOption.SO_REUSEADDR, true).connectAddress(() -> new InetSocketAddress(PORT));
    UdpServer.create(opts).newHandler((in, out) -> {
        in.receive().asString().log().subscribe(line -> {
            assertThat(line).startsWith("APPNAME.myTimer");
            receiveLatch.countDown();
        });
        return Flux.never();
    }).doOnSuccess(v -> bindLatch.countDown()).doOnTerminate(terminateLatch::countDown).subscribe(server::replace);
    try {
        assertTrue(bindLatch.await(10, TimeUnit.SECONDS));
        registry.timer("my.timer", "application", "APPNAME");
        assertTrue(receiveLatch.await(10, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        fail("Failed to wait for line", e);
    } finally {
        server.dispose();
        registry.stop();
        try {
            terminateLatch.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            fail("Failed to terminate UDP server listening for Graphite metrics", e);
        }
    }
}
Also used : Disposable(reactor.core.Disposable) Nullable(io.micrometer.core.lang.Nullable) Disposable(reactor.core.Disposable) ChannelOption(io.netty.channel.ChannelOption) MockClock(io.micrometer.core.instrument.MockClock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) CountDownLatch(java.util.concurrent.CountDownLatch) Assertions.fail(org.assertj.core.api.Assertions.fail) Duration(java.time.Duration) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) UdpServer(reactor.ipc.netty.udp.UdpServer) ClientOptions(reactor.ipc.netty.options.ClientOptions) Disposables(reactor.core.Disposables) InetSocketAddress(java.net.InetSocketAddress) Duration(java.time.Duration) CountDownLatch(java.util.concurrent.CountDownLatch) Nullable(io.micrometer.core.lang.Nullable) Test(org.junit.jupiter.api.Test)

Example 4 with Nullable

use of io.micrometer.core.lang.Nullable in project micrometer by micrometer-metrics.

the class SampleRegistries method prometheus.

/**
 * To use pushgateway instead:
 * new PushGateway("localhost:9091").pushAdd(registry.getPrometheusRegistry(), "samples");
 *
 * @return A prometheus registry.
 */
public static PrometheusMeterRegistry prometheus() {
    PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(new PrometheusConfig() {

        @Override
        public Duration step() {
            return Duration.ofSeconds(10);
        }

        @Override
        @Nullable
        public String get(String k) {
            return null;
        }
    });
    try {
        HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
        server.createContext("/prometheus", httpExchange -> {
            String response = prometheusRegistry.scrape();
            httpExchange.sendResponseHeaders(200, response.length());
            OutputStream os = httpExchange.getResponseBody();
            os.write(response.getBytes());
            os.close();
        });
        new Thread(server::start).run();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return prometheusRegistry;
}
Also used : PrometheusMeterRegistry(io.micrometer.prometheus.PrometheusMeterRegistry) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) HttpServer(com.sun.net.httpserver.HttpServer) PrometheusConfig(io.micrometer.prometheus.PrometheusConfig) Duration(java.time.Duration) IOException(java.io.IOException) Nullable(io.micrometer.core.lang.Nullable)

Aggregations

Nullable (io.micrometer.core.lang.Nullable)4 InetSocketAddress (java.net.InetSocketAddress)2 Duration (java.time.Duration)2 TimeUnit (java.util.concurrent.TimeUnit)2 HttpServer (com.sun.net.httpserver.HttpServer)1 io.micrometer.core.instrument (io.micrometer.core.instrument)1 MockClock (io.micrometer.core.instrument.MockClock)1 MissingRequiredConfigurationException (io.micrometer.core.instrument.config.MissingRequiredConfigurationException)1 StepMeterRegistry (io.micrometer.core.instrument.step.StepMeterRegistry)1 DoubleFormat (io.micrometer.core.instrument.util.DoubleFormat)1 MeterPartition (io.micrometer.core.instrument.util.MeterPartition)1 PrometheusConfig (io.micrometer.prometheus.PrometheusConfig)1 PrometheusMeterRegistry (io.micrometer.prometheus.PrometheusMeterRegistry)1 ChannelOption (io.netty.channel.ChannelOption)1 java.io (java.io)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Field (java.lang.reflect.Field)1 HttpURLConnection (java.net.HttpURLConnection)1 Socket (java.net.Socket)1