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);
}
}
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;
}
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());
}
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);
}
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();
}
Aggregations