Search in sources :

Example 1 with ManualClock

use of com.netflix.spectator.api.ManualClock in project java-chassis by ServiceComb.

the class TestMetricsRestPublisher method measure_normal.

@Test
public void measure_normal() {
    Clock clock = new ManualClock();
    GlobalRegistry globalRegistry = new GlobalRegistry();
    Registry registry = new DefaultRegistry(clock);
    registry.timer(registry.createId("name", "t1", "v1", "t2", "v2"));
    globalRegistry.add(registry);
    EventBus eventBus = new EventBus();
    publisher.init(globalRegistry, eventBus, new MetricsBootstrapConfig());
    Map<String, Double> result = publisher.measure();
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(0, result.get("name(statistic=count,t1=v1,t2=v2)"), 0);
    Assert.assertEquals(0, result.get("name(statistic=totalTime,t1=v1,t2=v2)"), 0);
}
Also used : ManualClock(com.netflix.spectator.api.ManualClock) GlobalRegistry(org.apache.servicecomb.foundation.metrics.registry.GlobalRegistry) MetricsBootstrapConfig(org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) GlobalRegistry(org.apache.servicecomb.foundation.metrics.registry.GlobalRegistry) Registry(com.netflix.spectator.api.Registry) EventBus(com.google.common.eventbus.EventBus) ManualClock(com.netflix.spectator.api.ManualClock) Clock(com.netflix.spectator.api.Clock) Test(org.junit.Test)

Example 2 with ManualClock

use of com.netflix.spectator.api.ManualClock in project incubator-servicecomb-java-chassis by apache.

the class TestPublishModelFactory method prepareRegistry.

protected Registry prepareRegistry() {
    CompositeRegistry globalRegistry = SpectatorUtils.createCompositeRegistry(null);
    Registry registry = new DefaultRegistry(new ManualClock());
    EventBus eventBus = new EventBus();
    DefaultMetricsInitializer metricsInitializer = new DefaultMetricsInitializer() {

        protected Registry createRegistry(MetricsBootstrapConfig config) {
            return registry;
        }
    };
    metricsInitializer.init(globalRegistry, eventBus, new MetricsBootstrapConfig());
    new MockUp<System>() {

        @Mock
        long nanoTime() {
            return 10;
        }
    };
    invocationType = InvocationType.CONSUMER;
    new MockUp<Invocation>() {

        @Mock
        InvocationType getInvocationType() {
            return invocationType;
        }

        @Mock
        boolean isConsumer() {
            return InvocationType.CONSUMER.equals(invocationType);
        }

        @Mock
        String getRealTransportName() {
            return Const.RESTFUL;
        }

        @Mock
        String getMicroserviceQualifiedName() {
            return "m.s.o";
        }

        @Mock
        long getStartExecutionTime() {
            return 5;
        }
    };
    new Expectations() {

        {
            response.getStatusCode();
            result = 200;
        }
    };
    InvocationFinishEvent finishEvent = new InvocationFinishEvent(invocation, response);
    eventBus.post(finishEvent);
    invocationType = InvocationType.PRODUCER;
    eventBus.post(finishEvent);
    return registry;
}
Also used : Expectations(mockit.Expectations) ManualClock(com.netflix.spectator.api.ManualClock) InvocationFinishEvent(org.apache.servicecomb.core.event.InvocationFinishEvent) CompositeRegistry(com.netflix.spectator.api.CompositeRegistry) MetricsBootstrapConfig(org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) MockUp(mockit.MockUp) CompositeRegistry(com.netflix.spectator.api.CompositeRegistry) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) Registry(com.netflix.spectator.api.Registry) EventBus(com.google.common.eventbus.EventBus) DefaultMetricsInitializer(org.apache.servicecomb.metrics.core.DefaultMetricsInitializer)

Example 3 with ManualClock

use of com.netflix.spectator.api.ManualClock in project java-chassis by ServiceComb.

the class TestPrometheusPublisher method collect.

@Test
public void collect() throws IllegalAccessException, IOException {
    new Expectations(RegistrationManager.INSTANCE) {

        {
            RegistrationManager.INSTANCE.getAppId();
            result = "testAppId";
        }
    };
    ArchaiusUtils.setProperty(PrometheusPublisher.METRICS_PROMETHEUS_ADDRESS, "localhost:0");
    publisher.init(globalRegistry, null, null);
    Registry registry = new DefaultRegistry(new ManualClock());
    globalRegistry.add(registry);
    Counter counter = registry.counter("count.name", "tag1", "tag1v", "tag2", "tag2v");
    counter.increment();
    HTTPServer httpServer = (HTTPServer) FieldUtils.readField(publisher, "httpServer", true);
    com.sun.net.httpserver.HttpServer server = (HttpServer) FieldUtils.readField(httpServer, "server", true);
    URL url = new URL("http://localhost:" + server.getAddress().getPort() + "/metrics");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    try (InputStream is = conn.getInputStream()) {
        Assert.assertEquals("# HELP ServiceComb_Metrics ServiceComb Metrics\n" + "# TYPE ServiceComb_Metrics untyped\n" + "count_name{appId=\"testAppId\",tag1=\"tag1v\",tag2=\"tag2v\",} 1.0\n", IOUtils.toString(is, StandardCharsets.UTF_8));
    }
    publisher.destroy();
}
Also used : Expectations(mockit.Expectations) HttpServer(com.sun.net.httpserver.HttpServer) InputStream(java.io.InputStream) GlobalRegistry(org.apache.servicecomb.foundation.metrics.registry.GlobalRegistry) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) Registry(com.netflix.spectator.api.Registry) URL(java.net.URL) ManualClock(com.netflix.spectator.api.ManualClock) Counter(com.netflix.spectator.api.Counter) HTTPServer(io.prometheus.client.exporter.HTTPServer) HttpURLConnection(java.net.HttpURLConnection) HttpServer(com.sun.net.httpserver.HttpServer) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) Test(org.junit.Test)

Aggregations

DefaultRegistry (com.netflix.spectator.api.DefaultRegistry)3 ManualClock (com.netflix.spectator.api.ManualClock)3 Registry (com.netflix.spectator.api.Registry)3 EventBus (com.google.common.eventbus.EventBus)2 Expectations (mockit.Expectations)2 MetricsBootstrapConfig (org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig)2 GlobalRegistry (org.apache.servicecomb.foundation.metrics.registry.GlobalRegistry)2 Test (org.junit.Test)2 Clock (com.netflix.spectator.api.Clock)1 CompositeRegistry (com.netflix.spectator.api.CompositeRegistry)1 Counter (com.netflix.spectator.api.Counter)1 HttpServer (com.sun.net.httpserver.HttpServer)1 HTTPServer (io.prometheus.client.exporter.HTTPServer)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 MockUp (mockit.MockUp)1 InvocationFinishEvent (org.apache.servicecomb.core.event.InvocationFinishEvent)1 DefaultMetricsInitializer (org.apache.servicecomb.metrics.core.DefaultMetricsInitializer)1