Search in sources :

Example 1 with MetricsContext

use of io.cdap.cdap.api.metrics.MetricsContext in project cdap by caskdata.

the class MetricsReporterHook method postCall.

@Override
public void postCall(HttpRequest request, HttpResponseStatus status, HandlerInfo handlerInfo) {
    if (collectorCache == null) {
        return;
    }
    try {
        MetricsContext collector = collectorCache.get(createContext(handlerInfo));
        String name;
        int code = status.code();
        if (code < 100) {
            name = "unknown";
        } else if (code < 200) {
            name = "information";
        } else if (code < 300) {
            name = "successful";
        } else if (code < 400) {
            name = "redirect";
        } else if (code < 500) {
            name = "client-error";
        } else if (code < 600) {
            name = "server-error";
        } else {
            name = "unknown";
        }
        // todo: report metrics broken down by status
        collector.increment("response." + name, 1);
        // store response time metric
        long currTime = System.nanoTime();
        String startTimeStr = request.headers().get(HttpHeaderNames.CDAP_REQ_TIMESTAMP_HDR);
        if (startTimeStr != null) {
            long responseTimeNanos = currTime - Long.parseLong(startTimeStr);
            collector.event(LATENCY_METRIC_NAME, responseTimeNanos);
        }
    } catch (Throwable e) {
        LOG.error("Got exception while getting collector", e);
    }
}
Also used : MetricsContext(io.cdap.cdap.api.metrics.MetricsContext)

Example 2 with MetricsContext

use of io.cdap.cdap.api.metrics.MetricsContext in project cdap by caskdata.

the class ServiceHttpServer method createDelegatorContexts.

@Override
protected List<HandlerDelegatorContext> createDelegatorContexts() throws Exception {
    // Constructs all handler delegator. It is for bridging ServiceHttpHandler and HttpHandler (in netty-http).
    List<HandlerDelegatorContext> delegatorContexts = new ArrayList<>();
    InstantiatorFactory instantiatorFactory = new InstantiatorFactory(false);
    for (HttpServiceHandlerSpecification handlerSpec : serviceSpecification.getHandlers().values()) {
        Class<?> handlerClass = getProgram().getClassLoader().loadClass(handlerSpec.getClassName());
        @SuppressWarnings("unchecked") TypeToken<HttpServiceHandler> type = TypeToken.of((Class<HttpServiceHandler>) handlerClass);
        MetricsContext metrics = httpServiceContext.getProgramMetrics().childContext(BasicHttpServiceContext.createMetricsTags(handlerSpec, getInstanceId()));
        delegatorContexts.add(new HandlerDelegatorContext(type, instantiatorFactory, handlerSpec, contextFactory, metrics));
    }
    return delegatorContexts;
}
Also used : InstantiatorFactory(io.cdap.cdap.common.lang.InstantiatorFactory) HttpServiceHandler(io.cdap.cdap.api.service.http.HttpServiceHandler) AbstractSystemHttpServiceHandler(io.cdap.cdap.api.service.http.AbstractSystemHttpServiceHandler) MetricsContext(io.cdap.cdap.api.metrics.MetricsContext) ArrayList(java.util.ArrayList) HttpServiceHandlerSpecification(io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification)

Example 3 with MetricsContext

use of io.cdap.cdap.api.metrics.MetricsContext in project cdap by caskdata.

the class MetricsReporterHookTest method testReponseTimeCollection.

@Test
public void testReponseTimeCollection() throws InterruptedException {
    MetricsContext mockCollector = mock(MetricsContext.class);
    MetricsCollectionService mockCollectionService = mock(MetricsCollectionService.class);
    when(mockCollectionService.getContext(anyMap())).thenReturn(mockCollector);
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://ignore");
    HandlerInfo handlerInfo = new HandlerInfo(TESTHANDLERNAME, TESTMETHODNAME);
    MetricsReporterHook hook = new MetricsReporterHook(mockCollectionService, TESTSERVICENAME);
    hook.preCall(request, null, handlerInfo);
    hook.postCall(request, HttpResponseStatus.OK, handlerInfo);
    verify(mockCollector).event(eq("response.latency"), anyLong());
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) MetricsContext(io.cdap.cdap.api.metrics.MetricsContext) HandlerInfo(io.cdap.http.internal.HandlerInfo) Test(org.junit.Test)

Example 4 with MetricsContext

use of io.cdap.cdap.api.metrics.MetricsContext in project cdap by caskdata.

the class ProgramNotificationSubscriberService method emitStartingTimeMetric.

private void emitStartingTimeMetric(ProgramRunId programRunId, long startDelayTime) {
    Map<String, String> tags = Collections.emptyMap();
    MetricsContext metricsContext = ProgramRunners.createProgramMetricsContext(programRunId, tags, metricsCollectionService);
    metricsContext.gauge(Constants.Metrics.Program.PROGRAM_STARTING_DELAY_SECONDS, startDelayTime);
}
Also used : MetricsContext(io.cdap.cdap.api.metrics.MetricsContext)

Example 5 with MetricsContext

use of io.cdap.cdap.api.metrics.MetricsContext in project cdap by caskdata.

the class HttpHandlerGenerator method generateConstructor.

/**
 * Generates the constructor. The constructor generated has signature {@code (DelegatorContext, MetricsContext)}.
 */
private void generateConstructor(TypeToken<?> delegateType, ClassWriter classWriter) {
    Method constructor = Methods.getMethod(void.class, "<init>", DelegatorContext.class, MetricsContext.class);
    String signature = Signatures.getMethodSignature(constructor, TypeToken.of(void.class), getContextType(delegateType), TypeToken.of(MetricsContext.class));
    // Constructor(DelegatorContext, MetricsContext)
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, signature, null, classWriter);
    // super(context, metricsContext);
    mg.loadThis();
    mg.loadArg(0);
    mg.loadArg(1);
    mg.invokeConstructor(Type.getType(AbstractHttpHandlerDelegator.class), Methods.getMethod(void.class, "<init>", DelegatorContext.class, MetricsContext.class));
    mg.returnValue();
    mg.endMethod();
}
Also used : MetricsContext(io.cdap.cdap.api.metrics.MetricsContext) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method)

Aggregations

MetricsContext (io.cdap.cdap.api.metrics.MetricsContext)29 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)6 Test (org.junit.Test)6 NoopMetricsContext (io.cdap.cdap.api.metrics.NoopMetricsContext)4 ArrayList (java.util.ArrayList)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Injector (com.google.inject.Injector)2 DatasetSpecification (io.cdap.cdap.api.dataset.DatasetSpecification)2 MetricDeleteQuery (io.cdap.cdap.api.metrics.MetricDeleteQuery)2 MetricValues (io.cdap.cdap.api.metrics.MetricValues)2 MetricsSystemClient (io.cdap.cdap.api.metrics.MetricsSystemClient)2 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)2 InstantiatorFactory (io.cdap.cdap.common.lang.InstantiatorFactory)2 TableId (io.cdap.cdap.data2.util.TableId)2 MetricsQueryService (io.cdap.cdap.metrics.query.MetricsQueryService)2 MetricsCleanUpService (io.cdap.cdap.metrics.store.MetricsCleanUpService)2 DatasetSpecificationSummary (io.cdap.cdap.proto.DatasetSpecificationSummary)2 DatasetId (io.cdap.cdap.proto.id.DatasetId)2